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

SciPy · Function

signal.welch

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.

How it works

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.

Syntax

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')

Parameters

NameTypeDescription
xarray_likeTime series of measurement values.
fsfloat, optionalSampling frequency of the x time series; default 1.0.
windowstr, tuple, or array_like, optionalWindow applied to each segment; a string or tuple is passed to get_window, or an array can be supplied.
npersegint, optionalLength of each segment; defaults to 256 for a named or tuple window, or the window length for an array window.
noverlapint, optionalNumber of points to overlap between segments; default is nperseg // 2.
nfftint, optionalLength of the FFT used; default is nperseg.
detrendstr or function or False, optionalDetrending applied to each segment, or a callable; default 'constant'.
return_onesidedbool, optionalIf true, return a one-sided spectrum for real data; default True.
scaling{‘density’, ‘spectrum’}, optionalEither 'density' or 'spectrum'; default 'density'.
axisint, optionalAxis along which the estimate is calculated; default -1.
average{‘mean’, ‘median’}, optionalAveraging method, 'mean' or 'median'; default 'mean'.

Returns

Returns f, the sample frequencies, and Pxx, the power spectral density or power spectrum of x.

Key points

  • Use welch to estimate the power spectral density of measured time-series data.
  • Its estimate combines modified periodograms from overlapping segments, reducing reliance on a single segment.
  • fs specifies the sampling rate and therefore sets the frequency units of the returned grid.
  • window selects the taper applied to each segment; a Hann window is used by default.
  • nperseg controls segment length, balancing frequency resolution against the number of averaged segments.

Common mistakes

  • return_onesided does not produce a one-sided result for complex-valued input.

Example: Estimate a signal's dominant frequency

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.

When to use it

Find dominant frequency components

Inspecting measured sensor or audio data

Compute a power spectral density

Comparing signal power across frequency bands

Use median segment averaging

Reducing the influence of isolated spectral outliers

Interview questions

QHow does Welch’s method work?

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.

QWhat is the difference between density and spectrum scaling?

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.

QWhat does scipy.signal.welch return?

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.

  • periodogram
  • spectrogram
  • csd
  • coherence
  • get_window

Source: Welch. Checked 2026-10-15.