Outline
9.1 Why Time–Frequency Analysis?
9.2 The Short-Time Fourier Transform
9.3 Windowing: Shape and Length
9.4 The Spectrogram
9.5 STFT as a Filterbank
9.6 STFT Analysis–Synthesis
9.7 Summary
Exercises
DSP Study Guide · Chapter 9

Short-Time Fourier Transform

Time–frequency analysis of nonstationary signals: windowing, the spectrogram, the resolution trade-off, and the filterbank view of the STFT

9.1 Why Time–Frequency Analysis?

Every transform we have used so far — DTFT, $z$-transform, DFT — analyzes a signal as a whole. The basis functions $e^{j\omega n}$ extend over all time, so a single Fourier coefficient mixes together contributions from the entire signal. That is perfectly adequate for stationary signals(平穩訊號)whose spectral content does not change, but most signals we actually care about — speech, music, radar returns, biomedical signals — are nonstationary(非平穩訊號): their frequency content evolves with time. A melody is precisely a sequence of different frequencies at different times.

9.1.1 The Fourier transform hides timing

The lecture opens with the Fourier transform of an amplitude-modulated sine: the FT shows which frequencies are present, but tells us almost nothing about when they occur. Here is the cleanest demonstration. Take two signals built from the same two tones, played in opposite order:

Two orderings of the same tones $$x_1(t)=\begin{cases}\sin(2\pi\cdot 50\,t), & 0\le t<0.5\\ \sin(2\pi\cdot 120\,t), & 0.5\le t<1\end{cases} \qquad x_2(t)=\begin{cases}\sin(2\pi\cdot 120\,t), & 0\le t<0.5\\ \sin(2\pi\cdot 50\,t), & 0.5\le t<1\end{cases}$$
2026-06-12T22:35:52.751144 image/svg+xml Matplotlib v3.10.8, https://matplotlib.org/ 0.0 0.2 0.4 0.6 0.8 1.0 t (s) −1.0 −0.5 0.0 0.5 1.0 x(t) 50 Hz then 120 Hz (switch at t = 0.5 s) 0 25 50 75 100 125 150 175 200 f (Hz) 0.0 0.1 0.2 |X(f)| Magnitude spectra of both orderings — nearly identical 50 Hz then 120 Hz 120 Hz then 50 Hz
Fig. 9-1 — Top: a signal that switches from 50 Hz to 120 Hz at $t=0.5$ s. Bottom: the magnitude spectra of both orderings are nearly identical — the FT magnitude cannot tell us when each tone occurred.(傅立葉轉換的振幅頻譜只回答「有哪些頻率」,完全看不出「何時出現」;時間資訊全部藏在相位裡。)
Concept — where did the timing go?
Both signals contain exactly the same frequencies for the same total durations, so their magnitude spectra $|X(f)|$ coincide. The timing information is not destroyed — it is encoded in the phase $\angle X(f)$, in a form that is essentially impossible to read by eye. Global Fourier analysis is a perfectly invertible description, but a very inconvenient one for nonstationary signals.

9.1.2 FT of short segments

The fix suggested by the slides is intuitive: if the spectrum changes with time, chop the signal into short segments and Fourier-transform each segment separately. Within a segment of a few tens of milliseconds, speech or music is approximately stationary(短時間內近似平穩), so a per-segment spectrum is meaningful. Doing this for every segment position, and stacking the resulting spectra side by side as columns of an image, is exactly the idea of the spectrogram (頻譜圖/聲譜圖), and the underlying transform is the Short-Time Fourier Transform (STFT, 短時傅立葉轉換).

直觀解釋(點擊展開)
傅立葉轉換像是把一整首歌「壓縮」成一張總表:哪些音高出現過、總共出現多久,但完全不記錄順序。 STFT 的做法是拿一個「短窗」沿著時間軸滑動,每停一個位置就對窗內的訊號做一次傅立葉轉換, 等於把一首歌切成一小節一小節分別記譜。把每一小節的頻譜直立起來、按時間排好,就得到頻譜圖 — 橫軸是時間、縱軸是頻率、顏色深淺是能量大小,跟樂譜的概念完全一樣。

9.2 The Short-Time Fourier Transform

9.2.1 Definition

Let $w[n]$ be a window function(窗函數)of length $L$ — real, nonnegative, concentrated around its center, and zero outside $0\le n\le L-1$. The STFT of $x[n]$ is the DTFT of the windowed signal, with the window slid to time position $n$:

Short-Time Fourier Transform $$X(n,\omega)\;=\;\sum_{m=-\infty}^{\infty} x[m]\,w[m-n]\,e^{-j\omega m}$$

For each fixed window position $n$, $X(n,\omega)$ is an ordinary DTFT of the segment $x[m]\,w[m-n]$ — a function of continuous frequency $\omega$, periodic in $2\pi$. As $n$ varies, the window slides along the signal, so the STFT is a two-dimensional function: one time axis, one frequency axis.

Key — one transform, two readings

The STFT can be grouped two ways, and both matter:

  • Fourier-transform view: fix $n$ — then $X(n,\omega)$ is the DTFT of one windowed frame. This is how we compute it (Section 9.2.2).
  • Filterbank view: fix $\omega$ — then $X(n,\omega)$ as a function of $n$ is the output of a bandpass filter centered at $\omega$ (Section 9.5). This is how we interpret each horizontal line of a spectrogram.

9.2.2 Discrete STFT (frames + DFT)

In practice we evaluate the STFT only on a grid: time is sampled every $M$ samples (the hop size, 跳距), and frequency is sampled at the $N$ DFT bins. With frame index $m$ and bin index $k$:

Discrete STFT (what software computes) $$X_m[k]\;=\;\sum_{n=0}^{N-1} x[n+mM]\;w[n]\;e^{-j2\pi kn/N}, \qquad k=0,1,\dots,N-1$$

In words: take the $N$ samples starting at $mM$, multiply pointwise by the window, and take an $N$-point FFT. Each $m$ produces one frame(音框)— one column of the time–frequency image. For a signal of length $L_x$, the number of complete frames is

Number of frames $$N_{\text{frames}}=\left\lfloor \frac{L_x-N}{M}\right\rfloor+1$$

which is precisely the loop bound used in the homework's myspectrogram function. The frequency spacing between adjacent bins is $f_s/N$ — but as we will see in Section 9.3.2, the true resolution is set by the window length, not by $N$.

9.2.3 Two limiting slices

Two elementary inputs reveal exactly what the STFT can and cannot resolve:

So the window $w[n]$ acts as the "point spread function" of the analysis in both axes simultaneously. Choosing $w$ is the entire art of the STFT — which is why the lecture spends four slides on it.

9.3 Windowing: Shape and Length

9.3.1 Window shape

Truncating a signal with a rectangular window(矩形窗)is multiplication in time, hence convolution with the window spectrum in frequency. The rectangular window's spectrum is the periodic sinc (Dirichlet kernel): a narrow mainlobe, but sidelobes(旁瓣) only $-13$ dB down. Those sidelobes cause leakage(頻譜洩漏): a strong tone splatters energy across the whole band and buries weak neighbors. Tapered windows trade a wider mainlobe for far lower sidelobes.

Hann window (as used in dsp10.m) $$w[n]\;=\;\tfrac12\!\left(1-\cos\frac{2\pi n}{L}\right),\qquad n=0,1,\dots,L-1$$
2026-06-12T22:37:21.927303 image/svg+xml Matplotlib v3.10.8, https://matplotlib.org/ 0 10 20 30 40 50 60 n 0.0 0.2 0.4 0.6 0.8 1.0 w[n] Time domain (L = 64) Rectangular Hann Hamming Blackman 0.0 0.1 0.2 0.3 0.4 ω/π −100 −80 −60 −40 −20 0 |W(e^jω)| (dB) Magnitude spectrum (normalized)
Fig. 9-2 — Common analysis windows, $L=64$. Left: time-domain shapes. Right: magnitude spectra (dB, normalized). The rectangular window has the narrowest mainlobe but only $-13$ dB sidelobes; Hann/Hamming/Blackman taper to zero at the edges, doubling or tripling the mainlobe width in exchange for $-31$ to $-57$ dB sidelobes.(窗形的選擇 = 主瓣寬度與旁瓣高度的交換。)
WindowMainlobe width (null–null)Peak sidelobeTypical use
Rectangular(矩形)$4\pi/L$$-13$ dBmaximum resolution, transient analysis
Hann(漢恩)$8\pi/L$$-31$ dBdefault for spectrograms / STFT
Hamming(漢明)$8\pi/L$$-41$ dBspeech analysis
Blackman(布雷克曼)$12\pi/L$$-57$ dBlarge dynamic range
Tip — why Hann is the workhorse
The Hann window is smooth (no edge discontinuity), has decent $-31$ dB sidelobes that also roll off at $-18$ dB/octave, its mainlobe is only twice the rectangular width, and shifted copies at 50% or 75% overlap sum to an exact constant — which makes perfect reconstruction in analysis–synthesis trivial (Section 9.6). That is why dsp10.m and virtually every audio tool default to it.

9.3.2 Window length and the resolution trade-off

Window shape sets the sidelobe behavior; window length $L$ sets the fundamental trade-off. The mainlobe width scales as $1/L$, so in hertz the frequency resolution of a Hann-windowed STFT is approximately

