---
title: "Adaptive filters: LMS and echo cancellation"
heading: Filters that tune their own taps to minimize an error, the least-mean-squares update and its step size, convergence speed against stability and excess noise, normalized LMS, and cancelling the echo in a speakerphone call
eyebrow: D5 · Lesson 10 · Adaptive filters
xp: 40
---

::: hook
On a speakerphone call, the far talker's voice comes out of your loudspeaker, bounces around your room and goes back into your microphone, so they hear themselves a fraction of a second later. The echo path depends on the room, where you're sitting and who walks past, so no fixed filter can remove it. An **adaptive filter** learns it: it predicts the echo from the loudspeaker signal, subtracts the prediction from the microphone, and nudges its taps after every sample to make what's left smaller. The algorithm that does this, Widrow and Hoff's least-mean-squares rule from 1960, is three lines of code and runs in every phone.
:::

::: play
- The simplest case: an LMS filter with one weight learning an unknown gain from an input of power P. Each step multiplies the weight's error by $(1 - \mu P)$.
- Small steps converge slowly and smoothly; steps with μP between 1 and 2 overshoot and alternate; above 2 the error grows and the filter diverges.
- The readout gives the convergence time constant. Real filters with many taps trade this speed against the noise the fast updates leave behind.
:::

```sim plot
view: graph
x: n = 0..40
y: e = -1.5..1.5
slider: a = 0.02..2.2 step 0.02 = 0.2
plot: Re((1 - a)^n) | stems | label: weight error, sample by sample (a = μP)
readout: error multiplier per step = 1 - a | digits: 3
readout: time constant, samples = -1/ln(abs(1 - a) + 1e-9) | digits: 3
```

::: predict
## An echo canceller samples at 8 kHz and must cover echoes up to 64 ms after the loudspeaker sound. How many taps does its adaptive filter need?

- [ ] 64
- [ ] 125
- [x] 512
- [ ] 8000

::: reveal
The filter must reach 64 ms into the past: 0.064 × 8000 = 512 samples, so 512 taps. Each sample takes 512 multiply-accumulates to compute the echo estimate and 512 more to update the taps, about 8.2 million per second.
:::
:::

::: nameit
## The LMS algorithm

An FIR filter with taps $\mathbf w$ sees the recent inputs $\mathbf x_n$ and produces $y_n = \mathbf w^T\mathbf x_n$. With a desired signal $d_n$ (the microphone, in an echo canceller), the error is $e_n = d_n - y_n$ and each step moves the taps downhill on the squared error:

$$\mathbf w_{n+1} = \mathbf w_n + \mu e_n\mathbf x_n$$

## Step size

For a single weight with input power P, the weight's error shrinks by $(1 - \mu P)$ each step, so it converges if $0 < \mu < 2/P$, with a time constant of $-1/\ln|1 - \mu P|$ steps. With N taps and white input, a safe bound is roughly $\mu < 2/(NP)$, and smaller in practice. A larger μ converges faster but keeps jittering around the best taps, leaving an excess error, the **misadjustment**.

## Normalized LMS

Dividing the step by the input's current energy, $\mu = \tilde\mu/(\epsilon + \mathbf x_n^T\mathbf x_n)$ with $0 < \tilde\mu < 2$, makes the behavior independent of the signal's loudness, which matters for speech. For white input it converges with a time constant of about $N/(\tilde\mu(2 - \tilde\mu))$ samples and leaves a misadjustment of about $\tilde\mu/(2 - \tilde\mu)$.

## Echo cancellation

The loudspeaker signal is the filter's input, the microphone is d, and the error e is what's sent back to the far talker, with the echo removed. A **double-talk detector** freezes adaptation while the near talker speaks, so that their voice isn't mistaken for echo.
:::

::: example
## The 512-tap canceller uses NLMS with $\tilde\mu$ = 0.5. How long does it take to converge, and what misadjustment does it leave?

::: solution
1. **Convergence in samples.**
   $$\begin{aligned} \frac{N}{\tilde\mu(2 - \tilde\mu)} &= \frac{512}{0.75} \\ &= 683\ \text{samples} \end{aligned}$$
2. **In time.** At 8 kHz:
   $$\frac{683}{8000\ \text{Hz}} = 85\ \text{ms}$$
   Speech is far from white, so real convergence is slower.
3. **Misadjustment.**
   $$\begin{aligned} \frac{\tilde\mu}{2 - \tilde\mu} &= \frac{0.5}{1.5} \\ &= \frac{1}{3} \end{aligned}$$
   The leftover error power is a third more than the best possible.

**Answer:** about 683 samples (85 ms) to converge, with a misadjustment of 1/3.
:::
:::

::: example
## A single-weight LMS has input power 0.25. What's the largest stable step, and what's the time constant at μP = 0.1?

::: solution
1. **Largest stable step.**
   $$\begin{aligned} \mu &< \frac{2}{0.25} \\ &= 8 \end{aligned}$$
2. **Time constant.** At μP = 0.1 the error shrinks by 0.9 per step:
   $$-\frac{1}{\ln 0.9} = 9.5\ \text{steps}$$

