The Uncertainty Principle
A signal cannot be both short in time and narrow in frequency. The scaling rule hinted at this; the uncertainty principle makes it exact: the product of a signal's spread in time and its spread in frequency is at least , with equality only for Gaussians. It limits every spectrogram, every radar and, in physics, the simultaneous knowledge of position and momentum.
Measuring spread
Treat as a probability density in time and as one in frequency ( is the energy). Their standard deviations are the duration and the bandwidth :
The Heisenberg-Gabor inequality states
The proof is the Cauchy-Schwarz inequality applied to and , using .
import numpy as np
dt = 0.001
t = np.arange(-20, 20, dt)
def spread(density, x, dx):
p = density / (np.sum(density) * dx)
m = np.sum(x * p) * dx
return np.sqrt(np.sum((x - m) ** 2 * p) * dx)
def time_freq_product(f):
F = np.fft.fftshift(np.fft.fft(f)) * dt # samples of the transform
s = np.fft.fftshift(np.fft.fftfreq(t.size, dt))
return spread(np.abs(f) ** 2, t, dt) * spread(np.abs(F) ** 2, s, s[1] - s[0])
print("bound 1/(4 pi) =", round(1 / (4 * np.pi), 5))
for width in (0.5, 1, 3):
g = np.exp(-np.pi * (t / width) ** 2)
print(f"Gaussian, width {width}: product {time_freq_product(g):.5f}")
print(f"two-sided exponential: product {time_freq_product(np.exp(-np.abs(t))):.5f}")
print(f"triangle: product {time_freq_product(np.clip(1 - np.abs(t), 0, None)):.5f}")bound 1/(4 pi) = 0.07958
Gaussian, width 0.5: product 0.07958
Gaussian, width 1: product 0.07958
Gaussian, width 3: product 0.07958
two-sided exponential: product 0.11257
triangle: product 0.08719
Gaussians of every width hit the bound exactly; other shapes are above it. (The FFT here is only a fast way to evaluate the transform at many frequencies; see The FFT in NumPy.)
The practical trade-off
To tell apart two tones Hz apart, you must observe the signal for roughly seconds. A guitar tuner that must distinguish 440 Hz from 441 Hz needs about one second of sound; a spectrogram with 10 ms frames cannot resolve frequencies finer than about 100 Hz.
import numpy as np
fs = 1000
for duration in (0.25, 1.0, 4.0):
t = np.arange(0, duration, 1 / fs)
x = np.sin(2 * np.pi * 100 * t) + np.sin(2 * np.pi * 102 * t)
X = np.abs(np.fft.rfft(x, n=1 << 16)) # zero-padded to evaluate on a fine grid
f = np.fft.rfftfreq(1 << 16, 1 / fs)
band = (f > 95) & (f < 107)
Xb, fb = X[band], f[band]
peaks = [fb[i] for i in range(1, len(Xb) - 1) if Xb[i] > Xb[i - 1] and Xb[i] > Xb[i + 1] and Xb[i] > 0.5 * Xb.max()]
print(f"{duration:4} s of signal: peaks near 100-102 Hz at {np.round(peaks, 2).tolist()}")0.25 s of signal: peaks near 100-102 Hz at [100.98]
1.0 s of signal: peaks near 100-102 Hz at [99.87, 102.13]
4.0 s of signal: peaks near 100-102 Hz at [99.99, 102.01]
With 0.25 s the two tones merge into one peak; with 1 s or more they separate.
In software
- Choosing an STFT window length is choosing a point on this trade-off (The Short-Time Fourier Transform); wavelets choose different trade-offs at different frequencies.
- Gabor's Gaussian-windowed atoms, which meet the bound, are used in time-frequency analysis and in the Gabor filters of early computer vision.
Exercises
Exercise 1: Minimal duration
A pulse must occupy a bandwidth of at most 1 kHz. What is the smallest possible duration ?
Answer
About 80 microseconds.
Solution
s, reached by a Gaussian pulse.
Exercise 2: Observation time
Roughly how long must you record to separate two tones 0.5 Hz apart?
Answer
About 2 seconds or more.
Solution
The frequency resolution is about , so s. Windowing and noise make the real requirement somewhat larger.
Exercise 3: The rectangle's paradox (code)
Compute the time spread of the rectangle . Why is its frequency spread infinite?
Answer
; is infinite because does not decay.
Solution
import numpy as np
t = np.linspace(-0.5, 0.5, 100001)
print(round(float(np.sqrt(np.mean(t ** 2))), 4), round(1 / np.sqrt(12), 4))0.2887 0.2887
oscillates without decaying, so its integral diverges. The inequality holds trivially, but is a poor measure of bandwidth for signals with jumps.
Test yourself
The uncertainty principle bounds…
- the energy
Which signals achieve equality?
- rectangles
- sinc functions
- Gaussians
- exponentials
To resolve two tones 1 Hz apart you need about…
- 1 ms of signal
- 1 s of signal
- 1 sample
- 1 minute