Lecture 8 · Sampling, granular synthesis, and the phase vocoder
| Block | Representation | Generator |
|---|---|---|
| Filters and effects | LTI and time-varying systems | hand-designed |
| Classical synthesis | samples, partials, modes, physical state | hand-designed |
| DDSP | the same structured models | parameters learned |
| Reverberation | a recursive dynamical system | hand-designed, fittable |
| Diffusion | a learned representation | learned |
The four questions
Lecture 7 described sounds by their spectrum. This lecture starts from a recording and divides it into pieces of decreasing length. For each division we ask which parameters become controllable.
| Guitar riff from lecture 7 | |
|---|---|
| original | |
| four times slower, same pitch | |
| frozen at 2.1 s | |
| scrubbed back and forth | |
| cloud in octaves and fifths |
Question
All five sounds are built from pieces of the first recording. Which parameter changes from row to row?
Answer
The read position in the recording. It advances at normal speed, at a quarter of the speed, stops, oscillates, or jumps at random. Granular synthesis makes read position, speed and pitch independent parameters.
| Representation | Unit | Controls |
|---|---|---|
| the recording (sampling) | a whole note, seconds long | choice of note, loop points, playback rate |
| grains (granular synthesis) | windowed segments of 10–200 ms | position, density, duration, pitch, randomness |
| short-time Fourier transform (STFT) frames (phase vocoder) | grains with a Fourier transform: magnitude and phase per bin | time and pitch independently |
From top to bottom the units get shorter and the representation gets more structure. More parameters can be changed before the sound is damaged.
In digital audio, a sample is one value of a sampled signal, taken every \(T = 1/f_s\) seconds: \[x[n] = x_c(nT)\]
In a sampler, a sample is a recorded sound such as a note or a drum hit, stored as the sequence \(x[0], \dots, x[L-1]\).
Reconstruction
If \(x_c\) is bandlimited to \(f_s/2\), its sample values determine it at every instant: \[x_c(t) = \sum_n x[n]\,\operatorname{sinc}\!\Big(\frac{t - nT}{T}\Big), \quad \operatorname{sinc}(u) = \frac{\sin \pi u}{\pi u}\]
Common form of the methods in this lecture
The methods evaluate \(x_c\) at new instants \(\tau_k(t)\), multiply by windows \(w_k(t)\), and add: \[y(t) = \sum_k w_k(t)\; x_c\big(\tau_k(t)\big)\] A sampler uses one read position and an amplitude envelope. Granular synthesis uses many read positions with short windows. The phase vocoder also modifies the Fourier transform of each windowed segment.
\[y[n] = x_c\big(\tau[n]\,T\big), \qquad \tau[n+1] = \tau[n] + r[n]\]
\(\tau[n]\) is the read position in samples of the recording, \(r[n]\) the playback rate.
| Rate \(r[n]\) | Result |
|---|---|
| \(1\) | original playback |
| \(2^{s/12}\) | transposed by \(s\) semitones, duration divided by \(r\) |
| \(0 < r < 1\) | slower and lower |
| \(0\) | constant output |
| \(-1\) | reversed |
| periodic | vibrato, scrubbing |

A single pointer with \(r = 0\) outputs the constant value \(x_c(\tau T)\), which is silent. An audible freeze needs many short windowed segments around \(\tau\). These are the grains discussed later in this lecture.
Mellotron, 1963: one tape strip per key · 120years
Fairlight CMI, 1979 · 120years
Limitation
A recording stores one performance, with one duration, one pitch and one playing style.

Segments in a sampler
Attack: played once as recorded. Sustain: a loop that repeats while the key is held. Release: an envelope after note-off, in piano libraries often with a recorded release noise.

