首页 > 代码库 > 《DSP using MATLAB》示例 Example 9.14

《DSP using MATLAB》示例 Example 9.14

技术分享

代码:

%% ------------------------------------------------------------------------
%%            Output Info about this m-file
fprintf(‘\n***********************************************************\n‘);
fprintf(‘        <DSP using MATLAB> Exameple 9.14 \n\n‘);

time_stamp = datestr(now, 31);
[wkd1, wkd2] = weekday(today, ‘long‘);
fprintf(‘      Now is %20s, and it is %7s  \n\n‘, time_stamp, wkd2);
%% ------------------------------------------------------------------------

% Given Parameters:

D = 2; Rp = 0.1; As = 50; wxp = pi/8; wxs = pi/D; wp = wxp; ws = (2*pi/D)-wxs; 

% Filter Design:
[delta1, delta2] = db2delta(Rp, As); 

[N, F, A, weights] = firpmord([wp, ws]/pi, [1, 0], [delta1, delta2], 2); N = ceil(N/2)*2;
h = firpm(N, F, A, weights); 
delay = N/2;                        % delay imparted by the filter  


%% -----------------------------------------------------------------
%%                             Plot
%% -----------------------------------------------------------------  

% Input signal x(n) = cos(2*pi*n/16)
n = [0:256]; x = cos(pi*n/8);
n1 = n(1:33); x1 = x(33:65);            % for plotting purposes

Hf1 = figure(‘units‘, ‘inches‘, ‘position‘, [1, 1, 8, 6], ...
	‘paperunits‘, ‘inches‘, ‘paperposition‘, [0, 0, 6, 4], ...
	‘NumberTitle‘, ‘off‘, ‘Name‘, ‘Exameple 9.14‘);
set(gcf,‘Color‘,‘white‘); 

TF = 10;

subplot(2, 2, 1); 
Hs1 = stem(n1, x1, ‘filled‘); set(Hs1, ‘markersize‘, 2, ‘color‘, ‘g‘);
axis([-2, 34, -1.2, 1.2]); grid on;
xlabel(‘n‘, ‘vertical‘, ‘middle‘); ylabel(‘Amplitude‘, ‘vertical‘, ‘middle‘); 
title(‘Input Singal: x(n) = cos(\pin/8) ‘, ‘fontsize‘, TF, ‘vertical‘, ‘baseline‘);
set(gca, ‘xtick‘, [0:8:32]); 
set(gca, ‘ytick‘, [-1, 0, 1]);

% Decimation of x(n): D = 2
y = upfirdn(x, h, 1, D);
m = delay+1:1:128/D+delay+1; y1 = y(m); m = 0:16; y1 = y1(14:30);

subplot(2, 2, 3); 
Hs2 = stem(m, y1, ‘filled‘); set(Hs2, ‘markersize‘, 2, ‘color‘, ‘m‘);
axis([-1, 17, -1.2, 1.2]); grid on;
xlabel(‘m‘, ‘vertical‘, ‘middle‘); ylabel(‘Amplitude‘, ‘vertical‘, ‘middle‘); 
title(‘Output Singal: y(n): D=2‘, ‘fontsize‘, TF, ‘vertical‘, ‘baseline‘);
set(gca, ‘xtickmode‘, ‘manual‘, ‘xtick‘, [0:8:32]/D); 
set(gca, ‘ytickmode‘, ‘manual‘, ‘ytick‘, [-1, 0, 1]);

% Filter Design Plots
[Hr, w, a, L] = Hr_Type1(h); Hr_min = min(Hr); w_min = find(Hr == Hr_min);
H = abs(freqz(h, 1, w)); Hdb = 20*log10(H/max(H)); min_attn = Hdb(w_min);
subplot(2, 2, 2); 
plot(w/pi, Hr, ‘m‘, ‘linewidth‘, 1.0); axis([0, 1, -0.1, 1.1]); grid on;
xlabel(‘Frequency in \pi units‘, ‘fontsize‘, TF, ‘vertical‘, ‘middle‘);
ylabel(‘Amplitude‘, ‘vertical‘, ‘cap‘);
title(‘Amplitude Response‘, ‘fontsize‘, TF, ‘vertical‘, ‘baseline‘);
set(gca, ‘xtick‘, [0, wp/pi, ws/pi, 1], ‘ytick‘, [0, 1]);


subplot(2, 2, 4); 
plot(w/pi, Hdb, ‘m‘, ‘linewidth‘, 1.0); axis([0, 1, -60, 10]); grid on;
xlabel(‘Frequency in \pi units‘, ‘fontsize‘, TF, ‘vertical‘, ‘middle‘);
ylabel(‘Decibels‘, ‘vertical‘, ‘cap‘);
title(‘Log-magnitude Response‘, ‘fontsize‘, TF, ‘vertical‘, ‘baseline‘);
set(gca, ‘xtick‘, [0, wp/pi, ws/pi, 1], ‘ytick‘, [-60, round(min_attn), 0]);

  运行结果:

技术分享

     设计的滤波器达到51dB的衰减,并且减采样的信号是正确的。

 

《DSP using MATLAB》示例 Example 9.14