7

What is the best filter to use to remove noise from an ECG signal with matlab?

5
  • What is a "heart.wav"? Is that an EKG?
    – gnovice
    Nov 20, 2009 at 22:53
  • yes. i want to to analyse heart sounds
    – kristia
    Nov 20, 2009 at 23:13
  • if by "heart sound" you meant EKG signal, then you should probably rerphrase the question as such..
    – Amro
    Nov 20, 2009 at 23:24
  • yes i want to know which is the best filter to use to remove noise from a signal of heart. the signal i have is a .wav file. i want to create a fuction for filter and i want to insert the signal file to remove the noise from this file and the new signal to be display and saved.
    – kristia
    Nov 21, 2009 at 14:28
  • I just updated my answer with a number of ways to filter signals
    – Amro
    Nov 21, 2009 at 17:50

2 Answers 2

16

If you have access to the Signal Processing Toolbox, then check out the Savitzky-Golay filter, namely the function sgolay. There's an accompanying demo, just run sgolaydemo.


The following is an example to show the various ways you can apply filtering and de-noising to a signal. Note some of these functions requires certain toolboxes to be present:

% load ecg: simulate noisy ECG
Fs=500;
x = repmat(ecg(Fs), 1, 8);
x = x + randn(1,length(x)).*0.18;

% plot noisy signal
figure
subplot(911), plot(x), set(gca, 'YLim', [-1 1], 'xtick',[])
title('noisy')

% sgolay filter
frame = 15;
degree = 0;
y = sgolayfilt(x, degree, frame);
subplot(912), plot(y), set(gca, 'YLim', [-1 1], 'xtick',[])
title('sgolayfilt')

% smooth
window = 30;
%y = smooth(x, window, 'moving');
%y = smooth(x, window/length(x), 'sgolay', 2);
y = smooth(x, window/length(x), 'rloess');
subplot(913), plot(y), set(gca, 'YLim', [-1 1], 'xtick',[])
title('smooth')

% moving average filter
window = 15;
h = ones(window,1)/window;
y = filter(h, 1, x);
subplot(914), plot(y), set(gca, 'YLim', [-1 1], 'xtick',[])
title('moving average')

% moving weighted window
window = 7;
h = gausswin(2*window+1)./window;
y = zeros(size(x));
for i=1:length(x)
    for j=-window:window;
        if j>-i && j<(length(x)-i+1) 
            %y(i) = y(i) + x(i+j) * (1-(j/window)^2)/window;
            y(i) = y(i) + x(i+j) * h(j+window+1);
        end
    end
end
subplot(915), plot( y ), set(gca, 'YLim', [-1 1], 'xtick',[])
title('weighted window')

% gaussian
window = 7;
h = normpdf( -window:window, 0, fix((2*window+1)/6) );
y = filter(h, 1, x);
subplot(916), plot( y ), set(gca, 'YLim', [-1 1], 'xtick',[])
title('gaussian')

% median filter
window = 15;
y = medfilt1(x, window);
subplot(917), plot(y), set(gca, 'YLim', [-1 1], 'xtick',[])
title('median')

% filter
order = 15;
h = fir1(order, 0.1, rectwin(order+1));
y = filter(h, 1, x);
subplot(918), plot( y ), set(gca, 'YLim', [-1 1], 'xtick',[])
title('fir1')

% lowpass Butterworth filter
fNorm = 25 / (Fs/2);               % normalized cutoff frequency
[b,a] = butter(10, fNorm, 'low');  % 10th order filter
y = filtfilt(b, a, x);
subplot(919), plot(y), set(gca, 'YLim', [-1 1])
title('butterworth')

screenshot

5
  • 2
    I know this is an old answer, but keep in mind there are very specific frequency ranges which need to be preserved for diagnostic accuracy of a surface ECG. Specifically, 0.05-1Hz should be preserved for the highest fidelity ST-segments, and perhaps a low-pass of 40Hz for adults and 150Hz for peds in the remainder of the ECG (An appropriate notch filter for the line frequency is encouraged as well). I'm not as familiar with the Savitzky-Golay FIR, but care should be taken to ensure it preserves important frequencies in the ECG.
    – user7116
    Jul 8, 2013 at 15:44
  • 1
    +1 thank you for the information. I should point out that I dont have much domain knowledge of ECG signals, the above answer was simply from a pure signal processing perspective (by listing various functions one could use to filter a signal in general). Truth is I'm not that familiar with S-G filter either, I've mentioned it because I've often seen it in the literature related to ECG: uap-bd.edu/jcit_papers/vol-1_no-2/IJCIT-110126.pdf
    – Amro
    Jul 8, 2013 at 16:35
  • Neat paper, thank you for the reference! A morphological comparison of the resultant ECG's is the most important factor when considering filters. However, for simple ambulatory monitoring (what many would call "rhythm interpretation") you have a wide latitude in filter selection as you are Ok with some signal distortion.
    – user7116
    Jul 8, 2013 at 16:50
  • my experience with ECG was limited to extracting RR-intervals and the use of Poincaré plots and other HRV features as input to a data-mining/machine-learning application
    – Amro
    Jul 8, 2013 at 17:08
  • This is the best answer! Also, I can plot my CSV unit like this: veri= 'C:\ADS00003.CSV'; delimiter = ','; HEADERLINES = 15; yeniveri = importdata(veri, delimiter, HEADERLINES); EKG=yeniveri.data; B=(1/200)*ones(1,200); A=1; x=filter(B,A,EKG);
    – Bay
    Apr 6, 2014 at 11:43
2

Two filter design tools/demos that you may want to check out:

These should give you a chance to try out different filters and filter parameters to see how they perform with EKG/ECG data.

1
  • When choosing filters, a high-pass of no greater than 0.05Hz is typically necessary to ensure high fidelity of the ST-segments for diagnostic uses of the ECG. Low-pass filters around 40Hz are Ok for adults, 150Hz for peds, and if you'd like to see pacemaker impulses, 150-200Hz is nice to have available.
    – user7116
    Jul 8, 2013 at 15:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.