I am fairly new to both Matlab and FFT. I would like someone to explain to me the relationship between the frequency of the wave being processed, the number of point (usually called NFFT), the Nyquist Fs and frequency vector. Basically how do you find the peak frequencies? how does FFT represent the frequencies? Any and all help is welcome. Thank you
3 Answers
Although I think Jason's answer contains all the information you requested, maybe what you're looking for is a little less theoretical.
Let's assume you have a signal of $$x(t)=cos(2\pi t f_x)$$ where $t$ is the time in seconds and its frequency $f_x=20$Hz. Let's assume further you sample this signal at $f_s=1000$Hz over $T=3$ seconds, i.e. you have $K=3000$ samples $x[k]$, where $k$ is the integer sample index of the signal $x(t)$.
The sampling theorem basically says that your sampling frequency $f_s$ must be at least twice the maximum signal frequency (which is the Nyquist frequency $f_s/2$) in order to reproduce it without aliasing. For our example: $f_s=1000>2f_x=40$.
You choose the "number of points" $N$ for the FFT (you called it NFFT) yourself, depending on how many different frequency samples you need to distinguish. The maximum frequency resolution you get by using all available samples you have for calculating the FFT, which - depending on the $f_s$ and $T$ of your signal - might not be feasible.
For our example, using all 3000 samples $x[k]$ is not a problem on a PC running Matlab, so let's assume we do that get $N=3000$ complex frequency samples $X[n]$ where $n$ is the integer frequency index.
Now to your question where in $X[n]$ to find a specific frequency $f$ you're interested in if $N=K$: $$n = f T$$
Therefore, the Nyquist frequency $f_s/2$ bin is at $n_s = T f_s/2 = 1500$, right in the middle of the FFT result vector. If you plot the amplitude spectrum $abs(X)/N$, you'll notice that there are peaks at $n_x = T f_x = 60$ and $n_x' = 2940 = N - 60$, each of magnitude $1/2$. This is because the spectrum is "mirrored" around $f_s$ and the signal amplitude of 1 is divided between both peaks $n_x$ and $n_x'$.
For Matlab indices, I think you'll have to add 1 to the $n$ mentioned above because Matlab's vector indices start at 1 instead of 0.
Note that if you have frequencies $f$ which are not a multiple of your sampling frequency $f_s$, the amplitude will spread to neighbouring bins and you'll have to round $n$ to the nearest integer.
See also Matlab's FFT documentation, which has a nice example.
There is no fixed relationship between the frequencies present in the input signal and the length of the DFT that you perform. You can choose the DFT length to be any value that you want, irrespective of what the input signal is. Whether you get the results that you want is another story, but there's no fixed relationship.
Briefly, a DFT cross-correlates the input signal against a set of complex exponential functions at equally spaced frequencies between zero and the Nyquist frequency. The result of that correlation for each exponential function is the corresponding DFT output for that frequency. This interpretation is based upon a direct reading of the equation describing the DFT:
$$ X[k] = \sum_{n=0}^{N-1}x[n] e^{\frac{-j2\pi nk}{N}}, k = 0, \ldots , N-1 $$
$N$ is the DFT length in this case. Therefore, you get a grid of frequencies starting at zero frequency and spaced in normalized angular frequency by $\frac{2\pi}{N}$ radians. So, if you want a finer grid, you would use a larger DFT. This requires either zero-padding the input data (which only interpolates the DFT output, not giving you any extra resolution) or collecting more samples of the input signal before performing the DFT.
To your other question of how to locate the peak frequencies in a signal, one way would be to perform the DFT on some sufficiently-large segment of the signal, then search the result for the largest $M$ values, where $M$ is the number of frequency components that you expect. The DFT length $N$ in this case would need to be set so that the frequency grid spacing on the transform output is small enough to resolve the minimum spacing between frequency components that you expect to see.
-
$\begingroup$ first thank you for your answer. However, to be honest I am confused by the answer you gave me for finding the peak frequency, I would really like to know how to find correct bins in general where the peak frequency will fall, could you please be kind and explain it more? thank you so much for your time. $\endgroup$– user3723Commented Mar 26, 2013 at 16:06
The FFT doesn't represent absolute frequencies, only complex magnitudes (magnitudes with a phase). If you know your time domain signal contained a strongest sinusoid at frequency f0, then it's magnitude would end up mostly in bin round( f0 / (Fs/N) ), which will vary with both the sample rate and length represented by the input. If you don't know f0, you would just search all the FFT result bins from 0 to N/2, and look for the one containing the largest magnitude compared to all the other bins. From there you can estimate f0.