This post is about the FFT function, and anyone wants to know how to specify the frequencies for plot the values.
A few of theory:
If your signal has N values [0, N - 1], then the Fourier Transform has N distinct values.
The function fft(.) returns the signal in the interval [0, N - 1], so you can use the function fftshift(.), over the function fft(.) like this: X = fftshift(fft(x)), that it returns the Fourier Transform in the intervals:
- [-(N - 1)/2, (N - 1)/2], if N is odd.
- [-N/2, N/2 - 1], if N is even.
About the frequencies, if your signal was sampled with a rate T (T samples by second), the indexes are given multiplying the interval by: 2*%pi*T.
Look the example:
N = 100;
n = 1:N;
T = 0.1; // one sample by each 0.1s
w = 0.5; // frequency of the sampled signal (in radians)
x = cos(w*n);
plot(n*T, x); // plot the signal indexed by seconds
f = [-N/2:N/2 - 1];
X = fftshift(fft(x));
plot(f*2*%pi*T, X); // plot the signal indexed by radians
The result is:
Observing that w = 0.5 is the frequency of the sampled signal, the true frequency (of the analog source signal) is given by w_source = w/T = 0.5/0.1 = 5 rad/s. Now, click on the image and look where the peaks are.