# Sketch the magnitude and phase plot of x(n)=sin(4​πn/21)+cos​(10πn/21)+​1 using DTFS representation.

18 次查看（过去 30 天）
Pratiksha Sahoo 2022-7-27

function [Xk]=dfs(xn, N)
% Computes Discrete Fourier Series Coefficients
% ---------------------------------------------
% [Xk] = dfs(xn,N)
% Xk = DFS coeff. array over 0 <= k <= N-1
% xn = One period of periodic signal over 0 <= n <= N-1
% N = Fundamental period of xn
n=-N:0:N; % row vector for n
k=-N:0:N; % row vector for k
WN=exp(-1i.*2.*pi/N); % Wn factor
nk=n'*k; % creates a N by N matrix of nk values
WNnk=WN.^nk; % DFS matrix
Xk=xn*WNnk; % row vector for DFS coefficients
subplot(2,1,1);
stem(n,abs(Xk));
xlabel('n');
ylabel('Magnitude plot');
subplot(2,1,2);
stem(n,angle(Xk));
xlabel('n');
ylabel('Phase plot');
Command window:
N=10;
xn=1+sin(4*pi/21*n)+cos(10*pi/21*n);
Xk=dfs(xn,N)
##### 6 个评论显示 5更早的评论隐藏 5更早的评论
Pratiksha Sahoo 2022-7-28
I made those changes but the output still ain't coming, vector ranges from -10 to 10.
Magnitude plot is of amplitude 1 at  and 0.5 each at [-5,-2,2,5]
For the phase plot, the amplitude is pi/2 at [-2] and -pi/2 at 

### 回答（1 个）

Harsh Kumar 2023-6-24
Hi Pratiksha ,
It is my understanding that you are trying to implement the Discrete Time Fourier Series programmatically in MATLAB and unable to get the plots of output.
To resolve this issue, you have to define a global variable n to parse into your input function xn and have to use non-zero steps in your variables n and k instead of 0 as currently they are pointing to a NULL Array.
Please refer to the below code snippet for better understanding.
N=10;
n=-N:1:N;
xn=1+sin(4*pi/21*n)+cos(10*pi/21*n);
Xk=dfs(xn,N);
function [Xk]=dfs(xn, N)
% row vector for k
k=-N:1:N;
n=-N:1:N;
% Wn factor
WN=exp(-1i.*2.*pi/N);
% creates a N by N matrix of nk values
nk=n'*k;
% DFS matrix
WNnk=WN.^nk;
% row vector for DFS coefficients
Xk=xn*WNnk;
subplot(2,1,1);
stem(n,abs(Xk));
xlabel('n');
ylabel('Magnitude plot');
subplot(2,1,2);
stem(n,angle(Xk));
xlabel('n');
ylabel('Phase plot');
end

### 类别

Help CenterFile Exchange 中查找有关 Signal Processing Toolbox 的更多信息

R2022a

### Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!