If a fft of vector x is plotted then what would be the x-axis?
91 views (last 30 days)
Show older comments
Prajan Pradhan
on 24 Sep 2014
Commented: Darcy Cordell
on 13 Jun 2023
x=rand(1:1000)>0.5;
X=fft(x);
X_mag=abs(X);
plot(x_mag)
There would appear 1000 values in x-axis , is it frequency or what?
0 Comments
Accepted Answer
Geoff Hayes
on 24 Sep 2014
Edited: Geoff Hayes
on 24 Sep 2014
The x-axis is frequency. You can calculate it as
freqHz = (0:1:length(X_mag)-1)*Fs/N;
where Fs is your sampling rate (Hz) and N is your FFT block size (in your case N=length(x)).
You can then plot the magnitude vs frequency as
plot(freqHz,X_mag);
5 Comments
Allen Goldstein
on 16 Oct 2019
Edited: Allen Goldstein
on 16 Oct 2019
This "accepted answer" is not correct. The fft creates positive and negative frequencies and is invertable to the original signal. The frequency at either end of the fft vector is 0 and the center is length (X_mag)*Fs/N. The matlab help on fft gives a good example of how to display the spectrum by creating the "analytic" FFT (though the help does not call it analytic). The thing about the analytic FFT is that the inverse FFT creates the analytic signal which is complex with the imaginary part being phase shifted by 90 degrees (the imaginary part of the iFFT is the Hilbert transformation of the original signal). The original signal is the real part of the analytic signal.
I will try to provide a better answer...
Florian Behner
on 27 Jan 2021
Edited: Florian Behner
on 27 Jan 2021
Geoff Hayes answer is indeed correct, although usually not what you expect. The result of the FFT is periodic in frequency, as it is also assumed for the time domain signal. Thus the frequency result is ambiguous with the sampling frequency. Any frequency bin may be shifted by integer multiples of the sample rate.
We are usually using the follwing code to determine the frequency of the bins:
frequency=[0:ceil(nbrSamples/2)-1,-floor(nbrSamples/2):-1]'/sampleInterval/nbrSamples;
%To plot the frequency vector and the FFT output in order use
plot(fftshift(frequency),fftshift(fftresult))
More Answers (1)
Allen Goldstein
on 16 Oct 2019
To plot the entire original fft use:
N = length(X_mag)
f = horzcat(-linspace(0,N/2,N/2)*Fs/N,linspace(N/2,0,N/2)*Fs/N);
To create the frequency spectrum and stem (f,X_Mag), This does a little weirdness behind the scenes because it splits the fft vector in half then plots the two ends of the vector in the middle. If you just plot(X_Mag) you will see what I mean..
One more thing, it is better to use stem() to plot ffts because the bins of the fft are independant (orthogonal) and plot interpolates a connection between the elements which can be misleading.
1 Comment
Darcy Cordell
on 13 Jun 2023
Thanks for the answer (and I agree that the other answer is incomplete). Just to clarify here though: does this mean that the MATLAB fft produces two identical f=0 frequency bins? In your frequency axis, you have a duplicate f=0 at f(1) and f(end).
Also, does this work for both even and odd-length functions?
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!