📄 low_pass_filter.m
字号:
%Create a signalt = [0:0.001:1];sub_t = 100;signal=cos(2*pi*50*t);%+2*sin(2*pi*120*t);figure;subplot(2,1,1);plot(t(1:sub_t),signal(1:sub_t),'g'),title('ORIGINAL SIGNAL'),xlabel('time (s)');%Add noise to the signalrandn('state',0);sign_and_noise = signal + 0.5*randn(size(t));hold on;subplot(2,1,1);plot(t(1:sub_t),sign_and_noise(1:sub_t),'b'), title('SIGNAL');xlabel('time (s)');legend('original','disturbed');N=2048;SIG= abs(fft(signal,N));SIG = fftshift(SIG);F = [-N/2:N/2-1]/N;figure;subplot(3,1,1);plot(F,SIG),title('ORIGINAL SIGNAL IN FREQUENCY DOMAIN');xlabel('freq');ylabel('magnitude (dB)');SIG_NOISE= abs(fft(sign_and_noise,N));SIG_NOISE = fftshift(SIG_NOISE);subplot(3,1,2);plot(F,SIG_NOISE);title('DISTURBED SIGNAL IN FREQUENCY DOMAIN');xlabel('freq');ylabel('magnitude (dB)');%Create An Ideal LowPassf = [0:length(t)-1]/length(t);low_pass = rectpuls(f, 0.15);subplot(3,1,3);plot(f, low_pass);title('LOW PASS FILTER');xlabel('freq');ylabel('magnitude (dB)');lp_t = fftshift(real(ifft(low_pass)));figureplot(lp_t);%Convolution in time domainres = conv(sign_and_noise, real(ifft(low_pass)));figure(1);subplot(2,1,2);plot(t(1:sub_t), res(1:sub_t),'k');%Product in frequency domainres_freq = fft(sign_and_noise) .* (low_pass);res_time = real(ifft(res_freq));hold on, subplot(2,1,2);plot(t(1:sub_t), res_time(1:sub_t),'r'),title('RECONSTRUCTED SIGNAL');xlabel('time (s)');hold on, subplot(2,1,2);plot(t(1:sub_t), signal(1:sub_t),'b-.');legend('convolution in t','product in f','original');
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -