又到清明时节,……

注意:带阻滤波器不能用第2类线性相位滤波器实现,我们采用第1类,长度为基数,选M=61

代码:

%% ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
%% Output Info about this m-file
fprintf('\n***********************************************************\n');
fprintf(' <DSP using MATLAB> Problem 7.24 \n\n'); banner();
%% ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ % bandstop filter
% Type-2 FIR ---- No highpass or bandstop
wp1 = 0.3*pi; ws1 = 0.4*pi; ws2 = 0.6*pi; wp2 = 0.7*pi;
As = 50; Rp = 0.2;
tr_width = min( ws1-wp1, wp2-ws2 ); T1 = 0.5925; T2=0.1099;
M = 61; alpha = (M-1)/2; l = 0:M-1; wl = (2*pi/M)*l;
n = [0:1:M-1]; wc1 = (ws1+wp1)/2; wc2 = (wp2+ws2)/2; Hrs = [ones(1,10),T1,T2,zeros(1,7),T2,T1,ones(1,20),T1,T2,zeros(1,7),T2,T1,ones(1,9)]; % Ideal Amp Res sampled
Hdr = [1, 1, 0, 0, 1, 1]; wdl = [0, 0.3, 0.4, 0.6, 0.7, 1]; % Ideal Amp Res for plotting
k1 = 0:floor((M-1)/2); k2 = floor((M-1)/2)+1:M-1; %% ----------------------------------
%% Type-1 LPF
%% ----------------------------------
angH = [-alpha*(2*pi)/M*k1, alpha*(2*pi)/M*(M-k2)];
H = Hrs.*exp(j*angH); h = real(ifft(H, M)); [db, mag, pha, grd, w] = freqz_m(h, 1); delta_w = 2*pi/1000;
[Hr, ww, a, L] = Hr_Type1(h); Rp = -(min(db(1 :1: floor(wp1/delta_w)))); % Actual Passband Ripple
fprintf('\nActual Passband Ripple is %.4f dB.\n', Rp);
As = -round(max(db(floor(ws1/delta_w)+1 : 1 : 0.55*pi/delta_w))); % Min Stopband attenuation
fprintf('\nMin Stopband attenuation is %.4f dB.\n', As); [delta1, delta2] = db2delta(Rp, As) %Plot figure('NumberTitle', 'off', 'Name', 'Problem 7.24a FreSamp Method')
set(gcf,'Color','white');
subplot(2,2,1); plot(wl(1:31)/pi, Hrs(1:31), 'o', wdl, Hdr, 'r'); axis([0, 1, -0.1, 1.1]);
set(gca,'YTickMode','manual','YTick',[0,0.5,1]);
set(gca,'XTickMode','manual','XTick',[0,0.3,0.4,0.6,0.7,1]);
xlabel('frequency in \pi nuits'); ylabel('Hr(k)'); title('Frequency Samples: M=61,T1=0.5925,T2=0.1099');
grid on; subplot(2,2,2); stem(l, h); axis([-1, M, -0.3, 0.8]); grid on;
xlabel('n'); ylabel('h(n)'); title('Impulse Response'); subplot(2,2,3); plot(ww/pi, Hr, 'r', wl(1:31)/pi, Hrs(1:31), 'o'); axis([0, 1, -0.2, 1.2]); grid on;
xlabel('frequency in \pi units'); ylabel('Hr(w)'); title('Amplitude Response');
set(gca,'YTickMode','manual','YTick',[0,0.5,1]);
set(gca,'XTickMode','manual','XTick',[0,0.3,0.4,0.6,0.7,1]); subplot(2,2,4); plot(w/pi, db); axis([0, 1, -100, 10]); grid on;
xlabel('frequency in \pi units'); ylabel('Decibels'); title('Magnitude Response');
set(gca,'YTickMode','manual','YTick',[-90,-58,0]);
set(gca,'YTickLabelMode','manual','YTickLabel',['90';'58';' 0']);
set(gca,'XTickMode','manual','XTick',[0,0.3,0.4,0.6,0.7,1]); figure('NumberTitle', 'off', 'Name', 'Problem 7.24 h(n) FreSamp Method')
set(gcf,'Color','white'); subplot(2,2,1); plot(w/pi, db); grid on; axis([0 1 -120 10]);
set(gca,'YTickMode','manual','YTick',[-90,-58,0])
set(gca,'YTickLabelMode','manual','YTickLabel',['90';'58';' 0']);
set(gca,'XTickMode','manual','XTick',[0,0.3,0.4,0.6,0.7,1]);
xlabel('frequency in \pi units'); ylabel('Decibels'); title('Magnitude Response in dB'); subplot(2,2,3); plot(w/pi, mag); grid on; %axis([0 1 -100 10]);
xlabel('frequency in \pi units'); ylabel('Absolute'); title('Magnitude Response in absolute');
set(gca,'XTickMode','manual','XTick',[0,0.3,0.4,0.6,0.7,1,1.3,1.4,1.6,1.7,2]);
set(gca,'YTickMode','manual','YTick',[0,1.0]); subplot(2,2,2); plot(w/pi, pha); grid on; %axis([0 1 -100 10]);
xlabel('frequency in \pi units'); ylabel('Rad'); title('Phase Response in Radians');
subplot(2,2,4); plot(w/pi, grd*pi/180); grid on; %axis([0 1 -100 10]);
xlabel('frequency in \pi units'); ylabel('Rad'); title('Group Delay'); figure('NumberTitle', 'off', 'Name', 'Problem 7.24 AmpRes of h(n), FreSamp Method')
set(gcf,'Color','white'); plot(ww/pi, Hr); grid on; %axis([0 1 -100 10]);
xlabel('frequency in \pi units'); ylabel('Hr'); title('Amplitude Response');
set(gca,'YTickMode','manual','YTick',[-delta2, 0,delta2, 1-0.035, 1,1+0.035]);
%set(gca,'YTickLabelMode','manual','YTickLabel',['90';'45';' 0']);
set(gca,'XTickMode','manual','XTick',[0,0.3,0.4,0.6,0.7,1]); %% ------------------------------------
%% fir2 Method
%% ------------------------------------
f = [0 wp1 ws1 ws2 wp2 pi]/pi;
m = [1 1 0 0 1 1];
h_check = fir2(M+1, f, m); % if M is odd, then M+1; order
[db, mag, pha, grd, w] = freqz_m(h_check, [1]);
%[Hr,ww,P,L] = ampl_res(h_check);
[Hr, ww, a, L] = Hr_Type1(h_check); fprintf('\n-------------fir2 Method start-----------------\n');
Rp = -(min(db(1 :1: floor(wp1/delta_w)))); % Actual Passband Ripple
fprintf('\nActual Passband Ripple is %.4f dB.\n', Rp);
%As = -round(max(db(floor(0.45*pi/delta_w)+1 : 1 : ws2/delta_w))); % Min Stopband attenuation
As = -round(max(db(floor(0.45*pi/delta_w)+1 : 1 : 0.55*pi/delta_w)));
fprintf('\nMin Stopband attenuation is %.4f dB.\n', As); [delta1, delta2] = db2delta(Rp, As) figure('NumberTitle', 'off', 'Name', 'Problem 7.24 fir2 Method')
set(gcf,'Color','white'); subplot(2,2,1); stem(n, h); axis([0 M-1 -0.3 0.8]); grid on;
xlabel('n'); ylabel('h(n)'); title('Impulse Response'); %subplot(2,2,2); stem(n, w_ham); axis([0 M-1 0 1.1]); grid on;
%xlabel('n'); ylabel('w(n)'); title('Hamming Window'); subplot(2,2,3); stem([0:M+1], h_check); axis([0 M+1 -0.3 0.8]); grid on;
xlabel('n'); ylabel('h\_check(n)'); title('Actual Impulse Response'); subplot(2,2,4); plot(w/pi, db); axis([0 1 -120 10]); grid on;
set(gca,'YTickMode','manual','YTick',[-90,-64,-21,0])
set(gca,'YTickLabelMode','manual','YTickLabel',['90';'64';'21';' 0']);
set(gca,'XTickMode','manual','XTick',[0,0.3,0.4,0.6,0.7,1]);
xlabel('frequency in \pi units'); ylabel('Decibels'); title('Magnitude Response in dB'); figure('NumberTitle', 'off', 'Name', 'Problem 7.24 h(n) fir2 Method')
set(gcf,'Color','white'); subplot(2,2,1); plot(w/pi, db); grid on; axis([0 1 -120 10]);
xlabel('frequency in \pi units'); ylabel('Decibels'); title('Magnitude Response in dB');
set(gca,'YTickMode','manual','YTick',[-90,-64,-21,0]);
set(gca,'YTickLabelMode','manual','YTickLabel',['90';'64';'21';' 0']);
set(gca,'XTickMode','manual','XTick',[0,0.3,0.4,0.6,0.7,1,1.3,1.4,1.6,1.7,2]); subplot(2,2,3); plot(w/pi, mag); grid on; %axis([0 1 -100 10]);
xlabel('frequency in \pi units'); ylabel('Absolute'); title('Magnitude Response in absolute');
set(gca,'XTickMode','manual','XTick',[0,0.3,0.4,0.6,0.7,1,1.3,1.4,1.6,1.7,2]);
set(gca,'YTickMode','manual','YTick',[0,1.0]); subplot(2,2,2); plot(w/pi, pha); grid on; %axis([0 1 -100 10]);
xlabel('frequency in \pi units'); ylabel('Rad'); title('Phase Response in Radians');
subplot(2,2,4); plot(w/pi, grd*pi/180); grid on; %axis([0 1 -100 10]);
xlabel('frequency in \pi units'); ylabel('Rad'); title('Group Delay'); figure('NumberTitle', 'off', 'Name', 'Problem 7.24 AmpRes of h(n),fir2 Method')
set(gcf,'Color','white'); plot(ww/pi, Hr); grid on; %axis([0 1 -100 10]);
xlabel('frequency in \pi units'); ylabel('Hr'); title('Amplitude Response');
set(gca,'YTickMode','manual','YTick',[-0.004, 0,0.004, 1-0.004, 1,1+0.004]);
%set(gca,'YTickLabelMode','manual','YTickLabel',['90';'45';' 0']);
set(gca,'XTickMode','manual','XTick',[0,0.3,0.4,0.6,0.7,1]);

