%% 设计截止频率为50HZ的6阶巴特沃斯低通滤波器 %[filtered_signal,filtb,filta]=lopass_butterworth(inputsignal,cutoff_freq,Fs,order) % INPUTS: % inputsignal = input time series % cutoff_freq = filter corner frequency % Fs = data sampling frequency % order = order of Butterworth filter % OUTPUTS: % filtered_signal = the filtered time series % filtb, filta = filter numerator and denominator (optional) clear;clc; t=0:0.005:5; T=0.005;%时间间隔 Fs=1/T;%采样频率 y1=5*sin(2*pi*10*t)+5*sin(2*pi*60*t); y2=5*sin(2*pi*10*t); [filtered_signal,filtb,filta]=lopass_butterworth(y1,50,Fs,6); figure(1) subplot(3,1,1) plot(t,y1) title('原始信号') subplot(3,1,2) plot(t,filtered_signal) title('滤波后信号') subplot(3,1,3) plot(t,y2) title('5*sin(2*pi*10*t)') %滤波器的幅频相频曲线 figure(2) [h,w] = freqz(filtb,filta,256); %[h,f] = freqz(filtb,filta,256,Fs); subplot(2,1,1) plot(w/pi,20*log10(abs(h))) %plot(f,20*log10(abs(h))) xlabel('\times\pi') %xlabel('Hz') ylabel('gain(dB)') subplot(2,1,2) plot(w/pi,angle(h)) %plot(f,angle(h)) xlabel('\times\pi') %xlabel('Hz') ylabel('phase (rad)')
%% 设计截止频率为20HZ的6阶巴特沃斯高通滤波器 %[filtered_signal,filtb,filta]=hipass_butterworth(inputsignal,cutoff_freq,Fs,order) % INPUTS: % inputsignal = input time series % cutoff_freq = filter corner frequency % Fs = data sampling frequency % order = order of Butterworth filter % OUTPUTS: % filtered_signal = the filtered time series % filtb, filta = filter numerator and denominator (optional) clear;clc; t=0:0.01:5; T=0.01;%时间间隔 Fs=1/T;%采样频率 y1=5*sin(2*pi*10*t)+5*sin(2*pi*30*t); y2=5*sin(2*pi*30*t); [filtered_signal,filtb,filta]=hipass_butterworth(y1,20,Fs,6); figure(1) subplot(3,1,1) plot(t,y1) title('原始信号') subplot(3,1,2) plot(t,filtered_signal) title('滤波后信号') subplot(3,1,3) plot(t,y2) title('5*sin(2*pi*30*t)') %滤波器的幅频相频曲线 figure(2) [h,w] = freqz(filtb,filta,256); %[h,f] = freqz(filtb,filta,256,Fs); subplot(2,1,1) plot(w/pi,20*log10(abs(h))) %plot(f,20*log10(abs(h))) xlabel('\times\pi') %xlabel('Hz') ylabel('gain(dB)') subplot(2,1,2) plot(w/pi,angle(h)) %plot(f,angle(h)) xlabel('\times\pi') %xlabel('Hz') ylabel('phase (rad)')
注:lopass_butterworth和hipass_butterworth函数不为MATLAB自带函数。
lopass_butterworth和hipass_butterworth.rar