---
title: The cepstrum and homomorphic processing
heading: Taking the logarithm of a spectrum to separate things that were convolved together
eyebrow: S7 · Lesson 41 · The cepstrum and homomorphic processing
xp: 50
---

::: hook
Linear filters separate signals that were added. Many signals are combined by convolution instead: a voice is a pulse train from the vocal cords convolved with the throat's resonances; a recording is a sound convolved with a room's echoes. In the frequency domain convolution becomes multiplication, and a logarithm turns multiplication into addition. Take the inverse transform of the log spectrum and the convolved parts land in different places along a new axis, called **quefrency**. The result, punningly named the **cepstrum** in 1963, finds echoes, measures the pitch of a voice, and underlies the mel-frequency cepstral coefficients that speech recognizers used for decades. Lesson 13's minimum-phase systems fall out of it too.
:::

::: play
- The log-magnitude spectrum of a signal with an echo of strength $a$ and delay $D$ samples: $\log|1 + ae^{-j\omega D}|$, a ripple that repeats every $2\pi/D$ in frequency.
- A ripple that's periodic in frequency is a single "frequency" of its own, and its inverse transform puts a peak at quefrency $D$. Change the echo's delay: the ripple tightens and the peak moves out.
:::

```sim plot
view: graph
x: w = 0..3.1416
y: L = -2..1
slider: a = 0.1..0.95 step 0.05 = 0.5
slider: D = 2..20 step 1 = 8
plot: 0.5*ln(1 + a^2 + 2*a*cos(D*w)) | samples: 1500 | label: log magnitude of the echo
readout: ripple period in rad/sample = 2*pi/D
readout: first cepstral peak (complex cepstrum) = a
```

::: predict
## A recording sampled at 8 kHz contains an echo delayed by 25 ms. At what quefrency index will its cepstrum show a peak?

- [ ] 25
- [ ] 40
- [x] 200
- [ ] 2000

::: reveal
25 ms at 8000 samples per second is 200 samples, and the echo's log-spectrum ripple repeats every $2\pi/200$ radians per sample, so its cepstrum peaks at quefrency 200 (and smaller peaks at 400, 600, …). Quefrency is measured in samples, or seconds: it's a time.
:::
:::

::: nameit
## The cepstrum

The **real cepstrum** of $x[n]$ is the inverse DFT of its log magnitude spectrum:

$$c[n] = \text{IDFT}\{\ln|X(k)|\}$$

The **complex cepstrum** uses the complex logarithm (log magnitude plus unwrapped phase) and can be inverted. If $x = s * h$, then $\ln X = \ln S + \ln H$, and the cepstrum is the sum of the two signals' cepstra: convolution has become addition.

## Echoes

For an echo, $H(z) = 1 + az^{-D}$ with $|a| < 1$:

$$\ln(1 + az^{-D}) = az^{-D} - \frac{a^2}{2}z^{-2D} + \frac{a^3}{3}z^{-3D} - \cdots$$

so the complex cepstrum has impulses at $D, 2D, 3D, \ldots$ with amplitudes $a, -a^2/2, a^3/3, \ldots$ (the "rahmonics"). The real cepstrum is the even part, with half those amplitudes at $\pm D, \pm 2D, \ldots$

## Pitch and envelope

Voiced speech is a pulse train at the pitch period $T_0$ convolved with the vocal tract's slowly varying response. In the cepstrum, the vocal tract occupies low quefrencies (a few milliseconds), while the pitch shows as a peak at $T_0$ and its multiples. Keeping only the low quefrencies (**liftering**) smooths the spectrum into the vocal-tract envelope; finding the peak above them gives the pitch.

## Minimum phase

A system is minimum phase exactly when its complex cepstrum is zero for $n < 0$. Folding a real cepstrum onto positive quefrencies ($\hat c[0] = c[0]$, $\hat c[n] = 2c[n]$ for $n > 0$, zero for $n < 0$) gives the minimum-phase system with the same magnitude, the companion of lesson 13's all-pass decomposition.
:::

::: example
## An echo of strength 0.5 is delayed 200 samples. List the first three complex-cepstrum impulses and the real cepstrum's value at quefrency 200.

