You are on page 1of 2

% Calculating DFS and IDFS

clc; clf; clear all; close all;


x=randn(1,10);
figure(1);
stem(x);
C_k=dfs(x,10);
x_n=idfs(C_k,10);
hold on;
stem(real(x_n),'r--');
figure(2);
subplot(121); stem(real(C_k));
subplot(122); stem(imag(C_k));
%% x(n)={...,0,1,2,3,0 ? ,1,2,3,0,1,2,3,...}
x=[0 1 2 3]
figure(1);
stem(x);
C_k=dfs(x,4);
%x_n=idfs(C_k,10);
%hold on;
%stem(real(x_n),'r--');
figure(2);
subplot(121); stem(real(C_k));
subplot(122); stem(imag(C_k));

function [xn] = idfs(Xk,N)


%Calculating xn from Frequency domain co-efficients
% Ck=DFS co-efficient array over 0<=k<N-1
% N= Fundamental period of xn
n=[0:1:N-1];
k=[0:1:N-1];
Wn=exp(-1j*2*pi/N);
nk=n'*k;
Wnk=Wn.^(-nk);
xn=(Xk*Wnk);
end

function [ Ck ] = dfs( xn,N )


% Calculating Discrete Fourier Series Coefficients
% Ck=DFS co-efficient array over 0<=k<N-1
% N= Fundamental period of xn
n=0:1:N-1;
k=0:1:N-1;
Wn=exp(-1j*2*pi/N);
nk=n'*k;
Wnk=Wn.^nk;
Ck=xn*Wnk/N;
end

You might also like