You are on page 1of 4

EXPERIMENT-3

AIM: To write MATLAB code for DFT and FFT of discrete time sequences.
MATLAB has a built-in function dft and fft for carrying out the dft and fft between two
sequences.

MATLAB CODE : DFT

function [Xk]=dft(xn,N);
xn=input('enter the xn :');
N=input('enter the N :');
n=[0:1:N-1];
k=[0:1:N-1];
WN=exp(-j*2*pi/N);
nk=n'*k;
WNnk=WN.^nk;
Xk=xn*WNnk;
plot(Xk);

enter the xn :[0 1 2 3];


enter the N :4
ans =

6.0000 + 0.0000i -2.0000 + 2.0000i -2.0000 - 0.0000i -2.0000 - 2.0000i


Fig. DFT of input sequence xn

FFT Of a sequence :
MATLAB CODE :

x1=[0 2 4 1 5];
y=fft(x1);
>> plot(y);
Ans:
y=

12.0000 + 0.0000i -1.8820 + 1.0898i -4.1180 + 4.6165i -4.1180 -


4.6165i -1.8820 - 1.0898i
Fig. FFT of input sequence x1

FFT of Mixed Signals :

MATLAB CODE :

t=linspace(0,1,256);
s=sin(2*pi*10*t)+sin(2*pi*20*t)+sin(2*pi*30*t)+sin(2*pi*40*t)
+sin(2*pi*50*t);
subplot(2,1,1);
plot(s);
axis([0 256 -5 5]);
title('mixed signal');
xlabel('Time');
ylabel('Amplitude');
subplot(2,1,2);
y=abs(fft(s));
l=length(s);
f=1:l/2;
plot(f,y(1:l/2));
axis([0 60 0 200]);
title('FFT of mixed signal');

Fig. FFT of mixed signals

You might also like