Algorithm Search⌘K

Algorithmadvanced

Whitened matched filter

Also called Forney whitened matched filter.

A matched filter followed by a noise-whitening filter. It turns an ISI channel into a causal minimum-phase channel f with white noise, the front end of the Viterbi equalizer and the DFE.

Time
O(T L^2 + K L) worst
Space
O(K + L) extra
Where
K = the number of samples; L = the number of channel taps; T = the number of root-finding iterations
Properties
deterministiclinearneeds channeloutput minimum phasepreserves ml metric
Origin
G. David Forney Jr., 1972

The whitened matched filter (WMF) is the front end that Forney proposed for sequence detection on channels with intersymbol interference. It is a matched filter followed by a noise-whitening filter. The matched filter alone keeps all the information about the symbols, but its output noise is colored, with the same spectrum as the folded channel R(z)=H(z)H∗(1/z∗)R(z) = H(z) H^*(1/z^*). The whitening filter 1/F∗(1/z∗)1/F^*(1/z^*) removes the color, where FF is the minimum-phase spectral factor of RR. The output is y′=f∗x+n′y' = f * x + n' with white noise of the same variance. So the receiver sees a new causal channel ff of the same length, with its energy as early as possible. The Viterbi equalizer runs on ff with a Euclidean metric. The Ungerboeck receiver uses the matched filter output directly and needs no whitening. The decision feedback equalizer and reduced-state sequence estimation work best on the minimum-phase channel, because they decide the first taps first.

In plain words

A matched filter collects the energy of each symbol, but it makes neighbouring noise samples correlated. Many detectors assume the noise samples are independent. The whitening filter undoes the correlation without losing any information, and the noise becomes independent again. The price is that the channel the detector sees is a new one. Among all channels with the same frequency response magnitude, it is the one whose energy comes earliest. That is good for detectors that decide one symbol at a time, because the first tap carries the most signal.

The problem

The input is a channel h=(h0,…,hL−1)h = (h_0, \ldots, h_{L-1}) known at the receiver and the received samples y[k]=∑lhl x[k−l]+w[k]y[k] = \sum_l h_l\, x[k-l] + w[k], with ww white CN(0,N0)\mathcal{CN}(0, N_0). The output is a channel f=(f0,…,fL−1)f = (f_0, \ldots, f_{L-1}) with f0>0f_0 > 0, all zeros of F(z)F(z) in the closed unit disk and ∣F(ejω)∣=∣H(ejω)∣|F(e^{j\omega})| = |H(e^{j\omega})|, and filtered samples y′[k]=∑lfl x[k−l]+n′[k]y'[k] = \sum_l f_l\, x[k-l] + n'[k] with n′n' white CN(0,N0)\mathcal{CN}(0, N_0). The two models give the same maximum likelihood decisions.

The algorithm

The folded spectrum R(z)=∑kRkz−kR(z) = \sum_k R_k z^{-k}, with Rk=∑lhl+khˉlR_k = \sum_l h_{l+k} \bar h_l, has the zeros of H(z)H(z) and their conjugate reciprocals. A spectral factor of RR keeps one zero of each pair. The minimum-phase factor keeps the one inside the unit circle. When hh is known, the zeros of HH give the factor directly.

  1. WHITENED-MATCHED-FILTER(h, y)
  2. 1f ← MIN-PHASE-FACTOR(h)
  3. 2ρ_1, …, ρ_{L-1} ← the roots of h0 z^(L-1) + h1 z^(L-2) + … + h_{L-1}
  4. 3ρ'_i ← ρ_i if |ρ_i| ≤ 1, else 1/conj(ρ_i) ▷ reflect into the unit disk
  5. 4f ← γ Π_i (1 - ρ'_i z⁻¹), γ = |h0| Π_{|ρ_i|>1} |ρ_i|
  6. 5z[k] ← Σ_l conj(h[l]) y[k + l] for every k ▷ the matched filter
  7. 6for k ← K - 1 down to 0 ▷ the whitening filter 1/F*(1/z*)
  8. 7y'[k] ← (z[k] - Σ_{l≥1} conj(f[l]) y'[k + l]) / conj(f[0])
  9. 8return f, y'