Two seconds of a piano note (A2) are stored, six seconds are played:
| stored, 2 s | |
| 37 ms loop | |
| 455 ms loop, matched | |
| real note |
Repeating the segment \(s[n] = x[a+n]\), \(n = 0, \dots, L-1\), gives the periodic signal \(y[n] = s[n \bmod L]\) with the Fourier series
\[y[n] = \frac{1}{L}\sum_{k=0}^{L-1} S[k]\, e^{j 2\pi k n / L}, \qquad S[k] = \sum_{n=0}^{L-1} s[n]\, e^{-j2\pi kn/L}\]
Its spectrum has lines at \(f = k\, f_s / L\) only. Consequences:
Example
\(f_0 = 110\) Hz at \(f_s = 44.1\) kHz: one period is 400.91 samples.
The loop end should continue the waveform at the loop start. Compare \(W\) samples after the start with \(W\) samples after a candidate end:
\[\rho(L) = \frac{\sum_{n=0}^{W-1} x[a+n]\; x[a+L+n]}{\|\mathbf{x}_a\|\;\|\mathbf{x}_{a+L}\|}\]
For segments of equal energy, \[\|\mathbf{x}_a - \mathbf{x}_{a+L}\|^2 = \|\mathbf{x}_a\|^2 + \|\mathbf{x}_{a+L}\|^2 - 2\,\langle \mathbf{x}_a, \mathbf{x}_{a+L}\rangle\] is smallest where \(\rho(L)\) is largest.

Piano A2, \(a = 1\) s, \(W = 4096\). The waveform nearly repeats after each period, and the correlation peaks at these lengths; the first peak is at 9.07 ms (\(T_0 = 9.09\) ms). The 455 ms loop is the highest peak between 450 and 550 ms.
A loop of many periods keeps the slow beating between the strings. A loop of one period is a single-cycle wavetable (lecture 7).
Two segments with equal power \(\sigma^2\) and correlation coefficient \(\rho\) are mixed: \[y = g_1 x_1 + g_2 x_2\] \[\mathbb{E}\big[y^2\big] = \sigma^2\big(g_1^2 + g_2^2 + 2\rho\, g_1 g_2\big)\]

Loop junction
The two segments at the piano loop junction have \(\rho = 0.79\). At the midpoint of the fade, equal power raises the level by 2.5 dB and equal gain lowers it by 0.5 dB. The loop example uses equal gain.
Playing a recording \(r\) times faster is a time scaling: \[y(t) = x_c(rt) \;\;\Longleftrightarrow\;\; Y(f) = \frac{1}{|r|}\,X_c\!\Big(\frac{f}{r}\Big)\]
Derivation: substitute \(u = rt\) in \(\int x_c(rt)\,e^{-j2\pi f t}\,dt = \frac{1}{r}\int x_c(u)\, e^{-j2\pi (f/r) u}\,du\).
All frequencies are multiplied by \(r\): the fundamental, the harmonics and the spectral envelope. The duration is divided by \(r\). Samplers therefore store a separate recording every few semitones.

Speech, \(r = 2^{5/12} = 1.335\)
\(f_0\) 202 → 269 Hz; envelope peak 900 Hz → 1.2 kHz; duration 1.86 s → 1.39 s.
| original | |
| a fourth up |
For \(r \neq 1\) the read position falls between stored samples, and the value there is interpolated. Example: a 64-sample sawtooth table with 8 harmonics, played one semitone above A4 at \(f_s = 22.05\) kHz. The read pointer advances by 1.35 table values per output sample.
Drop-sample (order 0): the pointer is rounded down, so about one table value in three is skipped.
Linear (order 1): straight lines between neighbouring values.

The red lines mark the 8 harmonics. All other lines are aliased interpolation error.
Interpolation convolves the sample values with a kernel \(h\): \[\hat x_c(t) = \sum_n x[n]\; h\big(t/T - n\big)\] \[\Longleftrightarrow\;\; \hat X_c(f) = T\,H(f/f_s)\, X(e^{j2\pi f/f_s})\]
The spectrum of \(x[n]\) repeats around every multiple of \(f_s\). The frequency response \(H\) determines how much of these images remains.
The remaining images are then read at rate \(r\) and alias into the audio band.

