Clear Filters
Clear Filters

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

6 views (last 30 days)
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 Comments
Steven Lord
Steven Lord on 28 Jul 2022
What were you hoping or expecting those lines of code to do? What specific vector were you hoping to create?
If (as an example) you were hoping to create the vector [-5 -4 -3 -2 -1 0 1 2 3 4 5] when N was 5, just use colon with the default increment of 1 (or you can explicitly specify the increment.)
N = 5;
k1 = -N:N
k1 = 1×11
-5 -4 -3 -2 -1 0 1 2 3 4 5
k2 = -N:1:N
k2 = 1×11
-5 -4 -3 -2 -1 0 1 2 3 4 5
Your version failed to do what you wanted because it's not possible to get from -5 to 5 by taking steps of length 0 as you asked MATLAB to do.
Pratiksha Sahoo
Pratiksha Sahoo on 28 Jul 2022
I made those changes but the output still ain't coming, vector ranges from -10 to 10.
Magnitude plot is of amplitude 1 at [0] and 0.5 each at [-5,-2,2,5]
For the phase plot, the amplitude is pi/2 at [-2] and -pi/2 at [2]

Sign in to comment.

Answers (1)

Harsh Kumar
Harsh Kumar on 24 Jun 2023
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

Categories

Find more on Signal Processing Toolbox in Help Center and File Exchange

Products


Release

R2022a

Community Treasure Hunt

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

Start Hunting!