Sunday, June 26, 2011

MATLAB Programs 07: Finding the DFT and IDFT of a sequence without using inbuilt functions


THEORY

A little basics will aid to get a better understanding of the program 

Basic eqation to find the DFT of a sequence is given below.



Basic equation to find the IDFT of a sequence is given below.  



It is based on these two equations the program is written.




PROGRAM


%program to find the DFT/IDFT of a sequence without using the inbuilt functions


close all;
clear all;
xn=input('Enter the sequence x(n)'); %Get the sequence from user 
ln=length(xn); %find the length of the sequence
xk=zeros(1,ln); %initilise an array of same size as that of input sequence
ixk=zeros(1,ln); %initilise an array of same size as that of input sequence


%code block to find the DFT of the sequence


%-----------------------------------------------------------
for k=0:ln-1
    for n=0:ln-1
        xk(k+1)=xk(k+1)+(xn(n+1)*exp((-i)*2*pi*k*n/ln));
    end
end
%------------------------------------------------------------


%code block to plot the input sequence


%------------------------------------------------------------
t=0:ln-1;
subplot(221);
stem(t,xn);
ylabel ('Amplitude');
xlabel ('Time Index');
TITLE ('Input Sequence');
%---------------------------------------------------------------




magnitude=abs(xk); % Find the magnitudes of individual DFT points


%code block to plot the magnitude response


%------------------------------------------------------------
t=0:ln-1;
subplot(222);
stem(t,magnitude);
ylabel ('Amplitude');
xlabel ('K');
TITLE ('Magnitude Response');
%------------------------------------------------------------


phase=angle(xk); % Find the phases of individual DFT points


%code block to plot the magnitude sequence


%------------------------------------------------------------
t=0:ln-1;
subplot(223);
stem(t,phase);
ylabel ('Phase');
xlabel ('K');
TITLE ('Phase Response');
%------------------------------------------------------------


% Code block to find the IDFT of the sequence


%------------------------------------------------------------
for n=0:ln-1
    for k=0:ln-1
        ixk(n+1)=ixk(n+1)+(xk(k+1)*exp(i*2*pi*k*n/ln));
    end
end


ixk=ixk./ln;
%------------------------------------------------------------


%code block to plot the input sequence


%------------------------------------------------------------
t=0:ln-1;
subplot(224);
stem(t,xn);
ylabel ('Amplitude');
xlabel ('Time Index');
TITLE ('IDFT sequence');
%-----------------------------------------------------------



Download the MATLAB file of this program.



16 comments:

Avishek said...

this code was helpful .. but i have a question if we want to convolve 2 sequence shall we follow the same process till dft without function and then do a matrix multiplication after that and find the phase angle and the magnitude of the function



Avishek
Master's Embedded systems
California

Anonymous said...

shut up.. n get a lyf

Anonymous said...

Chutia

Bokhtiar Mehedy Farsim said...

What should be the sequence as input?

SIDKIDBOY said...

WHAT SHOULD BE CODE FOR SOLUTION OF DIFFRENTIAL EQUATION?
HAVING HOMOGENEOUUS AND PARTICULAR SOLUTION...

Frank May said...

There is a very large mistake in your code, you plotted your input signal (xn) rather than plotting the data from the IDFT sequence (ixk).

Just change
stem(t,xn);
to
stem(t,ixk);
at the end of your code.


It's like a teacher asked you to draw an apple, you drew an apple, then handed her the apple back and said it was your drawing.


Shruthi Sugruvu said...

please can any one explain why we have taken the xk(xk+1) instead of xk?
because the sequence starts from xk(0) not from xk(1)...

Surendra Bodapati said...

what is the input sequnce

Anonymous said...

@shruti- i hv tried wth xk(0)..but it isnt working...well i aint intelligent to understand wht was going on.... but i wasnt getting the proper output - jeet

Anil C S said...

@Frank May

Thanks a lot for pointing out that mistake.

Anonymous said...

@Shruthi Sugruvu
xk(xk+1) ... has been used becuase unlike many other programming languages where the array or table index starts from 0 in matlab the all the indexes starts from 1. So The loop will run from index 1 to N etc.

I hope it's clear

Eugene Nii Arday Ankrah said...

Can i get help with a matlab function that is suppose to compute inverse DFT function?

Anonymous said...

sexy bichi

Anonymous said...

anil cs pod mara ja

Anonymous said...

frank may i will fuck u in may

Anonymous said...

lai bhari bhawa

Post a Comment

Twitter Delicious Facebook Digg Stumbleupon Favorites More

 
Design by Free WordPress Themes | Bloggerized by Lasantha - Premium Blogger Themes | coupon codes