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.