Zero-phase digital filtering
y = filtfilt(b,a,x)
y = filtfilt(SOS,G,x)
y = filtfilt(b,a,x) performs zero-phase digital filtering by processing the input data, x, in both the forward and reverse directions . filtfilt operates along the first nonsingleton dimension of x. The vector b provides the numerator coefficients of the filter and the vector a provides the denominator coefficients. If you use an all-pole filter, enter 1 for b. If you use an all-zero filter (FIR), enter 1 for a. After filtering the data in the forward direction, filtfilt reverses the filtered sequence and runs it back through the filter. The result has the following characteristics:
A filter transfer function, which equals the squared magnitude of the original filter transfer function
A filter order that is double the order of the filter specified by b and a
filtfilt minimizes start-up and ending transients by matching initial conditions, and you can use it for both real and complex inputs. Do not use filtfilt with differentiator and Hilbert FIR filters, because the operation of these filters depends heavily on their phase response.
Note: The length of the input x must be more than three times the filter order defined as max(length(b)-1,length(a)-1).
y = filtfilt(SOS,G,x) zero-phase filters the data x using the second-order section (biquad) filter represented by the matrix SOS and scale values G. The matrix SOS is an L-by-6 matrix containing the L second-order sections. The matrix SOS must be of the form:
where each row are the coefficients of a biquad filter. The vector of filter scale values, G, must have a length between 1 and L+1.
Zero-phase filtering helps preserve features in the filtered time waveform exactly where those features occur in the unfiltered waveform. To illustrate the use of filtfilt for zero-phase filtering, consider an electrocardiogram waveform as an example.
plot(ecg(500)); %plot ECG signal axis([0 500 -1.25 1.25]);
The QRS complex is an important feature in the ECG waveform beginning around time point 160 in this example.
The following example corrupts the ECG waveform with additive noise, constructs a lowpass FIR equiripple filter, and filters the noisy waveform using both zero-phase and conventional filtering. Because the filter is an all-zero (FIR) filter, the denominator equals 1. Seed the random number generator for reproducible results.
rng default; x=ecg(500)'+0.25*randn(500,1); %noisy waveform h=fdesign.lowpass('Fp,Fst,Ap,Ast',0.15,0.2,1,60); d=design(h,'equiripple'); %Lowpass FIR filter y=filtfilt(d.Numerator,1,x); %zero-phase filtering y1=filter(d.Numerator,1,x); %conventional filtering subplot(211); plot([y y1]); title('Filtered Waveforms'); legend('Zero-phase Filtering','Conventional Filtering'); subplot(212); plot(ecg(500)); title('Original Waveform');
Zero-phase filtering reduces noise in the signal and preserves the QRS complex at the same time it occurs in the original signal. Conventional filtering reduces noise in the signal, but delays the QRS complex.
Repeat the above using a Butterworth second-order section filter:
h=fdesign.lowpass('N,F3dB',12,0.15); d1 = design(h,'butter'); y = filtfilt(d1.sosMatrix,d1.ScaleValues,x); plot(x,'b-.'); hold on; plot(y,'r','linewidth',3); legend('Noisy ECG','Zero-phase Filtering','location','NorthEast');
 Oppenheim, A.V., and R.W. Schafer, Discrete-Time Signal Processing, Prentice-Hall, 1989, pp.284–285.
 Mitra, S.K., Digital Signal Processing, 2nd ed., McGraw-Hill, 2001, Sections 4.4.2 and 8.2.5.
 Gustafsson, F., Determining the initial states in forward-backward filtering, IEEE® Transactions on Signal Processing, April 1996, Volume 44, Issue 4, pp.988–992.