-5

Can someone explain these nested loops of DFT in matlab:

N=length(x);
for k=1:N
  X(k)=0;
  for n=1:N
    X(k)=X(k)+x(n).*exp(-1j.*2.*pi.*(n-1).*(k-1)./N);
  end
end
SleuthEye
  • 14,379
  • 2
  • 32
  • 61

1 Answers1

4

The inner loop over n is a straightforward implementation of the Discrete Fourier Transform equation for a specific frequency bin k:

enter image description here

adjusted for 1-based indexing (as opposed to the 0-based indexing formula from Wikipedia).

The outer loop over k simply compute the equation for all N frequency bins.

SleuthEye
  • 14,379
  • 2
  • 32
  • 61
  • I normally don't upvote answers on downvoted questions, but I've always loved your signal processing answers. This one especially. Short and succinct, and brings up the fact about the indexing. +1. – rayryeng Dec 11 '15 at 19:03
  • thanku so much for helping...but the equation in the loop is not same as fourier transform equation., it inputs (n-1 )* k(-1) instead of n*k.why is that? – noman saeed Dec 13 '15 at 09:33
  • As I mentioned, "adjusted for 1-based indexing". Wikipedia equation uses 0 to N-1. With the change the change of variables n = n'-1 and k = k'-1, you can loop over n' and k' from 1 to N and get the same result. Try writing down a few terms if it helps. – SleuthEye Dec 13 '15 at 13:35