Resolution vs window length $$\Delta t \;\approx\; \frac{L}{f_s}\ \text{(seconds)}, \qquad \Delta f \;\approx\; c\,\frac{f_s}{L}\ \text{(Hz)},\quad c=\begin{cases}2 & \text{rectangular}\\ 4 & \text{Hann}\end{cases}$$

You cannot win on both axes at once. This is the signal-processing form of the uncertainty principle(測不準原理):

Time–bandwidth uncertainty $$\Delta t \cdot \Delta f \;\ge\; \frac{1}{4\pi}$$

with equality only for the Gaussian window. Improving time resolution by shortening the window necessarily degrades frequency resolution in exact proportion, and vice versa.

Warning — zero-padding is not resolution
Increasing the FFT length $N$ beyond the window length $L$ (zero-padding) only interpolates the spectrum on a finer grid — the picture gets smoother, not sharper. Two tones closer than the mainlobe width $\approx c\,f_s/L$ Hz remain a single ridge no matter how large $N$ is. Resolution is bought only with window length (i.e., with actual data). 補零只是把同一條曲線取樣得更密,並不會增加解析度。

9.3.3 Tiling the time–frequency plane

A useful mental picture: each STFT coefficient $X_m[k]$ "owns" a cell of the time–frequency plane of width $\Delta t$ and height $\Delta f$, with area bounded below by the uncertainty principle. The window length only decides the aspect ratio of the cells — never their area.

Short window L fine Δt, coarse Δf (wideband) t f Δt Δf Long window L coarse Δt, fine Δf (narrowband) t f Δt Δf Δt · Δf ≥ 1/4π equal cell area
Fig. 9-3 — STFT tiling of the time–frequency plane. A short window gives narrow-and-tall cells (good time localization), a long window gives wide-and-flat cells (good frequency localization). The cell area is fixed by the uncertainty principle; the window length only chooses its shape.(窗長只能改變格子的長寬比,不能縮小面積。)

9.4 The Spectrogram

9.4.1 Definition

The spectrogram(頻譜圖)is the squared magnitude of the STFT, almost always displayed in decibels as an image: horizontal axis time, vertical axis frequency, color/brightness $=$ energy. It discards the STFT phase and keeps the part the eye can read.

Spectrogram $$S[m,k]\;=\;\bigl|X_m[k]\bigr|^2, \qquad S_{\text{dB}}[m,k]\;=\;10\log_{10}\bigl|X_m[k]\bigr|^2$$

Axis calibration for sampling rate $f_s$, hop $M$, DFT length $N$ (exactly the axes constructed in dsp10.m):

Physical axes $$t_m=\frac{mM}{f_s}\ \text{(s)},\qquad f_k=\frac{k\,f_s}{N}\ \text{(Hz)},\quad k=0,\dots,\tfrac N2-1$$

For real signals only bins $0\le k< N/2$ are kept ($f$ up to $f_s/2$); the upper half is the mirror image. The dB scale matters: audio features of interest (harmonics, formants, reverb tails) span 60–80 dB of dynamic range and would be invisible on a linear scale.

9.4.2 Chirp: short vs long windows

The classic test signal for time–frequency analysis is the linear chirp (線性掃頻訊號), whose instantaneous frequency rises linearly:

Linear chirp and instantaneous frequency $$x(t)=\cos\!\Bigl(2\pi\bigl(f_0 t+\tfrac{\beta}{2}t^2\bigr)\Bigr), \qquad f_i(t)=\frac{1}{2\pi}\frac{d\phi}{dt}=f_0+\beta t$$
2026-06-12T22:35:52.883665 image/svg+xml Matplotlib v3.10.8, https://matplotlib.org/ 0.00 0.25 0.50 0.75 1.00 1.25 1.50 1.75 2.00 t (s) −1.0 −0.5 0.0 0.5 1.0 x(t) Linear chirp: instantaneous frequency 100 Hz → 400 Hz
Fig. 9-4 — Linear chirp, $f_s=2$ kHz: instantaneous frequency sweeps 100 → 400 Hz over 2 s. In the raw waveform the oscillations visibly tighten, but the global FT would show only a smeared block of energy from 100 to 400 Hz.
2026-06-12T22:39:04.807617 image/svg+xml Matplotlib v3.10.8, https://matplotlib.org/ 0.0 0.5 1.0 1.5 2.0 t (s) 0 100 200 300 400 500 600 f (Hz) L = 64 (32 ms) — wideband 0.0 0.5 1.0 1.5 2.0 t (s) 0 100 200 300 400 500 600 L = 1024 (512 ms) — narrowband
Fig. 9-5 — Hann-window spectrograms of the chirp (scipy stft, 87.5% overlap, 60 dB display range). Left, $L=64$ (32 ms): the ridge is thick in frequency but time events would be sharp — wideband. Right, $L=1024$ (512 ms): the ridge is spectrally thin, but the line blurs along time and bends at the edges because the chirp sweeps ~77 Hz within each window — narrowband.(同一訊號、同一不確定性原理:短窗時間準、頻率糊;長窗頻率準、時間糊。)
Key — read the trade-off off the figure
At $f_s=2000$ Hz the chirp rate is $\beta=150$ Hz/s. The Hann ridge width is $\approx 4f_s/L$: for $L=64$ that is $125$ Hz (thick ridge); for $L=1024$ it is $7.8$ Hz — but during one 512 ms window the chirp itself moves $\beta L/f_s \approx 77$ Hz, so the long-window "resolution" is wasted on a signal that refuses to sit still. The best window matches the signal's own rate of change.