Line L2 finds the zeros with the Durand-Kerner method. Line L3 keeps the magnitude response: a zero ρ\rho outside the circle and the zero 1/ρˉ1/\bar\rho give the same ∣1−ρz−1∣|1 - \rho z^{-1}| up to the factor ∣ρ∣|\rho|, which line L4 puts into γ\gamma. Line L5 is the matched filter, which correlates yy with hh. Lines L6 and L7 apply 1/F∗(1/z∗)1/F^*(1/z^*), an anticausal filter, by back substitution from the end of the block. If only RR is known, factor zL−1R(z)z^{L-1} R(z) instead and keep the L−1L-1 roots inside the circle.

Example

Take h=(0.5,1)h = (0.5, 1), a channel whose second tap is larger than its first. Its zero is at −2-2, outside the unit circle. Line L3 reflects it to −0.5-0.5, and line L4 gives γ=0.5×2=1\gamma = 0.5 \times 2 = 1, so f=(1,0.5)f = (1, 0.5). Both channels have R0=1.25R_0 = 1.25 and R1=0.5R_1 = 0.5. Send x=(1,−1,−1,1,1)x = (1, -1, -1, 1, 1) with no noise:

kk 0 1 2 3 4 5
y[k]=0.5 x[k]+x[k−1]y[k] = 0.5\, x[k] + x[k-1] 0.5 0.5 -1.5 -0.5 1.5 1
z[k]=0.5 y[k]+y[k+1]z[k] = 0.5\, y[k] + y[k+1] 0.75 -1.25 -1.25 1.25 1.75 0.5
y′[k]=z[k]−0.5 y′[k+1]y'[k] = z[k] - 0.5\, y'[k+1] 1 -0.5 -1.5 0.5 1.5 0.5
(f∗x)[k]=x[k]+0.5 x[k−1](f * x)[k] = x[k] + 0.5\, x[k-1] 1 -0.5 -1.5 0.5 1.5 0.5

The back substitution starts at k=5k = 5 with y′[5]=z[5]=0.5y'[5] = z[5] = 0.5. The output is f∗xf * x exactly. On the original channel, a zero-forcing DFE with one feedforward tap has the noise gain 1/∣h0∣2=41/|h_0|^2 = 4. On the whitened channel it has 1/f02=11/f_0^2 = 1, which is 6 dB better.

On the channel B of Proakis, h=(0.407,0.815,0.407)h = (0.407, 0.815, 0.407), the zeros are at −0.9516-0.9516 and −1.0508-1.0508. The second one is reflected to −0.9516-0.9516, and f=(0.42768,0.81400,0.38732)f = (0.42768, 0.81400, 0.38732). The change is small, because the channel is almost linear phase with both zeros near the circle.

Correctness

Claim. ∣F∣=∣H∣|F| = |H| on the unit circle and ff is minimum phase. Without noise, y′=f∗xy' = f * x exactly. For any noise and any candidate sequence xx, ∥y−h∗x∥2−∥y′−f∗x∥2=∥y∥2−∥y′∥2\|y - h * x\|^2 - \|y' - f * x\|^2 = \|y\|^2 - \|y'\|^2, so maximum likelihood on y′y' with the channel ff gives the same decisions as on yy with hh. On an infinite sequence, the noise after the filter is white with variance N0N_0.

Key step. Equal magnitudes give equal autocorrelations, so HHH=FHF\mathbf{H}^H \mathbf{H} = \mathbf{F}^H \mathbf{F} for the full convolution matrices. The back substitution solves FsHy′=HsHy\mathbf{F}_s^H y' = \mathbf{H}_s^H y, so the cross terms of the two metrics agree too. The noise passes through H∗(1/z∗)/F∗(1/z∗)H^*(1/z^*)/F^*(1/z^*), which is allpass. The proofs folder has the details, and also the proof that ff has the largest partial energies among all factors.

Proof: The whitened matched filter gives the minimum-phase channel with white noise, and maximum likelihood on its output equals maximum likelihood on the received samplesfull

The channel taps are h0,…,hL−1h_0, \ldots, h_{L-1} with h0≠0h_0 \ne 0 and hL−1≠0h_{L-1} \ne 0, and H(z)=∑lhlz−lH(z) = \sum_l h_l z^{-l}. The received block is y=Hx+wy = \mathbf{H} x + w, where x=(x0,…,xN−1)x = (x_0, \ldots, x_{N-1}), H\mathbf{H} is the (N+L−1)×N(N+L-1) \times N full convolution matrix with Hkj=hk−j\mathbf{H}_{kj} = h_{k-j}, and w∼CN(0,N0I)w \sim \mathcal{CN}(0, N_0 I). Write n=N+L−1n = N + L - 1.

