MATLAB Examples

Removing High-Frequency Noise from an ECG Signal

This examples shows you how to filter an ECG signal that has high-freqquency noise, and remove the noise by low-pass filtering.

% Create one period of ECG signal
x = ecg(500).';
y = sgolayfilt(x,0,5);
Fs = 1000;
[M,N] = size(y);

% Initialize scopes
TS = dsp.TimeScope('SampleRate',Fs,...
                      'TimeSpan',1.5,...
                      'YLimits',[-1 1],...
                      'ShowGrid',true,...
                      'NumInputPorts',2,...
                      'LayoutDimensions',[2 1],...
                      'Title','Noisy and Filtered Signals');

% Design lowpass filter
Fpass  = 200;
Fstop = 400;
Dpass = 0.05;
Dstop = 0.0001;
F     = [0 Fpass Fstop Fs/2]/(Fs/2);
A     = [1 1     0     0];
D     = [Dpass Dstop];
b = firgr('minorder', F, A, D);
LP = dsp.FIRFilter('Numerator',b);

% Design Highpass Filter
Fstop = 200;
Fpass = 400;
Dstop = 0.0001;
Dpass = 0.05;
F = [0 Fstop Fpass Fs/2]/(Fs/2); % Frequency vector
A = [0 0     1     1]; % Amplitude vector
D = [Dstop   Dpass];   % Deviation (ripple) vector
b  = firgr('minord', F, A, D);
HP = dsp.FIRFilter('Numerator', b);

% Stream
tic;
while toc < 30
    x = .1 * randn(M,N);
    highFreqNoise = HP(x);
    noisySignal    = y + highFreqNoise;
    filteredSignal = LP(noisySignal);
    TS(noisySignal,filteredSignal);
end

% Finalize
release(TS)