9.4.3 Narrowband vs wideband

The lecture's slide "Narrowband vs. Wideband" summarizes the effect of varying window length on real sounds (speech is the standard example):

Narrowband spectrogram(窄帶)Wideband spectrogram(寬帶)
Window lengthlong (e.g. > 20 ms for speech)short (e.g. < 5 ms)
Filter bandwidthnarrow → fine $\Delta f$wide → fine $\Delta t$
What you seeindividual pitch harmonics as horizontal lines(諧波線)individual glottal pulses as vertical striations(聲門脈衝直紋)
What blursonsets, plosives, fast transitionsclosely spaced harmonics merge; formant envelopes(共振峰)stand out

9.4.4 Hop size and overlap

The hop size $M$ (frame advance) controls how densely the time axis is sampled. Overlap(重疊)is $L-M$ samples, i.e. overlap fraction $1-M/L$. Typical choices are 50% ($M=L/2$) or 75% ($M=L/4$ — the choice in dsp10.m, where $M=N/4$).

9.4.5 Spectrogram in MATLAB

MATLAB's Signal Processing Toolbox provides spectrogram directly; the homework instead builds it from first principles (frame → window → FFT → magnitude squared). Both in one place:

% Toolbox one-liner: Hann window, length N, 75% overlap, N-point DFT
N = 1024; M = N/4;
spectrogram(x, hann(N), N-M, N, Fs, 'yaxis');   % draws dB image directly

