《DSP using MATLAB》Problem 5.4-LMLPHP

代码:

%% ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
%% Output Info about this m-file
fprintf('\n***********************************************************\n');
fprintf(' <DSP using MATLAB> Problem 5.4 \n\n'); banner();
%% ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ % ------------------------------------------
% 1 x1(n)=5sin(0.1pi*n) n=[0:19], N=40
% ------------------------------------------
L = 20; n = [0:L-1]; N = 40; k1 = [0 : N-1]; % wave parameters
%k2 = [-(N-1):0];
xn_1 = [5*sin(0.1*pi*n), zeros(1, N-L)];
Xk_1 = dfs(xn_1, N); % DFS
magXk = abs( [ Xk_1(1 : N)] ); % DFS magnitude
angXk = angle( Xk_1 )/pi; % DFS angle xn_2 = [xn_1(1:20) -xn_1(1:20)];
Xk_2 = dfs(xn_2, N);
magXk_2 = abs( [Xk_2(1:N)]);
angXk_2 = angle( Xk_2)/pi; figure('NumberTitle', 'off', 'Name', 'P5.4 xn_1 and xn_2')
set(gcf,'Color','white');
subplot(2,1,1); stem([0:N-1], xn_1);
xlabel('n'); ylabel('x(n)');
title('xn1 sequence in Problem 5.3, N=40'); grid on;
subplot(2,1,2); stem([0:N-1], xn_2);
xlabel('n'); ylabel('x(n)');
title('xn2 sequence, N=40'); grid on; %% ------------------------------------------------------
%% DFS(k) of xn1 sequence
%% ------------------------------------------------------
figure('NumberTitle', 'off', 'Name', 'P5.4 DFS(k) of xn_1')
set(gcf,'Color','white');
subplot(2,1,1); stem(k1, magXk); %axis([-N/2, N/2, -0.5, 50.5]);
xlabel('k'); ylabel('magnitude(k)');
title('DFS magnitude of xn1, N=40'); grid on;
subplot(2,1,2); stem(k1, angXk); %axis([-N/2, N/2, -0.5, 50.5]);
xlabel('k'); ylabel('angle(k)');
title('DFS angle of xn1, N=40'); grid on; %% ------------------------------------------------------
%% DFS(k) of xn2 sequence
%% ------------------------------------------------------
figure('NumberTitle', 'off', 'Name', 'P5.4 DFS(k) of xn_2')
set(gcf,'Color','white');
subplot(2,1,1); stem(k1, magXk_2); %axis([-N/2, N/2, -0.5, 50.5]);
xlabel('k'); ylabel('magnitude(k)');
title('DFS magnitude of xn2, N=40'); grid on;
subplot(2,1,2); stem(k1, angXk_2); %axis([-N/2, N/2, -0.5, 50.5]);
xlabel('k'); ylabel('angle(k)');
title('DFS angle of xn2, N=40'); grid on;

  运行结果:

《DSP using MATLAB》Problem 5.4-LMLPHP

《DSP using MATLAB》Problem 5.4-LMLPHP

《DSP using MATLAB》Problem 5.4-LMLPHP

上面是从0到N-1范围内的图形。还可以得到[-N/2,N/2]范围内的。

《DSP using MATLAB》Problem 5.4-LMLPHP

《DSP using MATLAB》Problem 5.4-LMLPHP

05-11 14:46