1. 程式人生 > >《DSP using MATLAB》示例Example 9.9

《DSP using MATLAB》示例Example 9.9

time 產生 images length img art markers 濾波器 峰值

技術分享

代碼:

%% ------------------------------------------------------------------------
%%            Output Info about this m-file
fprintf(‘\n***********************************************************\n‘);
fprintf(‘        <DSP using MATLAB> Exameple 9.9 \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:
I = 5; Rp = 0.1; As = 30; wp = pi/I; ws = pi*0.32; 
[delta1, delta2] = db2delta(Rp, As); weights = [delta2/delta1, 1];
n = [0:50]; x = cos(0.5*pi*n);
n1 = n(1:17); x1 = x(17:33);     % for plotting purposes



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

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

TF = 10;

subplot(2, 2, 1); 
Hs1 = stem(n1, x1, ‘filled‘); set(Hs1, ‘markersize‘, 2, ‘color‘, ‘g‘);
axis([-1, 17, -1.2, 1.2]); grid on;
xlabel(‘n‘, ‘vertical‘, ‘middle‘); ylabel(‘Amplitude‘); 
title(‘Input Signal x(n)‘, ‘fontsize‘, TF);
set(gca, ‘xtick‘, [0:4:16]); 
set(gca, ‘ytick‘, [-1, 0, 1]);


% Interpolation with Filter Design: Length M=31
M = 31; F = [0, wp, ws, pi]/pi; A = [I, I, 0, 0];
h = firpm(M-1, F, A, weights); y = upfirdn(x, h, I);
delay = (M-1)/2;           % Delay imparted by the filter
m = delay+1:1:50*I+delay+1; y = y(m); m = 1:81; y = y(81:161);   % for plotting


subplot(2, 2, 2); 
Hs2 = stem(m, y, ‘filled‘); axis([-5, 85, -1.2, 1.2]); grid on;
xlabel(‘n‘, ‘vertical‘, ‘middle‘); ylabel(‘Amplitude‘); 
title(‘ Output y(n): I = 5, Filter length=31‘, ‘fontsize‘, TF);
set(gca, ‘xtick‘, [0:4:16]*I); 
set(gca, ‘ytick‘, [-1, 0, 1]);

% Interpolation with Filter Design: Length M = 51
M = 51; F = [0, wp, ws, pi]/pi; A = [I, I, 0, 0];
h = firpm(M-1, F, A, weights); y = upfirdn(x, h, I);
delay = (M-1)/2;           % Delay imparted by the filter
m = delay+1:1:50*I+delay+1; y = y(m); m = 1:81; y = y(81:161);   % for plotting

subplot(2, 2, 3); 
Hs3 = stem(m, y, ‘filled‘); axis([-5, 85, -1.2, 1.2]); grid on;
set(Hs3, ‘markersize‘, 2, ‘color‘, ‘m‘);
xlabel(‘n‘, ‘vertical‘, ‘middle‘); ylabel(‘Amplitude‘); 
title(‘Output y(n): I = 5, Filter length=51 ‘, ‘fontsize‘, TF);
set(gca, ‘xtick‘, [0:4:16]*I); 
set(gca, ‘ytick‘, [-1, 0, 1]);

% Interpolation with Filter Design : Length M = 81
M = 81; F = [0, wp, ws, pi]/pi; A = [I, I, 0, 0];
h = firpm(M-1, F, A, weights); y = upfirdn(x, h, I);
delay = (M-1)/2;           % Delay imparted by the filter
m = delay+1:1:50*I+delay+1; y = y(m); m = 1:81; y = y(81:161);   % for plotting

subplot(2, 2, 4); 
Hs4 = stem(m, y, ‘filled‘); axis([-5, 85, -1.2, 1.2]); grid on;
set(Hs4, ‘markersize‘, 2, ‘color‘, ‘m‘);
xlabel(‘n‘, ‘vertical‘, ‘middle‘); ylabel(‘Amplitude‘); 
title(‘Output y(n): I = 5, Filter length=81 ‘, ‘fontsize‘, TF);
set(gca, ‘xtick‘, [0:4:16]*I); 
set(gca, ‘ytick‘, [-1, 0, 1]);

  運行結果:

技術分享

左上圖是輸入信號x(n)的一部分,右上圖是使用長度為31的濾波器後得到的輸出y(n)。對於濾波器延遲和過渡帶響應來說,該圖是正確的。令人驚訝的是插值後的信號不是其應該的模樣。

峰值超過了1,形狀有些變形。仔細看圖9.20中的濾波器響應表現為寬的過渡帶和小的衰減,必然會導致一些譜能量的泄漏,產生變形。

對於較大的階數來說,濾波器低通特征較好。信號峰值接近1,並且其形狀接近余弦波形。因此,一個好的濾波器設計甚至對一個簡單的信號都是嚴格適用的。

《DSP using MATLAB》示例Example 9.9