DSPRelated.com
Code

Discrete Fourier Transform (DFT)

Miguel De Jesus Rosario October 27, 201011 comments Coded in Matlab

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).