FFT in MATLAB: Frequency Axis, fftshift and Spectral Leakage
Use the FFT in MATLAB correctly: build the frequency axis, scale the amplitude, apply fftshift, and understand spectral leakage, windows and zero padding.
What fft returns
The function fft computes the discrete Fourier transform of a vector of samples. For N samples it returns N complex numbers, one for each frequency bin. The result has no frequency labels. You must build the frequency axis yourself from the sample rate.
The three numbers to keep in mind are the sample rate fs, the number of samples N, and the record length T = N / fs. The spacing between bins is fs / N, which is also 1 / T. A longer record gives finer bins.
A first example
This script finds the spectrum of a 50 Hz sine with amplitude 1, sampled at 1 kHz for one second.
fs = 1000; N = 1000; t = (0:N-1)/fs; x = sin(2*pi*50*t); X = fft(x); f = (0:N-1)*fs/N; A = abs(X)*2/N; plot(f(1:N/2), A(1:N/2))
The plot shows a single line at 50 Hz with height 1. Without the factor 2/N the height would be N/2 = 500. The factor 2 appears because half of the energy of a real signal sits at the mirrored negative frequency, which the plot leaves out.
The frequency axis and fftshift
The output of fft is ordered from 0 Hz upward to just below fs, and the upper half really means the negative frequencies. To see a two sided spectrum with zero in the middle, use fftshift on both the data and the axis.
f2 = (-N/2 : N/2-1) * fs/N; X2 = fftshift(fft(x))/N; plot(f2, abs(X2))
- The highest frequency you can see is fs / 2, called the Nyquist frequency.
- A signal above fs / 2 folds back and appears at a wrong, lower frequency. This is aliasing. Choose fs at least twice the highest frequency, and more in practice.
- The unit of the axis is hertz only if the time vector is in seconds.
Spectral leakage
The FFT assumes the record repeats forever. If the record holds a whole number of cycles, the end joins the start smoothly and the spectrum is a clean line. If it does not, the repeated record has a jump at the join and the energy spreads over many bins. This is spectral leakage.
Try a sine at 50.5 Hz with the same one second record. The record holds 50.5 cycles, so the line at 50 Hz drops and skirts of smaller lines appear on both sides. Nothing is wrong with the code. The record simply ends in the middle of a cycle.
Windows and zero padding
A window fades the record to zero at both ends before the transform, which removes the jump. The price is a slightly wider main line. The Hann window is a common choice.
w = hann(N)'; Xw = fft(x .* w); A = abs(Xw) * 2 / sum(w);
- The scale factor sum(w) replaces N so that a sine still reads its true amplitude.
- Zero padding means adding zeros to the end with fft(x, 4*N). It draws a smoother curve between the bins but adds no new information.
- Resolution depends on the record length. To separate two tones that are 2 Hz apart, record at least about 0.5 seconds, and more with a window.
Checklist
Before you trust a spectrum plot, check these points: the sample rate is high enough, the axis is built from fs and N, the amplitude is scaled, the record is long enough for your resolution, and a window is used if the signal does not fit a whole number of cycles.
Finding the size and place of a peak
Take a signal with two tones, 100 Hz at amplitude 2 and 250 Hz at amplitude 0.5, sampled at 2 kHz for one second. Find the strongest peak and show the spectrum in decibels like an analyzer.
fs = 2000; N = 2000; t = (0:N-1)/fs; x = 2*sin(2*pi*100*t) + 0.5*sin(2*pi*250*t); A = abs(fft(x))*2/N; f = (0:N-1)*fs/N; [pk, k] = max(A(1:N/2)); % pk = 2, f(k) = 100 Hz plot(f(1:N/2), 20*log10(A(1:N/2) + eps))
The peaks read 2 and 0.5, which is 6.0 dB and −6.0 dB. The small number eps stops the logarithm of zero. Because both tones fit a whole number of cycles in one second, the lines are clean and the floor is at about −300 dB, which is only rounding error.
Try it in the virtual lab
SinBench has free 3D benches and interactive demos for this topic. Open one to try the ideas above with real instruments, no sign up needed.
MATLAB and Simulink Basics for Signals
Questions and answers
How do I make the frequency axis for an FFT in MATLAB?
Use f = (0:N-1)*fs/N for the one sided order, or f = (-N/2:N/2-1)*fs/N together with fftshift for a two sided plot, where fs is the sample rate and N the number of samples.
What does fftshift do?
It swaps the two halves of the FFT output so that zero frequency is in the middle, which gives the usual two sided spectrum with negative frequencies on the left.
What is spectral leakage?
It is the spreading of a spectral line into neighbouring bins. It happens when the record does not hold a whole number of cycles. A window reduces it.
Does zero padding improve resolution?
No. It interpolates between existing bins and gives a smoother curve. Only a longer record gives finer frequency resolution.
Related topics
Fourier Transform Basics: From Time to Frequency
Learn what the Fourier transform does, how time and frequency views of a signal connect and how to read a spectrum, with worked examples and a free lab.
Communication SystemsButterworth Filters and Group Delay
Understand the Butterworth low pass filter: flat response, cutoff and order, how to pick the order, and why group delay matters for pulses, with MATLAB code.
Communication SystemsHow to Use a Spectrum Analyzer: Span, RBW and Reference Level
A practical guide to spectrum analyzer settings: center frequency, span, resolution bandwidth, reference level, dBm and sweep time, with worked examples.