Label an FFT spectrum
Convert FFT array positions into physical frequency values for plotting or peak detection.
SciPy · Function
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.
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.
scipy.fft.fftfreq(n, d=1.0, *, xp=None, device=None)| Name | Type | Description |
|---|---|---|
n | int | Window length. |
d | scalar, optional | Sample spacing (inverse of the sampling rate). Defaults to 1. |
xp | array_namespace, optional | The namespace for the return array. Default is None, where NumPy is used. |
device | device, optional | The device for the return array. Only valid when xp.fft.fftfreq implements the device parameter. |
f ndarray: Array of length n containing the 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.
Convert FFT array positions into physical frequency values for plotting or peak detection.
Translate a known sampling interval into cycles per unit of time or distance.
Create frequency coordinates using a compatible array namespace.
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.
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).
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.fftscipy.fft.rfftscipy.fft.rfftfreqscipy.fft.fftshift