Find dominant frequency components
Inspecting measured sensor or audio data
SciPy · Function
Estimate a signal’s power spectral density with Welch’s method by averaging modified periodograms from overlapping segments. The function can also return a squared-magnitude spectrum.
welch divides a time series into overlapping segments, computes a modified periodogram for each segment, and averages those periodograms. It returns frequency bins together with either a power spectral density or a squared-magnitude spectrum, depending on scaling.
scipy.signal.welch(x, fs=1.0, window='hann', nperseg=None, noverlap=None, nfft=None, detrend='constant', return_onesided=True, scaling='density', axis=-1, average='mean')| Name | Type | Description |
|---|---|---|
x | array_like | Time series of measurement values. |
fs | float, optional | Sampling frequency of the x time series; default 1.0. |
window | str, tuple, or array_like, optional | Window applied to each segment; a string or tuple is passed to get_window, or an array can be supplied. |
nperseg | int, optional | Length of each segment; defaults to 256 for a named or tuple window, or the window length for an array window. |
noverlap | int, optional | Number of points to overlap between segments; default is nperseg // 2. |
nfft | int, optional | Length of the FFT used; default is nperseg. |
detrend | str or function or False, optional | Detrending applied to each segment, or a callable; default 'constant'. |
return_onesided | bool, optional | If true, return a one-sided spectrum for real data; default True. |
scaling | {‘density’, ‘spectrum’}, optional | Either 'density' or 'spectrum'; default 'density'. |
axis | int, optional | Axis along which the estimate is calculated; default -1. |
average | {‘mean’, ‘median’}, optional | Averaging method, 'mean' or 'median'; default 'mean'. |
Returns f, the sample frequencies, and Pxx, the power spectral density or power spectrum of x.
import numpy as np
from scipy import signal
fs = 1000.0
t = np.arange(0, 2, 1 / fs)
x = np.sin(2 * np.pi * 50 * t)
f, pxx = signal.welch(x, fs=fs, nperseg=1024)
print(f[np.argmax(pxx)])Computes a one-sided PSD and prints the frequency with the largest estimated power.
Inspecting measured sensor or audio data
Comparing signal power across frequency bands
Reducing the influence of isolated spectral outliers
welch estimates power spectral density by splitting the input into overlapping segments, computing a modified periodogram for each, and averaging them. This generally reduces variance compared with using one periodogram.
Follow-up: How would you trade frequency resolution against variance? Increase nperseg for finer resolution, while recognizing that fewer segments can increase estimator variance.
With scaling='density', the result is a power spectral density with units of signal-squared per hertz. With scaling='spectrum', it is a squared-magnitude spectrum with units of signal-squared.
Follow-up: Which output is suitable for integrating power over frequency? Use the density form, whose frequency integral represents signal power.
The function returns a frequency array f and a spectral estimate Pxx. For real input, return_onesided=True produces a one-sided estimate; complex input always produces a two-sided spectrum.
Follow-up: What determines the number of frequency bins? The FFT length nfft determines the bin grid, while the selected one- or two-sided output determines how many bins are returned.
periodogramspectrogramcsdcoherenceget_window