× {{alert.msg}} Never ask again
Get notified about new tutorials RECEIVE NEW TUTORIALS

Should I use lfilter or filtfilt to filter a signal?

Jan 29, 2016
<p><span style="color:rgb(95, 99, 102)">There are different functions to filter signals in SciPy:</span></p> <p><code>filtfilt</code> runs the filter forward and backward in time across the data, which produces a <span style="color:rgb(95, 99, 102)">zero-phase response.  No frequencies are shifted in time, only attenuated.</span>  Filtering backwards in time requires full knowledge of the future, though, so it can't be used in "online" real-life applications, only for offline processing of recordings of signals.  This would be best for something like smoothing of a financial or weather data series, for instance, where you don't want any phase shift.</p> <p><code>lfilter</code> is causal forward-in-time filtering only, similar to a real-life electronic filter.  It is usually not linear-phase, and adds some delay to the signal.  This would be better for audio processing, to avoid "pre-echo", for instance, or for <span style="color:rgb(95, 99, 102)">real-time processing that can't predict the future</span>.</p> <p>Here's an example.  On the left is <code>scipy.<span style="color:rgb(95, 99, 102)">signal.filtfilt(b, a, sig)</span></code><span style="color:rgb(95, 99, 102)">, and on the right is <code>scipy.signal.lfilter(b, a, sig)</code> (applied twice, to get the same order filter).  The frequency responses of both are the same, but you can see that the time-domain responses are not the same, with the <code>lfilter</code> response delayed and slightly different.</span></p> <p><img alt="" src="http://i.imgur.com/wNngfRo.png" style="height:577px; width:714px"></p> <p> </p>
comments powered by Disqus