Level of the first image relative to the component
| Component | Order 0 | Order 1 |
|---|---|---|
| 1 kHz | −33 dB | −65 dB |
| 5 kHz | −18 dB | −36 dB |
A multisampled instrument stores recordings of every key, at several velocities and with several playing techniques.
Storage estimate for a grand piano
88 keys × 8 velocity layers × 2 minutes = 23.5 hours of audio, or 14.9 GB as 16-bit stereo at 44.1 kHz. The sampler keeps the beginning of each sample in RAM and streams the rest from disk (GigaSampler, 1998).
Current piano libraries also include round-robin variants, pedal resonance, release noises and several microphone positions, with tens of gigabytes per instrument.
A sampler can only reproduce situations that were recorded. A half-pressed pedal or a string that is still sounding requires additional recordings.
A key zone is a range of \(Z\) adjacent keys served by one recording at its centre, the root key.
Velocity layers are recordings of the same key at different velocities. Between two layers the sampler mixes both recordings. They are nearly uncorrelated, so the mix uses equal-power gains.
Brightness increases with velocity. A low-pass filter controlled by velocity can interpolate the timbre between layers.
Question
A sampler stores every third key, each recording at the middle key of its zone. By how much is a note transposed at most, and how far do its formants move?
Answer
At most one semitone, so the formants move by up to \(2^{1/12} - 1 = 5.9\,\%\).
One drum hit, 24 times in a row:
| single hit | |
| identical copies | |
| slightly varied copies |
For each hit: a new velvet-noise filter (8 random-sign taps within 4 ms) applied to a low-shelf-filtered copy, mixed 14 dB below the direct sound (Fagerström et al. 2021), plus accents and ±2 ms timing variation.

Identical repetitions sound mechanical (the machine-gun effect). Samplers use round-robin recordings (several per note) or variation synthesis, a small random processing of each hit.
Early drum machines such as the LM-1 (1980) used one sample per drum and show this effect. Synthesized drums such as the 808 (lecture 7) vary from hit to hit because their noise source and oscillators run freely.
Gabor (1947) proposed to describe sound by elementary signals localised in both time and frequency: \[g_{u,f}(t) = w(t-u)\; e^{j2\pi f t}\]
Let \(\Delta t\) and \(\Delta f\) be the standard deviations of \(|g|^2\) and \(|G|^2\).
Uncertainty relation
\[\Delta t\;\Delta f \;\ge\; \frac{1}{4\pi}\] Equality holds for the Gaussian window.

Short atoms are heard as clicks with a spectral colour, long atoms as tones. Granular synthesis builds sounds from thousands of such atoms per second (Xenakis 1959, Roads 1978).

\[y(t) = \sum_k w(t - t_k)\; x_c\big(p_k + r_k\,(t - t_k)\big)\]
Grain \(k\) has onset \(t_k\), read position \(p_k\), playback rate \(r_k\) and window \(w\). In the common form: \(\tau_k(t) = p_k + r_k(t - t_k)\), \(w_k(t) = w(t - t_k)\).
A grain is the product of the source and a window, so its spectrum is a convolution: \[g(t) = w(t)\,x_c(t) \;\Longleftrightarrow\; G(f) = (W * X_c)(f)\]
Every harmonic of the source is replaced by a copy of \(W\). The main lobe of a Hann window of length \(T_g\) is \(4/T_g\) wide. Neighbouring harmonics remain separate if \(4/T_g \le f_0\): \[T_g \;\ge\; 4/f_0 \quad \text{(four periods)}\]

Riff at 1.41 s, \(f_0 = 147\) Hz, minimum grain length 27 ms:
| Grain length | |
|---|---|
| 5 ms | |
| 20 ms | |
| 80 ms | |
| 250 ms |
Below 27 ms the pitch is lost; the 5 ms grains are heard as clicks.

Grains with spacing \(H\) add up without amplitude modulation if \[\sum_k w(t - kH) = C \quad \text{for all } t \qquad \text{(COLA)}\]
For the Hann window \(w(t) = \sin^2(\pi t/N)\) and \(H = N/2\): \[w(t) + w(t - N/2) = \sin^2 + \cos^2 = 1\] For \(H = N/4\) two such pairs overlap and \(C = 2\).
Poisson summation gives \[\sum_k w(t - kH) = \frac{1}{H}\sum_l W\!\Big(\frac{l}{H}\Big)\, e^{j2\pi l t/H}\] The sum is constant if \(W(l/H) = 0\) for all \(l \neq 0\). The Hann window of length \(N\) has spectral zeros at \(l/N\) for \(|l| \ge 2\), which include all multiples of \(1/H\) for \(H = N/2\).
One grain \(g\), repeated with spacing \(H\), gives a periodic output: \[Y(f) = \frac{1}{H}\sum_l G\Big(\frac{l}{H}\Big)\,\delta\Big(f - \frac{l}{H}\Big)\] Its spectrum has lines at multiples of the grain rate \(1/H\).
With random onset shifts \(t_k = kH + \delta_k\), \(\delta_k \sim \mathcal{N}(0, \sigma^2)\): \[\mathbb{E}\,Y(f) = G(f)\sum_k e^{-j2\pi f kH}\;\underbrace{\mathbb{E}\,e^{-j2\pi f \delta}}_{e^{-2\pi^2 f^2 \sigma^2}}\] The expected amplitude of each line is reduced by the factor \(e^{-2\pi^2 f^2\sigma^2}\). The remaining power becomes broadband noise.

