Low pass filter in PyTorch?

kinwai_cheuk: <p>It seems there’s no low pass filter in PyTorch.<br>
If I want to write my own low pass filter function <code>low_pass()</code></p>
<p>How do I make sure that it can apply to whole batch?</p>
<p>During training, the dataloader will give me a tensor <code>X</code> with the shape <code>(batch, audio_len)</code>.<br>
But my neural network design takes in 4 inputs</p>
<ol>
<li>The origianl <code>X</code>
</li>
<li>The lowpassed+downsampled version of <code>X</code>, let’s call it <code>X_down1</code>
</li>
<li>lowpassed+downsampled <code>X_down1</code> again to get <code>X_down2</code>
</li>
<li>Same opeations again to get <code>X_down3</code>
</li>
</ol>
<p>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.</p>
<p>And I think I need to write my own <code>low_pass</code> function. But how to make sure that this function also applies to the whole batch just like Average pooling?</p>

Shisho_Sama: <p>Hi, I guess you can do sth like this</p>
<pre><code class="lang-auto"># 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
</code></pre>
<p>or simply use the Conv1d and assign the new weight to it and then freeze the weight so it doesnt get tuned.</p>
<pre><code class="lang-auto">self.myop = nn.Conv1d(*args)
self.myop.weight = nn.Parameter(torch.randn(size=(x)) 
self.myop.requires_grad = False
</code></pre>
<p>Then use it along side other layers, etc.</p>
<p>You can also create your own module. so its up to you.</p>

kinwai_cheuk: <p>I am relatively new to digital signal process. May I ask what weight corresponds to a low pass filter?</p>

Shisho_Sama: <p>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.<br>
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.</p>

Markus_B: <p>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 (<a href="https://pytorch.org/audio/functional.html" rel="nofollow noopener">https://pytorch.org/audio/functional.html</a>). 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.<br>
Hope that helps.</p>
<p>Edit:<br>
There is also a prepared biquad low pass filter in torchaudio if that’s feasible for your use case.</p>

TriKri: <p><a class="mention" href="/u/kinwai_cheuk">@kinwai_cheuk</a>  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.</p>
<p>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).</p>

Butian_Xiong: <p><a class="mention" href="/u/shisho_sama">@Shisho_Sama</a> 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.</p>
<p>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.</p>
<p>You can check out the info about FFT in this course: <a href="https://youtu.be/gZNm7L96pfY" class="inline-onebox" rel="noopener nofollow ugc">Lecture 1 | The Fourier Transforms and its Applications - YouTube</a></p>