**Answer:** μ must stay below 8, and at μP = 0.1 the time constant is 9.5 steps.
:::
:::

::: example
## An echo canceller at 8 kHz must cover 64 ms of echo. How many taps does it need, and what does LMS cost per second?

::: solution
1. **Taps.**
   $$0.064\ \text{s} \times 8000\ \text{Hz} = 512$$
2. **Cost.** LMS spends about two multiply-adds per tap per sample, one for the filter and one for the update:
   $$\begin{aligned} &2 \times 512 \times 8000 \\ &= 8.2 \times 10^6\ \text{per second} \end{aligned}$$
3. **In context.** That's an easy load for a modern DSP but the reason early cancellers used shorter tails.

**Answer:** 512 taps, and about 8.2 million multiply-adds a second.
:::
:::

::: check
given: T = 16 | 64 | 128; s = 8 | 16
answer: T*s
tolerance: 0.5%

## An echo canceller at {s} kHz must cover echoes up to {T} ms. How many taps does it need?

::: right
Right. $Tf_s$ = {answer} taps.
:::

::: misses
- [any] Not yet. The echo's length in samples.
:::
:::

::: check
given: P = 0.1 | 0.5 | 2
answer: 2/P
tolerance: 0.5%

## A single-weight LMS filter has input power {P}. What's the largest step size μ that still converges?

::: right
Right. $2/P$ = {answer}.
:::

::: misses
- [1/P] That's the fastest, one-step convergence; it stays stable up to twice that.
- [any] Not yet. $|1 - \mu P| < 1$.
:::
:::

::: check
given: a = 0.05 | 0.1 | 0.3 | 0.5
answer: -1/ln(1 - a)
tolerance: 0.5%

## A single-weight LMS has μP = {a}. What's its convergence time constant in steps?

::: right
Right. $-1/\ln(1 - \mu P)$ = {answer} steps.
:::

::: misses
- [1/a] That's the small-step approximation; use the logarithm.
- [any] Not yet. Solve $(1 - \mu P)^n = 1/e$.
:::
:::

::: check
given: N = 128 | 512 | 1024; m = 0.25 | 0.5 | 1.5; s = 8 | 16
answer: N/(m*(2 - m))/s
unit: ms
tolerance: 0.5%

## A {N}-tap NLMS filter at {s} kHz uses $\tilde\mu$ = {m}. About how long does it take to converge on white input?

::: right
Right. $N/(\tilde\mu(2 - \tilde\mu))$ samples = {answer} ms.
:::

::: misses
- [N/m/s] Use $\tilde\mu(2 - \tilde\mu)$ in the denominator.
- [any] Not yet. Convert the time constant in samples to time.
:::
:::

::: check
kind: parts
given: t = 32 | 64 | 128; fs = 8 | 16

## An echo canceller at {fs} kHz must cover {t} ms of echo tail.

::: part
answer: t*fs
unit: taps
tolerance: 0.5%

### How many taps does it need?

::: misses
- [any] Tail × rate.
:::
:::

::: part
answer: 2*t*fs*fs*1000/1e6
tolerance: 0.5%

### What does LMS cost, in millions of multiply-adds a second, at two per tap per sample?

::: misses
- [any] 2 × taps × rate.
:::
:::
:::

::: check
kind: parts
given: P = 0.1 | 0.25 | 1; m = 0.05 | 0.1 | 0.2

## A single-weight LMS has input power {P} and step μ with μP = {m}.

::: part
answer: 2/P
tolerance: 0.5%

### What is the largest stable step?

::: misses
- [any] $\mu < 2/P$.
:::
:::

::: part
answer: -1/ln(1 - 2*m)
unit: iterations
tolerance: 0.5%

### What is its time constant (the error shrinks by $1 - 2\mu P$ per step)?

::: misses
- [any] $-1/\ln(1 - 2\mu P)$.
:::
:::
:::

::: explain
## Why does a larger LMS step size make the filter converge faster but end up noisier?

::: model
Each LMS update moves the taps in the direction that would reduce the current error, by an amount proportional to the step size and to that error. A bigger step covers more distance toward the best taps on each update, so the filter converges in fewer samples. But each update is based on a single noisy sample of the error, so even when the taps are already right the filter keeps making corrections in response to noise and signal fluctuations, and a bigger step makes those random corrections bigger. The taps then jitter around the best solution, leaving extra error, and if the step is too large the corrections overshoot so much that the filter diverges.
:::
:::

::: practice

::: check
given: T = 1 | 500; s = 8 | 48
answer: T*s
tolerance: 0.5%

## Taps for {T} ms at {s} kHz?

::: misses
- [any] $Tf_s$.
:::
:::

::: check
given: m = 0.05 | 0.1 | 0.25 | 0.5 | 1 | 1.5 | 1.9
answer: m/(2 - m)
tolerance: 0.5%

## NLMS misadjustment at $\tilde\mu$ = {m}?

::: misses
- [any] $\tilde\mu/(2 - \tilde\mu)$.
:::
:::
:::