With \(H = 25\) ms and \(\sigma = 8\) ms the 40 Hz line drops by 17.6 dB; the higher lines disappear.
| 50 ms grains | |
|---|---|
| onsets every 25 ms | |
| jitter \(\sigma\) = 8 ms |
A corpus contains recorded units (frames, grains or notes), each described by features such as pitch, loudness and spectral envelope. The target is a sequence of feature vectors \(t_1, \dots, t_M\). The units \(u_1, \dots, u_M\) are chosen to minimise \[C = \sum_m C^{\text{t}}(u_m, t_m) + \sum_m C^{\text{c}}(u_{m-1}, u_m)\]
The minimum is found by dynamic programming (Viterbi), as in unit-selection speech synthesis (Hunt & Black 1996). CataRT (Schwarz 2006) applies the method to music.
Speech reproduced from the course recordings
Corpus: guitar riff, drums, piano. Features: dB levels of 40 mel bands (a perceptual frequency scale), mean removed. Units are chosen greedily frame by frame; continuing the previous unit costs 20 % less.
| target | |
| mosaic |
In the source-filter model of lecture 7, voiced speech is a sequence of vocal-tract responses \(h\), one per glottal pulse: \[x_c(t) \approx \sum_k h(t - kT_0)\]

| A fourth up | |
|---|---|
| original | |
| resampled | |
| PSOLA |
Measured here: 202 → 269 Hz. Method: Moulines & Charpentier (1990).
Time-scale modification (TSM) changes the duration and keeps the pitch, as in the second opening example.
Frames are written with hop \(H_s\) and read with hop \(H_a = H_s/\alpha\); the output is \(\alpha\) times longer. The methods differ in how consecutive frames are aligned:
| Method | Alignment of consecutive frames |
|---|---|
| OLA | none |
| WSOLA (waveform similarity OLA) | time shift of each frame by up to \(\pm\Delta\) |
| PSOLA | frames centred on pitch marks |
| phase vocoder | phase correction in every frequency bin |
Speech, 1.8 times longer:
| OLA | |
| WSOLA |
The phase vocoder versions follow in the next section.
Review: Driedger & Müller 2016.
If frame \(m-1\) was read at \(p_{m-1}\), the input continues naturally at \(p_{m-1} + H_s\). The next frame is taken near its nominal position \(mH_a\), at the shift that best matches this continuation:
\[p_m = \arg\max_{|p - mH_a| \le \Delta}\; \sum_{n=0}^{N-1} x[p+n]\; x[p_{m-1} + H_s + n]\]
The frame \(x[p_m + n]\, w[n]\) is then added at \(mH_s\).
Verhelst & Roelands 1993.
\[X(m, k) = \sum_{n=0}^{N-1} x[mH_a + n]\; w[n]\; e^{-j 2\pi k n / N}\]
Time stretching reads frames with hop \(H_a\) and writes them with hop \(H_s > H_a\).
Failure case: frames moved with their original phases
Consecutive frames no longer fit together. Their overlap partly cancels, and the result beats.
| 1.8 times longer | |
|---|---|
| frames moved (OLA) | |
| phase vocoder |
Let \(x[n] = A\cos(\omega_0 n + \varphi_0)\) with \(\omega_0\) near \(\omega_k\). The positive-frequency part gives \[X(m,k) = \frac{A}{2}\sum_n e^{j(\omega_0 (mH_a + n) + \varphi_0)}\, w[n]\, e^{-j\omega_k n}\] \[= \frac{A}{2}\; e^{j(\omega_0 m H_a + \varphi_0)}\; W(\omega_k - \omega_0)\] with the window spectrum \(W(\omega) = \sum_n w[n]\, e^{-j\omega n}\).
The phase increment gives \(\omega_0\) much more accurately than the bin spacing \(f_s/N = 21.5\) Hz.
Question
\(f_s = 44.1\) kHz, \(H_a = 256\), bin centre 1000 Hz, partial at 1010 Hz. By how much do the phase increments per frame differ?
Answer
\(2\pi \cdot 10 \cdot 256 / 44100 = 0.36\) rad.
Bin \(k\) predicts a phase increment of \(\omega_k H_a\) per frame. The deviation from this prediction gives the frequency of the partial in that bin:
\[\hat{\omega}_k = \omega_k + \frac{\operatorname{princarg}\big(\angle X(m{+}1,k) - \angle X(m,k) - \omega_k H_a\big)}{H_a}\]
\(\operatorname{princarg}\) wraps a phase into \((-\pi, \pi]\). The output phase is advanced with this frequency and the synthesis hop:
\[\varphi(m{+}1, k) = \varphi(m, k) + \hat{\omega}_k H_s\]
Median pitch of the speech example:
| \(f_0\) | |
|---|---|
| original | 202 Hz |
| frames moved (OLA) | 172 Hz |
| phase vocoder, 1.8 times longer | 201 Hz |
Flanagan & Golden, Bell Labs, 1966. Dolson’s tutorial (1986) introduced the method to computer music.
A pitch shift by \(r\) is a time stretch by \(r\) followed by resampling by \(r\). The duration is unchanged and the pitch is multiplied by \(r\).
| A fourth up | |
|---|---|
| resampled: shorter | |
| phase vocoder: same length |
Measured: 202 → 269 Hz, ratio 1.33 (target \(2^{5/12} = 1.335\)).
Remaining artefacts
A partial occupies all bins of a main lobe. The phase vocoder updates these bins independently; small errors accumulate and their relative phases drift. The result sounds distant and reverberant (phasiness).
Identity phase locking (Laroche & Dolson 1999):
| Speech, 1.8 times longer | |
|---|---|
| OLA | |
| WSOLA | |
| phase vocoder | |
| with phase locking |
Relative difference between the modified STFT and the STFT of its resynthesis: 0.76 without and 0.19 with phase locking (OLA: 0.79).

In a spectrogram, sustained tones form horizontal lines and percussive onsets vertical lines. Median filters along time and along frequency keep one orientation each (Fitzgerald 2010): \[\tilde H = \operatorname{med}_{\text{time}} |X|, \quad \tilde P = \operatorname{med}_{\text{freq}} |X|, \quad M_H = \frac{\tilde H^2}{\tilde H^2 + \tilde P^2}\] The harmonic part \(M_H X\) is stretched with the phase vocoder, the percussive part \((1 - M_H) X\) with OLA of short frames, and the results are added (Driedger, Müller & Ewert 2014).
| Guitar and drums | |
|---|---|
| mixture | |
| harmonic part | |
| percussive part |
The source-filter model writes the magnitude spectrum as \(|X(f)| = E(f)\,|S(f)|\). The cepstrum separates the slowly varying envelope \(\log E\) from the harmonic fine structure: \[c[q] = \mathrm{IDFT}\{\log|X|\}, \qquad \log E = \mathrm{DFT}\{c[q]\,\ell[q]\}\] The lifter \(\ell[q]\) is 1 for \(|q| < q_c\) and 0 otherwise, with \(q_c\) smaller than the pitch period (here \(q_c = 40\) samples, period 218 samples).
After the pitch shift, the original envelope is restored frame by frame: \[Y'(m,k) = Y(m,k)\;\frac{E_x(m,k)}{E_y(m,k)}\]

| A fourth up | |
|---|---|
| phase vocoder | |
| envelope restored |
With hop \(H < N\), the STFT has \(N/H\) times more values than the signal. The examples here use \(N = 2048\) and \(H = 256\), a factor of 8. A modified array \(Y(m,k)\) with changed magnitudes or phases is in general not the STFT of any signal.
Griffin & Lim (1984) showed that the signal whose STFT is closest to \(Y\) in the least-squares sense is the weighted overlap-add of the inverse DFTs \(y_m\): \[\hat y[n] = \frac{\sum_m w[n - mH]\; y_m[n - mH]}{\sum_m w^2[n - mH]}\]
Consistency
An array \(Y\) is consistent if \(\mathrm{STFT}\big(\mathrm{ISTFT}(Y)\big) = Y\), where the inverse STFT (ISTFT) is the resynthesis formula on the left.
A phase vocoder produces a modified array. Resynthesis with the formula on the left yields the nearest consistent array.
Only \(|X(m,k)|\) is kept, and a phase is chosen:
| original | |
| zero phase | |
| random phase | |
| Griffin–Lim, 100 iterations | |
| fast Griffin–Lim, 100 iterations |
Griffin–Lim algorithm (1984)
Alternate two steps: replace the magnitude of the current STFT by the target magnitude, then resynthesise and compute the STFT again. The magnitude error does not increase from one iteration to the next.
Spectral error \(\| |X| - |\hat X| \| / \| |X| \|\): random phase 0.74, Griffin–Lim 0.087, fast Griffin–Lim 0.022.
Many signals have nearly the same STFT magnitude, and Griffin–Lim returns one of them. Its phase differs from the original, which is audible as a slightly metallic sound.
The algorithm alternates between two sets of arrays:
Each iteration computes \(Y \leftarrow P_{\mathcal A} P_{\mathcal C}\, Y\). Since \(\mathcal A\) is not convex, the result depends on the initial phase.
Fast Griffin–Lim (Perraudin, Balazs & Søndergaard 2013) adds a momentum term: \[t_i = P_{\mathcal A} P_{\mathcal C}\, c_{i-1}, \qquad c_i = t_i + \gamma\,(t_i - t_{i-1})\]

Speech example, same random initial phase: error 0.087 (Griffin–Lim) and 0.022 (fast Griffin–Lim, \(\gamma = 0.99\)) after 100 iterations.
Text-to-speech systems such as Tacotron 2 (2018) and FastSpeech (2019) generate a mel spectrogram, a magnitude spectrogram on a coarse, perceptually spaced frequency axis. A neural vocoder converts it into a waveform:
\[M(m, b) \;\longrightarrow\; f_\theta(M) \;\longrightarrow\; x[n]\]
The network \(f_\theta\) is trained to produce a waveform that is likely to have the spectrogram \(M\). Examples are autoregressive (WaveNet, 2016), adversarial (HiFi-GAN, 2020) and diffusion models (lecture 12).
| Channel vocoder (lecture 7) | Neural vocoder | |
|---|---|---|
| representation | band envelopes | mel spectrogram |
| generator | carrier and filters | learned network |
| missing information | set by design | learned from data |
Question for every representation
Which information does it keep, and which must the generator infer?
| Sampling | Granular | Phase vocoder | |
|---|---|---|---|
| Representation | the recording | windowed grains | STFT magnitude and phase |
| Controls | note, loop, rate | position, rate, density, duration, jitter | time and pitch independently |
| Built-in structure | read pointer and interpolator | overlap-add (COLA) | one sinusoid per bin, with estimated frequency |
| Fixed vs. learned | recorded | recorded, rearranged | analysed; magnitude-only versions are completed by neural vocoders |
Recording as a signal
All methods read the bandlimited signal behind the samples at new instants. Faster reading scales time and frequency together, interpolation acts as a low-pass filter, and a loop is a periodic signal.
Separating time and pitch
Windowed grains make read position and pitch independent. WSOLA and PSOLA align grains in time, the phase vocoder corrects the phase of each bin, and formant preservation restores the spectral envelope.
Magnitude-only representations
They discard the phase. Griffin–Lim finds a consistent phase; neural vocoders learn a plausible one.
Examples in this deck: slides/make_samples_media.py (phase vocoder, phase locking, WSOLA, PSOLA, Griffin–Lim and the mosaic in NumPy/SciPy). Interpolation examples: slides/make_synthesis_media.py. Guitar, speech and drum recordings from the pyFDN audio set.