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!
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:
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:63is dictated by the output offft, 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):
and the output: