Not a member?

# Discrete Fourier Transform (DFT)

Language: Matlab

Processor: Not Relevant

Submitted by Miguel De Jesus on Oct 27 2010

# Discrete Fourier Transform (DFT)

This is a basic Matlab implementation of the Discrete Fourier Transform also known as DFT. The function receives as input a discrete-time signal (a sequence of N real numbers)and returns the frequency spectrum (a sequence of N complex numbers).

function xdft=fdft(x)

N=length(x);

xdft(1,N)=0; % defining a 1xN sequence with all the elements equals to zero.

%*************THIS IS THE DFT IMPLEMENTATION**************
%For each value of k, the Summatory function is computed over the interval 0<n<N-1.

for k=0:N-1,
for n=0:N-1,
xdft(k+1)=xdft(k+1) + x(n+1)*exp(-j*2*pi*k*n/N);
end
end

%NOTE: You may skip the use of for-loops by defining k and n as two N-points sequences. This is:

% k=0:N-1;  n=0:N-1;

%The DFT is now computed in matrix form (as a matrix multiplication of the N-points Fourier Matrix and the discrete-time sequence x).

Rate this code snippet:
2.6

posted by Miguel De Jesus
Miguel De Jesus possessed a BS in Electrical Engineering. He also pursued graduate studies obtaining a MSEE with specialization in Digital Signal Processing from the Polytechnic University of Puerto Rico. He has experience in research and development in several DSP areas such as time/frequency analysis, array processing, and parallel processing.

# Synbios wrote:

11/4/2010

Useless unless you're taking DSP 101

# mnentwig wrote:

11/5/2010

>> Useless
keep in mind, the "call for code snippets" explicitly mentioned "from the simplest filter implementation to the most complex DSP algorithm". Many *are* actually taking DSP 101, nothing wrong with that.
Besides, if more people would take the time to *understand* those five lines of code, the world (or at least comp.dsp) would be a better place :-)

# mdejesus wrote:

11/19/2010

Synbios,
The DFT is one of the most fundamental and simplest concept in DSP.   This code and other more complex codes will be useful for you, when enroll any DSP course.

You can't use something you don't understand.

# gilgamash wrote:

11/29/2010

I thought we are looking for rather new things here. Sorry, but this is high school stuff. If this was at least some speed improved fft code or some nice twitch with the primitive roots of unity or whatever, fine. But imho this is just too basic.

Regards,
G.

# mdejesus wrote:

11/30/2010

You are right, this is too basic.  If you are looking for advanced things, maybe the DFT is not what you're looking for.  This code is intended to be used by beginners in DSP.

# stephaneb wrote:

11/30/2010

> I thought we are looking for rather new things here

Not necessarily.  This could be of some value to students or people learning DSP.

# aerodude wrote:

12/15/2010

Hi, I wrote this for learning purposes. But I am stuck after this stage. How do I go from this output (xdft in your case) to the desired periodogram (amplitude vs frequency) graph? I take it that to find out the peak frequencies of the system, this is what you need.

Any help on getting to this step would be great. Thanks in advance.

# mdejesus wrote:

12/15/2010

Hi Aerodude,

Precisely, my objective here is help people in the dsp learning process.  If you want to plot the spectrum (amplitude vs frequency) just add the following piece of code in the Matlab Command Window or  in a m-file :

N=length(xdft); %N is the number of frequency samples

k=0:N-1; % k is a vector containing the dft index

faxis=k/N*fs; % mapping k-axis to  f-axis (frequency axis in Hz) NOTE: fs is the sampling frequency used to sampling the signal x.

plot(faxis,abs(xdft)) % plot the frequency spectrum

# lmorales wrote:

12/15/2010

This is a good code to illustrate this fundamental concept.  This is useful for students and DSP beginners.  Congratulations!!

# aerodude wrote:

12/20/2010

Mdejesus,

Thanks for the help, really useful for people (like me) who come from a different background and have to use DFT. Thanks a lot.

12/20/2010

Good Luck!!!