preface

The basic principle of signal filtering is Fourier transform. After Fourier transform of the signal, spectrum distribution can be obtained, which can be filtered in turn, including high-pass, low-pass, band-pass and so on.

Basic function

FFT (Fast Fourier Transform)

IFFT (Inverse Fourier transform)

X = FFT (X); X=FFT(x,N); x=IFFT(X); x=IFFT(X,N);Copy the code

Function implementation

Create a number of intermittent wave superimposed signal, through filtering, and display the image before and after processing.

Low pass filtering part of the implementation

clc clear close all; %input signal------------------------- Fs =1000; T=1/Fs; L=1000; t=(0:L-1)*T; Y = 0.7 * sin (2 * PI * 10 * t) + 2 * sin (2 * PI * 120 * t); Figure (1) the subplot (3, 2, 1); plot(t,y) grid on %side ft---------------------------------- NFFT=L; Y=fft(y,NFFT)/L; F = Fs / 2 * linspace (0, 1, NFFT / 2 + 1); Subplot (3,2,2); plot(f,2*abs(Y(1:NFFT/2+1))); grid on; %low filter--------------------------- fn =1002; fp=60; fs =100; Rp=2; Rs=30 Wp=fp/(fn/2); Ws=fs/(fn/2); [n,Wn]=buttord(Wp,Ws,Rp,Rs); [b,a]=butter(n,Wn); [H, F] = freqz (b, a, NFFT / 2 + 1100 2); % subplot (filling); % plot(F,20*log10(abs(H))); grid on; % subplot (3 and 4); pha=angle(H)*180/pi; % plot(F,pha); grid on; A = 2*abs(Y(1:NFFT/2+1)).*(abs(H)'); Subplot (3, 2, 3); plot(f,A); grid on; % %ifft------------------------------- for(i =1:L-(NFFT/2+1)) A(1,((NFFT/2+1)+i))=A(1,((NFFT/2+1)-i)); end z=A.*exp(1i*angle(Y)); z2=real(ifft(z)); Subplot (3, 4-trichlorobenzene); Y = 0.7 * sin (2 * PI * 10 * t); plot(t,y); The axis ([0, 1, 2, 2]). % subplot (3,2,7); % plot(t,z2*NFFT/2); %high filter-------------------------- fn=1002; fp=60; fs=30; Rp=3; Rs=20; Wp=fp/(fn/2); Ws=fs/(fn/2); [n,Wn]=buttord(Wp,Ws,Rp,Rs); [b,a]=butter(n,Wn,'high'); [H, F] = freqz (b, a, NFFT / 2 + 1100 2); pha=angle(H)*180/pi; A=(2*abs(Y(1:NFFT/2+1))).*(abs(H)'); Subplot (3,2,5); plot(f,A); grid on; %ifft----------------------------- for i=1:L-(NFFT/2+1) A(1,((NFFT/2+1)+i))=A(1,((NFFT/2+1)-i)); end z=A.*exp(1i*angle(Y)); z2=real(ifft(z)); Subplot (3,2,6); y = sin(2*pi*120*t); plot(t,y); The axis ([0, 1, 2, 2]).Copy the code

Implementation of band pass part

clc clear close all %???????? Fs = 1000; T=1/Fs; L=1000; t=(0:L-1)*T; Y = 0.7 * cos (2 * PI * 100 * t) + sin (2 * PI * 200 * t) + 0.4 * sin (2 * PI * * t) 40 + 5 * cos (2 * PI * 400 * t); Subplot (3,2,1) plot(t,y) grid on NFFT=L; Y=fft(y,NFFT)/L; F = Fs / 2 * linspace (0, 1, NFFT / 2 + 1); ,2,2 subplot (3) the plot (f, 2 * abs (Y (1: NFFT / 2 + 1))) xlabel () '(Hz)' ylabel (' ') grid on fn = 1002; Fp = [80230]; Fs = [60300]; Rp=3; Rs=30; Wp=fp/(fn/2); Ws=fs/(fn/2); [n,Wn]=buttord(Wp,Ws,Rp,Rs); [b,a]=butter(n,Wn); [H, F] = freqz (b, a, 501100 (2); %plot(F,20*log10(abs(H))) %axis([0 400 -30 3]) %xlabel('???? (Hz)'); Ylabel (' u?? (dB) ') % grid on % subplot pha (3, 2, 3) = Angle (H) * 180 / PI; % plot(F,pha) % xlabel('???? (Hz)'); ylabel('? A???? ') % grid on %±??????????? A=2*abs(Y(1:NFFT/2+1)).*(abs(H)'); Plot (f,A) grid on %? ifft for i=1:L-(NFFT/2+1) A(1,((NFFT/2+1)+i))=A(1,((NFFT/2+1)-i)); end z=A.*exp(1i*angle(Y)); z2=real(ifft(z)); Subplot (3, 4-trichlorobenzene) y = 0.7 * cos (2 * PI * 100 * t) + sin (2 * PI * 200 * t); plot(t,y); % subplot (3,2,5) % plot (t, z2 * NFFT / 2); %high filter--------------------------------- fn=1002; Fp = [60300]; Fs = [70250]; Rp=3; Rs=30; Ws=fs/(fn/2); [n,Wn]=buttord(Wp,Ws,Rp,Rs); [b,a]=butter(n,Wn,'stop'); [H, F] = freqz (b, a, 501100 (2); Subplot (3,2,5) plot (F, 20 * log10 (abs (H))) axis ([0 400-35 3]) xlabel ('???? (Hz)'); Ylabel (' u?? (dB)') grid on %subplot(3,2,6) pha= Angle (H)*180/ PI; plot(F,pha) xlabel('???? (Hz)'); ylabel('? A???? ') grid on %------------------- A=(2*abs(Y(1:NFFT/2+1))).*(abs(H)'); %ifft----------------------------------- for i=1:L-(NFFT/2+1) A(1,((NFFT/2+1)+i))=A(1,((NFFT/2+1)-i)); end z=A.*exp(1i*angle(Y)); z2=real(ifft(z)); % subplot (3,2,6) y = 0.4 * sin (2 * PI * 40 * t) + 5 * cos (2 * PI * 400 * t); plot(t,y); Subplot (2,1,2) plot (t, z2 * NFFT / 2);Copy the code