::: solution
1. **Complex cepstrum.** The impulses fall at multiples of the delay:
   $$\begin{aligned} \hat x[200] &= a \\ &= 0.5 \\ \hat x[400] &= -a^2/2 \\ &= -0.125 \\ \hat x[600] &= a^3/3 \\ &= 0.0417 \end{aligned}$$
2. **Real cepstrum.** The real cepstrum is the even part, so at ±200 it's 0.25.
3. **In a recording.** A measured cepstrum of a noisy recording shows a peak near 0.24 there, on top of the source's own cepstrum.

**Answer:** 0.5 at 200, −0.125 at 400 and 0.0417 at 600; the real cepstrum is 0.25 at quefrency 200.
:::
:::

::: example
## A voice has a pitch of 125 Hz and is sampled at 16 kHz. Where does its cepstral pitch peak appear, and how many low-quefrency coefficients should a lifter keep to capture the vocal-tract envelope without the pitch?

::: solution
1. **Pitch peak.** The pitch period is 8 ms, 128 samples, so the peak is at quefrency 128.
2. **Lifter.** The vocal tract's features vary over a few milliseconds; keeping the first 30 or so coefficients (under 2 ms) captures the envelope and stays well below 128.
3. **Higher voices.** For a 300 Hz child's voice the peak moves to 53, so the lifter must stay below the shortest pitch period expected, which is set by the highest pitch.

**Answer:** the pitch peak is at quefrency 128, and a lifter keeping about the first 30 coefficients captures the envelope.
:::
:::

::: example
## Folding a real cepstrum $c[n]$ that is 0.1 at $n$ = ±1 and zero elsewhere except $c[0]$ = 0: what minimum-phase system results, to first order?

::: solution
1. **Fold.**
   $$\begin{aligned} \hat c[1] &= 2 \times 0.1 \\ &= 0.2 \end{aligned}$$
   and zero for other $n > 0$.
2. **Match to a system.** Since $\ln(1 + bz^{-1}) = bz^{-1} - \ldots$, a cepstrum of 0.2 at $n$ = 1 is, to first order, the system
   $$1 + 0.2z^{-1}$$
3. **Check.** Its zero at −0.2 is inside the unit circle: minimum phase, as folding guarantees.

**Answer:** to first order, $1 + 0.2z^{-1}$.
:::
:::

::: check
given: t = 5 | 12.5 | 25 | 40; fs = 8 | 16 | 44.1
answer: t*1e-3*fs*1e3
tolerance: 0.5%

## An echo arrives {t} ms after the direct sound in a recording sampled at {fs} kHz. At what quefrency index does its first cepstral peak appear?

::: right
Right. The delay in samples, {answer}.
:::

::: misses
- [t] Convert the delay to samples.
- [fs*1e3/(t*1e-3)] Quefrency is a time: delay × sample rate.
- [any] Not yet. $D = t \cdot f_s$.
:::
:::

::: check
given: a = 0.3 | 0.5 | 0.8; k = 2 | 4
answer: (-1)^(k + 1)*a^k/k
tolerance: 0.5%

## An echo of strength {a} gives complex-cepstrum impulses at multiples of its delay. What is the amplitude of impulse number {k}?

::: right
Right. $(-1)^{k+1}a^k/k$ = {answer}, from the series for $\ln(1 + x)$.
:::

::: misses
- [a^k] Divide by k: the log series is $x - x^2/2 + x^3/3 - \ldots$
- [a^k/k] Alternate the signs.
- [any] Not yet. Expand $\ln(1 + az^{-D})$.
:::
:::

::: check
given: f0 = 80 | 125 | 220 | 300; fs = 8 | 16
answer: fs*1e3/f0
tolerance: 0.5%

## A voice with pitch {f0} Hz is sampled at {fs} kHz. At what quefrency index is its cepstral pitch peak?

::: right
Right. The pitch period in samples: {answer}.
:::

::: misses
- [f0/(fs*1e3)] The peak is at the period, $f_s/f_0$.
- [any] Not yet. $f_s/f_0$.
:::
:::

::: check
given: a = 0.2 | 0.5 | 0.9
answer: a/2
tolerance: 0.5%

## An echo of strength {a}: what value does the real cepstrum show at the echo's delay?

::: right
Right. The real cepstrum is the even part of the complex one: $a/2$ = {answer}.
:::

