《DSP using MATLAB》Problem 7.27

代码:

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

');

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

% bandpass
ws1 = 0.25*pi; wp1 = 0.35*pi; wp2=0.65*pi; ws2 = 0.75*pi;
M=40;
delta_w = 2*pi/1000;

%% ------------------------------------
%%     fir2 Method
%% ------------------------------------
f = [0 ws1 wp1 wp2 ws2 pi]/pi;
m = [0  1  2   2    1  0 ];
h_check = fir2(M-1, f, m, hamming(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, b, L] = Hr_Type2(h_check);

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

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

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


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

subplot(2,1,1); stem([0:M-1], h_check); axis([0 M-1 -0.7 0.9]); grid on;
xlabel('n'); ylabel('h\_check(n)'); title('Actual Impulse Response');

subplot(2,1,2); plot(w/pi, db); axis([0 1 -70 10]); grid on;
set(gca,'YTickMode','manual','YTick',[-60,-21,0])
set(gca,'YTickLabelMode','manual','YTickLabel',['60';'21';' 0']);
set(gca,'XTickMode','manual','XTick',[0,0.25,0.35,0.65,0.75,1]);
xlabel('frequency in pi units'); ylabel('Decibels'); title('Magnitude Response in dB');


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

subplot(2,2,1); plot(w/pi, db); grid on; axis([0 2 -70 10]); 
xlabel('frequency in pi units'); ylabel('Decibels'); title('Magnitude Response in dB');
set(gca,'YTickMode','manual','YTick',[-60,-21,0]);
set(gca,'YTickLabelMode','manual','YTickLabel',['60';'21';' 0']);
set(gca,'XTickMode','manual','XTick',[0,0.25,0.35,0.65,0.75,1,1.25,1.35,1.65,1.75,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.25,0.35,0.65,0.75,1,1.25,1.35,1.65,1.75,2]);
set(gca,'YTickMode','manual','YTick',[0,1.0,2.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.27 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, 1, 1.98,2.02]);
%set(gca,'YTickLabelMode','manual','YTickLabel',['90';'45';' 0']);
set(gca,'XTickMode','manual','XTick',[0,0.25,0.35,0.65,0.75,1]);

  运行结果:

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