MatLab - Fast Fourier Transform

908 Views Asked by At

I have just started messing around with the fft function in MatLab, but can't quite understand the output.

Say I write the following:

k = 1:64;
y = cos(2*k*pi/64);
fy = fft(y);
plot(k,abs(fy))

This gives me a nice graph, but the highest value in the frequency domain occurs at k=2. I don't quite understand why we do not get k=1 here. After all, if we take plot(k,y), we see clearly that the function has frequency 1 on the interval $[0, 2 \pi]$. So why does this happen? I tried another exmaple by letting f = @(t) cos(4*t), and then choosing:

y = f(2*k*pi/64)

Again, taking the Fast Fourier Transform, and plotting the result, the highest value in the frequency domain occurs at k=5, and NOT at k=4, which is where I would assume the peak should be. So it seems like MatLab always gives me a k-value which is one above the value I would expect to get.

If anyone can explain this to me, I would be very grateful!

1

There are 1 best solutions below

1
On BEST ANSWER

The first thing to realize is that a function $f$ and its Fourier transform $\hat f$ have different arguments. One can say that the argument of $f$ is time, while the argument of $\hat f$ is frequency. Your code does not make this distinction. Here is the correct approach:

t = 1:64;
y = cos(t*2*pi/64);
fy = fft(y);
k = 0:63;
plot(k,abs(fy))

The peak is at $k=1$, as expected. (There is also its mirror reflection: since the input is real, the transform is conjugate-symmetric. You may want to keep only the first half of the transform, k=0:31).

The reason to have k=0:63 is dictated by the output of fft, which begins with the zeroth frequency.

To emphasize the difference between time interval and frequency interval, here is another time interval (of the same length):

t = 123:186;
y = cos(t*7*2*pi/64);
fy = fft(y);
k = 0:63;
plot(k,abs(fy))

and the output:

FFT