::: misses
- [a] That's the complex cepstrum. The real cepstrum splits it between ±D.
- [any] Not yet. Half the complex cepstrum's $a$.
:::
:::

::: check
kind: choice

## Why can't an ordinary linear filter separate a voice's pitch pulses from its vocal-tract resonances, while the cepstrum can?

- [x] They're combined by convolution, which a linear filter can't undo by frequency selection; the log spectrum turns it into addition, where they occupy different quefrencies
- [ ] The pitch and resonances are at the same frequency
- [ ] Linear filters can't handle speech's bandwidth
- [ ] The cepstrum uses a longer window

::: right
Right. Linear filters separate sums by frequency. The logarithm turns the product $S(\omega)H(\omega)$ into a sum, and the inverse transform sorts its parts by how fast they vary in frequency: quefrency.
:::

::: misses
- [2] They overlap in frequency, which is exactly why a filter can't separate them; that's not the reason the cepstrum can.
- [3] Bandwidth is not the obstacle.
- [4] Window length isn't what separates them.
:::
:::

::: check
kind: derivation
vars: a = 0.05..0.9

## Expand the log of an echo, $\ln(1 + ax)$ with $x = z^{-D}$.

::: step
answer: a

### What is the coefficient of $x$?
:::

::: step
answer: -a^2/2

### And of $x^2$?
:::

::: right
Right. $\ln(1 + ax) = ax - a^2x^2/2 + a^3x^3/3 - \cdots$, which converges because $|a| < 1$: a minimum-phase echo has a causal cepstrum.
:::
:::

::: check
kind: python
given: D = 100 | 200 | 333; a = 0.3 | 0.6; seed = 1 | 2

## Detect an echo with the cepstrum. Make `s = numpy.random.default_rng(seed).standard_normal(8192)` and `x = s` plus `a` = {a} times `s` delayed by `D` = {D} samples (zeros before the start). Compute the real cepstrum `c = numpy.fft.ifft(numpy.log(numpy.abs(numpy.fft.fft(x)))).real`, and set `D_est`, the index of the largest value of `c` between 20 and 4096, and `peak`, the value there.

```python starter
import numpy as np
D_est = 0
peak = 0.0
```

```python solution
import numpy as np
s = np.random.default_rng(seed).standard_normal(8192)
x = s.copy()
x[D:] += a * s[:-D]
c = np.fft.ifft(np.log(np.abs(np.fft.fft(x)))).real
D_est = int(np.argmax(c[20:4096]) + 20)
peak = float(c[D_est])
```

```python tests
import numpy as np
assert D_est == D, f"The cepstral peak should sit at the delay, {D} samples."
assert abs(peak - a / 2) < 0.06, "Its height should be near a/2."
```

::: right
Right. The echo stands out at its delay even though the source is random noise, because the noise's own log spectrum is spread thinly over all quefrencies.
:::
:::

::: explain
## A speech recognizer computes mel-frequency cepstral coefficients and keeps only the first dozen. Explain why keeping low quefrencies captures what was said while ignoring who said it at what pitch.

::: model
A voiced sound is a pitch pulse train convolved with the vocal tract's response, and the vocal tract's shape, which determines the vowel or consonant, sets the spectrum's broad envelope. In the cepstrum, convolution becomes addition: the slowly varying envelope lands at low quefrencies, while the pitch, a fine ripple every $f_0$ hertz in the spectrum, lands at a high quefrency, the pitch period. Keeping only the first dozen coefficients keeps the envelope, the information about which sound was made, and drops the pitch peak, which depends on the speaker and intonation. The mel warping before the cepstrum spaces frequencies the way hearing does, so the coefficients emphasize the differences listeners use.
:::
:::

::: practice

::: check
given: t = 2 | 30; fs = 22.05 | 48
answer: t*1e-3*fs*1e3
tolerance: 0.5%

## At what quefrency index does a {t} ms echo appear at {fs} kHz?

::: misses
- [t] Convert to samples.
- [any] $t \cdot f_s$.
:::
:::

::: check
given: a = 0.4 | 0.7; k = 2 | 4
answer: (-1)^(k + 1)*a^k/k
tolerance: 0.5%

## What is rahmonic {k} of an echo with strength {a}?

::: misses
- [a^k/k] Mind the alternating sign.
- [any] $(-1)^{k+1}a^k/k$.
:::
:::
:::
