# Apply “non-causal” filter buffer-wise, a.k.a “soft real-time filtering”

I am dealing with digital filtering of signals, both offline and in real-time. Typical filtering purposes are highpass filter or bandpass filter.

So far I worked on prerecorded signals (e.g. wav files) so I could use a non-causal filter, such as Python's SciPy filtfilt or MATLAB's filtfilt, which produces zero-phase filtering (see What is zero-phase filtering and forward-backward filtering?). This has the desired effect of canceling any phase distortion. So far it worked very well off-line. However, since according to theory this kind of filter is non-causal (e.g. the implementation of filtfilt has a stage in which the signal is filtered backwards from its end to its start), it can't be used for "hard" real-time filtering (i.e. reading single sample at a time and immediately filtering it).

I ask if this non-causal filter can be used for "soft" real-time filtering, that is: I read a buffer of n samples, then filter the buffer using filtfilt in the time-domain or by using its frequency response (which is purely real, obtained from the filter by the SciPy function freqz) in the frequency domain (via FFT), and then I read the next buffer and so on.

Another question is what is the recommended way to apply this filter: in the time-domain via filtfilt or in the frequency domain (multiplying the FFT coefficient $$F(k)$$ with the real positive frequency response $$|H(k)|$$)? The purpose is to have a minimally-distorted filtered signal and fast and computationally efficient filtering (to run in "soft" real-time).

In my application a filtering in the frequency space is desired, and multiplying $$F(k)$$ by $$|H(k)|$$ seems to work, even in real-time. But multiplying by the absolute value is like performing zero-phase filtering, which by theory is non-causal. However, since I apply it buffer-wise, this seems to be the loophole. Am I correct?

Thanks.

• Why don't you use a linear phase FIR filter? – Matt L. Aug 26 '20 at 12:25
• I used an IIR filter which had a very good response: sharp roll-off and a very flat pass without ripples typical to FIR, and with zero-phase. This yielded good results, so I stuck to it. – Triceratops Aug 26 '20 at 13:41
• @Triceratops for some applications Butterworth filter might be good enough – Gideon Genadi Kogan Aug 26 '20 at 15:27
• Perhaps you are interested in the Powell-Chau thingie. That's real-time filtfilt() but there is some big buffering going on. – robert bristow-johnson Aug 27 '20 at 4:09
• really, if you're trying to forward-backward filter with an IIR, you need to truncate the IIR to an FIR, or you can't ever make it causal. That will hurt your design objectives. Really, just design a linear phase FIR and be done with it. Then you don't have to filtfilt. – Marcus Müller Aug 27 '20 at 14:58