《DSP using MATLAB》Problem 9.2

        前幾天看了看博客,從16年末到如今,3年了,終於看書到第9章了。都怪本身愚鈍不堪,惟有吃苦努力,一點一點一頁一頁慢慢啃了。git

代碼:this

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

banner();
%% ------------------------------------------------------------------------


% ------------------------------------------------------------
%                  PART 1   
% ------------------------------------------------------------

% Discrete time signal

n1_start = 0; n1_end = 60;
      n1 = [n1_start:1:n1_end];
    
xn1 = (0.9).^n1 .* stepseq(0, n1_start, n1_end);             % digital signal

 D = 2;                      % downsample by factor D 
 y = downsample(xn1, D);
ny = [n1_start:n1_end/D];

figure('NumberTitle', 'off', 'Name', 'Problem 9.2 xn1 and y')
set(gcf,'Color','white'); 
subplot(2,1,1); stem(n1, xn1, 'b');
xlabel('n'); ylabel('x(n)');
title('xn1 original sequence');  grid on;
subplot(2,1,2); stem(ny, y, 'r');
xlabel('ny'); ylabel('y(n)');
title('y sequence, downsample by D=2 ');  grid on;


% ----------------------------
%       DTFT of xn1
% ----------------------------
M = 500;
[X1, w] = dtft1(xn1, n1, M);

magX1  = abs(X1);  angX1  = angle(X1);  realX1  = real(X1);  imagX1  = imag(X1);


%% --------------------------------------------------------------------
%%              START X(w)'s  mag ang real imag
%% --------------------------------------------------------------------
figure('NumberTitle', 'off', 'Name', 'Problem 9.2 X1 DTFT');
set(gcf,'Color','white'); 
subplot(2,1,1); plot(w/pi,magX1); grid on;  %axis([-1,1,0,1.05]); 
title('Magnitude Response');
xlabel('digital frequency in \pi units'); ylabel('Magnitude  |H|'); 
subplot(2,1,2); plot(w/pi, angX1/pi); grid on;  %axis([-1,1,-1.05,1.05]);
title('Phase Response');
xlabel('digital frequency in \pi units'); ylabel('Radians/\pi');

figure('NumberTitle', 'off', 'Name', 'Problem 9.2 X1 DTFT');
set(gcf,'Color','white'); 
subplot(2,1,1); plot(w/pi, realX1); grid on;
title('Real Part');
xlabel('digital frequency in \pi units'); ylabel('Real');
subplot(2,1,2); plot(w/pi, imagX1); grid on;
title('Imaginary Part');
xlabel('digital frequency in \pi units'); ylabel('Imaginary');
%% -------------------------------------------------------------------
%%             END X's  mag ang real imag
%% -------------------------------------------------------------------


% ----------------------------
%       DTFT of y
% ----------------------------
M = 500;
[Y, w] = dtft1(y, ny, M);

magY_DTFT = abs(Y);  angY_DTFT = angle(Y);  realY_DTFT = real(Y);  imagY_DTFT = imag(Y);


%% --------------------------------------------------------------------
%%              START Y(w)'s  mag ang real imag
%% --------------------------------------------------------------------
figure('NumberTitle', 'off', 'Name', 'Problem 9.2 Y DTFT');
set(gcf,'Color','white'); 
subplot(2,1,1); plot(w/pi, magY_DTFT); grid on;  %axis([-1,1,0,1.05]); 
title('Magnitude Response');
xlabel('digital frequency in \pi units'); ylabel('Magnitude  |H|'); 
subplot(2,1,2); plot(w/pi, angY_DTFT/pi); grid on;  %axis([-1,1,-1.05,1.05]);
title('Phase Response');
xlabel('digital frequency in \pi units'); ylabel('Radians/\pi');

figure('NumberTitle', 'off', 'Name', 'Problem 9.2 Y DTFT');
set(gcf,'Color','white'); 
subplot(2,1,1); plot(w/pi, realY_DTFT); grid on;
title('Real Part');
xlabel('digital frequency in \pi units'); ylabel('Real');
subplot(2,1,2); plot(w/pi, imagY_DTFT); grid on;
title('Imaginary Part');
xlabel('digital frequency in \pi units'); ylabel('Imaginary');
%% -------------------------------------------------------------------
%%             END Y's  mag ang real imag
%% -------------------------------------------------------------------



figure('NumberTitle', 'off', 'Name', 'Problem 9.2 X1 & Y, DTFT of x and y');
set(gcf,'Color','white'); 
subplot(2,1,1); plot(w/pi,magX1); grid on;  %axis([-1,1,0,1.05]); 
title('Magnitude Response');
xlabel('digital frequency in \pi units'); ylabel('Magnitude  |H|');  
hold on;
plot(w/pi, magY_DTFT, 'r'); gtext('magY(\omega)', 'Color', 'r');
hold off;

subplot(2,1,2); plot(w/pi, angX1/pi); grid on;  %axis([-1,1,-1.05,1.05]);
title('Phase Response');
xlabel('digital frequency in \pi units'); ylabel('Radians/\pi');
hold on;
plot(w/pi, angY_DTFT/pi, 'r'); gtext('AngY(\omega)', 'Color', 'r');
hold off;


  運行結果:3d

        原始序列x和按D=2抽取後序列y,以下blog

        原始序列x的DTFT,這裏只放幅度譜和相位譜,DTFT的實部和虛部的圖不放了(代碼中有計算)。博客

        抽取後序列y的DTFT以下圖,也是幅度譜和相位譜,實部和虛部的圖不放了(代碼中有計算)。it

        將上述兩張圖疊合到一塊兒作對比,紅色曲線是抽取後序列的DTFT,藍色曲線是原始序列的DTFT。class

可見,紅色曲線的幅度近似爲藍色曲線的二分之一(1/D,這裏D=2)。file

相關文章
相關標籤/搜索