Torchaudio.functional.lfilter VS scipy.signal.lfilter

I’m testing some filters features using torchaudio.
I wanted to filter a sinus sweep using an IIR A-Weighting filter (cf. A-weighting - Wikipedia).
I also use an old version of torchaudio (0.7.2) but I’ve done my tests using the latest one with the same results.
There is the code that I used with Python 3.8.3: GitHub - a7brusco/torchaudio_vs_scipy: Testing torchaudio feature and scipy.signal (specially filters)
Is there an meaning why the torchaudio lfilter and the scipy lfilter functions are so different ?

1 Like

After many tries with my code, I found that the difference between the 2 filters are caused by precision error on my coeffs. Scipy used my coeffs as an float64 np.ndarray while pytorch converted the coeffs as FloatTensor (which have float32 precision). IIR filters are pretty sensible about precision error (especially high order filters). Moreover, it seems that the torchaudio.functional.lfilter doesn’t support DoubleTensor (float64 dtype). I have no choice to use the scipy function for that kind of filter…

Yes, the precision error can be magnified when dealing with IIR filters, especially when the order is high. This is also stated in the online document. I recommend you either use double-precision or break down the filter coefficients into cascaded second-order filters using some root-finding algorithms (e.g. np.roots).

Moreover, it seems that the torchaudio.functional.lfilter doesn’t support DoubleTensor (float64 dtype).

torchaudio.functional.lfilter does support double-precision, which is included in our unit tests. If you believe it’s a bug please submit an issue to our repo so we can look into it.

Thank you for the response. I’m using an old version of torchaudio (0.7.2) which explains why torchaudio.functional.lfilter doesn’t support double-precision for me. I retry using the last version and double precision and it works prefectly !

1 Like