%實驗三(1) 設計要求的數字濾波器 clc;clear; clear all; wp=0.2;ws=0.35;Ap=1;As=15; [N,fc]=buttord(wp,ws,Ap,As); %求出符合參數的濾波器階數和截止頻率 [B,A]=butter(N,fc); %得到濾波器H(z)的系數 [h,w]=freqz(B,A,1024); %計算模擬濾波器響應 plot(w/pi,20*log10(abs(h)/abs(h(1)))); title('wp=0.2π;ws=0.35π;Ap=1dB;As=15dB的巴特沃思數字低通濾波器'); grid on;xlabel('w/pi');ylabel('幅度(dB)'); axis([0,1,-30,0]) line([0,1],[-1,-1],'linestyle','--'); line([0,1],[-15,-15],'linestyle','--'); line([0.2,0.2],[-30,0],'linestyle','--'); line([0.35,0.35],[-30,0],'linestyle','--');
編制計算設計的數字濾波器幅度特性和相位特 性的程序,並進行實驗驗證。
%實驗三(2) 數字濾波器的幅度特性與相位特性 clc; close all; wp=0.2;ws=0.35;Ap=1;As=15; [N,fc]=buttord(wp,ws,Ap,As); %求出符合參數的巴特沃思濾波器階數和截止頻率 [b,a]=butter(N,fc); %得到濾波器H(z)的系數 [h,w]=freqz(b,a,1024); %計算數字濾波器響應 subplot(2,1,1);plot(w/pi,abs(h));title('幅度響應'); %數字濾波器幅度特性 axis([0,1,0,1.2]);line([0.2,0.2],[0,1.2],'linestyle','--'); line([0.35,0.35],[0,1.2],'linestyle','--'); xlabel('w/pi');ylabel('幅度'); subplot(2,1,2);plot(w/pi,angle(h)/pi);title('相位響應'); %數字濾波器相頻特性 axis([0,1,-1,1]);line([0.2,0.2],[-1,1],'linestyle','--'); line([0.35,0.35],[-1,1],'linestyle','--'); xlabel('w/pi');ylabel('相位/pi');
編制實現該數字濾波器程序並且實現數字濾波
%實驗三(3) 實現各個頻率的正弦波的數字濾波 clc; clear; close all; wp=0.2;ws=0.35;Ap=1;As=15; [N,fc]=buttord(wp,ws,Ap,As); %求出符合參數的巴特沃思濾波器階數和截止頻率 [b,a]=butter(N,fc); %得到濾波器H(z)的系數 [H,w]=freqz(b,a,1024); %計算數字濾波器響應 [h,n]=impz(b,a,30); w1=0.15*pi;w2=0.3*pi;w3=0.4*pi; x1=sin(w1.*n);x2=sin(w2.*n);x3=sin(w3.*n); %正弦波頻率:通帶、過渡帶、阻帶 figure subplot(3,2,1); stem(n,x1); title('通帶正弦波'); %原通帶正弦波 subplot(3,2,2); y1=conv(x1,h); stem(0:length(y1)-1,y1);axis([0,60,-1,1]); title('濾波后的通帶正弦波'); subplot(3,2,3); stem(n,x2); title('過渡帶正弦波'); %原過渡帶正弦波 subplot(3,2,4); y2=conv(x2,h); stem(0:length(y2)-1,y2);axis([0,60,-1,1]); title('濾波后的過渡帶正弦波'); subplot(3,2,5); stem(n,x3); title('阻帶正弦波'); %原阻帶正弦波 subplot(3,2,6); y3=conv(x3,h); stem(0:length(y3)-1,y3);axis([0,60,-1,1]); title('濾波后的阻帶正弦波'); figure w4=0.6*pi;x4=sin(w4.*n); %驗證濾波器的模擬截止頻率,濾波后的幅度幾乎全0 subplot(2,1,1); stem(n,x4); title('截止頻率處的正弦波'); subplot(2,1,2); y4=conv(x4,h); stem(0:length(y4)-1,y4);axis([0,60,-1,1]); title('濾波后的正弦波');
%實驗三(4) 沖激響應不變法與雙極性變換法的對比 clc; close all; fp=200;fs=300;Rp=1;As=25;T=0.001; %使用脈沖響應不變法(存在頻率混疊) wp=2*pi*fp*T;ws=2*pi*fs*T; %wp、ws為數字頻率 Wp=wp/T;Ws=ws/T; %Wp、Ws為模擬頻率 [N1,fc]=buttord(Wp,Ws,Rp,As,'s'); [b,a]=butter(N1,fc,'s'); [b1,a1]=impinvar(b,a,1/T); %沖擊響應不變法變換到數字濾波器 [h1,w]=freqz(b1,a1); %使用雙極性變換法(不存在頻率混疊) Wp=2/T*tan(wp/2);Ws=2/T*tan(ws/2); %頻率預畸變 [N1,fc]=buttord(Wp,Ws,Rp,As,'s'); [b,a]=butter(N1,fc,'s'); [b2,a2]=bilinear(b,a,1/T); %雙極性變換法變換到數字濾波器 [h2,w]=freqz(b2,a2); f=w/2/pi/T; figure plot(f,abs(h1),'-.',f,abs(h2),'-'); text(90,0.66,'脈沖響應不變法\rightarrow'); text(250,0.46,'\leftarrow雙極性變換法'); axis([0,500,0,1]);grid on; xlabel('頻率/Hz');ylabel('幅度');