Cut off frequency doesn't works.
Show older comments
The change in Cut off frequency doen't reflects in output. How to correct ? Also I wnta to display Y axis in dB.
clc;
close all;
clear all;
Fs=200e3;
Ts=1/Fs;
t=0:Ts:(5e-3-Ts);
y=5*sin(2*pi*1000*t)+5*sin(2*pi*20000*t)+10*sin(2*pi*30000*t);
nfft=length(y);
nfft2=2.^nextpow2(nfft);
fy=fft(y,nfft2);
fy=fy(1:nfft2/2);
xfft=Fs.*(0:nfft2/2-1)/nfft2;
cut_off=1.5e3/Fs/2;
order=32;
h=fir1(order,cut_off);
con=conv(y,h);
fh=fft(h,nfft2);
fh=fh(1:nfft2/2);
mul=fh.*fy;
figure(1)
subplot(4,1,1);
plot(t,y);title('Raw signal');xlabel('Time in Sec'),ylabel('Amplitude');
subplot(4,1,2);plot(xfft,abs(fy/max(fy)));title('Freq domain Spark');xlabel('Frequency in Hz'),ylabel('Magnitude');
subplot(4,1,3);plot(abs(mul/max(mul)));title('Filtered output-Frequency domain');xlabel('Frequency in Hz'),ylabel('Magnitude');
subplot(4,1,4);plot(con);title('Filtered output-Time domain');xlabel('Frequency in Hz'),ylabel('Amplitude');
Answers (1)
Fs=200e3;
Ts=1/Fs;
t=0:Ts:(5e-3-Ts);
y=5*sin(2*pi*1000*t)+5*sin(2*pi*20000*t)+10*sin(2*pi*30000*t);
nfft=length(y);
nfft2=2.^nextpow2(nfft);
fy=fft(y,nfft2);
fy=fy(1:nfft2/2);
xfft=Fs.*(0:nfft2/2-1)/nfft2;
% normalize frequency by Fs/2
% cut_off=1.5e3/Fs/2;
cut_off=1.5e3/(Fs/2);
order=32;
h=fir1(order,cut_off);
con=conv(y,h);
fh=fft(h,nfft2);
fh=fh(1:nfft2/2);
mul=fh.*fy;
figure(1)
subplot(4,1,1);
plot(t,y);title('Raw signal');xlabel('Time in Sec'),ylabel('Amplitude');
subplot(4,1,2);plot(xfft,abs(fy/max(fy)));title('Freq domain Spark');xlabel('Frequency in Hz'),ylabel('Magnitude');
subplot(4,1,3);
plot(xfft, abs(mul/max(mul)));
title('Filtered output-Frequency domain');xlabel('Frequency in Hz'),ylabel('Magnitude');
subplot(4,1,4);
plot((0:length(con)-1)/Fs, con);
title('Filtered output-Time domain');xlabel('Time in Sec'),ylabel('Amplitude');
7 Comments
Reji G
on 5 May 2022
First, you have a very small cutoff freq (100/(Fs/2)) since Fs is very high. Therefore your filter may not be able to cut off properly. Second your filter order for such case is two small. Adjust these values (or change the filter types) can overcome these issues to certain extent.
Bwlow, we increase the filter order. The amplitude of the filtered signal is much reduced.
Fs=200e3;
Ts=1/Fs;
t=0:Ts:(5e-3-Ts);
y=5*sin(2*pi*1000*t)+5*sin(2*pi*20000*t)+10*sin(2*pi*30000*t);
nfft=length(y);
nfft2=2.^nextpow2(nfft);
fy=fft(y,nfft2);
fy=fy(1:nfft2/2);
xfft=Fs.*(0:nfft2/2-1)/nfft2;
% normalize frequency by Fs/2
% cut_off=1.5e3/Fs/2;
cut_off=100/(Fs/2);
order=32*16;
h=fir1(order,cut_off);
con=conv(y,h);
fh=fft(h,nfft2);
fh=fh(1:nfft2/2);
mul=fh.*fy;
figure(1)
subplot(4,1,1);
plot(t,y);title('Raw signal');xlabel('Time in Sec'),ylabel('Amplitude');
subplot(4,1,2);plot(xfft,abs(fy/max(fy)));title('Freq domain Spark');xlabel('Frequency in Hz'),ylabel('Magnitude');
subplot(4,1,3);
plot(xfft, abs(mul/max(mul)));
title('Filtered output-Frequency domain');xlabel('Frequency in Hz'),ylabel('Magnitude');
subplot(4,1,4);
plot((0:length(con)-1)/Fs, con);
title('Filtered output-Time domain');xlabel('Time in Sec'),ylabel('Amplitude');
Reji G
on 6 May 2022
For a signal covering frequency range of (50Hz to 10MHz), Fs should be greater than 20MHz. The signal of interest ins below 100Hz. The normalized freq shoud be (100/(Fs/2) = 1e-5. Any filter with such cut off requires a super large order and expensive to implement.
This often can be done in analog filtering and sample signal in much lower rate.
In your case, you can consider multirate processing. Conver the signal into lower freq range first with anti-aliasing filter. There are special filters for such purpose.
fs = 20e6;
ns = 1000000;
t=(0:ns-1)'/fs;
y=5*sin(2*pi*50*t) + 5*sin(2*pi*200*t) + 5*sin(2*pi*20000*t)+10*sin(2*pi*6e6*t);
% desired | interferences
% down sample useing dsp.SampleRateConverter
fs1 = fs/100;
src = dsp.SampleRateConverter('InputSampleRate', fs, 'OutputSampleRate', fs1);
%whos
y1 = src(y);
% down sample again
fs2 = fs1/100;
y2 = src(y1);
Now we can design the filter more easily wilt lower sampling frequency
fc = 100;
order=32;
h=fir1(order,fc/(fs2/2));
% filter response
freqz(h, 1, 8192, fs2)
y3 = filter(h, 1, y2); % filter
figure
t1 = (0:length(y1)-1)/fs1;
t2 = (0:length(y2)-1)/fs2;
plot(t, y, 'b', t1, y1, 'r',t2,y2,'g', t2, y3, 'k')
legend('Orignal', 'DS100', 'DS10000', 'filter')
>>> I changed the fc value to 49Hz. Still the graph is same as you plotted and uploaded here.
If the cut-off is 49 Hz and signal is 50Hz, you may not able to filter out the signal. The filter response is never an ideal cut off it has transition band. Zoom in the filter response and check out how much attenuation you have. if you change the cut-off to a smaller value eg. 20Hz), you may filter out the signal at 50Hz.
>>> As far as my knowlege concerned, If my cutoff freq is 49Hz then signals upto 49Hz should be available at the output rest of the signals should be rejected. Am I correct ? (If I'm wrong, plz correct me. I'm a beginner).
For ideal filter with a sharp edge, this is true. For any real implemental filter, this is wrong. Again, check out the filter response.
==> Also I need y axis in dB, x axis in frequency. So that I can see the frequency corresponds to -3dB. Any help would be highly appreciated.
The magnitude response of the filter is in dB. If you want to show the spectraum of signal, you can alwase use 20*log10(abs(fft(x))).
Reji G
on 10 May 2022
Categories
Find more on Analog Filters in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!


