《DSP using MATLAB》Problem 7.36
代码:
%% ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
%% Output Info about this m-file
fprintf('\n***********************************************************\n');
fprintf(' <DSP using MATLAB> Problem 7.36 \n\n'); banner();
%% ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ % arbitury shape pass
w1 = 0; w2 = 0.20*pi; delta1 = 0.05; gain1 = 0.00;
w3 = 0.25*pi; w4 = 0.45*pi; delta2 = 0.10; gain2 = 2.00;
w5 = 0.50*pi; w6 = 0.70*pi; delta3 = 0.05; gain3 = 0.00;
w7 = 0.75*pi; w8 = pi; delta4 = 0.15; gain4 = 4.15; fprintf('\n --- Filter Specifications START ---\n');
Rp1 = -20*log10((gain2-delta2)/(gain2+delta2))
As1 = -20*log10(delta1/(gain2+delta2)) Rp2 = -20*log10((gain4-delta4)/(gain4+delta4))
As2 = -20*log10(delta3/(gain4+delta4))
fprintf('\n --- Filter Specifications E N D ---\n'); As = min(As1, As2); fprintf('\n --- Fix Window Method ---\n');
tr_width = min((w3-w2), (w5-w4)); %% ---------------------------------------------------
%% 1 Rectangular Window
%% ---------------------------------------------------
M = ceil(1.8*pi/tr_width) + 1; % Rectangular Window M=37
M=M+190;
fprintf('\n\n#1.Rectangular Window, Filter Length M=%d.\n', M); n = [0:1:M-1]; wc1 = (w2+w3)/2; wc2 = (w4+w5)/2; wc3 = (w6+w7)/2; hd = 2*(ideal_lp(wc2, M) - ideal_lp(wc1, M)) + 4.15*(ideal_lp(pi, M) - ideal_lp(wc3, M));
w_rect = (boxcar(M))'; h = hd .* w_rect;
[db, mag, pha, grd, w] = freqz_m(h, [1]); delta_w = 2*pi/1000;
[Hr,ww,P,L] = ampl_res(h); w1i = floor(w1/delta_w)+1; w2i = floor(w2/delta_w)+1;
w3i = floor(w3/delta_w)+1; w4i = floor(w4/delta_w)+1;
w5i = floor(w5/delta_w)+1; w6i = floor(w6/delta_w)+1;
w7i = floor(w7/delta_w)+1; w8i = floor(w8/delta_w)+1; Rp1 = -(min(db(w3i :1: w4i))); % Actual Passband Ripple
Rp2 = -(min(db(w7i :1: w8i))); % Actual Passband Ripple
fprintf('\nActual Passband Ripple is %.4f and %.4f dB.\n', Rp1, Rp2); As1 = -round(max(db(1 : 1 : w2i))); % Min Stopband attenuation
As2 = -round(max(db(w5i : 1 : w6i))); % Min Stopband attenuation
fprintf('\nMin Stopband attenuation is %.4f and %.4f dB.\n', As1, As2); [delta1_rect1, delta2_rect1] = db2delta(Rp1, As1)
[delta1_rect2, delta2_rect2] = db2delta(Rp2, As2) %% ----------------------------
%% Plot
%% ----------------------------- figure('NumberTitle', 'off', 'Name', 'Problem 7.36.1 ideal_lp Rect Method')
set(gcf,'Color','white'); subplot(2,2,1); stem(n, hd); axis([0 M-1 -1.0 2.0]); grid on;
xlabel('n'); ylabel('hd(n)'); title('Ideal Impulse Response');
subplot(2,2,2); stem(n, w_rect); axis([0 M-1 0 1.1]); grid on;
xlabel('n'); ylabel('w(n)'); title('Rectangular Window, M=227');
subplot(2,2,3); stem(n, h); axis([0 M-1 -1.0 2.0]); grid on;
xlabel('n'); ylabel('h(n)'); title('Actual Impulse Response'); subplot(2,2,4); plot(w/pi, db); axis([0 1 -100 10]); grid on;
set(gca,'YTickMode','manual','YTick',[-90,-33,-28,0]);
set(gca,'YTickLabelMode','manual','YTickLabel',['90';'33';'28';' 0']);
set(gca,'XTickMode','manual','XTick',[0,0.2,0.25,0.45,0.5,0.7,0.75,1]);
xlabel('frequency in \pi units'); ylabel('Decibels'); title('Magnitude Response in dB'); figure('NumberTitle', 'off', 'Name', 'Problem 7.36.1 h(n) ideal_lp Rect Method')
set(gcf,'Color','white'); subplot(2,2,1); plot(w/pi, db); grid on; %axis([0 1 -100 10]);
xlabel('frequency in \pi units'); ylabel('Decibels'); title('Magnitude Response in dB');
set(gca,'YTickMode','manual','YTick',[-90,-33,-28,0])
set(gca,'YTickLabelMode','manual','YTickLabel',['90';'33';'28';' 0']);
set(gca,'XTickMode','manual','XTick',[0,0.2,0.25,0.45,0.5,0.7,0.75,1,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.2,0.25,0.45,0.5,0.7,0.75,1,2]);
set(gca,'YTickMode','manual','YTick',[0.0,2.0,4.15]) 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.36.1 h(n) by Rect 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',[-delta1, 0, delta1, 2-delta2, 2+ delta2, 4.15- delta4, 4.15+delta4])
set(gca,'XTickMode','manual','XTick',[0,0.2,0.25,0.45,0.5,0.7,0.75,1]); %% ---------------------------------------------------
%% 2 Bartlett Window
%% ---------------------------------------------------
M = ceil(6.1*pi/tr_width) + 1; % Bartlett Window M=123
M=M+90;
fprintf('\n\n#2.Bartlett Window, Filter Length M=%d.\n', M); n = [0:1:M-1]; wc1 = (w2+w3)/2; wc2 = (w4+w5)/2; wc3 = (w6+w7)/2; %wc = (ws + wp)/2, % ideal LPF cutoff frequency hd = 2*(ideal_lp(wc2, M) - ideal_lp(wc1, M)) + 4.15*(ideal_lp(pi, M) - ideal_lp(wc3, M));
w_bart = (bartlett(M))'; h = hd .* w_bart;
[db, mag, pha, grd, w] = freqz_m(h, [1]); delta_w = 2*pi/1000;
[Hr,ww,P,L] = ampl_res(h); Rp1 = -(min(db(w3i :1: w4i))); % Actual Passband Ripple
Rp2 = -(min(db(w7i :1: w8i))); % Actual Passband Ripple
fprintf('\nActual Passband Ripple is %.4f and %.4f dB.\n', Rp1, Rp2); As1 = -round(max(db(1 : 1 : w2i))); % Min Stopband attenuation
As2 = -round(max(db(w5i : 1 : w6i))); % Min Stopband attenuation
fprintf('\nMin Stopband attenuation is %.4f and %.4f dB.\n', As1, As2); [delta1_rect1, delta2_rect1] = db2delta(Rp1, As1)
[delta1_rect2, delta2_rect2] = db2delta(Rp2, As2) %% --------------------------
%% Plot
%% -------------------------- figure('NumberTitle', 'off', 'Name', 'Problem 7.36.2 ideal_lp Bartlett Method')
set(gcf,'Color','white'); subplot(2,2,1); stem(n, hd); axis([0 M-1 -1.0 2.0]); grid on;
xlabel('n'); ylabel('hd(n)'); title('Ideal Impulse Response'); subplot(2,2,2); stem(n, w_bart); axis([0 M-1 0 1.1]); grid on;
xlabel('n'); ylabel('w(n)'); title('Bartlett Window, M=213'); subplot(2,2,3); stem(n, h); axis([0 M-1 -1.0 2.0]); grid on;
xlabel('n'); ylabel('h(n)'); title('Actual Impulse Response'); subplot(2,2,4); plot(w/pi, db); axis([0 1 -100 10]); grid on;
set(gca,'YTickMode','manual','YTick',[-90,-31,-25,0]);
set(gca,'YTickLabelMode','manual','YTickLabel',['90';'31';'25';' 0']);
set(gca,'XTickMode','manual','XTick',[0,0.2,0.25,0.45,0.5,0.7,0.75,1]);
xlabel('frequency in \pi units'); ylabel('Decibels'); title('Magnitude Response in dB'); figure('NumberTitle', 'off', 'Name', 'Problem 7.36.2 h(n) ideal_lp Bartlett Method')
set(gcf,'Color','white'); subplot(2,2,1); plot(w/pi, db); grid on; axis([0 2 -100 10]);
xlabel('frequency in \pi units'); ylabel('Decibels'); title('Magnitude Response in dB');
set(gca,'YTickMode','manual','YTick',[-90,-31,-25,0])
set(gca,'YTickLabelMode','manual','YTickLabel',['90';'31';'25';' 0']);
set(gca,'XTickMode','manual','XTick',[0,0.2,0.25,0.45,0.5,0.7,0.75,1,2]); subplot(2,2,3); plot(w/pi, mag); grid on; %axis([0 2 -100 10]);
xlabel('frequency in \pi units'); ylabel('Absolute'); title('Magnitude Response in absolute');
set(gca,'XTickMode','manual','XTick',[0,0.2,0.25,0.45,0.5,0.7,0.75,1,2]);
set(gca,'YTickMode','manual','YTick',[0.0,2.0,4.15]) 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.36.2 h(n) by Bartlett 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',[-delta1, 0, delta1, 2-delta2, 2+ delta2, 4.15- delta4, 4.15+delta4])
set(gca,'XTickMode','manual','XTick',[0,0.2,0.25,0.45,0.5,0.7,0.75,1]); %% ---------------------------------------------------
%% 3 Hann Window
%% ---------------------------------------------------
M = ceil(6.2*pi/tr_width) + 1; % Hann Window fprintf('\n\n#3.Hann Window, Filter Length M=%4d.\n', M); n = [0:1:M-1]; wc1 = (w2+w3)/2; wc2 = (w4+w5)/2; wc3 = (w6+w7)/2; %wc = (ws + wp)/2, % ideal LPF cutoff frequency hd = 2*(ideal_lp(wc2, M) - ideal_lp(wc1, M)) + 4.15*(ideal_lp(pi, M) - ideal_lp(wc3, M));
w_hann = (hann(M))'; h = hd .* w_hann;
[db, mag, pha, grd, w] = freqz_m(h, [1]); delta_w = 2*pi/1000;
[Hr,ww,P,L] = ampl_res(h); Rp1 = -(min(db(w3i :1: w4i))); % Actual Passband Ripple
Rp2 = -(min(db(w7i :1: w8i))); % Actual Passband Ripple
fprintf('\nActual Passband Ripple is %.4f and %.4f dB.\n', Rp1, Rp2); As1 = -round(max(db(1 : 1 : w2i))); % Min Stopband attenuation
As2 = -round(max(db(w5i : 1 : w6i))); % Min Stopband attenuation
fprintf('\nMin Stopband attenuation is %.4f and %.4f dB.\n', As1, As2); [delta1_rect1, delta2_rect1] = db2delta(Rp1, As1)
[delta1_rect2, delta2_rect2] = db2delta(Rp2, As2) %% --------------------------
%% Plot
%% -------------------------- figure('NumberTitle', 'off', 'Name', 'Problem 7.36.3 ideal_lp Hann Method')
set(gcf,'Color','white'); subplot(2,2,1); stem(n, hd); axis([0 M-1 -1.0 2.0]); grid on;
xlabel('n'); ylabel('hd(n)'); title('Ideal Impulse Response'); subplot(2,2,2); stem(n, w_hann); axis([0 M-1 0 1.1]); grid on;
xlabel('n'); ylabel('w(n)'); title('Hann Window, M=125'); subplot(2,2,3); stem(n, h); axis([0 M-1 -1.0 2.0]); grid on;
xlabel('n'); ylabel('h(n)'); title('Actual Impulse Response'); subplot(2,2,4); plot(w/pi, db); axis([0 1 -100 10]); grid on;
set(gca,'YTickMode','manual','YTick',[-90,-49,-41,0]);
set(gca,'YTickLabelMode','manual','YTickLabel',['90';'49';'41';' 0']);
set(gca,'XTickMode','manual','XTick',[0,0.2,0.25,0.45,0.5,0.7,0.75,1]);
xlabel('frequency in \pi units'); ylabel('Decibels'); title('Magnitude Response in dB'); figure('NumberTitle', 'off', 'Name', 'Problem 7.36.3 h(n) ideal_lp Hann Method')
set(gcf,'Color','white'); subplot(2,2,1); plot(w/pi, db); grid on; axis([0 2 -100 10]);
xlabel('frequency in \pi units'); ylabel('Decibels'); title('Magnitude Response in dB');
set(gca,'YTickMode','manual','YTick',[-90,-49,-41,0])
set(gca,'YTickLabelMode','manual','YTickLabel',['90';'49';'41';' 0']);
set(gca,'XTickMode','manual','XTick',[0,0.2,0.25,0.45,0.5,0.7,0.75,1,2]); subplot(2,2,3); plot(w/pi, mag); grid on; %axis([0 2 -100 10]);
xlabel('frequency in \pi units'); ylabel('Absolute'); title('Magnitude Response in absolute');
set(gca,'XTickMode','manual','XTick',[0,0.2,0.25,0.45,0.5,0.7,0.75,1,2]);
set(gca,'YTickMode','manual','YTick',[0.0,2.0,4.15]) 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.36.3 h(n) by Hann 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',[-delta1, 0, delta1, 2-delta2, 2+ delta2, 4.15- delta4, 4.15+delta4])
set(gca,'XTickMode','manual','XTick',[0,0.2,0.25,0.45,0.5,0.7,0.75,1]); %% ---------------------------------------------------
%% 4 Hamming Window
%% ---------------------------------------------------
M = ceil(6.6*pi/tr_width) + 1; % Hamming Window
fprintf('\n\n#4.Hamming Window, Filter Length M=%4d.\n', M); n = [0:1:M-1]; wc1 = (w2+w3)/2; wc2 = (w4+w5)/2; wc3 = (w6+w7)/2; %wc = (ws + wp)/2, % ideal LPF cutoff frequency hd = 2*(ideal_lp(wc2, M) - ideal_lp(wc1, M)) + 4.15*(ideal_lp(pi, M) - ideal_lp(wc3, M));
w_hamm = (hamming(M))'; h = hd .* w_hamm;
[db, mag, pha, grd, w] = freqz_m(h, [1]); delta_w = 2*pi/1000;
[Hr,ww,P,L] = ampl_res(h); Rp1 = -(min(db(w3i :1: w4i))); % Actual Passband Ripple
Rp2 = -(min(db(w7i :1: w8i))); % Actual Passband Ripple
fprintf('\nActual Passband Ripple is %.4f and %.4f dB.\n', Rp1, Rp2); As1 = -round(max(db(1 : 1 : w2i))); % Min Stopband attenuation
As2 = -round(max(db(w5i : 1 : w6i))); % Min Stopband attenuation
fprintf('\nMin Stopband attenuation is %.4f and %.4f dB.\n', As1, As2); [delta1_rect1, delta2_rect1] = db2delta(Rp1, As1)
[delta1_rect2, delta2_rect2] = db2delta(Rp2, As2) %% --------------------------
%% Plot
%% -------------------------- figure('NumberTitle', 'off', 'Name', 'Problem 7.36.4 ideal_lp Hamming Method')
set(gcf,'Color','white'); subplot(2,2,1); stem(n, hd); axis([0 M-1 -1.0 2.0]); grid on;
xlabel('n'); ylabel('hd(n)'); title('Ideal Impulse Response'); subplot(2,2,2); stem(n, w_hamm); axis([0 M-1 0 1.1]); grid on;
xlabel('n'); ylabel('w(n)'); title('Hamming Window, M=133'); subplot(2,2,3); stem(n, h); axis([0 M-1 -1.0 2.0]); grid on;
xlabel('n'); ylabel('h(n)'); title('Actual Impulse Response'); subplot(2,2,4); plot(w/pi, db); axis([0 1 -100 10]); grid on;
set(gca,'YTickMode','manual','YTick',[-90,-59,-49,0]);
set(gca,'YTickLabelMode','manual','YTickLabel',['90';'59';'49';' 0']);
set(gca,'XTickMode','manual','XTick',[0,0.2,0.25,0.45,0.5,0.7,0.75,1]);
xlabel('frequency in \pi units'); ylabel('Decibels'); title('Magnitude Response in dB'); figure('NumberTitle', 'off', 'Name', 'Problem 7.36.4 h(n) ideal_lp Hamming Method')
set(gcf,'Color','white'); subplot(2,2,1); plot(w/pi, db); grid on; axis([0 2 -100 10]);
xlabel('frequency in \pi units'); ylabel('Decibels'); title('Magnitude Response in dB');
set(gca,'YTickMode','manual','YTick',[-90,-59,-49,0])
set(gca,'YTickLabelMode','manual','YTickLabel',['90';'59';'49';' 0']);
set(gca,'XTickMode','manual','XTick',[0,0.2,0.25,0.45,0.5,0.7,0.75,1,1.3,1.5,1.8,2]); subplot(2,2,3); plot(w/pi, mag); grid on; %axis([0 2 -100 10]);
xlabel('frequency in \pi units'); ylabel('Absolute'); title('Magnitude Response in absolute');
set(gca,'XTickMode','manual','XTick',[0,0.2,0.25,0.45,0.5,0.7,0.75,1,1.3,1.5,1.8,2]);
set(gca,'YTickMode','manual','YTick',[0.0,2.0,4.15]) 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.36.4 h(n) by Hamming 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',[-delta1, 0, delta1, 2-delta2, 2+ delta2, 4.15- delta4, 4.15+delta4])
set(gca,'XTickMode','manual','XTick',[0,0.2,0.25,0.45,0.5,0.7,0.75,1]); %% ---------------------------------------------------
%% 5 Blackman Window
%% ---------------------------------------------------
M = ceil(11*pi/tr_width) + 1; % Blackman Window
fprintf('\n\n#5.Blackman Window, Filter Length M=%d.\n', M); n = [0:1:M-1]; wc1 = (w2+w3)/2; wc2 = (w4+w5)/2; wc3 = (w6+w7)/2; %wc = (ws + wp)/2, % ideal LPF cutoff frequency hd = 2*(ideal_lp(wc2, M) - ideal_lp(wc1, M)) + 4.15*(ideal_lp(pi, M) - ideal_lp(wc3, M));
w_bla = (blackman(M))'; h = hd .* w_bla;
[db, mag, pha, grd, w] = freqz_m(h, [1]); delta_w = 2*pi/1000;
[Hr,ww,P,L] = ampl_res(h); Rp1 = -(min(db(w3i :1: w4i))); % Actual Passband Ripple
Rp2 = -(min(db(w7i :1: w8i))); % Actual Passband Ripple
fprintf('\nActual Passband Ripple is %.4f and %.4f dB.\n', Rp1, Rp2); As1 = -round(max(db(1 : 1 : w2i))); % Min Stopband attenuation
As2 = -round(max(db(w5i : 1 : w6i))); % Min Stopband attenuation
fprintf('\nMin Stopband attenuation is %.4f and %.4f dB.\n', As1, As2); [delta1_rect1, delta2_rect1] = db2delta(Rp1, As1)
[delta1_rect2, delta2_rect2] = db2delta(Rp2, As2) %% --------------------------
%% Plot
%% -------------------------- figure('NumberTitle', 'off', 'Name', 'Problem 7.36.5 ideal_lp Blackman Method')
set(gcf,'Color','white'); subplot(2,2,1); stem(n, hd); axis([0 M-1 -1.0 2.0]); grid on;
xlabel('n'); ylabel('hd(n)'); title('Ideal Impulse Response'); subplot(2,2,2); stem(n, w_bla); axis([0 M-1 0 1.1]); grid on;
xlabel('n'); ylabel('w(n)'); title('Blackman Window, M=221'); subplot(2,2,3); stem(n, h); axis([0 M-1 -1.0 2.0]); grid on;
xlabel('n'); ylabel('h(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,-80,-68,0]);
set(gca,'YTickLabelMode','manual','YTickLabel',['90';'80';'68';' 0']);
set(gca,'XTickMode','manual','XTick',[0,0.2,0.25,0.45,0.5,0.7,0.75,1]);
xlabel('frequency in \pi units'); ylabel('Decibels'); title('Magnitude Response in dB'); figure('NumberTitle', 'off', 'Name', 'Problem 7.36.5 h(n) ideal_lp Blackman 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,-80,-68,0])
set(gca,'YTickLabelMode','manual','YTickLabel',['90';'80';'68';' 0']);
set(gca,'XTickMode','manual','XTick',[0,0.2,0.25,0.45,0.5,0.7,0.75,1,1.3,1.5,1.8,2]); subplot(2,2,3); plot(w/pi, mag); grid on; %axis([0 2 -120 10]);
xlabel('frequency in \pi units'); ylabel('Absolute'); title('Magnitude Response in absolute');
set(gca,'XTickMode','manual','XTick',[0,0.2,0.25,0.45,0.5,0.7,0.75,1,1.3,1.5,1.8,2]);
set(gca,'YTickMode','manual','YTick',[0.0,2.0,4.15]) 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.16.5 h(n) by Blackman 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',[-delta1, 0, delta1, 2-delta2, 2+ delta2, 4.15- delta4, 4.15+delta4])
set(gca,'XTickMode','manual','XTick',[0,0.2,0.25,0.45,0.5,0.7,0.75,1]); %% ---------------------------------------------------
%% 6 Kaiser Window
%% ---------------------------------------------------
As = 40;
M = ceil((As-7.95)/(2.285*tr_width)) + 1; % Kaiser Window 26--even if As > 21 || As < 50
beta = 0.5842*(As-21)^0.4 + 0.07886*(As-21);
else
beta = 0.1102*(As-8.7);
end
fprintf('\n\n#6.Kaiser Window, Filter Length M=%d, beta=%.4f\n', M,beta); n = [0:1:M-1]; wc1 = (w2+w3)/2; wc2 = (w4+w5)/2; wc3 = (w6+w7)/2; %wc = (ws + wp)/2, % ideal LPF cutoff frequency hd = 2*(ideal_lp(wc2, M) - ideal_lp(wc1, M)) + 4.15*(ideal_lp(pi, M) - ideal_lp(wc3, M));
w_kai = (kaiser(M,beta))'; h = hd .* w_kai;
[db, mag, pha, grd, w] = freqz_m(h, [1]); delta_w = 2*pi/1000;
[Hr,ww,P,L] = ampl_res(h); Rp1 = -(min(db(w3i :1: w4i))); % Actual Passband Ripple
Rp2 = -(min(db(w7i :1: w8i))); % Actual Passband Ripple
fprintf('\nActual Passband Ripple is %.4f and %.4f dB.\n', Rp1, Rp2); As1 = -round(max(db(1 : 1 : w2i))); % Min Stopband attenuation
As2 = -round(max(db(w5i : 1 : w6i))); % Min Stopband attenuation
fprintf('\nMin Stopband attenuation is %.4f and %.4f dB.\n', As1, As2); [delta1_rect1, delta2_rect1] = db2delta(Rp1, As1)
[delta1_rect2, delta2_rect2] = db2delta(Rp2, As2) %% --------------------------
%% Plot
%% -------------------------- figure('NumberTitle', 'off', 'Name', 'Problem 7.36.6 ideal_lp Kaiser Method')
set(gcf,'Color','white'); subplot(2,2,1); stem(n, hd); axis([0 M-1 -1.0 2.0]); grid on;
xlabel('n'); ylabel('hd(n)'); title('Ideal Impulse Response'); subplot(2,2,2); stem(n, w_kai); axis([0 M-1 0 1.1]); grid on;
xlabel('n'); ylabel('w(n)'); title('Kaiser Window, M=119'); subplot(2,2,3); stem(n, h); axis([0 M-1 -1.0 2.0]); grid on;
xlabel('n'); ylabel('h(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,-55,-47,0]);
set(gca,'YTickLabelMode','manual','YTickLabel',['90';'55';'47';' 0']);
set(gca,'XTickMode','manual','XTick',[0,0.2,0.25,0.45,0.5,0.7,0.75,1]);
xlabel('frequency in \pi units'); ylabel('Decibels'); title('Magnitude Response in dB'); figure('NumberTitle', 'off', 'Name', 'Problem 7.36.6 h(n) ideal_lp Kaiser 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,-55,-47,0])
set(gca,'YTickLabelMode','manual','YTickLabel',['90';'55';'47';' 0']);
set(gca,'XTickMode','manual','XTick',[0,0.2,0.25,0.45,0.5,0.7,0.75,1,1.3,1.5,1.8,2]); subplot(2,2,3); plot(w/pi, mag); grid on; %axis([0 2 -100 10]);
xlabel('frequency in \pi units'); ylabel('Absolute'); title('Magnitude Response in absolute');
set(gca,'XTickMode','manual','XTick',[0,0.2,0.25,0.45,0.5,0.7,0.75,1,1.3,1.5,1.8,2]);
set(gca,'YTickMode','manual','YTick',[0.0,2.0,4.15]) 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.16.6 h(n) by Kaiser 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',[-delta1, 0, delta1, 2-delta2, 2+ delta2, 4.15- delta4, 4.15+delta4])
set(gca,'XTickMode','manual','XTick',[0,0.2,0.25,0.45,0.5,0.7,0.75,1]);
运行结果:
窗函数法,使用了矩形窗、三角窗、Hann窗、Hamming窗、Blackman窗、Kaiser窗,
1、Rectangular窗
2、Bartlett三角窗
3、Hann、Hamming窗、Blackman窗的图这里不放了,直接放Kaiser窗的结果
4、频率采样方法
5、PM法
6、小结
以上用了窗函数法、频率采样法、Parks-McClellan法,下面对其得到的滤波器长度作对比,
序号 | 设 计 方 法 |
滤波器长度 M |
阻带衰减 As(dB) |
1 |
Rectangular矩形窗 |
227 | 28 |
2 | Bartlett三角窗 | 213 | 27 |
3 | Hann窗 | 125 | 41 |
4 | Hamming窗 | 133 | 49 |
5 | Blackman窗 | 221 | 68 |
6 | Kaiser窗 | 91 | 39 |
7 | 频率采样法 | 81 | 47 |
8 | Parks-McClellan法 | 67 | 30 |
结合滤波器指标要求和实际情况,Kaiser窗、频率采样法和P-M法可以采用。
《DSP using MATLAB》Problem 7.36的更多相关文章
- 《DSP using MATLAB》Problem 5.36
第1小题 代码: %% ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ ...
- 《DSP using MATLAB》Problem 8.36
上代码: function [wpLP, wsLP, alpha] = lp2lpfre(wplp, wslp) % Band-edge frequency conversion from lowpa ...
- 《DSP using MATLAB》Problem 4.15
只会做前两个, 代码: %% ---------------------------------------------------------------------------- %% Outpu ...
- 《DSP using MATLAB》Problem 7.27
代码: %% ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ %% Output In ...
- 《DSP using MATLAB》Problem 7.26
注意:高通的线性相位FIR滤波器,不能是第2类,所以其长度必须为奇数.这里取M=31,过渡带里采样值抄书上的. 代码: %% +++++++++++++++++++++++++++++++++++++ ...
- 《DSP using MATLAB》Problem 7.25
代码: %% ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ %% Output In ...
- 《DSP using MATLAB》Problem 7.24
又到清明时节,…… 注意:带阻滤波器不能用第2类线性相位滤波器实现,我们采用第1类,长度为基数,选M=61 代码: %% +++++++++++++++++++++++++++++++++++++++ ...
- 《DSP using MATLAB》Problem 7.23
%% ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ %% Output Info a ...
- 《DSP using MATLAB》Problem 7.16
使用一种固定窗函数法设计带通滤波器. 代码: %% ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ ...
随机推荐
- ASP.NET Core Web应用在发布时选择是否对视图进行编译
原文:ASP.NET Core Web应用在发布时选择是否对视图进行编译 在我们发布ASP.NET Core Web应用程序时,选择以文件形式发布,发布方法选择文件系统 默认情况下,会把Views的视 ...
- Linux 服务器 个人常用操作命令记录
1.实时查看log:tail -f 日志文件名 2.查看Apache运行的用户组或用户名:ps aux | grep httpd 或者是: ps -ef | grep httpd 3.查看cronta ...
- Python学习笔记(五)——异常处理
Python 异常总结 异常名称 解释 AssertionError 断言语句(assert)失败:当assert关键字后边的条件为假时,程序将抛出该异常,一般用于在代码中置入检查点 OSError ...
- 分布式唯一ID实现
ID生成的核心需求 全局唯一 趋势有序 为什么要全局唯一 避免ID冲突 著名的例子就是身份证号码,身份证号码确实是对人唯一的,然而一个人是可以办理多个身份证的,例如你身份证丢了,又重新补办了一张,号码 ...
- 启动web项目,监听器、过滤器、拦截器启动顺序
启动顺序:监听器 > 过滤器 > 拦截器 记忆技巧:接到命令,监听电报,过滤敌情,拦截行动.
- js--判断当前环境是否为app环境
/** *判断是否是app环境 */ function getIsApp () { var ua = navigator.userAgent.toLowerCase(); if (ua.match(/ ...
- php开发面试题---面试常用英语(你能介绍你自己吗?)
php开发面试题---面试常用英语(你能介绍你自己吗?) 一.总结 一句话总结: Could you please describe yourself? 1.为什么觉得自己适合这份工作? Why do ...
- 记录一次hexo托管到coding失败,页面总是404,可是相同的代码托管到github是没问题的。
文章目录 问题描述: 问题原因: 问题解决 2019.1.23 问题,coding又挂了. 弃疗 个人博客:https://mmmmmm.me 源码:https://github.com/dataiy ...
- ASP.NET的底层体系2
文章引导 1.ASP.NET的底层体系1 2.ASP.NET的底层体系2 引言 接着上一篇ASP.NET的底层体系1我们继续往下走 一.System.Web.HttpRuntime.ProcessRe ...
- PAT甲级——A1109 Group Photo【25】
Formation is very important when taking a group photo. Given the rules of forming K rows with Npeopl ...