Digital filter not removing noise at specific frequency Matlab 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!



[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
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):



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.