Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

SciPy · Function

fft.fftfreq

Returns the discrete Fourier transform’s sample-frequency bins for a window of length n. Frequencies are scaled by the sample spacing, start at zero, and include negative-frequency bins.

How it works

Given a window length and sample spacing, fftfreq returns the frequency-bin centers associated with a discrete Fourier transform. Frequencies are expressed in cycles per unit of spacing, with zero first; negative-frequency bins follow the positive bins according to whether the length is even or odd.

Syntax

scipy.fft.fftfreq(n, d=1.0, *, xp=None, device=None)

Parameters

NameTypeDescription
nintWindow length.
dscalar, optionalSample spacing (inverse of the sampling rate). Defaults to 1.
xparray_namespace, optionalThe namespace for the return array. Default is None, where NumPy is used.
devicedevice, optionalThe device for the return array. Only valid when xp.fft.fftfreq implements the device parameter.

Returns

f ndarray: Array of length n containing the sample frequencies.

Key points

  • Use fftfreq to construct the frequency coordinates corresponding to DFT output bins.
  • The result is a floating-point array whose entries represent bin centers rather than FFT coefficients.
  • Frequency units follow the reciprocal of the unit used for d; seconds produce cycles per second.
  • The first returned frequency is always the zero-frequency bin.
  • d describes the interval between samples and therefore sets the frequency scale.
  • xp lets callers choose the array namespace used to create the returned frequency array.

Common mistakes

  • The device argument cannot be used unless the selected array namespace supports a device parameter for fftfreq.

Example: Get FFT sample frequencies

import numpy as np
from scipy.fft import fft, fftfreq

signal = np.array([-2, 8, 6, 4, 1, 0, 3, 5], dtype=float)
frequencies = fftfreq(signal.size, d=0.1)
spectrum = fft(signal)

print(frequencies)
print(spectrum)

Build the frequency-bin centers for an FFT sampled every 0.1 time units, then pair them with the transform output.

When to use it

Label an FFT spectrum

Convert FFT array positions into physical frequency values for plotting or peak detection.

Set frequency units

Translate a known sampling interval into cycles per unit of time or distance.

Work with alternate array backends

Create frequency coordinates using a compatible array namespace.

Interview questions

QWhat does scipy.fft.fftfreq return?

fftfreq returns an array of length n containing the FFT sample frequencies. The values are measured in cycles per unit of the sample spacing, and the first value is zero.

Follow-up: How does d affect the values? It scales the frequency grid by the inverse of the sample spacing, so increasing d decreases every frequency magnitude.

QHow are the frequency bins arranged for even and odd lengths?

For even n, the bins are formed from 0 through n/2−1, followed by −n/2 through −1, and all are divided by d·n. For odd n, the positive portion ends at (n−1)/2 and the negative portion begins at −(n−1)/2.

Follow-up: Which bin is the most negative for even n? It is −n/2 divided by d·n, giving the Nyquist-frequency bin at −1/(2d).

QWhat are the important parameters?

The n argument is the window length, while d is the sample spacing and defaults to 1. The optional xp argument selects the array namespace for the returned array; when omitted, NumPy is used.

Follow-up: What can device control? It specifies the return-array device, but only when the selected namespace's fftfreq implementation supports that parameter.

  • scipy.fft.fft
  • scipy.fft.rfft
  • scipy.fft.rfftfreq
  • scipy.fft.fftshift

Source: fftfreq. Checked 2026-10-15.