You are here : matlabSignal Processingfiltfilt

filtfilt() - Signal Processing

```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: Zero-phase distortionA filter transfer function, which equals the squared
magnitude of the original filter transfer functionA filter order that is double the order of the filter
specified by b and afiltfilt 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. SOS is an L-by-6
matrix containing the L second-order sections. SOS must
be of the form(b01b11b21a01a11a21b02b12b22a02a12a22⋮⋮⋮⋮⋮⋮b0Lb1Lb2La0La1La2L)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.Note:

When implementing zero-phase filtering using a second-order
section filter, the length of the input, x, must
be more than three times the filter order. You can use filtord to obtain the order of the filter.y = filtfilt(d,x) zero-phase filters the
input data, x, using a digital filter, d.
Use designfilt to generate d based
on frequency-response specifications.```

Syntax

`y = filtfilt(b,a,x)y = filtfilt(SOS,G,x)y = filtfilt(d,x)`

Example

```Zero-Phase Filtering of an Electrocardiogram WaveformOpen This Example
Zero-phase filtering helps preserve features in a filtered time waveform exactly where they occur in the unfiltered signal.
To illustrate the use of filtfilt for zero-phase filtering, consider an electrocardiogram waveform.wform = ecg(500);

plot(wform)
axis([0 500 -1.25 1.25])
text(155,-0.4,'Q')
text(180,1.1,'R')
text(205,-1,'S')

The QRS complex is an important feature in the ECG. Here it begins around time point 160.Corrupt the ECG with additive noise. Reset the random number generator for reproducible results. Construct a lowpass FIR equiripple filter and filter the noisy waveform using both zero-phase and conventional filtering.rng default

x = wform' + 0.25*randn(500,1);
d = designfilt('lowpassfir', ...
'PassbandFrequency',0.15,'StopbandFrequency',0.2, ...
'PassbandRipple',1,'StopbandAttenuation',60, ...
'DesignMethod','equiripple');
y = filtfilt(d,x);
y1 = filter(d,x);

subplot(2,1,1)
plot([y y1])
title('Filtered Waveforms')
legend('Zero-phase Filtering','Conventional Filtering')

subplot(2,1,2)
plot(wform)
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. Conventional filtering reduces noise in the signal, but delays the QRS complex.Repeat the above using a Butterworth second-order section filter.d1 = designfilt('lowpassiir','FilterOrder',12, ...
'HalfPowerFrequency',0.15,'DesignMethod','butter');
y = filtfilt(d1,x);

subplot(1,1,1)
plot(x)
hold on
plot(y,'LineWidth',3)
legend('Noisy ECG','Zero-Phase Filtering')```