gjmvanboxtel / gsignal

R implementation of the Octave signal package
GNU General Public License v3.0
22 stars 6 forks source link

Feature request: use fftfilter in filtfilt #24

Closed dipterix closed 1 month ago

dipterix commented 2 months ago

FIR filter is super slow when used in filtfilt because it's calculated in time-domain. I wonder if it's possible to call fftfilter when length(a)==1 to speed up this process?

gjmvanboxtel commented 2 months ago

Hi,

Thanks. I am hesitant about this request because both the Octave and Matlab functions use filter not fftfilt. The function filtfilt is is used for forward and reverse filtering, and although it supports FIR filters, it is actually not necessary in that case. Because FIR filters have a constant delay at each frequency (inspect via grpdelay), you can easily do only forward filtering and correct for the delay. No reverse filtering is needed in that case. And of course you can either use filter or fftfilt as it suits you.

A simple example of correcting for the phase delay is shown in the vignette. You might want to extend that procedure by prepending the signal by the reflected initial number of samples equal to the filter order, and by computing the initial conditions to remove the DC offset.

Best, Geert