运行结果:

过渡带中有两个采样值,优化值直接抄书上的。

采用频率采样方法得到的脉冲响应

采用fir2函数 的方法得到滤波器脉冲响应

《DSP using MATLAB》Problem 7.24的更多相关文章

  1. 《DSP using MATLAB》Problem 6.24

    代码: %% ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ %% Output In ...

  2. 《DSP using MATLAB》Problem 4.24

    Y(z)部分分式展开, 零状态响应部分分式展开, 零输入状态部分分式展开,

  3. 《DSP using MATLAB》Problem 6.15

    代码: %% ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ %% Output In ...

  4. 《DSP using MATLAB》Problem 6.8

    代码: %% ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ %% Output In ...

  5. 《DSP using MATLAB》Problem 5.24-5.25-5.26

    代码: function y = circonvt(x1,x2,N) %% N-point Circular convolution between x1 and x2: (time domain) ...

  6. 《DSP using MATLAB》Problem 4.15

    只会做前两个, 代码: %% ---------------------------------------------------------------------------- %% Outpu ...

  7. 《DSP using MATLAB》Problem 2.16

    先由脉冲响应序列h(n)得到差分方程系数,过程如下: 代码: %% ------------------------------------------------------------------ ...

  8. 《DSP using MATLAB》 Problem 2.3

    本题主要是显示周期序列的. 1.代码: %% ------------------------------------------------------------------------ %% O ...

  9. 《DSP using MATLAB》Problem 7.29

    代码: %% ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ %% Output In ...

