Written and maintained by CASRAI Editorial Board
Last updated
MATLAB has no function called fftfreq. That name belongs to NumPy (numpy.fft.fftfreq). In MATLAB you build the frequency vector yourself from the sampling rate and the signal length, using the idiom f = Fs/L*(0:(L/2)) for a single-sided spectrum or Fs/L*(-L/2:L/2-1) with fftshift for a two-sided one. This guide is for analysts who have a vector X of L samples taken at Fs hertz and need the matching frequency axis, and for people translating a NumPy script.
What it is used for in research
fft returns complex values indexed by bin number, not by hertz. Anything that reads a spectrum, such as locating a vibration peak, a mains-hum component or an oscillation in a recorded trace, needs the frequency that belongs to each bin. NumPy ships that helper as fftfreq; MATLAB’s documentation shows the same result computed from Fs and L. For the NumPy side see the NumPy Fourier transform guide; for wider filtering and spectra work see SciPy signal processing and the plain-language overview What is signal processing?
Install or access (official source only)
fft and fftshift are documented MATLAB functions (MathWorks documentation: fft and fftshift), so no add-on is needed for the recipes below. MATLAB itself is obtained from MathWorks; licensing terms are on their site. Your institution may already provide access.
Step-by-step: build the frequency vector
Assume X is a vector of samples, Fs is the sampling frequency in Hz and L = length(X). The spacing between bins is Fs/L.
Single-sided spectrum (the usual case for real signals)
- Transform:
Y = fft(X); - Undo the fft scaling and take magnitude:
P2 = abs(Y/L); - Keep the first half (L even):
P1 = P2(1:L/2+1); - Double the interior bins:
P1(2:end-1) = 2*P1(2:end-1);The zero-frequency bin and the Nyquist bin are not doubled because they have no negative-frequency partner. - Frequency axis:
f = Fs/L*(0:(L/2)); - Plot:
plot(f,P1)
These steps are the MathWorks fft documentation’s single-sided example (read 2026-11-07). The colon operator builds the 0:(L/2) index vector.
Two-sided, centred spectrum
Y = fft(X);- Plot against a centred axis:
plot(Fs/L*(-L/2:L/2-1),abs(fftshift(Y)))
For even L the axis runs from -Fs/2 to Fs/2-Fs/L in steps of Fs/L. An equivalent form in the documentation is z = fftshift(y); ly = length(y); f = (-ly/2:ly/2-1)/ly*Fs;
Even and odd lengths
fftshift swaps the left and right halves of a vector. For an even length, [1 2 3 4 5 6] becomes [4 5 6 1 2 3]. For an odd length the middle element belongs to the left half, so [1 2 3 4 5 6 7] becomes [5 6 7 1 2 3 4]. Following from that rule (an editorial derivation, not a documented formula), an odd L has the zero-frequency bin in the middle of the shifted vector, and the centred axis is Fs/L*(-(L-1)/2:(L-1)/2). The even-length formula -L/2:L/2-1 is not valid for odd L.
Output and how to read it
f and P1 have the same length, so plot(f,P1) shows amplitude against hertz. The highest frequency on the single-sided axis is Fs/2 (the Nyquist frequency); the finest frequency step is Fs/L, so a longer record gives a finer axis at the same Fs. Dividing by L in step 2 removes the scaling that fft applies, so peak heights reflect signal amplitude rather than growing with record length.
Troubleshooting
- “Undefined function fftfreq”: expected; MATLAB does not define it. Use the idioms above.
- Peak amplitude looks too large: you did not divide by
L. - Single-sided amplitudes are half what you expect: the interior bins were not doubled.
- Length mismatch error in
plot: the axis and spectrum must have the same number of elements.0:(L/2)hasL/2+1elements and matchesP2(1:L/2+1). - Peaks sit on the wrong side of zero: you applied the centred axis to an unshifted
fftoutput or the reverse; applyfftshiftto the spectrum whenever you use the centred axis.
Free alternatives (and the NumPy translation)
NumPy is free, and its numpy.fft.fftfreq(n, d=1.0, device=None) returns the frequency of every bin, where d is the sample spacing, the inverse of the sampling rate (NumPy manual, read 2026-11-07). To reproduce it in MATLAB, set d = 1/Fs and n = L.
| Item | NumPy fftfreq(n, d) |
MATLAB idiom |
|---|---|---|
| Function exists | Yes | No; compute from Fs and L |
| Bin spacing | 1/(d*n) |
Fs/L (the same value when d = 1/Fs) |
| Even-length order | [0, 1, ..., n/2-1, -n/2, ..., -1]/(d*n) |
Same order as unshifted fft output: Fs/L*[0:L/2-1, -L/2:-1] (derived from the NumPy order) |
| Odd-length order | [0, 1, ..., (n-1)/2, -(n-1)/2, ..., -1]/(d*n) |
Same order as unshifted fft output |
| Centred axis | Shift the result with numpy.fft.fftshift |
Fs/L*(-L/2:L/2-1) (even L) with fftshift(Y) |
The NumPy order is the order in which unshifted fft output is stored, which is why fftshift turns it into a centred axis.
FAQ
Is there an fftfreq function in MATLAB?
No. fftfreq is a NumPy function. In MATLAB use f = Fs/L*(0:(L/2)) for the single-sided axis or Fs/L*(-L/2:L/2-1) for a centred two-sided axis (even L).
How do I get the frequencies of an fft in MATLAB?
Multiply the bin index by the bin spacing Fs/L, where Fs is the sampling frequency and L the number of samples.
Why are the interior bins doubled?
For a real signal the spectrum is symmetric, so the single-sided spectrum folds the negative-frequency half onto the positive half. The zero and Nyquist bins have no partner and are left alone.
Sources (official documentation)
- MathWorks, fft, MATLAB documentation, read 2026-11-07.
- MathWorks, fftshift, MATLAB documentation, read 2026-11-07.
- NumPy, numpy.fft.fftfreq, NumPy manual, read 2026-11-07.







