Low pass filter in PyTorch?

It seems there’s no low pass filter in PyTorch.
If I want to write my own low pass filter function low_pass()

How do I make sure that it can apply to whole batch?

During training, the dataloader will give me a tensor X with the shape (batch, audio_len).
But my neural network design takes in 4 inputs

  1. The origianl X
  2. The lowpassed+downsampled version of X, let’s call it X_down1
  3. lowpassed+downsampled X_down1 again to get X_down2
  4. Same opeations again to get X_down3

For the downsample part, I can use the Average pooling 1D to get what I want, and it’s fast, since it applies to the whole batch.

And I think I need to write my own low_pass function. But how to make sure that this function also applies to the whole batch just like Average pooling?

Hi, I guess you can do sth like this

# instead of random values, put your own 
weight = nn.Parameter(torch.randn(size=(x)) 
def my_op(input, weight) : 
   output = F.conv1d(input, weight, bias=None, stride=1, padding=0, dilation=1, groups=1)  
   return output

or simply use the Conv1d and assign the new weight to it and then freeze the weight so it doesnt get tuned.

self.myop = nn.Conv1d(*args)
self.myop.weight = nn.Parameter(torch.randn(size=(x)) 
self.myop.requires_grad = False

Then use it along side other layers, etc.

You can also create your own module. so its up to you.

I am relatively new to digital signal process. May I ask what weight corresponds to a low pass filter?

Unfortunately I’m not an expert in audio domain. You have to figure this out yourself. You can ask this on stackoverflow or reddit or wait for someone here to answer you.
Make sure to edit your first post and indicate that you do not know the low pass filter for audio files so anyone that opens this thread can see it and help you with it.

I’m maybe a bittle late on this one but if anyone stumbles across it (as me), you might want to pay attention to torchaudio (https://pytorch.org/audio/functional.html). You can use it to create an IIR filter or an FIR filter if your signal is deterministic and you are fine with loosing a few samples equal to the filter size (number of weights). For the latter case just set the denominator to 1. You can find the right weights with a little research on Google (note that those need to be selected according to your sampling rate) or by playing around a little with scipys filter functions.
Hope that helps.

Edit:
There is also a prepared biquad low pass filter in torchaudio if that’s feasible for your use case.

1 Like

@kinwai_cheuk A low pass filter is just any filter that lets frequency components with low frequencies pass but attenuates components with high frequencies. For example, any filter that blurs an image (e.g. Gaussian blur or box blur) can be considered a low pass filter, because it removes the details, which are high frequency in nature. In contrast, a high pass filter lets high frequency components pass but attenuates low frequency components.

A low pass filter is often used to filter out noise in data, since the noise-free data usually has most of its information stored in relatively low frequencies (therefore you want to preserve the low frequencies), while the noise typically has its information stored in a much wider range of frequencies (therefore you want to remove all other frequencies).

1 Like

@Shisho_Sama Hi, as TriKri has said before, a low pass filter is a filter that lets low frequencies signal pass in. The easiest way and most common way is by using the FFT(fast Fourier Transformation) to transform original spatial information into frequency information.

For instance, you can use FFT to transform a picture into a spectrum picture. Where the centre of the spectrum picture represents the low frequency and the edge of the spectrum represents the high frequency.

You can check out the info about FFT in this course: Lecture 1 | The Fourier Transforms and its Applications - YouTube

1 Like