filtfilt

Filter a signal forward and then backward, compensating for end effects.

Syntax

filtfilt(b,a,x)

Inputs

b
The numerator polynomial coefficients of the filter.
Dimension: vector
a
The denominator polynomial coefficients of the filter.
Dimension: vector
x
The signal to be filtered. If x is a matrix, then each column is filtered.
Dimension: vector | matrix

Outputs

y
The filtered signal.

Example

Filter a signal with a 100 Hz Butterworth filter using filtfilt.

 % define signal
f1 = 62.5;
f2 = 250;
omega1 = 2*pi*f1;
omega2 = 2*pi*f2;
fs = 2000;
ts = 1/fs;
n = 100;
t = [0:ts:n*ts];
signal = sqrt(2) * sin(0.5*omega1*t) + sin(omega1*t) + 0.25 * sin(omega2*t);

% define filter
[a,b] = butter(4,100/(fs/2));

% filter signal and plot
output = filtfilt(a,b,signal);
plot(t,signal);
hold on;
plot(t,output);
legend('raw signal', 'filtered signal');
Figure 1. filtfilt figure 1

Comments

Since the signal is filtered forward and backward, the attenuation is 6dB at the cutoff frequency. To obtain a 3dB, the cutoff frequency must be adjusted. This can be done with buttord. The advantage of filtfilt is that filtering in both directions eliminates the time lag that is introduced with a single pass filter. filtfilt compensates for filtering end effects by using a reflection technique, combined with matching the initial filter conditions to the signal endpoints.

When x is a matrix, the function operates along the first dimension, which is the column dimension in the 2D case.