随机推荐

  1. Java并发编程阅读笔记-锁和活跃性问题

  2. JDBCUtils——C3P0

    需要导入的包: mysql-connector-java-5.1.37-bin.jar c3p0-0.9.2-pre5.jar mchange-commons-java-0.2.3.jar 如果使用D ...

  3. linux c TCP连接通讯

    服务端: 1.申请服务端自己的socket 2.对addr赋值 3.bind文件描述符和地址信息 4.listen监听服务 5.等待accept客户端的连接 6.处理建立好的连接 7.关闭socket ...

  4. jmeter如何进行MQTT性能测试(测试前期准备二,MQTT插件及协议了解)

    jmeter插件下载地址及使用,已经有大佬总结好了 大佬的博客地址: https://blog.csdn.net/yellowanwu/article/details/50889677 添加线程组:添 ...

  5. C++中:(*p)++和*(p++)和*p++的区别

    1. 理解自增自减运算符      (1)后置自增自减运算符优先级>前置自增自减运算符. (2)前置自增自减运算符“变量先自增自减再使用”. (3)后置自增自减运算符“变量先使用再自增自减”. ...

  6. [2003_p1]乒乓球

    一道因为输出不一样疯狂超时的题目(是我太菜,但是我jio得代码是ok的) 题目描述 国际乒联现在主席沙拉拉自从上任以来就立志于推行一系列改革,以推动乒乓球运动在全球的普及.其中11分制改革引起了很大的 ...

  7. 查询数据库:models.Books.objects.all()[10: 20]与models.Books.objects.filter(id__gt=10, id__lt=20).values() 的区别

    1. models.Books.objects.all()[10: 20] (10:20  之间是冒号,不是逗号.)查出的是 QuerySet对象,如需进行操作,得进一步进行剥皮. 查询代码: 查询结 ...

  8. Hacker一月间

    我给自己的新赛季制定了一个计划,就是研究Kali,一个大集成开源软件系统,不过是用来做Hacker的. 以前其实想玩玩这个,但负责信息安全,总是担心安全漏洞这里安全漏洞哪儿,其实自己很害怕有安全漏洞, ...

  9. KendoUi 学习笔记一

    本系列主要是记录KendoUI的学习过程. KendoUi的特点有以下特点: 1. 70+UI控件 控件有DataGrids,DropDowns,Menus和Buttons,还有一些商业的控件,比如C ...

  10. python3读取MySQL-Front的MYSQL密码

    python3读取MySQL-Front的MYSQL密码 python3 mysql 密码 MySQL-Front 前言 同样的套路又来了,继续尝试从配置文件中读取敏感的信息,这次轮到的是MySQL- ...