Matlab Simulink建模与仿真例题源代码
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
〔實例3.1〕試設計一個模擬低通濾波器,f p = 2400Hz,f s = 5000Hz,R p = 3 dB,R s =
25dB。分別用巴特沃斯和橢圓濾波器原型,求出其3dB 截止頻率和濾波器階數,傳遞函數,並作出幅頻、相頻特性曲線。
巴特沃斯濾波器設計的程序代碼如下:
% ch3example1A.m
clear;
f_p=2400; f_s=5000; R_p=3; R_s=25; % 設計要求指標
[n, fn]=buttord(f_p,f_s,R_p,R_s, 's'); % 計算階數和截止頻率
Wn=2*pi*fn; % 轉換為角頻率
[b,a]=butter(n, Wn, 's'); % 計算H(s)
f=0:100:10000; % 計算頻率點和頻率範圍
s=j*2*pi*f; % s=jw=j*2*pi*f
H_s=polyval(b,s)./polyval(a,s); % 計算相應頻率點處H(s)的值
figure(1);
subplot(2,1,1); plot(f, 20*log10(abs(H_s))); % 幅頻特性
axis([0 10000 -40 1]);
xlabel('頻率Hz');ylabel('幅度dB');
subplot(2,1,2); plot(f, angle(H_s)); % 相頻特性
xlabel('頻率Hz');ylabel('相角rad');
figure(2); freqs(b,a); % 也可用指令freqs直接畫出H(s)的頻率響應曲線。
橢圓濾波器設計的程序代碼如下:
〔程序代碼〕ch3example1B.m
% ch3example1B.m
clear;
f_p=2400; f_s=5000; R_p=3; R_s=25; % 設計要求指標
[n, fn]=ellipord(f_p,f_s,R_p,R_s,'s'); % 計算階數和截止頻率
Wn=2*pi*fn; % 轉換為角頻率
[b,a]=ellip(n,R_p,R_s,Wn,'s'); % 計算H(s)
f=0:100:10000; % 計算頻率點和頻率範圍
s=j*2*pi*f; % s=jw=j*2*pi*f
H_s=polyval(b,s)./polyval(a,s); % 計算相應頻率點處H(s)的值
figure(1);
subplot(2,1,1); plot(f, 20*log10(abs(H_s))); % 幅頻特性
axis([0 10000 -40 1]);
xlabel('頻率Hz');ylabel('幅度dB');
subplot(2,1,2); plot(f, angle(H_s)); % 相頻特性
xlabel('頻率Hz');ylabel('相角rad');
figure(2); freqs(b,a); % 也可用指令freqs直接畫出H(s)的頻率響應曲線。
〔實例3.2〕試設計一個巴特沃斯型數字低通濾波器,設採樣率為8000Hz,f p =
2100Hz,f s = 2500Hz,R p = 3dB,R s = 25dB。
設計程序代碼如下:
〔程序代碼〕ch3example2A.m
% ch3example2A.m
f_N=8000; % 採樣率
f_p=2100; f_s=2500; R_p=3; R_s=25; % 設計要求指標
Ws=f_s/(f_N/2); Wp=f_p/(f_N/2); % 計算歸一化頻率
[n, Wn]=buttord(Wp,Ws,R_p,R_s); % 計算階數和截止頻率
[b,a]=butter(n, Wn); % 計算H(z)
figure(1);
freqz(b,a, 1000, 8000) % 作出H(z)的幅頻相頻圖, freqz(b,a, 計算點數, 採樣率)
subplot(2,1,1); axis([0 4000 -30 3])
figure(2); % 第二種作圖方法
f=0:40:4000; % 計算頻率點和頻率範圍
z=exp(j*2*pi*f./(f_N)); %
H_z=polyval(b,z)./polyval(a,z); % 計算相應頻率點處H(s)的值
subplot(2,1,1); plot(f, 20*log10(abs(H_z))); % 幅頻特性
axis([0 4000 -40 1]);
xlabel('頻率Hz');ylabel('幅度dB');
subplot(2,1,2); plot(f, angle(H_z)); % 相頻特性
xlabel('頻率Hz');ylabel('相角rad');
〔實例3.3〕試設計一個切比雪夫1 型高通數字濾波器,採樣率為8000Hz,f p =
1000Hz,f s = 700Hz,R p = 3dB,R s = 20dB。
設計程序代碼如下:
〔程序代碼〕ch3example3A.m
% ch3example3A.m
f_N=8000; % 採樣率
〔程序代碼〕ch3example3A.m
% ch3example3A.m
f_N=8000; % 採樣率
〔實例3.4〕試設計一橢圓型帶通數字濾波器。設採樣率為10000Hz,f p = [1000; 1500]Hz,f s = [600; 1900]Hz,R p = 3dB,R s = 20dB。
設計程序代碼如下:
〔程序代碼〕ch3example4A.m
% ch3example4A.m
f_N=10000; % 採樣率
f_p=[1000, 1500]; f_s=[600, 1900]; R_p=3; R_s=20; % 設計要求指標
Ws=f_s/(f_N/2); Wp=f_p/(f_N/2); % 計算歸一化頻率
[n, Wn]=ellipord(Wp,Ws,R_p,R_s); % 計算階數和截止頻率
[b,a]=ellip(n, R_p, R_s, Wn); % 計算H(z)