asteroid-team / torch-audiomentations

Fast audio data augmentation in PyTorch. Inspired by audiomentations. Useful for deep learning.
MIT License
928 stars 87 forks source link

Frequency filtering #98

Closed crimeacs closed 3 years ago

crimeacs commented 3 years ago

Frequency filtering produces artifacts at the beginning and the end of the signal. I suggest using tapering like this:

def taper(waveform, func, max_percentage, max_length=None):

        npts = len(waveform)

        # store all constraints for maximum taper length
        max_half_lenghts = []
        if max_percentage is not None:
            max_half_lenghts.append(int(max_percentage * npts))
        if max_length is not None:
            max_half_lenghts.append(int(max_length * self.stats.sampling_rate))

        # add full trace length to constraints
        max_half_lenghts.append(int(npts / 2))
        # select shortest acceptable window half-length
        wlen = min(max_half_lenghts)

        if 2 * wlen == npts:
            taper_sides = func(2 * wlen)
        else:
            taper_sides = func(2 * wlen + 1)
        taper = torch.tensor(np.hstack((taper_sides[:wlen], np.ones(npts - 2 * wlen),
                               taper_sides[len(taper_sides) - wlen:])))

        waveform *= taper
        return waveform

input = torch.randn(size=(100,))
a = taper(input, func=torch.hann_window, max_percentage=0.05)
iver56 commented 3 years ago

Can you please help me pinpoint the issue a bit better? E.g. show a waveform, share some audio files or show a spectrogram? I just listened to some low-pass-filtered, high-pass-filtered and band-pass-filtered outputs, and zoomed in on the start and the end of the waveforms, and found nothing noteworthy. After I understand the issue, I can understand whether tapering is a good solution, or if the issue should be fixed in the core algorithm instead.

I've attached the audio examples that I investigated:

filter_examples.zip

bilde

spectrograms

crimeacs commented 3 years ago

Perhaps you are right. I cannot reproduce this behaviour anymore, so perhaps I had some other problem in the pipeline that was leading to this behaviour.

iver56 commented 3 years ago

Ok, if you're able to narrow it down, please share your findings. In that case this issue can be reopened or a new one can be created