1. 程式人生 > >matlab對時域訊號進行帶阻濾波

matlab對時域訊號進行帶阻濾波

clear all; close all; %% 構建原始訊號 N = 500; %原始訊號長度:點數 Fs = 500; %取樣頻率:Hz Dt = 1/Fs; %取樣間隔時間:s t = [0:N-1]*Dt; %時間序列:s f1 = 10;f2 = 50;f3 = 70; %原始訊號頻率 y = cos(2*f1*t*pi)+cos(2*f2*t*pi)+cos(2*f3*t*pi); subplot(2,2,1);plot(t,y); %時域訊號圖 title('原始訊號-時域');xlabel('時間/s');ylabel('幅值/v'); % xlim([0 12]);ylim([-1.5 1.5]); %% FFT變換 FN = N; %FFT執行長度:點數 f0 = 1/(Dt*FN); %基頻 Fy = fft(y);  %對時域訊號進行FFT變換 mag = abs(Fy); n = 0:FN-1; Ff = n*f0;  %頻率序列 subplot(2,2,2);plot(Ff,mag); %繪製原始訊號的振幅圖 title('原始訊號-頻域');xlabel('頻率/Hz');ylabel('振幅'); % ylim([0 0.8]);xlim([0 50]); %% 帶通濾波器BPF BN = N; Bn = 0:BN-1; fmax = 60; fmin = 40; By = zeros(1, length(y)); for m = Bn     if (m*Fs/BN>fmin & m*Fs/BN<fmax) |  (m*Fs/BN>(Fs-fmax) & m*Fs/BN<(Fs-fmin));         By(m+1) = 0;     else         if m<BN-1;             By(m+1) = Fy(m+1);         end     end end subplot(2,2,4);plot(Ff,abs(By)*2/BN); title('帶通濾波-頻域');xlabel('頻率/Hz');ylabel('振幅'); subplot(2,2,3);plot(t,real(ifft(By))); title('帶通濾波-時域');xlabel('時間/s');ylabel('幅值/v');