0
$\begingroup$

Doing some work at the minute on digital filters in matlab, I have a file with artifical noise added (sine wave added at specific frequency). The goal is to filter the signal and get it as close as possible to the clean signal provided.

I've done an FFT and plotted the results and found a very large spike at 29.3Hz which is not present in the clean signal.

I've tried using a notch filter, which I thought would work since it operates at such a specific frequency, however it just seems to attenuate the signal and remove some power but not block it completely. I then added a bandstop filter to try and block any signals in that region and it simply attenuated the signal also. Does anyone have any thoughts? I just seem to be lowering power of the entire signal and not actually removing anything, getting the basic shape of the clean signal but still a lot of noise present after both filters. Thanks!

enter image description here

[b1,a1] = iirnotch((29.3*(2/fs)),0.99999);
IIR1 = filter(b1,a1,ecg58_DC_removed); 


FFT_resultFilter1 = (1/length(t))*fft(IIR1);
f=(0:1024)/1024*(200/2);
figure(4)
stem(f, 2*abs(FFT_resultFilter1(1:1025)));
xlabel ('Frequency (Hz)');
ylabel ('Spectral Magnitude');
title('First filter')
grid on

[a2,b2] = butter(2,[29.2 29.4]*2/fs, 'stop');
IIR2 = filter(a2,b2,IIR1); 

FFT_resultFilter2 = (1/length(t))*fft(IIR2);
f=(0:1024)/1024*(200/2);
figure(5)
stem(f, 2*abs(FFT_resultFilter2(1:1025)));
xlabel ('Frequency (Hz)');
ylabel ('Spectral Magnitude');
title('First filter')
grid on

figure (6)
plot(t(1:1000), IIR2(1:1000));
xlabel('time (s)')
ylabel('amplitude')
title('two filters');

b1 =

1.0e-04 *

0.1571   -0.1902    0.1571

a1 =

1.0000   -0.0000   -1.0000
$\endgroup$
7
  • $\begingroup$ Can you share some plots please? I can't run your code because I don't have your original data. BTW, no filter is perfect. Slight attenuation outside the filter's stop band is expected. You can design your filter based on how much attenuation you can tolerate outside your stop band, and how much you want to attenuate inside the stop band. $\endgroup$
    – Atul Ingle
    Dec 4, 2016 at 18:46
  • $\begingroup$ I think the problem is in the bandwidth you defined in the call to iirnotch. The bandwidth is very large, i.e. the Q factor of the filter is very small, resulting in a useless filter. According to the Mathworks doc page, you could try choosing BW=w0/35. $\endgroup$
    – Matt L.
    Dec 4, 2016 at 19:07
  • $\begingroup$ Sorry but for whatever reason stack wont allow me to reply. I have tried using w0/35 no filtering is really happening at all at this bandwidth, signal looks better at a bandwidth >0.9 $\endgroup$
    – John Smith
    Dec 4, 2016 at 19:12
  • $\begingroup$ Please add the filter coefficients [b1,a1] to your answer so we can see what's going on. $\endgroup$
    – Matt L.
    Dec 4, 2016 at 19:21
  • $\begingroup$ Filter Coefficients added! $\endgroup$
    – John Smith
    Dec 4, 2016 at 20:02

2 Answers 2

2
$\begingroup$

As mentioned in my comment, the filter returned by iirnotch is useless. From your filter coefficients you can see that the filter is only marginally stable due to two poles on the unit circle at DC and at Nyquist. Furthermore, even though the filter has a notch, it also attenuates all other frequencies quite strongly (apart from DC and Nyquist). The reason for that behavior is the extremely large bandwidth in your specification.

The figure below shows the magnitude responses of the filter you designed (top) and of a notch filter with a bandwidth BW = w0/35 (bottom) (note that the extremely large values very close to DC and Nyquist due to the poles are not shown in the top figure):

enter image description here

In any case, the bottom figure is what a notch filter should look like. If you tried that filter and it didn't do what you expected it to do, then the reason might be that your estimation of the noise frequency is wrong. Could it be that you got it wrong by a factor of $2$ (i.e., it would be a 60Hz hum)? [Also, doesn't the file name ecg58... suggest a disturbance at $2\cdot 29=58$Hz?]

So there might be several problems in your approach, but one is definitely the design of the notch filter, and if I may guess I would say that the other is the estimation of the noise frequency.

$\endgroup$
0
$\begingroup$

"I just seem to be lowering power of the entire signal and not actually removing anything". Yes, that is exactly it.

Suggestion: whenever you are trying to design a new figure, use the freqz() function to visualize the frequency response function. Using the coefficients you gave, I get the FRF below. As you can see, you are decreasing the amplitude of the entire signal by about 1000, and over a smaller region, decreasing it by about 1 billion. Probably not a useful filter.

Try using a smaller value for the BW, and see how that changes the filter. 0.1 or so is probably a lot more useful that 0.9999

enter image description here

$\endgroup$

Your Answer

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

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