import matplotlib
if not hasattr(matplotlib.RcParams, "_get"):
matplotlib.RcParams._get = dict.get
8.1 Issue 1: Finite signals#
The Fourier transform is defined over infinitely long signals \(x(t) : \mathbb{R} \to \mathbb{R}\). But what if we only have a signal of some finite duration \(T\), defined on \([0, T)\)? Or, more generally, what if we want the frequency content of just a segment of a longer signal, defined on \([a, b]\)?
We already saw the key trick in Chapter 7 when we analyzed sampling. There we learned two useful strategies that we will apply here: (1) multiplying a continuous signal by a specially-shaped discontinuous one lets us model discrete phenomena, and (2) the Fourier transform of a discontinuous signal is perfectly well defined.
Windowing#
To leverage a similar trick here, we first define a window function \(w_{a,b}(t)\). In general, a window is any function that is zero outside the interval of interest \([a, b]\) and non-negative within it:
The specific shape of the window inside \([a, b]\) is a design choice, and different shapes trade off different properties (as we will see shortly). The simplest choice is the rectangular window, which is exactly 1 on the interval and 0 outside:
The idea is that a finite signal defined on \([a, b]\) can be viewed as an infinitely long signal multiplied by a corresponding rectangular window. Multiplying zeroes out everything outside \([a, b]\) and leaves the signal untouched inside it. The figure below shows the effect in both domains, using the same running example as Chapter 7, \(x(t) = \sin(2\pi t) + \sin(2\pi 2 t)\):
Fig. 42 Windowing a signal to a finite interval, viewed in both domains. Multiplying \(x(t)\) by the rectangular window \(\text{Rect}_{a,b}(t)\) (top) has a side effect in the frequency domain (bottom): each sharp spectral line of \(|X(\omega)|\) is smeared into a lobe, a phenomenon called spectral leakage.#
Windowing was not free. Comparing the bottom-left and bottom-right panels, the sharp spectral spikes of the original signal have been smeared into lobes. This blurring is called spectral leakage: energy from each true frequency “leaks” into neighboring frequencies. We can still make out the basic shape of the spectrum, with peaks near the true frequencies of 1 and 2 Hz, but it is no longer exact.
Note
Leakage comes from the window’s own spectrum (the middle panel), which is a sinc function rather than a single spike. As we noted in Chapter 7, multiplication in time is convolution in frequency, so the true spectrum gets convolved with (smeared by) the window’s sinc.
Spectral leakage is the price of analyzing a finite slice of time, and it is unavoidable. However, we can potentially mitigate it by using a window whose own spectrum is better behaved than the rectangular window’s sinc. The abrupt jumps at the edges of the rectangular window are what create its strong side lobes; a window that instead tapers smoothly to zero at both ends has a much more compact spectrum. A common choice is the Hann window, a raised cosine that rises from zero at \(a\) to a peak at the center and back to zero at \(b\):
Repeating the same experiment with a Hann window in place of the rectangular one, the leakage is visibly reduced. The window’s spectrum (middle) has far smaller side lobes, so the windowed spectrum (bottom right) concentrates each component’s energy more tightly around its true frequency:
Fig. 43 The same windowing experiment with a Hann window. Compared to the rectangular window, the Hann window’s spectrum (middle) has much smaller side lobes, so its spectral leakage (bottom right) is more contained, at the cost of a slightly wider central lobe.#
We will revisit other implications of windowing, including this central-lobe-versus-side-lobe tradeoff, when we study frame-based processing in Chapter 10. For now, we’ll assume that we’re applying rectangular windows.
The widget below runs the same experiment at any window length. Drag the length, switch between the two shapes, and watch the peaks widen and the side lobes rise and fall.
Drag the window length and switch the shape. On the left, the gold curve is the window and the red curve is what the transform actually sees. On the right is the resulting amplitude spectrum in decibels, with the two true frequencies marked in gray. A shorter window widens every peak, and the Hann window trades a wider peak for much smaller side lobes.
The windowed Fourier transform#
Setting aside leakage, rectangular windowing gives us exactly what we wanted. To keep the algebra compact, let \(x_{a,b}(t) = x(t) \cdot \text{Rect}_{a,b}(t)\) denote the windowed signal. Splitting the Fourier transform at the window edges \(a\) and \(b\) gives three pieces:
The two outer integrals vanish because the window is zero outside \([a, b]\). In the surviving middle integral over \([a, b]\), the window is always one, so \(x_{a,b}(t) = x(t)\). What remains is a single integral over the finite window:
For a signal of duration \(T\) starting at time 0, we take \([a, b] = [0, T]\). This resolves the first issue: our work-in-progress transform \(\hat{X}(\omega)\) now integrates over a finite interval.
Note that, in practice, we do not actually multiply an infinitely-long signal by a rectangular window. Instead, we just evaluate the integral over whatever finite duration of audio we have actually observed. However, by evalauting over this finite interval, we are implicitly performing the rectangular windowing operation, so the mathematical consequences are real regardless.