Monday, March 15, 2010

Discrete Fourier Transform - DFT

I made some posts about Discrete Fourier Transform (DFT), they're here.

But, I received a comment asking me how to do a function that implements DFT.


Let's only write a script, not optimized, that calculates DFT:

function X = DFT(x)
n = length(x); //number of elements in 'x'

omega = exp(-2*%pi*%i/n);
j=0:(n - 1);
F=omega.^(j'*j); //Fourier matrix

X = F*x(:); //X = DFT(x)
endfunction;


If anyone wants to try the showed function, you could do like this:

N = 10;
x = rand(N, 1);

X1 = DFT(x)
X2 = dft(x, -1) // dft(.) function uses flag = -1 for direct transform and flag = 1 for inverse transform
X3 = fft(x)


Now, look the values of X1, X2 and X3.


Did I help?

2 comments: