《DSP using MATLAB》Problem 7.28

          又是一年五一节,朋友圈都是晒名山大川的,晒脑袋的,我这没钱的待在家里上网转转吧

         频率采样法设计带通滤波器,过渡带中有一个样点

代码:

%% ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
%%            Output Info about this m-file
fprintf('
***********************************************************
');
fprintf('        <DSP using MATLAB> Problem 7.28 

');

banner();
%% ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

% bandpass
ws1 = 0.3*pi; wp1 = 0.4*pi; wp2 = 0.6*pi; ws2 = 0.7*pi; As = 40; Rp = 0.5;
tr_width = min((wp1-ws1), (ws2-wp2));

T1=0.39;
M = 40; 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 = [zeros(1,7),T1,ones(1,5),T1,zeros(1,13),T1,ones(1,5),T1,zeros(1,6)];   % Ideal Amp Res sampled
Hdr = [0, 0, 1, 1, 0, 0]; 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-2 BPF
%% --------------------------------------------------  
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,P,L] = ampl_res(h);
[Hr, ww, a, L] = Hr_Type2(h);

Rp = -(min(db(floor(wp1/delta_w)+1 :1: floor(wp2/delta_w))));     % Actual Passband Ripple
fprintf('
Actual Passband Ripple is %.4f dB.
', Rp);

As = -round(max(db(ws2/delta_w+1 : 1 : 501)));        % Min Stopband attenuation
fprintf('
Min Stopband attenuation is %.4f dB.
', As);

[delta1, delta2] = db2delta(Rp, As)

% Plot

figure('NumberTitle', 'off', 'Name', 'Problem 7.28a FreSamp Method')
set(gcf,'Color','white'); 
subplot(2,2,1); plot(wl(1:21)/pi, Hrs(1:21), '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=40,T1=0.39');
grid on;

subplot(2,2,2); stem(l, h); axis([-1, M, -0.2, 0.3]); grid on;
xlabel('n'); ylabel('h(n)'); title('Impulse Response(Type-2)');

subplot(2,2,3); plot(ww/pi, Hr, 'r', wl(1:21)/pi, Hrs(1:21), '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,-40,0]);
set(gca,'YTickLabelMode','manual','YTickLabel',['90';'40';' 0']);
set(gca,'XTickMode','manual','XTick',[0,0.3,0.4,0.6,0.7,1]);


figure('NumberTitle', 'off', 'Name', 'Problem 7.28 h(n) FreSamp Method')
set(gcf,'Color','white'); 

subplot(2,2,1); plot(w/pi, db); grid on; axis([0 2 -120 10]); 
set(gca,'YTickMode','manual','YTick',[-90,-40,0])
set(gca,'YTickLabelMode','manual','YTickLabel',['90';'40';' 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]);
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.28 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-delta1, 1,1+delta1]);
%set(gca,'YTickLabelMode','manual','YTickLabel',['90';'40';' 0']);
set(gca,'XTickMode','manual','XTick',[0,0.3,0.4,0.6,0.7,1]);


%% ------------------------------------
%%     fir2 Method
%% ------------------------------------
f = [0 ws1 wp1 wp2 ws2 pi]/pi;
m = [0  0  1  1  0  0];
h_check = fir2(M-1, f, m);       % order
[db, mag, pha, grd, w] = freqz_m(h_check, [1]);  
%[Hr,ww,P,L] = ampl_res(h_check);
[Hr, ww, a, L] = Hr_Type2(h_check);

fprintf('
----------------------------------
');
fprintf('
 fir2 function Method 
');
fprintf('
----------------------------------
');

Rp = -(min(db(floor(wp1/delta_w)+1 :1: floor(wp2/delta_w))));            % Actual Passband Ripple
fprintf('
Actual Passband Ripple is %.4f dB.
', Rp);
As = -round(max(db(0.75*pi/delta_w+1 : 1 : 501)));             % Min Stopband attenuation
fprintf('
Min Stopband attenuation is %.4f dB.
', As);

[delta1, delta2] = db2delta(Rp, As)


figure('NumberTitle', 'off', 'Name', 'Problem 7.28 fir2 Method')
set(gcf,'Color','white'); 

subplot(2,2,1); stem(n, h); axis([0 M-1 -0.2 0.3]); 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.2 0.3]); 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,-61,-17,0])
set(gca,'YTickLabelMode','manual','YTickLabel',['90';'61';'17';' 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.28 h(n) fir2 Method')
set(gcf,'Color','white'); 

subplot(2,2,1); plot(w/pi, db); grid on; axis([0 2 -120 10]); 
xlabel('frequency in pi units'); ylabel('Decibels'); title('Magnitude Response in dB');
set(gca,'YTickMode','manual','YTick',[-90,-61,-17,0]);
set(gca,'YTickLabelMode','manual','YTickLabel',['90';'61';'17';' 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.28 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',[-delta2, 0,delta2, 1-delta1, 1,1+delta1]);
%set(gca,'YTickLabelMode','manual','YTickLabel',['90';'45';' 0']);
set(gca,'XTickMode','manual','XTick',[0,0.3,0.4,0.6,0.7,1]);

  运行结果:

          频率采样法,得到的脉冲响应、振幅响应和幅度响应:

        幅度响应、相位响应和群延迟

 

        采用fir2函数求脉冲响应序列

          matlab自带的fir2函数比频率采样法得到的脉冲响应,通带和阻带的波纹ripple较小。

牢记: 1、如果你决定做某事,那就动手去做;不要受任何人、任何事的干扰。2、这个世界并不完美,但依然值得我们去为之奋斗。
原文地址:https://www.cnblogs.com/ky027wh-sx/p/10808327.html