# notch filter to remove 20Hz noise of ECG signal

Started by December 19, 2005
```Hey
Im a student and need help using matlab. im extremeley new to signal
processing so this is a pretty basic question, im hoping someone can help
me.

im removing noise from an ecg signal and i need to remove this noise at
20hz using a notch filter, sampling rate is 200hz.  the matlab code is:

>>>a=[x x x]
>>>b=[x x x]
>>>zfiltered=filter(a,d,z)

'z' is my noise corrupted signal, but how do i work out the coeffs needed
for 'a' and 'b'?
sorry for the "newbie" question but im in desperte need of help.

Any help is much appreciated,
Mark

```
```If this is a time limited chunk of data and the results are not required in
real time, you might convert the signal to the frequency domain using the
discrete Fourier transform (DFT), windowing the frequency domain data to
eliminate the offending noise, and then performing the inverse discrete
Fourier transform.  This is as easy as it gets and works in Matlab.

If you have to do this in real time, junk Matlab.  If you have to use Matlab
for some reason, investigate the remez function and FIR filters for your data.

In article <zdWdnaXfZfAweTveRVn-iA@giganews.com>, "ashcroft2006"
<mark_ashcroft@hotmail.com> wrote:
>Hey
>Im a student and need help using matlab. im extremeley new to signal
>processing so this is a pretty basic question, im hoping someone can help
>me.
>
>im removing noise from an ecg signal and i need to remove this noise at
>20hz using a notch filter, sampling rate is 200hz.  the matlab code is:
>
>>>>a=[x x x]
>>>>b=[x x x]
>>>>zfiltered=filter(a,d,z)
>
>'z' is my noise corrupted signal, but how do i work out the coeffs needed
>for 'a' and 'b'?
>sorry for the "newbie" question but im in desperte need of help.
>
>Any help is much appreciated,
>Mark
>
>
```
```"ashcroft2006" <mark_ashcroft@hotmail.com> wrote in message
news:zdWdnaXfZfAweTveRVn-iA@giganews.com...
> Hey
> Im a student and need help using matlab. im extremeley new to signal
> processing so this is a pretty basic question, im hoping someone can help
> me.
>
> im removing noise from an ecg signal and i need to remove this noise at
> 20hz using a notch filter, sampling rate is 200hz.  the matlab code is:
>
> >>>a=[x x x]
> >>>b=[x x x]
> >>>zfiltered=filter(a,d,z)
>
> 'z' is my noise corrupted signal, but how do i work out the coeffs needed
> for 'a' and 'b'?
> sorry for the "newbie" question but im in desperte need of help.

Well...since you have matlab, there are several tools (from beginner to
You can either use 'fdatool' or 'sptool' - both give you fairly simple
interfaces to design filters. I suggest you start with an FIR filter (this
will make one set of your coeffs = 1, so you'd use filter(b,1,z) when you
invoke the matlab command).
The help files for either filter design program will give you plenty of info
on how to set various parameters.
Also, you need to know how good your notch filter needs to be at 20 Hz. If
you don't know how good, then you have more reading to do (ask your
professor or TA).

Cheers

```
```ashcroft2006 wrote:
> Hey
> Im a student and need help using matlab. im extremeley new to signal
> processing so this is a pretty basic question, im hoping someone can help
> me.
>
> im removing noise from an ecg signal and i need to remove this noise at
> 20hz using a notch filter, sampling rate is 200hz.  the matlab code is:
>
> >>>a=[x x x]
> >>>b=[x x x]
> >>>zfiltered=filter(a,d,z)
>
> 'z' is my noise corrupted signal, but how do i work out the coeffs needed
> for 'a' and 'b'?
> sorry for the "newbie" question but im in desperte need of help.
>
> Any help is much appreciated,
> Mark

Here is some sample code that you can modify:

Fs = 8000;
Fn = 1000;

r = 0.99;
wz = 2*pi*Fn/Fs;
c = cos(wz);
bnotch=[1, -2*c, 1];
anotch=[1, -2*r*c, r*r];
y = filter(bnotch, anotch, x);

```
```ashcroft2006 wrote:

> Hey
> Im a student and need help using matlab. im extremeley new to signal
> processing so this is a pretty basic question, im hoping someone can help
> me.
>
> im removing noise from an ecg signal and i need to remove this noise at
> 20hz using a notch filter, sampling rate is 200hz.  the matlab code is:
>
>
>>>>a=[x x x]
>>>>b=[x x x]
>>>>zfiltered=filter(a,d,z)
>
>
> 'z' is my noise corrupted signal, but how do i work out the coeffs needed
> for 'a' and 'b'?
> sorry for the "newbie" question but im in desperte need of help.
>
> Any help is much appreciated,
> Mark
>
>

*BUT*
Is your interference *ONLY* at 20 Hz?
Could it also be at harmonics of 20 Hz?

I assume that by "ecg" you mean "electrocardiogram" aka EKG.
More than 40 years ago, as a student tech, I was involved in recording
Electroencephalograph (EEG) traces of individual neurons. Back then I
did not understand "common mode" vs "normal mode" interference. The EE's
on this forum can appreciate just how far I got trying to "filter" the
noise :{

```
```ashcroft2006 wrote:

> Hey
> Im a student and need help using matlab. im extremeley new to signal
> processing so this is a pretty basic question, im hoping someone can help
> me.
>
> im removing noise from an ecg signal and i need to remove this noise at
> 20hz using a notch filter, sampling rate is 200hz.  the matlab code is:
>
> >>>a=[x x x]
> >>>b=[x x x]
> >>>zfiltered=filter(a,d,z)
>
> 'z' is my noise corrupted signal, but how do i work out the coeffs needed
> for 'a' and 'b'?
> sorry for the "newbie" question but im in desperte need of help.
>
> Any help is much appreciated,
> Mark

%%%%%%%%%%%%%%%%%%%%% Notch Filter
Fs = 200;              % sampling freq [Hz]
Fn = Fs/2;             % Nyquist   freq [Hz]

W0 = 20;              % notch frequency [Hz]
w0 = W0*pi/Fn;   % notch frequency normalized

BandWidth = 5;      % -3dB BandWidth [Hz]
B = BandWidth*pi/Fn;  % normalized bandwidth

k1 = -cos(w0);  k2 = (1 - tan(B/2))/(1 + tan(B/2));

b = [1+k2  2*k1*(1+k2)  1+k2];
a = [2  2*k1*(1+k2)  2*k2];

figure(1);       % look at the frequency response of your filter
freqz(b,a);
title('sampling frequency 200Hz, notch @ 20HzHz, notch bandwidth 5Hz');

zfiltered=filter(b,a,z);

% use different bandwidth for having different transient responses
% narrowing the bandwidth-> increasing transient duration
% if you want to check it, filter a unit step signal.
%%%%%%%%%%%%%%%%%%%%%%%%%%

Hope this could help
Bye

Jack

```
```Richard Owlett wrote:
> ashcroft2006 wrote:

> *BUT*
> Is your interference *ONLY* at 20 Hz?
> Could it also be at harmonics of 20 Hz?

I was guessing that it was the third harmonic of 60Hz mains, aliased
down to 20Hz by the 200Hz sampler.

If that's the case, it could be possible to reduce the level of the
20Hz interference by cancelling it (e.g. by sampling the mains,
estimating the 20Hz component, then subtracting it from the ECG
signal), rather than by filtering it out.

It might also be possible to fix this with a 180 Hz notch or low pass
filter prior to sampling at 200Hz.

Or you could sample at some higher frequency, and eliminate the 180Hz
with a simple low pass filter.

These solutions give lower distortion of the wanted signal than a
simple 20Hz notch filter.

Regards,
Allan

```
```allanherriman@hotmail.com wrote:
> Richard Owlett wrote:
>
>>ashcroft2006 wrote:
>
>
>>*BUT*
>>Is your interference *ONLY* at 20 Hz?
>>Could it also be at harmonics of 20 Hz?
>
>
> I was guessing that it was the third harmonic of 60Hz mains, aliased
> down to 20Hz by the 200Hz sampler.
>
> If that's the case, it could be possible to reduce the level of the
> 20Hz interference by cancelling it (e.g. by sampling the mains,
> estimating the 20Hz component, then subtracting it from the ECG
> signal), rather than by filtering it out.
>
> It might also be possible to fix this with a 180 Hz notch or low pass
> filter prior to sampling at 200Hz.
>
> Or you could sample at some higher frequency, and eliminate the 180Hz
> with a simple low pass filter.
>
> These solutions give lower distortion of the wanted signal than a
> simple 20Hz notch filter.

Allan,

If it is as you guess, I'd call it a failure of the anti-alias filter. I
don't see how cancellation would work if the sample clock and power line
weren't derived from the same master, and even so, the phase would
likely depend on the lead placement.

An important issue is whether the signals are already acquired, or if
the acquisition can be made cleaner. It does no good to filter the input
from the leads if there is hum on the power supply.

Jerry
--
Engineering is the art of making what you want from things you can get.
&#2013266095;&#2013266095;&#2013266095;&#2013266095;&#2013266095;&#2013266095;&#2013266095;&#2013266095;&#2013266095;&#2013266095;&#2013266095;&#2013266095;&#2013266095;&#2013266095;&#2013266095;&#2013266095;&#2013266095;&#2013266095;&#2013266095;&#2013266095;&#2013266095;&#2013266095;&#2013266095;&#2013266095;&#2013266095;&#2013266095;&#2013266095;&#2013266095;&#2013266095;&#2013266095;&#2013266095;&#2013266095;&#2013266095;&#2013266095;&#2013266095;&#2013266095;&#2013266095;&#2013266095;&#2013266095;&#2013266095;&#2013266095;&#2013266095;&#2013266095;&#2013266095;&#2013266095;&#2013266095;&#2013266095;&#2013266095;&#2013266095;&#2013266095;&#2013266095;&#2013266095;&#2013266095;&#2013266095;&#2013266095;&#2013266095;&#2013266095;&#2013266095;&#2013266095;&#2013266095;&#2013266095;&#2013266095;&#2013266095;&#2013266095;&#2013266095;&#2013266095;&#2013266095;&#2013266095;&#2013266095;&#2013266095;&#2013266095;
```
```Jerry Avins wrote:
> allanherriman@hotmail.com wrote:
> > Richard Owlett wrote:
> >
> >>ashcroft2006 wrote:
> >
> >
> >>*BUT*
> >>Is your interference *ONLY* at 20 Hz?
> >>Could it also be at harmonics of 20 Hz?
> >
> >
> > I was guessing that it was the third harmonic of 60Hz mains, aliased
> > down to 20Hz by the 200Hz sampler.
> >
> > If that's the case, it could be possible to reduce the level of the
> > 20Hz interference by cancelling it (e.g. by sampling the mains,
> > estimating the 20Hz component, then subtracting it from the ECG
> > signal), rather than by filtering it out.
> >
> > It might also be possible to fix this with a 180 Hz notch or low pass
> > filter prior to sampling at 200Hz.
> >
> > Or you could sample at some higher frequency, and eliminate the 180Hz
> > with a simple low pass filter.
> >
> > These solutions give lower distortion of the wanted signal than a
> > simple 20Hz notch filter.
>
> Allan,
>
> If it is as you guess, I'd call it a failure of the anti-alias filter. I
> don't see how cancellation would work if the sample clock and power line
> weren't derived from the same master, and even so, the phase would
> likely depend on the lead placement.
>
> An important issue is whether the signals are already acquired, or if
> the acquisition can be made cleaner.

This wasn't apparent from the OP's post :(

> It does no good to filter the input from the leads if there is
> hum on the power supply.

PSU ripple on a supply rail would give even harmonics, and magnetic
leakage from the transformer would be mostly fundamental (I think), so
I assumed that the third was being picked up through capacitive
coupling from the mains to the body under test.  Proper probing could
help here.

Too many guesses, and not enough information from the OP!  I'll stop
now.

Regards,
Allan

```