MATLAB: Why 50hz can’t be removed compeletely

50hz

Dear, I am try to remove the 50 hz. It works very well if I plot the result using amplitude. However when when I plot the result using 10*log10(amp), the 50hz will be still there with large amplitude.
%% build filter
N = 20; % Order
F3dB1 = 49; % First
F3dB2 = 51; % Second
Fs = 1000; % Sampling Frequency
d = designfilt('bandstopiir','FilterOrder',2, ...
'HalfPowerFrequency1',49,'HalfPowerFrequency2',51, ...
'DesignMethod','butter','SampleRate',Fs);
%% test data
fs = 1000;
t = 0:1/fs:5-1/fs;
x = cos(2*pi*50*t)+cos(2*pi*100*t);
%plot(t,x)
%plotChannelSpectral(x);
[psd,f] = pwelch(x,500,200,500,1000);
plot(f,10*log10(psd));
plot(f,(psd));
fdata=filtfilt(d,double(x));
[psd2,f2] = pwelch(fdata,500,200,500,1000);
figure;
plot(f2,psd2); % 50hz almost gone in this plot
plot(f,10*log10(psd2)); % however I still can see large 50hz in this plot, even I re-run the filtfilt 20 times, I still can see relative large amplitude around 50hz, though there is a groove at 50hz.
Does it means that I should be satisfied as long as I can't see the 50hz in amplitude? And there is no way to totally eliminate the large value under 10*log10(amplitude)?
Thanks in advance.

Best Answer

hello again
I like simple solutions - see demo code below adapted to your case
I hope 200 dB attenuation can be considered as "complete washed out" !!
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%



% FFT parameters
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
NFFT = 1024*4; %
NOVERLAP = round(0.75*NFFT);
w = hanning(NFFT); % Hanning window / Use the HANN function to get a Hanning window which has the first and last zero-weighted samples.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% load signal
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% test data
Fs = 1000;
t = 0:1/Fs:10-1/Fs;
signal = cos(2*pi*50*t)+cos(2*pi*100*t);
%% notch filter section %%%%%%
% H(s) = (s^2 + 1) / (s^2 + s/Q + 1)
fc = 50; % notch freq
wc = 2*pi*fc;
Q = 2; % adjust Q factor for wider (low Q) / narrower (high Q) notch
% at f = fc the filter has gain = 0
w0 = 2*pi*fc/Fs;
alpha = sin(w0)/(2*Q);
b0 = 1;
b1 = -2*cos(w0);
b2 = 1;
a0 = 1 + alpha;
a1 = -2*cos(w0);
a2 = 1 - alpha;
% analog notch (for info)
num1=[1/wc^2 0 1];
den1=[1/wc^2 1/(wc*Q) 1];
% digital notch (for info)
num1z=[b0 b1 b2];
den1z=[a0 a1 a2];
freq = linspace(fc-1,fc+1,200);
[g1,p1] = bode(num1,den1,2*pi*freq);
g1db = 20*log10(g1);
[gd1,pd1] = dbode(num1z,den1z,1/Fs,2*pi*freq);
gd1db = 20*log10(gd1);
figure(1);
plot(freq,g1db,'b',freq,gd1db,'+r');
title(' Notch: H(s) = (s^2 + 1) / (s^2 + s/Q + 1)');
legend('analog','digital');
xlabel('Fréquence (Hz)');
ylabel(' dB')
% now let's filter the signal
signal_filtered = filtfilt(num1z,den1z,signal);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% display : averaged FFT spectrum before / after notch filter
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
[sensor_spectrum, freq] = pwelch(signal,w,NOVERLAP,NFFT,Fs);
sensor_spectrum_dB = 20*log10(sensor_spectrum);% convert to dB scale (ref = 1)

[sensor_spectrum_filtered, freq] = pwelch(signal_filtered,w,NOVERLAP,NFFT,Fs);
sensor_spectrum_filtered_dB = 20*log10(sensor_spectrum_filtered);% convert to dB scale (ref = 1)
figure(2),semilogx(freq,sensor_spectrum_dB,'b',freq,sensor_spectrum_filtered_dB,'r');grid
title(['Averaged FFT Spectrum / Fs = ' num2str(Fs) ' Hz / Delta f = ' num2str(freq(2)-freq(1)) ' Hz ']);
legend('before notch filter','after notch filter');
xlabel('Frequency (Hz)');ylabel(' dB')
Related Question