Skip to main content
v2026.11,858 entries · CC-BY 4.0
LAC HealthMedical & Research SuppliesReagents, PPE & instruments — chain-of-custody documented.Fast, traceable sourcing built for regulated research environments, from bench consumables to instrumentation.Shop lac.us CodeCASRAIlac.us

fftfreq in MATLAB: Frequency Vector for fft (No fftfreq Needed)

MATLAB has no fftfreq. Build the frequency axis for fft from Fs and L: single-sided and two-sided recipes, odd lengths, and the NumPy translation.

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)

  1. Transform: Y = fft(X);
  2. Undo the fft scaling and take magnitude: P2 = abs(Y/L);
  3. Keep the first half (L even): P1 = P2(1:L/2+1);
  4. 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.
  5. Frequency axis: f = Fs/L*(0:(L/2));
  6. 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

  1. Y = fft(X);
  2. 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) has L/2+1 elements and matches P2(1:L/2+1).
  • Peaks sit on the wrong side of zero: you applied the centred axis to an unshifted fft output or the reverse; apply fftshift to 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.

Related: spectrum analyzers (swept, FFT and real-time).

Follow CASRAI

Research-administration guidance, standards updates and independent tool reviews.

Ask CASRAI · free to try

Ask about fftfreq in MATLAB: Frequency Vector for fft (No fftfreq Needed)

Ask your first 2 questions free below. Subscribers get 150 a day for $29 a month.

An AI assistant specialized in research administration. It cites the sources behind every answer, labels web answers and says when it can't answer.

Answers draw on CASRAI's guides and dictionary plus the federal and funder documents we index: Federal Register, Grants.gov, Regulations.gov and UKRI.

Works on this site and inside Claude, Cursor and the AI tools you already use.

Everything CASRAI publishes — this page, the dictionary, the guides and the news — stays free to read, with no account and no card.

Referenced across the research world

University of Cambridge logoColumbia University logoCrossref logoUniversity of Edinburgh logoHarvard University logoUniversity of Oxford logoPrinceton University logoStanford School of Medicine logoUniversity College London logoORCID logoUniversity of Cambridge logoColumbia University logoCrossref logoUniversity of Edinburgh logoHarvard University logoUniversity of Oxford logoPrinceton University logoStanford School of Medicine logoUniversity College London logoORCID logo
  • University of Cambridge logo
  • Columbia University logo
  • Crossref logo
  • University of Edinburgh logo
  • Harvard University logo
  • University of Oxford logo
  • Princeton University logo
  • Stanford School of Medicine logo
  • University College London logo
  • ORCID logo

View CASRAI adoption →