Power Spectra and Noise
For random signals (noise, vibration, speech), each FFT gives a different, very noisy spectrum. What is stable is the power spectral density (PSD): the average power per unit frequency. This lesson shows why a single periodogram is a poor estimate, how Welch's method averages it into a good one, and what white and coloured noise look like.
The periodogram and its problem
The periodogram of samples is (power per Hz). For white noise with variance the true PSD is flat at (two-sided), but each periodogram value fluctuates by about 100 % of its mean, however long the recording: more data gives more bins, not less noise per bin.
import numpy as np
rng = np.random.default_rng(0)
fs = 1000.0
for N in (256, 4096, 65536):
x = rng.normal(size=N) # white noise, variance 1
P = np.abs(np.fft.rfft(x)) ** 2 / (N * fs)
P = P[1:-1] # drop DC and Nyquist bins
print(f"N={N:6d}: mean PSD {P.mean():.2e} (true {1 / fs:.0e}) std/mean {P.std() / P.mean():.2f}")N= 256: mean PSD 1.02e-03 (true 1e-03) std/mean 1.06
N= 4096: mean PSD 9.97e-04 (true 1e-03) std/mean 0.98
N= 65536: mean PSD 9.99e-04 (true 1e-03) std/mean 0.99
The mean is right, but the relative spread stays near 1: the periodogram is an inconsistent estimator.
Welch's method
Split the signal into overlapping segments, window each, compute periodograms, and average. Averaging independent estimates reduces the spread by , at the cost of resolution (shorter segments):
import numpy as np
rng = np.random.default_rng(1)
fs, N = 1000.0, 65536
x = rng.normal(size=N)
def welch(x, fs, nper):
w = 0.5 - 0.5 * np.cos(2 * np.pi * np.arange(nper) / nper) # Hann
step = nper // 2 # 50 % overlap
segs = [x[i:i + nper] * w for i in range(0, len(x) - nper + 1, step)]
P = np.mean([np.abs(np.fft.rfft(s)) ** 2 for s in segs], axis=0) / (fs * np.sum(w ** 2))
return P[1:-1], len(segs)
for nper in (4096, 1024, 256):
P, K = welch(x, fs, nper)
print(f"segments of {nper:4d}: {K:3d} averages, resolution {fs / nper:5.2f} Hz, std/mean {P.std() / P.mean():.3f}")segments of 4096: 31 averages, resolution 0.24 Hz, std/mean 0.186
segments of 1024: 127 averages, resolution 0.98 Hz, std/mean 0.095
segments of 256: 511 averages, resolution 3.91 Hz, std/mean 0.052
Coloured noise
Noise is named by the slope of its PSD: white (flat), pink (, equal power per octave, common in nature and electronics), brown (, a random walk). Their PSD slopes in decibels per decade are 0, and :
import numpy as np
rng = np.random.default_rng(2)
N, fs = 2 ** 16, 1000.0
white = rng.normal(size=N)
brown = np.cumsum(white); brown -= brown.mean()
W = np.fft.rfft(rng.normal(size=N))
f = np.fft.rfftfreq(N, 1 / fs)
W[1:] /= np.sqrt(f[1:]); W[0] = 0
pink = np.fft.irfft(W, N) # shape white noise by 1/sqrt(f) in amplitude
def slope_db_per_decade(x):
P = np.abs(np.fft.rfft(x)) ** 2
band = (f > 5) & (f < 200)
return np.polyfit(np.log10(f[band]), 10 * np.log10(P[band]), 1)[0]
for name, x in (("white", white), ("pink", pink), ("brown", brown)):
print(f"{name:6s} PSD slope {slope_db_per_decade(x):6.1f} dB/decade")white PSD slope -0.1 dB/decade
pink PSD slope -10.0 dB/decade
brown PSD slope -19.6 dB/decade
In software
scipy.signal.welch(x, fs, nperseg=...)is the standard PSD estimator;periodogramgives the raw version. Units: forscaling="density", for"spectrum".- Noise specifications of sensors and amplifiers are given as PSDs (for example nV per root hertz); vibration testing uses PSD profiles.
Exercises
Exercise 1: White noise level
White noise with variance 4 is sampled at 2000 Hz. What is its one-sided PSD level?
Answer
0.004 per Hz.
Solution
The total power 4 is spread evenly over to Hz (one-sided), giving per Hz. The two-sided density is half that over to Hz.
Exercise 2: Trade-off
Welch with segments of 1024 samples at Hz: what is the frequency resolution? How many averages from 10 s of data with 50 % overlap?
Answer
8 Hz; 159 segments.
Solution
Resolution Hz. 10 s is 81920 samples; with step 512 the segment count is .
Exercise 3: Total power from the PSD (code)
Integrate the Welch PSD of seeded white noise with variance 1 over to (one-sided, so doubled) and check it recovers the variance.
Answer
About 1.0.
Solution
import numpy as np
rng = np.random.default_rng(3)
fs, nper = 1000.0, 1024
x = rng.normal(size=100000)
w = 0.5 - 0.5 * np.cos(2 * np.pi * np.arange(nper) / nper)
segs = [x[i:i + nper] * w for i in range(0, len(x) - nper + 1, nper // 2)]
P = np.mean([np.abs(np.fft.rfft(s)) ** 2 for s in segs], axis=0) / (fs * np.sum(w ** 2))
P[1:-1] *= 2 # one-sided
print(round(float(np.sum(P) * fs / nper), 3))0.998
By Parseval, the area under the PSD is the signal's power.
Test yourself
Why is a single periodogram a poor PSD estimate?
- it is biased low
- its values fluctuate by about 100 % no matter how long the recording
- it needs a window
- it only works for sines
Welch's method reduces the variance by…
- zero padding
- averaging periodograms of overlapping windowed segments
- using a larger FFT
- filtering the signal
Pink noise has a PSD proportional to…
- 1