% Equivalent from scratch (the dsp10.m approach)
w = 0.5*(1 - cos(2*pi*(0:N-1)'/N));             % Hann window
num_frames = floor((length(x)-N)/M) + 1;
X = zeros(N, num_frames);
for m = 1:num_frames
    seg      = x((m-1)*M+1 : (m-1)*M+N) .* w;   % frame x window
    X(:, m)  = abs(fft(seg)).^2;                % power spectrum
end
t = (0:num_frames-1)*M/Fs;  f = (0:N/2-1)*Fs/N;
image(t, f, 10*log10(X(1:N/2,:))); axis xy; colormap(hot(256)); colorbar;
Tip — argument order trap
spectrogram(x, window, noverlap, nfft, fs) takes the overlap (samples shared between consecutive frames), not the hop size. For hop $M$ pass noverlap = N - M. Passing the hop by mistake silently produces a spectrogram with the wrong time density. Also remember axis xy when using image — otherwise the frequency axis is upside-down.

9.5 STFT as a Filterbank

Fix one frequency $\omega_k$ and watch $X(n,\omega_k)$ evolve with $n$. Rearranging the STFT definition (with the window treated as a filter impulse response):

Filterbank form of the STFT $$X(n,\omega_k)\;=\;\sum_{m} x[m]\,e^{-j\omega_k m}\;w[m-n] \;=\;\Bigl(\,x[n]\,e^{-j\omega_k n}\,\Bigr)\;*\;\tilde w[n],\qquad \tilde w[n]=w[-n]$$

So each STFT frequency row is produced by: (1) heterodyne(混頻)— multiply by $e^{-j\omega_k n}$ to shift the band around $\omega_k$ down to DC; (2) lowpass filter with $\tilde w[n]$, whose bandwidth is the window mainlobe; (3) keep every $M$-th output sample (the hop). The STFT is therefore a uniform filterbank(濾波器組)of $N$ bandpass channels, each of bandwidth $\approx c\,f_s/L$, spaced $f_s/N$ apart, downsampled by $M$.

x[n] × e^(−jω₀n) LPF w[−n] ↓M X(mM, ω₀) × e^(−jω₁n) LPF w[−n] ↓M X(mM, ω₁) × e^(−jω_{N−1}n) LPF w[−n] ↓M X(mM, ω_{N−1})
Fig. 9-6 — Filterbank interpretation of the STFT: each of the $N$ channels heterodynes the band around $\omega_k=2\pi k/N$ to DC, lowpass filters with the (time-reversed) window, and downsamples by the hop $M$. One spectrogram row is one channel's output power.(STFT 的每一列=一個帶通濾波器通道的輸出。)

This view explains the vocabulary of Section 9.4.3: a long window is a narrow lowpass filter — hence "narrowband" spectrogram — and a short window is a wide one. It also connects the STFT to Chapter 8: a DFT of one windowed frame is just this filterbank evaluated at a single time instant.

9.6 STFT Analysis–Synthesis

9.6.1 Overlap-add reconstruction

The STFT is not just for pictures — because it is invertible (when sampled densely enough), we can modify a signal in the time–frequency domain and resynthesize: noise suppression, time stretching, pitch shifting, equalization. The standard synthesis is overlap-add (OLA, 重疊相加): inverse-FFT each (possibly modified) frame, then add the frames back at their original positions:

Overlap-add synthesis $$y[n]\;=\;\sum_{m}\,\underbrace{\Bigl(\tfrac1N\sum_{k=0}^{N-1}X_m[k]\,e^{\,j2\pi k(n-mM)/N}\Bigr)}_{\text{IFFT of frame }m}\;$$

9.6.2 The COLA condition

If the frames are unmodified, each IFFT returns exactly $x[n+mM]\,w[n]$, so the OLA sum gives $y[n]=x[n]\sum_m w[n-mM]$. Perfect reconstruction (up to a constant) therefore requires the shifted windows to add to a constant — the COLA (constant-overlap-add) condition(恆定重疊相加條件):

COLA condition $$\sum_{m=-\infty}^{\infty} w[n-mM]\;=\;C\quad\text{for all }n \;\;\Longrightarrow\;\; y[n]=C\,x[n]$$

For the length-$L$ periodic Hann window, COLA holds at hop $M=L/2$ (with $C=1$) and at $M=L/4$ (with $C=2$) — one reason the Hann window dominates analysis–synthesis practice. The four "STFT Analysis-Synthesis" slides walk this chain: window → FFT → modify → IFFT → overlap-add, with COLA guaranteeing transparency when nothing is modified.

Warning — modification breaks the algebra
COLA guarantees perfect reconstruction only for the unmodified STFT. After editing the coefficients (e.g. zeroing noise bins), the modified frames are generally not a valid STFT of any signal — frames disagree in their overlap regions, and OLA returns the least-squares compromise. Artifacts ("musical noise") live exactly in this gap. A synthesis window (weighted OLA, WOLA) is the standard mitigation.

9.7 Summary

STFT

$X_m[k]=\sum_{n=0}^{N-1}x[n+mM]w[n]e^{-j2\pi kn/N}$: slide a length-$L$ window by hops of $M$, FFT each frame. Time grid $mM/f_s$, frequency grid $kf_s/N$.

Spectrogram

$S[m,k]=|X_m[k]|^2$, displayed in dB. Rows = bandpass channel outputs, columns = frame spectra. Keep $k<N/2$ for real signals.

Window shape

Mainlobe ↔ resolution, sidelobes ↔ leakage. Rect: $4\pi/L$, $-13$ dB. Hann: $8\pi/L$, $-31$ dB, COLA at 50%/75% overlap — the default.

Window length

$\Delta t\approx L/f_s$, $\Delta f\approx c f_s/L$, $\Delta t\,\Delta f\ge 1/4\pi$. Long = narrowband (harmonics), short = wideband (transients). Zero-padding interpolates only.

Hop / overlap

Hop $M$, overlap $L-M$; 50–75% typical. spectrogram(x,hann(N),N-M,N,Fs) takes overlap, not hop. Frames: $\lfloor (L_x-N)/M\rfloor+1$.

Analysis–synthesis

IFFT frames + overlap-add; perfect reconstruction iff $\sum_m w[n-mM]=C$ (COLA). Hann at $M=L/2$: $C=1$; at $M=L/4$: $C=2$.

Exercises

Exercise 1 — Where did the timing go?

Problem: Let $x_1[n]$ consist of a 50 Hz tone for the first half-second followed by a 120 Hz tone for the second half-second ($f_s=1$ kHz), and let $x_2[n]$ play the same two tones in the opposite order (Fig. 9-1). (a) Explain why $|X_1(e^{j\omega})| \approx |X_2(e^{j\omega})|$. (b) Where, mathematically, does the FT store the ordering information? (c) Describe what the STFT magnitude of each signal looks like and why it distinguishes them.

Click to reveal solution

(a) Write $x_2[n]=x_1[n_0-1-n]$ approximately (the second signal is a time-reversal-plus-shift of the first up to phase of the tones; more simply, each is a sum of the same two gated tones, just gated by complementary halves). A gated tone $g[n]\cos(\omega_i n)$ has spectrum $\tfrac12 G(e^{j(\omega-\omega_i)})+\tfrac12 G(e^{j(\omega+\omega_i)})$ where $G$ is the gate's spectrum. Shifting the gate in time multiplies $G$ by a pure phase $e^{-j\omega n_d}$ and leaves $|G|$ unchanged. Since both signals are sums of the same two gated tones with gates that differ only by a time shift, and the two spectral lumps (50 and 120 Hz) barely overlap, the magnitude spectra are nearly identical — only the cross terms in the overlap region differ slightly.

(b) In the phase: a delay of $n_d$ samples contributes the linear phase factor $e^{-j\omega n_d}$. The ordering of events is encoded in how the phase of each spectral lump slopes with $\omega$ (group delay $-\,d\angle X/d\omega$ evaluated near each tone gives that tone's arrival time).

(c) With a window much shorter than 0.5 s, the STFT of $x_1$ shows a ridge at 50 Hz for $t<0.5$ s that jumps to 120 Hz afterwards; $x_2$ shows the mirror pattern. The STFT separates them because each frame sees only one regime — it converts the unreadable phase code into a readable time axis.

Exercise 2 — Window length design

Problem: A signal sampled at $f_s=8$ kHz contains two simultaneous tones at 1000 Hz and 1040 Hz. (a) Using a Hann window (mainlobe null-to-null width $8\pi/L$), find the minimum window length $L$ that resolves the two tones, and the corresponding window duration. (b) Would zero-padding the FFT from $N=L$ to $N=8L$ help if $L$ is below your answer? (c) For the rectangular window, what $L$ suffices, and why might you still prefer the Hann window?

Click to reveal solution

(a) The Hann mainlobe null-to-null width in rad/sample is $8\pi/L$, i.e. in hertz $\Delta f = \frac{8\pi/L}{2\pi}f_s = \frac{4f_s}{L}$. Two equal tones are (just) resolved when their spacing exceeds roughly half the null-to-null width, but the safe design rule is to require the full spacing to exceed the half-width $\frac{4f_s}{L}\cdot\frac12$... To keep it simple use the standard criterion $\Delta f_{\min}\approx 4f_s/L \le 40$ Hz:

Length requirement $$\frac{4f_s}{L}\le 40\ \text{Hz}\;\Longrightarrow\; L\ge \frac{4\cdot 8000}{40}=800\ \text{samples}$$

Choose the next power of two, $L=1024$, i.e. a window duration of $1024/8000=128$ ms. (Any consistent mainlobe criterion is acceptable; the scaling $L\propto f_s/\Delta f$ is the point.)

(b) No. Zero-padding evaluates the same windowed spectrum — the convolution of the true line spectrum with the same mainlobe — on a denser grid. If the two mainlobes have already merged into one bump, sampling that bump more finely cannot split it. Only more data (larger $L$) narrows the mainlobe.

(c) The rectangular mainlobe is $4\pi/L$ (half of Hann's), so $L\ge 2f_s/\Delta f = 400$ samples suffices. But its $-13$ dB sidelobes leak badly: if the two tones have unequal amplitudes (or there is any other strong component), the weaker tone can be buried under the stronger one's sidelobes. Hann's $-31$ dB sidelobes cost a factor of 2 in length but buy 18 dB of dynamic range.

Exercise 3 — Narrowband vs wideband speech analysis

Problem: A speech signal is sampled at $f_s=10$ kHz. The talker's pitch (fundamental) is $f_0=200$ Hz, so harmonics are spaced 200 Hz apart and glottal pulses occur every $1/f_0 = 5$ ms. Using the Hann resolution rule $\Delta f\approx 4f_s/L$: (a) find the window lengths (samples and ms) for a narrowband spectrogram that clearly resolves the harmonics ($\Delta f \le 100$ Hz). (b) Find the window length for a wideband spectrogram that resolves individual glottal pulses in time ($\Delta t \le 2.5$ ms). (c) Show that no single window can do both, by computing $\Delta t\,\Delta f$ demanded by (a)+(b) and comparing with the uncertainty bound.

Click to reveal solution

(a) Need $4f_s/L\le 100$ Hz $\Rightarrow L\ge 4\cdot10000/100=400$ samples $=40$ ms. A 40 ms Hann window spans 8 pitch periods; harmonics appear as clean horizontal lines, but each glottal event is smeared over 40 ms — this is the narrowband setting.

(b) Time resolution is the window duration: $\Delta t\approx L/f_s\le 2.5$ ms $\Rightarrow L\le 25$ samples... in practice $L=25$ is extreme; taking $\Delta t = L/f_s = 2.5$ ms gives $L = 25$ samples, and commonly one relaxes to $L\approx 30$–$50$ ($3$–$5$ ms). Each window now spans less than one pitch period: vertical striations mark the glottal pulses — the wideband setting. Its frequency resolution is $4f_s/L\approx 1600$ Hz at $L=25$: individual harmonics merge, and only the broad formant envelope survives.

(c) Demanding both simultaneously requires $\Delta t\,\Delta f \le (2.5\ \text{ms})(100\ \text{Hz}) = 0.25$. For a single Hann window the product is fixed: $\Delta t\,\Delta f = \frac{L}{f_s}\cdot\frac{4 f_s}{L} = 4$, independent of $L$ — sixteen times larger than required, and the theoretical floor for any window is $\Delta t\,\Delta f \ge 1/(4\pi)\approx 0.08$ with the practical mainlobe-based product always of order 1. Hence one analysis cannot satisfy both specs; speech researchers simply make two spectrograms, one narrowband and one wideband.

Exercise 4 — STFT of elementary signals

Problem: Using the definition $X(n,\omega)=\sum_m x[m]\,w[m-n]\,e^{-j\omega m}$ with a length-$L$ window $w$ whose DTFT is $W(e^{j\omega})$, derive the STFT of (a) the complex exponential $x[m]=e^{j\omega_0 m}$ and (b) the shifted impulse $x[m]=\delta[m-m_0]$. (c) Interpret both results in terms of the spectrogram image and the tiling picture of Fig. 9-3.

Click to reveal solution

(a) Substitute and change variable $r=m-n$:

Tone $$X(n,\omega)=\sum_m e^{j\omega_0 m} w[m-n] e^{-j\omega m} =e^{-j(\omega-\omega_0)n}\sum_r w[r]\,e^{-j(\omega-\omega_0)r} =e^{-j(\omega-\omega_0)n}\,W\!\bigl(e^{j(\omega-\omega_0)}\bigr)$$

Magnitude: $|X(n,\omega)|=|W(e^{j(\omega-\omega_0)})|$ for every $n$ — a horizontal ridge at $\omega_0$ whose cross-section is the window spectrum (mainlobe width sets the ridge thickness, sidelobes appear as faint parallel stripes).

(b) The sum collapses at $m=m_0$:

Impulse $$X(n,\omega)=w[m_0-n]\,e^{-j\omega m_0} \qquad\Longrightarrow\qquad |X(n,\omega)|=|w[m_0-n]|$$

A vertical stripe at times $n$ near $m_0$, identical at all frequencies, whose cross-section along $n$ is the (reversed) window shape — width $=$ window length.

(c) The tone (a horizontal line in the ideal time–frequency plane) is blurred vertically by the window's frequency width $\Delta f$; the impulse (a vertical line) is blurred horizontally by the window's duration $\Delta t$. Every signal feature is smeared by exactly one tile of Fig. 9-3 — the STFT renders the time–frequency plane at the resolution of its window, no finer.

Exercise 5 — Frames, hop size, and COLA

Problem: An audio clip has $L_x=480{,}000$ samples ($f_s=48$ kHz, 10 s). The STFT uses a periodic Hann window $w[n]=\tfrac12(1-\cos\frac{2\pi n}{N})$, $N=1024$, hop $M=N/2=512$. (a) How many complete frames are computed, and what is the spacing of the spectrogram's time axis? (b) Prove the COLA property $\sum_m w[n-mM]=1$ for this window at $M=N/2$. (c) What constant results at $M=N/4$, and how must overlap-add synthesis account for it?

Click to reveal solution

(a) $N_{\text{frames}}=\bigl\lfloor (L_x-N)/M \bigr\rfloor+1 =\bigl\lfloor (480000-1024)/512\bigr\rfloor+1 = 935+1 = 936$ frames. Time spacing $M/f_s = 512/48000 \approx 10.7$ ms per column.

(b) At hop $M=N/2$, sample $n$ is covered by exactly two shifted windows, at offsets $r$ and $r+N/2$ where $r\equiv n \pmod{N/2}$, $0\le r<N/2$:

COLA at 50% overlap $$w[r]+w[r+\tfrac N2] =\tfrac12\Bigl(1-\cos\tfrac{2\pi r}{N}\Bigr)+\tfrac12\Bigl(1-\cos\bigl(\tfrac{2\pi r}{N}+\pi\bigr)\Bigr) =\tfrac12(1-c)+\tfrac12(1+c)=1$$

where $c=\cos(2\pi r/N)$; the shifted cosine flips sign and the pair sums to exactly 1 for every $n$. (Note this uses the periodic Hann definition with denominator $N$, exactly the formula in dsp10.m — the "symmetric" Hann with denominator $N-1$ violates COLA slightly.)

(c) At $M=N/4$ each sample is covered by four windows at phases $\theta, \theta+\tfrac\pi2, \theta+\pi, \theta+\tfrac{3\pi}2$. The four cosines cancel in pairs, leaving $\sum_m w[n-mM] = 4\cdot\tfrac12 = 2$. OLA synthesis then returns $y[n]=2x[n]$, so the output must be divided by $C=2$ (in general by $C = \sum_m w[-mM]$) for unit gain.

Exercise 6 — MATLAB spectrogram from scratch (dsp10.m)

Problem: Write a MATLAB function myspectrogram(x, N, w, M) that computes the power spectrogram of a signal $x$ with DFT length $N$, window $w$, and hop $M$, without using the Signal Processing Toolbox. Use it to analyze an audio recording with a Hann window of length $N=1024$ and 75% overlap ($M=N/4$): plot the waveform, the window, and the spectrogram in dB with correctly calibrated time/frequency axes. Report the frequency resolution. (This is the Lecture-9 homework, dsp10.m.)

Click to reveal solution

Cleaned-up solution code:

clc; clear; close all;

% Step 1: load audio
[x, Fs] = audioread('guitar4.m4a');
t_audio = (0:length(x)-1) / Fs;

figure; plot(t_audio, x, 'r');
xlabel('Time (s)'); ylabel('Amplitude'); title('Audio Signal');

% Step 2: Hann window, N chosen so Fs/N is around 20-40 Hz
N = 2^10;                                   % DFT length (1024)
w = 0.5 * (1 - cos(2*pi*(0:N-1)'/N));       % periodic Hann window
disp(['Frequency resolution (bin spacing): ', num2str(Fs/N), ' Hz']);

figure; plot(w, 'r');
xlabel('Sample Index'); ylabel('Amplitude'); title('Hann Window');

% Step 3-4: spectrogram with hop M = N/4 (75% overlap)
M = N/4;
X = myspectrogram(x, N, w, M);

num_frames = size(X, 2);
t = (0:num_frames-1) * M / Fs;              % frame times
f = (0:N/2-1) * Fs / N;                     % bin frequencies

% Step 5: display in dB
figure;
image(t, f, 10*log10(X(1:N/2, :)));
axis xy;
xlabel('Time (s)'); ylabel('Frequency (Hz)'); title('Spectrogram');
colormap(hot(256)); colorbar;

function X = myspectrogram(x, N, w, M)
    L = length(x);
    num_frames = floor((L - N)/M) + 1;
    X = zeros(N, num_frames);
    for m = 1:num_frames
        idx = (m-1)*M + (1:N);              % frame indices
        X(:, m) = abs(fft(x(idx) .* w)).^2; % window, FFT, power
    end
end

Key design points:

  • Window/DFT length: $N=1024$ gives bin spacing $f_s/N$ (e.g. 43 Hz at $f_s=44.1$ kHz, ~21.5 Hz at 22.05 kHz) — fine enough to separate guitar-string harmonics. The resolvable spacing with a Hann window is $\approx 4f_s/N$, four bins.
  • Hop $M=N/4$: 75% overlap, smooth time axis, and a COLA-compliant configuration ($C=2$) should synthesis be needed later.
  • Axes: column $m$ sits at $t=mM/f_s$, row $k$ at $f=kf_s/N$; only rows $k<N/2$ are displayed for the real signal, and axis xy puts 0 Hz at the bottom.
  • dB display: $10\log_{10}|X|^2$ compresses the ~70 dB dynamic range of a plucked-string sound so decaying harmonics stay visible.

Result regenerated below with the identical pipeline (Hann $N=1024$, $M=256$) in NumPy:

2026-06-12T22:39:05.091053 image/svg+xml Matplotlib v3.10.8, https://matplotlib.org/ 0.0 0.5 1.0 1.5 2.0 2.5 3.0 t (s) −1 0 1 x(t) Synthetic signal: three "plucked notes" (G3, B3, E4) at t = 0, 1, 2 s 0.0 0.5 1.0 1.5 2.0 2.5 t (s) 0 500 1000 1500 2000 f (Hz) myspectrogram output: Hann window, N = 1024, hop M = 256 (dB) −20 0 20 40 dB
Fig. 9-7myspectrogram pipeline rerun on a synthetic guitar-like signal (three exponentially decaying harmonic "plucks" — G3 196 Hz, B3 246.9 Hz, E4 329.6 Hz — at $t=0,1,2$ s, $f_s=8$ kHz), since the original guitar4.m4a recording is not available here. Each pluck appears as a stack of harmonic lines starting at its onset and fading as the note decays — exactly the structure the homework observes on the real recording.(原始音檔不在手邊,故以合成撥弦訊號重現同一流程;諧波線、起音時刻與衰減皆清晰可見。)