next up previous contents
Next: Standard Analog Filter topologies Up: Some filter concepts Previous: Bandpass filters   Contents

filtfilt. Causal versus non-causal filters

Any filter that operates in ``real-time'' is a causal filter in that it's output at time t0 can only depend on events that have occured for t$ \le$t0. But, if we have recorded data and are processing it after the fact, then we have knowledge of what happened at t > t0, so the output at time t = t0 could make use of that. The best example of these so-called non-causal filters is matlab's filtfilt command, which applies a filter to a sequence of data, takes the output, reverses it, and applies the same filter again. The phase delays from the forward and backwards filtering cancel each other out, so there is no effective filter delay. Obviously such a process cannot happen in real-time and all real-time filters must have some delay.

An example of the use of filtfilt() is shown in Figure 1. You can see that although filter() and filtfilt() both remove the high frequency noise, the use of filter() introduces significant phase delay. filtfilt() has zero phase delay. Further, at t=0, the output of filter() starts at 0, whereas the output of filtfilt() has matched the initial conditions exactly 2.

Because it filters the data twice, once forward and once backward, filtfilt() also has a better amplitude response (i.e. sharper cutoff and more attentuation in the stop band).

For almost all of my day-to-day filtering needs, I use filtfilt(). I only use filter() when I specifically need to simulate a real-time process.

More information on causal versus non-causal filters can be found at http://en.wikipedia.org/wiki/Causal_filter

Figure 1: Example of using filtfilt() versus filter().
Image filtfilt

%code to generate the above figure

Fs = 300;

t = 0:(1/Fs):1;

sig = cos(5 * 2 * pi * t) + 0.4 * rand(1, length(t));

[b,a] = butter(4, 10 / (Fs/2));

figure;

plot(t, sig, t, filter(b,a, sig), t, filtfilt(b,a,sig));

legend('Original 5 Hz Signal with noise', 'Using filter(), 4 pole butterworth @ 10 Hz', 'Using filtfilt()');

xlabel('Time (s)');

ylabel('Response');

title('Effect of filtfilt() versus filter()')

ylim([-1 1.7])

print -dpng filtfilt.png



Footnotes

...2
It is possible to set the initial conditions using filter(), but it's a lot harder and generally not worth it. Just use filtfilt().

next up previous contents
Next: Standard Analog Filter topologies Up: Some filter concepts Previous: Bandpass filters   Contents " . $row['Name'] . " Posted on " . $row['DateTime']; echo "
"; echo $row['Comment']; echo "

"; } echo "
"; ?> Leave a comment on this page:

Name: (optional)
To prove you are not a robot, what is 2+3?

Creative Commons License
This work by Daniel Kiracofe (daniel dot kiracofe at gmail dot com) is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License./' $I