Lemma 1 (the factor). Write H(z)=h0∏i=1L−1(1−ρiz−1)H(z) = h_0 \prod_{i=1}^{L-1} (1 - \rho_i z^{-1}), where ρi\rho_i are the roots of h0zL−1+⋯+hL−1h_0 z^{L-1} + \cdots + h_{L-1} (line L2). Let ρi′=ρi\rho'_i = \rho_i if ∣ρi∣≤1|\rho_i| \le 1 and ρi′=1/ρˉi\rho'_i = 1/\bar\rho_i otherwise (line L3), and F(z)=γ∏i(1−ρi′z−1)F(z) = \gamma \prod_i (1 - \rho'_i z^{-1}) with γ=∣h0∣∏∣ρi∣>1∣ρi∣\gamma = |h_0| \prod_{|\rho_i| > 1} |\rho_i| (line L4). Then ∣F(ejω)∣=∣H(ejω)∣|F(e^{j\omega})| = |H(e^{j\omega})| for every ω\omega, all zeros of FF are in the closed unit disk, and the autocorrelations of ff and hh are equal: Rf[k]=Rh[k]R_f[k] = R_h[k] for every kk.

Proof. For ∣ρ∣>1|\rho| > 1 and ∣z∣=1|z| = 1, ∣1−ρz−1∣=∣ρ∣⋅∣ρ−1−z−1∣=∣ρ∣⋅∣ρˉ−1−zˉ−1∣|1 - \rho z^{-1}| = |\rho| \cdot |\rho^{-1} - z^{-1}| = |\rho| \cdot |\bar\rho^{-1} - \bar z^{-1}|, and zˉ−1=z\bar z^{-1} = z on the unit circle, so this is ∣ρ∣⋅∣z∣⋅∣ρˉ−1z−1−1∣=∣ρ∣⋅∣1−ρˉ−1z−1∣|\rho| \cdot |z| \cdot |\bar\rho^{-1} z^{-1} - 1| = |\rho| \cdot |1 - \bar\rho^{-1} z^{-1}|. Multiplying over the factors gives ∣F∣=∣H∣|F| = |H| on the circle. The autocorrelation R[k]R[k] is the kk-th Fourier coefficient of ∣H(ejω)∣2|H(e^{j\omega})|^2, so equal magnitudes give equal autocorrelations. □\square

So R(z)=H(z)H∗(1/z∗)=F(z)F∗(1/z∗)R(z) = H(z) H^*(1/z^*) = F(z) F^*(1/z^*), which is the spectral factorization of the folded spectrum. In particular, ∥f∥2=R[0]=∥h∥2\|f\|^2 = R[0] = \|h\|^2.

Lemma 2 (the matrices). Let F\mathbf{F} be the n×Nn \times N full convolution matrix of ff. Then HHH=FHF\mathbf{H}^H \mathbf{H} = \mathbf{F}^H \mathbf{F}.

Proof. The entry (i,j)(i, j) of HHH\mathbf{H}^H \mathbf{H} is ∑khˉk−ihk−j\sum_k \bar h_{k-i} h_{k-j}, summed over all kk because the full convolution keeps every product. This is Rh[i−j]R_h[i - j] (with R[−k]=R[k]‾R[-k] = \overline{R[k]}), and the same holds for ff. Lemma 1 ends the proof. □\square

The filter. Let Hs\mathbf{H}_s and Fs\mathbf{F}_s be the n×nn \times n lower triangular Toeplitz matrices of hh and ff (the convolution truncated to nn outputs). Line L5 computes z=HsHyz = \mathbf{H}_s^H y, the matched filter, with the samples after the block taken as 0. Lines L6 and L7 solve FsHy′=z\mathbf{F}_s^H y' = z by back substitution, from k=n−1k = n - 1 down to 0. FsH\mathbf{F}_s^H is upper triangular with the diagonal fˉ0=γ>0\bar f_0 = \gamma > 0, so the solution exists and is unique.

Theorem 1 (noiseless output). If w=0w = 0, then y′=f∗xy' = f * x exactly.

Proof. Pad xx with zeros to length nn. Then Hsx=Hx=h∗x\mathbf{H}_s x = \mathbf{H} x = h * x and Fsx=f∗x\mathbf{F}_s x = f * x, because the full convolutions have length nn. We need FsHFsx=HsHHsx\mathbf{F}_s^H \mathbf{F}_s x = \mathbf{H}_s^H \mathbf{H}_s x. Row ii of the right side is ∑k=in−1hˉk−i(h∗x)k=∑jxj∑khˉk−ihk−j\sum_{k=i}^{n-1} \bar h_{k-i} (h * x)_k = \sum_j x_j \sum_{k} \bar h_{k-i} h_{k-j}. A product hˉk−ihk−j\bar h_{k-i} h_{k-j} is nonzero only for i≤k≤j+L−1≤n−1i \le k \le j + L - 1 \le n - 1, so the inner sum runs over all kk and equals Rh[i−j]R_h[i - j], the full autocorrelation. The same holds for ff, and Lemma 1 gives Rf=RhR_f = R_h. So FsH(f∗x)=z\mathbf{F}_s^H (f * x) = z, and uniqueness gives y′=f∗xy' = f * x. □\square

Theorem 2 (the same maximum likelihood decisions). For every yy and every candidate xx supported on the first NN entries,

∥y−h∗x∥2−∥y′−f∗x∥2=∥y∥2−∥y′∥2,\|y - h * x\|^2 - \|y' - f * x\|^2 = \|y\|^2 - \|y'\|^2,

which does not depend on xx. So the sequence that minimizes ∥y′−f∗x∥2\|y' - f * x\|^2 over any alphabet is the maximum likelihood sequence for yy.

Proof. Expand both norms. The cross terms are 2 Re(xHHsHy)2\,\mathrm{Re}(x^H \mathbf{H}_s^H y) and 2 Re(xHFsHy′)2\,\mathrm{Re}(x^H \mathbf{F}_s^H y'), and FsHy′=z=HsHy\mathbf{F}_s^H y' = z = \mathbf{H}_s^H y, so they are equal. The quadratic terms are xHHHHxx^H \mathbf{H}^H \mathbf{H} x and xHFHFxx^H \mathbf{F}^H \mathbf{F} x, equal by Lemma 2. □\square

Theorem 3 (white noise, for the infinite filter). On an infinite sequence, the noise after the filter is A(z)W(z)A(z) W(z) with A(z)=H∗(1/z∗)/F∗(1/z∗)A(z) = H^*(1/z^*) / F^*(1/z^*), and ∣A(ejω)∣=1|A(e^{j\omega})| = 1. So the output noise is white with variance N0N_0, and y′=f∗x+n′y' = f * x + n' with n′∼CN(0,N0)n' \sim \mathcal{CN}(0, N_0) i.i.d. If no zero of HH lies on the unit circle, AA is a stable anticausal filter.

Proof. The filter is 1/F∗(1/z∗)1/F^*(1/z^*) after H∗(1/z∗)H^*(1/z^*). On the unit circle ∣H∗(1/z∗)∣=∣H(z)∣|H^*(1/z^*)| = |H(z)| and the same for FF, so ∣A∣=1|A| = 1 by Lemma 1. A linear filter with ∣A∣=1|A| = 1 maps white noise of spectrum N0N_0 to white noise of spectrum N0N_0, and a linear map of a circular Gaussian process is circular Gaussian. The zeros of F∗(1/z∗)F^*(1/z^*) are at 1/ρˉi′1/\bar\rho'_i, which lie outside the unit disk when ∣ρi′∣<1|\rho'_i| < 1, so the anticausal inverse is stable. □\square

For a finite block the back substitution starts at the end of the block, so the last samples carry an edge effect, which decays at the rate of the largest ∣ρi′∣|\rho'_i| away from the end. Theorem 2 needs no whiteness: it holds exactly for the finite block.

Theorem 4 (minimum phase has the most energy first). Among all gg with ∣G∣=∣H∣|G| = |H| on the unit circle that are obtained by reflecting a subset of the zeros, the factor ff has the largest partial energy ∑l=0m∣gl∣2\sum_{l=0}^{m} |g_l|^2 for every mm.

Proof. Compare a factor with the zero ρ′\rho' (∣ρ′∣<1|\rho'| < 1) and the same factor with 1/ρˉ′1/\bar\rho', with the gain kept so that the magnitudes on the circle agree: G1(z)=C(z)(1−ρ′z−1)G_1(z) = C(z)(1 - \rho' z^{-1}) and G2(z)=C(z)(ρˉ′−z−1)G_2(z) = C(z)(\bar\rho' - z^{-1}) up to a unit factor. Write cc for the taps of CC, with cl=0c_l = 0 for l<0l < 0. Then (g1)l=cl−ρ′cl−1(g_1)_l = c_l - \rho' c_{l-1} and (g2)l=ρˉ′cl−cl−1(g_2)_l = \bar\rho' c_l - c_{l-1}, and

∑l≤m∣(g1)l∣2−∑l≤m∣(g2)l∣2=(1−∣ρ′∣2) ∣cm∣2≥0,\sum_{l \le m} |(g_1)_l|^2 - \sum_{l \le m} |(g_2)_l|^2 = (1 - |\rho'|^2)\, |c_m|^2 \ge 0,

by a direct expansion where the cross terms cancel and the terms with cl−1c_{l-1} telescope. Reflecting one zero at a time from any factor to ff never lowers a partial energy. □\square

Complexity

The roots cost O(TL2)O(T L^2) for TT iterations of Durand-Kerner on a polynomial of degree L−1L-1, and building ff costs O(L2)O(L^2). Lines L5 to L7 cost O(L)O(L) per sample, so KK samples cost O(TL2+KL)O(T L^2 + K L) in the real RAM, with O(K+L)O(K + L) extra space for zz and y′y'. The filter needs the whole block before it can start, because it runs backward. A stream receiver uses a truncated FIR approximation of 1/F∗(1/z∗)1/F^*(1/z^*) with a delay.

Properties

  • Information lossless. The matched filter outputs are sufficient statistics, and the whitening filter is invertible, so no information is lost.
  • Same metric. The Euclidean metric on y′y' differs from the metric on yy by a constant for each block, so every maximum likelihood detector gives the same answer on both.
  • White noise. The noise map H∗(1/z∗)/F∗(1/z∗)H^*(1/z^*)/F^*(1/z^*) is allpass, so the noise stays white with variance N0N_0. In a finite block, the samples near the end carry an edge effect.
  • Causal and minimum phase. The output channel has LL taps, no precursors, and the most energy in its first taps among all channels with the same magnitude response.
  • Anticausal filter. 1/F∗(1/z∗)1/F^*(1/z^*) is stable only as an anticausal filter, so a block receiver runs it backward and a stream receiver needs a delay.
  • Unit circle zeros. If HH has a zero on the unit circle, the whitening filter has a pole on the circle and the inverse is unstable. The Ungerboeck receiver avoids the whitening step.

Variants and improvements

  • From the autocorrelation. If only RkR_k is known, factor the polynomial zL−1R(z)z^{L-1} R(z) of degree 2L−22L-2 and keep the roots inside the circle. Other methods are Bauer's method, the Cholesky factorization of a large Toeplitz matrix, and the cepstral method.
  • Linear prediction. The whitening filter is a prediction error filter for the matched filter noise, so a long linear prediction filter from the Levinson recursion approximates it.
  • MMSE prefilter. The feedforward filter of the MMSE-DFE also leaves a channel with its energy first, and it tends to the WMF at high SNR. At lower SNR it allows a little precursor ISI and bias for less noise. For RSSE and DFE receivers it often replaces the WMF.
  • Fractionally spaced front end. With more than one sample per symbol, the matched filter is applied in continuous time or at the higher rate, and the output is then sampled at the symbol rate before whitening.

When to use it

Use the WMF as the front end of a Viterbi equalizer with a Euclidean metric, of a DFE or of reduced-state sequence estimation, when the channel is known and has no zeros on the unit circle. For a full Viterbi equalizer, the Ungerboeck receiver avoids the whitening and works on the matched filter output. For suboptimal detectors that decide early, the minimum-phase output is the main reason to use the WMF.

Pitfalls

  • Running the whitening filter forward. 1/F∗(1/z∗)1/F^*(1/z^*) is unstable as a causal filter, because its pole 1/ρˉ′1/\bar\rho' lies outside the circle. Run it backward or use a truncated anticausal FIR.
  • Taking the maximum-phase factor. Keeping the roots outside the circle gives a valid factor with the energy last, which ruins a DFE or an RSSE.
  • Forgetting the conjugates. For complex channels, the matched filter correlates with hˉ\bar h, and the whitening uses fˉ\bar f. A real test channel hides the mistake.
  • Zeros near the circle. The whitening filter then decays slowly, so the edge effect at the block end is long, and the root finder needs more iterations. The channel B of Proakis is such a case.
  • A gain error in ff. The scale γ\gamma must give ∥f∥2=∥h∥2\|f\|^2 = \|h\|^2. A wrong scale changes the noise variance that the metric assumes.

Implementations

Each implementation follows the pseudocode above. A comment @line links a line of code to a line of the pseudocode. Point at a line of the pseudocode to see the matching code. The build runs the native tests of each language.

whitened_matched_filter.pyI0RawGitHub
# SPDX-License-Identifier: MIT
# Part of https://algorithm.tamnd.com/whitened-matched-filter/. The authors are listed in prov/35K4A5.jsonl.

"""Forney's whitened matched filter on a discrete-time ISI channel.

The channel is y[k] = sum_l h[l] x[k-l] + w[k] with w white CN(0, N0). The
matched filter output z = h~ * y (h~[l] = conj(h[-l])) has the folded spectrum
R(z) = H(z) H*(1/z*) of the channel autocorrelation, and colored noise.
Factor R(z) = F(z) F*(1/z*) with F minimum phase (all zeros inside or on the
unit circle). The whitening filter 1/F*(1/z*) after the matched filter gives
y' = f * x + n' with n' white CN(0, N0) again. The receiver can then run the
Viterbi equalizer on the causal, minimum-phase channel f.

Here the receiver knows h, so F is found by reflecting the zeros of H(z) that lie
outside the unit circle to their conjugate reciprocals, and scaling so that
|F| = |H| on the unit circle. The zeros come from the Durand-Kerner iteration.
"""

import cmath
import math


def autocorrelation(h: list[complex]) -> list[complex]:
    """R[k] = sum_l h[l + k] conj(h[l]) for k = 0..L-1; R[-k] = conj(R[k])."""
    n = len(h)
    return [sum(complex(h[l + k]) * complex(h[l]).conjugate() for l in range(n - k)) for k in range(n)]


def poly_roots(c: list[complex], iters: int = 500, tol: float = 1e-14) -> list[complex]:
    """The roots of c[0] z^d + c[1] z^(d-1) + ... + c[d] by the Durand-Kerner iteration."""
    while c and c[0] == 0:
        c = c[1:]
    d = len(c) - 1
    if d < 1:
        return []
    a = [complex(v) / c[0] for v in c]
    rad = 1.0 + max(abs(v) for v in a[1:])
    z = [rad * 0.9 * cmath.exp(2j * math.pi * (i + 0.25) / d) for i in range(d)]
    for _ in range(iters):
        move = 0.0
        for i in range(d):
            p = 0j
            for v in a:
                p = p * z[i] + v
            den = 1 + 0j
            for k in range(d):
                if k != i:
                    den *= z[i] - z[k]
            step = p / den if den != 0 else 1e-3
            z[i] -= step
            move = max(move, abs(step))
        if move < tol * rad:
            break
    return z


def min_phase_factor(h: list[complex]) -> list[complex]:
    """The minimum-phase f with |F| = |H| on the unit circle and f[0] real and positive."""
    while len(h) > 1 and h[-1] == 0:
        h = h[:-1]
    if not h or h[0] == 0:
        raise ValueError("need h[0] != 0")
    zeros = poly_roots(list(h))  # @line L2
    gain = abs(h[0])
    inside = []
    for r in zeros:  # @line L3
        if abs(r) > 1.0:
            gain *= abs(r)
            inside.append(1.0 / r.conjugate())
        else:
            inside.append(r)
    f = [complex(gain)]
    for r in inside:  # @line L4
        f = [(f[i] if i < len(f) else 0) - (r * f[i - 1] if i >= 1 else 0) for i in range(len(f) + 1)]
    return f


def all_spectral_factors(h: list[complex]) -> list[list[complex]]:
    """Every factor g with |G| = |H| on the unit circle, one per choice of reflected zeros (up to a phase)."""
    zeros = poly_roots(list(h))
    out = []
    for mask in range(1 << len(zeros)):
        gain = abs(h[0])
        rs = []
        for i, r in enumerate(zeros):
            if mask >> i & 1:
                gain *= abs(r)
                rs.append(1.0 / r.conjugate())
            else:
                rs.append(r)
        g = [complex(gain)]
        for r in rs:
            g = [(g[i] if i < len(g) else 0) - (r * g[i - 1] if i >= 1 else 0) for i in range(len(g) + 1)]
        out.append(g)
    return out


def freq(h: list[complex], w: float) -> complex:
    """H(e^{jw}) = sum_l h[l] e^{-jwl}."""
    return sum(complex(v) * cmath.exp(-1j * w * l) for l, v in enumerate(h))


def whitened_matched_filter(h: list[complex], y: list[complex]) -> tuple[list[complex], list[complex]]:
    """The minimum-phase channel f and the whitened samples y' with y' = f * x + white noise.

    y is the received block y[0..K-1]; samples after it are taken as 0. The
    output has the same length as y.
    """
    f = min_phase_factor(h)  # @line L1
    n, taps = len(y), len(h)
    z = [sum(complex(h[l]).conjugate() * y[k + l] for l in range(taps) if k + l < n) for k in range(n)]  # @line L5
    out = [0j] * n
    for k in range(n - 1, -1, -1):  # @line L6
        acc = z[k] - sum(f[l].conjugate() * out[k + l] for l in range(1, len(f)) if k + l < n)
        out[k] = acc / f[0].conjugate()  # @line L7
    return f, out  # @line L8
test_whitened_matched_filter.py 136 lines
# SPDX-License-Identifier: MIT
# Part of https://algorithm.tamnd.com/whitened-matched-filter/. The authors are listed in prov/35K4A5.jsonl.

import itertools
import math
import random

import pytest

from whitened_matched_filter import (
    all_spectral_factors,
    autocorrelation,
    freq,
    min_phase_factor,
    poly_roots,
    whitened_matched_filter,
)


def cn(rng, var):
    s = math.sqrt(var / 2)
    return complex(rng.gauss(0, s), rng.gauss(0, s))


def conv(a, b):
    out = [0j] * (len(a) + len(b) - 1)
    for i, x in enumerate(a):
        for k, y in enumerate(b):
            out[i + k] += x * y
    return out


def random_channel(rng, taps):
    # zeros away from the unit circle, so the whitening filter decays fast
    while True:
        h = [cn(rng, 1.0) for _ in range(taps)]
        if h[0] != 0 and all(abs(abs(r) - 1) > 0.15 for r in poly_roots(h)):
            return h


def test_example_on_the_page() -> None:
    f = min_phase_factor([0.5, 1])
    assert max(abs(a - b) for a, b in zip(f, [1, 0.5])) < 1e-12
    assert autocorrelation([0.5, 1]) == [1.25, 0.5]
    f = min_phase_factor([0.407, 0.815, 0.407])
    assert abs(sum(abs(v) ** 2 for v in f) - (0.407**2 * 2 + 0.815**2)) < 1e-12
    x = [1, -1, -1, 1, 1]
    y = conv([complex(v) for v in x], [0.5, 1])
    _, yw = whitened_matched_filter([0.5, 1], y)
    want = conv([complex(v) for v in x], [1, 0.5])
    assert max(abs(a - b) for a, b in zip(yw, want)) < 1e-12


def test_roots_of_known_polynomials() -> None:
    r = sorted(poly_roots([1, -3, 2]), key=lambda v: v.real)
    assert abs(r[0] - 1) < 1e-12 and abs(r[1] - 2) < 1e-12
    r = poly_roots([1, 0, 1])
    assert sorted(round(v.imag, 12) for v in r) == [-1, 1]


def test_factor_has_the_same_spectrum_and_is_minimum_phase() -> None:
    rng = random.Random(7401)
    for _ in range(40):
        h = random_channel(rng, rng.randint(1, 6))
        f = min_phase_factor(h)
        assert len(f) == len(h) and f[0].real > 0 and abs(f[0].imag) < 1e-12
        for t in range(32):
            w = 2 * math.pi * t / 32
            assert abs(abs(freq(f, w)) ** 2 - abs(freq(h, w)) ** 2) < 1e-9
        assert all(abs(r) <= 1 + 1e-9 for r in poly_roots(f))
        rh, rf = autocorrelation(h), autocorrelation(f)
        assert max(abs(a - b) for a, b in zip(rh, rf)) < 1e-9


def test_minimum_phase_factor_has_the_largest_partial_energies() -> None:
    rng = random.Random(7402)
    for _ in range(20):
        h = random_channel(rng, rng.randint(2, 5))
        f = min_phase_factor(h)
        for g in all_spectral_factors(h):
            for n in range(len(g)):
                assert sum(abs(v) ** 2 for v in f[: n + 1]) >= sum(abs(v) ** 2 for v in g[: n + 1]) - 1e-9


def test_noiseless_output_is_f_convolved_with_x() -> None:
    rng = random.Random(7403)
    for _ in range(20):
        h = random_channel(rng, rng.randint(1, 5))
        x = [complex(rng.choice((-1, 1)), rng.choice((-1, 1))) for _ in range(30)]
        f, yw = whitened_matched_filter(h, conv(x, h))
        want = conv(x, f)
        assert max(abs(a - b) for a, b in zip(yw, want)) < 1e-9


def test_ml_decisions_agree_before_and_after_the_filter() -> None:
    # the two metrics differ by a constant, so the brute force minimizers agree on every vector
    rng = random.Random(7404)
    for _ in range(25):
        h = random_channel(rng, rng.randint(2, 4))
        n = 8
        x = [complex(rng.choice((-1, 1))) for _ in range(n)]
        y = [v + cn(rng, 0.5) for v in conv(x, h)]
        f, yw = whitened_matched_filter(h, y)
        best_y, best_w, diffs = None, None, []
        for cand in itertools.product((-1, 1), repeat=n):
            c = [complex(v) for v in cand]
            my = sum(abs(a - b) ** 2 for a, b in zip(y, conv(c, h)))
            mw = sum(abs(a - b) ** 2 for a, b in zip(yw, conv(c, f)))
            diffs.append(my - mw)
            if best_y is None or my < best_y[0]:
                best_y = (my, cand)
            if best_w is None or mw < best_w[0]:
                best_w = (mw, cand)
        assert best_y[1] == best_w[1]
        assert max(diffs) - min(diffs) < 1e-8


def test_noise_map_is_white_away_from_the_block_end() -> None:
    rng = random.Random(7405)
    h = [0.3 + 0.2j, 1.0, -0.4j]
    n = 40
    cols = []
    for t in range(n):
        e = [0j] * n
        e[t] = 1
        cols.append(whitened_matched_filter(h, e)[1])
    # entry (i, t) of the noise map is cols[t][i]
    for i in range(20):
        for k in range(20):
            g = sum(cols[t][i] * cols[t][k].conjugate() for t in range(n))
            assert abs(g - (1 if i == k else 0)) < 1e-6


def test_input_checks() -> None:
    with pytest.raises(ValueError):
        min_phase_factor([0, 1])

History

In 1972, Forney showed that the matched filter outputs at the symbol rate are sufficient statistics for the transmitted sequence on a linear Gaussian channel with ISI. He then whitened their noise with the minimum-phase spectral factor, obtaining a causal channel with white noise, and applied the Viterbi algorithm to it. The WMF made the maximum likelihood sequence estimator a practical receiver structure. In 1974, Ungerboeck showed that the Viterbi algorithm can work on the matched filter outputs directly, with a modified metric. The minimum-phase prefilter later became a standard part of reduced-state receivers.

References

  1. G. David Forney, Jr., Maximum-likelihood sequence estimation of digital sequences in the presence of intersymbol interference, IEEE Transactions on Information Theory 18(3), 1972, pages 363 to 378, the whitened matched filter and the discrete-time white-noise model
  2. John G. Proakis and Masoud Salehi, Digital Communications, fifth edition, McGraw-Hill, 2008, Section 9.3 (the optimum receiver for channels with ISI), with 9.3-2 (the discrete-time model and the noise-whitening filter)

Where this leads

About this page

The build computes these levels from the evidence in the repository. Each row says what the next level needs. Read what the levels mean.

LadderLevelWhat the next level needs
Human proofH1 sketch
  • a full proof of time.worst
  • a full proof of space.extra
LeanL0 no Lean part
  • name a Lean module in the lean field (07)
Code (python)I0 not checked yet
  • the tests did not run in this build
VisualV0 none
  • add viz/scene.ts or a figure (10)
WritingW2 basic
  • at least three sources
  • a scene preset for the example