www.gusucode.com > signal 案例源码程序 matlab代码 > signal/ZeroPhaseFilteringOfAnElectrocardiogramWaveformExample.m

    %% Zero-Phase Filtering of an Electrocardiogram Waveform
% Zero-phase filtering helps preserve features in a filtered time waveform
% exactly where they occur in the unfiltered signal.

% Copyright 2015 The MathWorks, Inc.


%%
% 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')