Algorithm Search⌘K

Algorithmadvanced

Kay frequency estimator

Also called Kay weighted phase averager.

Estimates a tone frequency by a weighted average of phase differences of consecutive samples. It reaches the CRB above a threshold SNR; below it, phase wraps cause large errors.

Time
O(N) worst
Space
O(1) extra
Where
N = the number of samples
Properties
deterministicefficient above thresholdfeedforward
Origin
Steven M. Kay, 1989

The Kay frequency estimator finds the frequency ω\omega of one complex tone in white noise from NN samples. It computes the phase difference arg⁡(rt+1rt‾)\arg(r_{t+1}\overline{r_t}) of each pair of consecutive samples and takes a weighted average with the parabolic weights wt=6(t+1)(N−1−t)N(N2−1)w_t = \frac{6(t+1)(N-1-t)}{N(N^2-1)}. These weights are the best linear unbiased weights for the correlated noise of the differences, so above a threshold SNR its variance reaches the Cramer-Rao bound 6SNR N(N2−1)\frac{6}{\mathrm{SNR}\,N(N^2-1)}; in synchronization with known data this is the modified Cramer-Rao bound. It is one of the estimators for frequency offset estimation. The Rife and Boorstyn estimator finds the maximum likelihood frequency at the peak of the periodogram and has a lower threshold, at a higher cost. The Luise and Reggiannini estimator uses the angle of a sum of autocorrelations and works down to lower SNR with a smaller range. Kay's estimator covers the full range ∣ω∣<π|\omega| < \pi and costs one angle per sample, but each angle uses only two samples, so its threshold is set by the per-sample SNR and does not drop as NN grows.

In plain words

A pure tone turns by the same angle from each sample to the next, and that angle is the frequency. So the receiver measures the turn between each pair of neighbors and averages the measurements. Noise disturbs each sample, and each disturbance enters two neighboring measurements with opposite signs. A plain average would cancel almost everything except the first and last samples. Giving the middle measurements more weight, along a parabola, uses all samples in the best way. When noise is so strong that a single turn can look like a turn the other way round, the method makes large errors.

The problem

The input is rt=Aej(ωt+ϕ)+wtr_t = A e^{j(\omega t + \phi)} + w_t, t=0,…,N−1t = 0, \ldots, N - 1, where the amplitude A>0A > 0, the phase ϕ\phi and the frequency ω∈(−π,π)\omega \in (-\pi, \pi) in radians per sample are unknown, and wt∼CN(0,N0)w_t \sim \mathcal{CN}(0, N_0) is white, SNR=A2/N0\mathrm{SNR} = A^2/N_0. In a receiver, rtr_t is the received signal with the data removed: the known pilot symbols divided out, or, for M-PSK, the Mth power taken, which multiplies the frequency by MM. Output ω^\hat\omega.

The algorithm

  1. KAY(r[0..N-1])
  2. 1w[t] <- 6 (t + 1) (N - 1 - t) / (N (N^2 - 1)) for t = 0..N-2
  3. 2omega <- 0
  4. 3for t <- 0 to N - 2 do
  5. 4d <- arg(r[t+1] * conj(r[t])) ▷ phase difference in (-pi, pi]
  6. 5omega <- omega + w[t] * d
  7. 6return omega

Line L4 takes the angle of the product, not the difference of two angles, so no phase unwrapping is needed as long as each single difference stays in (−π,π](-\pi, \pi]. Line L1 gives a parabola that is largest in the middle. The weights sum to 1; the frequency in cycles per sample is ω^/(2π)\hat\omega/(2\pi).

Example

N=6N = 6, ω=0.4\omega = 0.4, ϕ=0\phi = 0, SNR=10\mathrm{SNR} = 10 dB (noise seed 8). The weights are 0.1429,0.2286,0.2571,0.2286,0.14290.1429, 0.2286, 0.2571, 0.2286, 0.1429.

tt rtr_t dtd_t (line L4) wtdtw_t d_t
0 1.083+0.566j1.083 + 0.566j 0.01930.0193 0.00280.0028
1 1.166+0.638j1.166 + 0.638j 0.26110.2611 0.05970.0597
2 0.842+0.803j0.842 + 0.803j 0.30300.3030 0.07790.0779
3 0.516+0.931j0.516 + 0.931j 0.73110.7311 0.16710.1671
4 −0.185+0.807j-0.185 + 0.807j 0.36930.3693 0.05280.0528

The last sample is r5=−0.666+0.985jr_5 = -0.666 + 0.985j. The estimate is ω^=0.3602\hat\omega = 0.3602, an error of −0.040-0.040. The bound is 6/(10⋅6⋅35)=0.0028576/(10 \cdot 6 \cdot 35) = 0.002857, a standard deviation of 0.0530.053, so this error is typical. The single differences range from 0.020.02 to 0.730.73, which shows how noisy each one is.

With N=32N = 32 and 500 random tones per point (seed 1, ω\omega uniform in [−0.5,0.5][-0.5, 0.5]), the mean square error over the CRB was (measurements):

SNR 0 dB 3 dB 5 dB 7 dB 10 dB 15 dB 20 dB
MSE/CRB 185185 4545 1818 5.15.1 0.990.99 0.950.95 0.940.94

Above about 10 dB the estimator is at the bound; between 5 and 10 dB it leaves it fast. At N=32N = 32 and 10 dB the bound is 1.833×10−51.833 \times 10^{-5} rad2^2. A ratio slightly below 1 is sampling error of the measurement, not a violation of the bound.

Correctness

Claim. The weights sum to 1, so without noise ω^=ω\hat\omega = \omega exactly for ∣ω∣<π|\omega| < \pi (theorem). In the high-SNR model, where the phase noise of each sample is ut∼N(0,12 SNR)u_t \sim \mathcal{N}(0, \frac{1}{2\,\mathrm{SNR}}) and no difference wraps, the differences are dt=ω+ut+1−utd_t = \omega + u_{t+1} - u_t with tridiagonal covariance, the weights are the best linear unbiased weights, and the variance is the CRB 6SNR N(N2−1)\frac{6}{\mathrm{SNR}\,N(N^2-1)} (a closed form under assumptions).

Key steps. ∑s=1N−1s(N−s)=N(N2−1)/6\sum_{s=1}^{N-1} s(N-s) = N(N^2-1)/6. For the covariance C=tridiag⁡(−1,2,−1)C = \operatorname{tridiag}(-1, 2, -1), CwCw is the negative second difference of s(N−s)s(N-s), which is the constant 22 times the scale, so w∝C−11w \propto C^{-1}\mathbf 1, the generalized least squares weights of the Gauss-Markov theorem. Then var⁡ω^=σ2w⊤Cw=12σ2N(N2−1)\operatorname{var}\hat\omega = \sigma^2 w^\top C w = \frac{12\sigma^2}{N(N^2-1)}. The tests check the weights against a direct solution of Cx=1C x = \mathbf 1.

Proof: Kay's weights sum to one and are the best linear unbiased weights for the phase differences, so the estimate is exact without noise and reaches the Cramer-Rao bound in the high-SNR modelfull

The input is rt=Aej(ωt+ϕ)+wtr_t = A e^{j(\omega t + \phi)} + w_t, t=0,…,N−1t = 0, \ldots, N - 1, N≥2N \ge 2, with ∣ω∣<π|\omega| < \pi, A>0A > 0, and wt∼CN(0,N0)w_t \sim \mathcal{CN}(0, N_0) white; SNR=A2/N0\mathrm{SNR} = A^2/N_0. Lines L1 to L5 form ω^=∑t=0N−2wtdt\hat\omega = \sum_{t=0}^{N-2} w_t d_t with dt=arg⁡(rt+1rt‾)∈(−π,π]d_t = \arg(r_{t+1}\overline{r_t}) \in (-\pi, \pi] and

wt=6(t+1)(N−1−t)N(N2−1).w_t = \frac{6(t + 1)(N - 1 - t)}{N(N^2 - 1)}.

Write s=t+1∈{1,…,N−1}s = t + 1 \in \{1, \ldots, N - 1\} and f(s)=s(N−s)f(s) = s(N - s), so wt=6f(s)N(N2−1)w_t = \frac{6 f(s)}{N(N^2-1)}.

Lemma 1 (the weights sum to one). ∑t=0N−2wt=1\sum_{t=0}^{N-2} w_t = 1.

Proof. ∑s=1N−1s(N−s)=N⋅(N−1)N2−(N−1)N(2N−1)6=N(N−1)(3N−2N+1)6=N(N2−1)6\sum_{s=1}^{N-1} s(N - s) = N \cdot \frac{(N-1)N}{2} - \frac{(N-1)N(2N-1)}{6} = \frac{N(N-1)(3N - 2N + 1)}{6} = \frac{N(N^2-1)}{6}. □\square

Theorem 1 (exact without noise). If wt=0w_t = 0 for all tt, then ω^=ω\hat\omega = \omega.

Proof. rt+1rt‾=A2ejωr_{t+1}\overline{r_t} = A^2 e^{j\omega}, and since ∣ω∣<π|\omega| < \pi, dt=ωd_t = \omega for every tt. By Lemma 1, ω^=ω∑twt=ω\hat\omega = \omega \sum_t w_t = \omega. □\square

The high-SNR model. Write rt=Aej(ωt+ϕ)(1+vt)r_t = A e^{j(\omega t + \phi)}(1 + v_t) with vt=wte−j(ωt+ϕ)/A∼CN(0,1/SNR)v_t = w_t e^{-j(\omega t + \phi)}/A \sim \mathcal{CN}(0, 1/\mathrm{SNR}), white. Then arg⁡rt=ωt+ϕ+ut\arg r_t = \omega t + \phi + u_t modulo 2π2\pi, with ut=arg⁡(1+vt)=Im⁡vt+O(∣vt∣2)u_t = \arg(1 + v_t) = \operatorname{Im} v_t + O(|v_t|^2). To first order, the utu_t are independent N(0,σ2)\mathcal{N}(0, \sigma^2) with σ2=12 SNR\sigma^2 = \frac{1}{2\,\mathrm{SNR}}. If no difference wraps, that is ∣ω+ut+1−ut∣<π|\omega + u_{t+1} - u_t| < \pi for all tt, then

dt=ω+ut+1−ut,d_t = \omega + u_{t+1} - u_t,

so d=ω1+ed = \omega \mathbf{1} + e with Ee=0E e = 0 and cov⁡e=σ2C\operatorname{cov} e = \sigma^2 C, where CC is the (N−1)×(N−1)(N-1) \times (N-1) tridiagonal matrix with 22 on the diagonal and −1-1 next to it. This is a linear model with one unknown ω\omega and correlated noise.

Theorem 2 (the weights are the BLUE). In this model, ω^\hat\omega is the best linear unbiased estimator of ω\omega from dd, and its variance is

var⁡ω^=12σ2N(N2−1)=6SNR N(N2−1),\operatorname{var}\hat\omega = \frac{12\sigma^2}{N(N^2 - 1)} = \frac{6}{\mathrm{SNR}\,N(N^2 - 1)},

which is the Cramer-Rao bound for ω\omega with unknown amplitude and phase.

Proof. By the Gauss-Markov theorem for correlated noise (generalized least squares), the BLUE of ω\omega in d=ω1+ed = \omega\mathbf 1 + e is 1⊤C−1d1⊤C−11\frac{\mathbf 1^\top C^{-1} d}{\mathbf 1^\top C^{-1}\mathbf 1}, so its weight vector is the unique vector proportional to C−11C^{-1}\mathbf 1 that sums to 11. It is enough to show that CwCw is a constant vector. Set f(0)=f(N)=0f(0) = f(N) = 0, which agrees with f(s)=s(N−s)f(s) = s(N - s). Row ss of CwCw is proportional to 2f(s)−f(s−1)−f(s+1)2f(s) - f(s-1) - f(s+1), the negative second difference of ff. As f(s)=Ns−s2f(s) = Ns - s^2, the second difference is −2-2 for every ss, so

(Cw)t=6N(N2−1)⋅2=12N(N2−1)(Cw)_t = \frac{6}{N(N^2-1)} \cdot 2 = \frac{12}{N(N^2-1)}

for every tt, including the first and the last rows, where the missing neighbor is f(0)=0f(0) = 0 or f(N)=0f(N) = 0. With Lemma 1, ww is the BLUE weight vector. Its variance is σ2w⊤Cw=σ212N(N2−1)∑twt=12σ2N(N2−1)\sigma^2 w^\top C w = \sigma^2 \frac{12}{N(N^2-1)} \sum_t w_t = \frac{12\sigma^2}{N(N^2-1)}. The Cramer-Rao bound for the frequency of a complex tone with unknown amplitude and phase in complex white Gaussian noise is 6SNR N(N2−1)\frac{6}{\mathrm{SNR}\,N(N^2-1)}, and 12σ2=6/SNR12\sigma^2 = 6/\mathrm{SNR}. □\square

What this proves and what it does not. Theorems 1 and 2 are exact statements; the claim that the true estimator has variance close to the bound is a closed form under assumptions: the first-order phase noise and no wrapped difference. Each dtd_t wraps when ∣ut+1−ut∣|u_{t+1} - u_t| is close to π−∣ω∣\pi - |\omega|, and the noise of dtd_t has standard deviation 2 σ=1/SNR\sqrt2\,\sigma = 1/\sqrt{\mathrm{SNR}} for every NN. So the probability of a wrap per difference depends on the per-sample SNR and on ω\omega, and does not fall as NN grows. Below a threshold SNR, wrapped differences add errors of size about 2πwt2\pi w_t, and the mean square error rises far above the bound. This is a rule, and the tests measure it.

Complexity. Line L1 computes N−1N - 1 weights, and lines L3 to L5 run N−1N - 1 times with one complex product and one angle each. The total is O(N)O(N) time; the weights can be computed on the fly, so the extra space is O(1)O(1).

Complexity

N−1N - 1 complex products, N−1N - 1 angles and O(N)O(N) additions: O(N)O(N) time in the real RAM, with O(1)O(1) extra space if the weights are computed on the fly. There is no FFT and no search, which is the main gain over the maximum likelihood periodogram peak.

Properties

  • Efficient above threshold. The variance equals the CRB in the high-SNR model, and the measurements at 10 dB and above match it.
  • Threshold set by the per-sample SNR. Each difference has noise of standard deviation about 1/SNR1/\sqrt{\mathrm{SNR}}, whatever NN is. A wrap of a single difference adds an error of about 2πwt2\pi w_t. So the threshold does not fall as NN grows, unlike the periodogram peak, whose threshold depends on N⋅SNRN\cdot\mathrm{SNR} (a rule).
  • Full range. It estimates any ∣ω∣<π|\omega| < \pi, but near ±π\pm\pi the differences wrap more often, so the threshold rises.
  • Unbiased in the model. In the high-SNR model the estimate is unbiased; with wraps it is pulled toward zero for large ∣ω∣|\omega|.
  • Not robust to fading. It assumes a constant amplitude. Deep fades make some differences pure noise.

Variants and improvements

  • Unweighted mean. Equal weights give 1N−1∑tdt\frac{1}{N-1}\sum_t d_t, which, without wraps, is (arg⁡rN−1−arg⁡r0)/(N−1)(\arg r_{N-1} - \arg r_0)/(N-1) and uses only two samples: its variance is about N/6N/6 times the bound. The tests show it.
  • Linear regression on the unwrapped phase. Tretter's estimator fits a line to arg⁡rt\arg r_t after unwrapping. It has the same high-SNR variance, but needs a correct phase unwrapping.
  • Smoothed or lagged differences. Averaging rt+1rt‾r_{t+1}\overline{r_t} over a few terms before the angle, or using differences at lag m>1m > 1, lowers the threshold and narrows the range; the Luise and Reggiannini estimator and the estimator of Fitz are built this way.
  • Data removal. For pilots, divide out the known symbols first; for M-PSK, apply Kay to rtMr_t^M and divide by MM, which reduces the range to ∣ω∣<π/M|\omega| < \pi/M.
  • Relation to linear prediction. The angle of a one-step prediction coefficient is a related estimator. Kay used the relation between the two to explain why the linear prediction estimator does not reach the bound even at high SNR.

When to use it

Use Kay's estimator for a fast frequency estimate at moderate to high SNR, for example 10 dB or more per sample for a few tens of samples, when the range must be wide and an FFT is too costly. For low SNR or long blocks, use the periodogram peak or a correlation estimator.

Pitfalls

  • Running it below threshold. At 5 dB with N=32N = 32 the error was 18 times the bound in the measurement. More samples do not help, because the threshold depends on the per-sample SNR.
  • Leaving the data in. Modulated symbols turn the phase from sample to sample. Remove them first, by pilots or a power.
  • Large frequencies after an Mth power. After rMr^M, the frequency is MωM\omega, and it must stay in (−π,π)(-\pi, \pi).
  • Mixing units. The output is in radians per sample. Divide by 2πT2\pi T for hertz.
  • Comparing with the wrong bound. The CRB 6SNR N(N2−1)\frac{6}{\mathrm{SNR}\,N(N^2-1)} is for unknown phase and amplitude in complex noise. Do not compare with a bound for a known phase or for real signals.

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.

kay_frequency_estimator.pyI0RawGitHub
# SPDX-License-Identifier: MIT
# Part of https://algorithm.tamnd.com/kay-frequency-estimator/. The authors are listed in prov/9RD1BP.jsonl.

"""Kay's weighted phase averager for the frequency of one complex tone.

For r[t] = A e^(j(omega t + phi)) + w[t], t = 0, ..., N-1, the phase
differences arg(r[t+1] conj(r[t])) are omega plus noise. At high SNR that
noise is a moving-average process, and the best linear unbiased weights are

  w_t = 6 (t + 1) (N - 1 - t) / (N (N^2 - 1)),  t = 0, ..., N-2,

a parabola that sums to 1. The estimate is omega_hat = sum_t w_t arg(r[t+1] conj(r[t]))
in radians per sample, for |omega| < pi. Its variance reaches the Cramer-Rao
bound 6 / (SNR N (N^2 - 1)) above a threshold SNR.
"""

import cmath
from typing import Sequence


def kay_weights(n: int) -> list[float]:
    """The N - 1 Kay weights 6 (t+1)(N-1-t) / (N (N^2 - 1))."""
    if n < 2:
        raise ValueError("need at least two samples")
    return [6.0 * (t + 1) * (n - 1 - t) / (n * (n * n - 1)) for t in range(n - 1)]


def kay_estimate(r: Sequence[complex]) -> float:
    """The frequency estimate in radians per sample, a weighted mean of angles in (-pi, pi]."""
    n = len(r)  # @line L1
    w = kay_weights(n)  # @line L1
    omega = 0.0  # @line L2
    for t in range(n - 1):  # @line L3
        d = cmath.phase(r[t + 1] * r[t].conjugate())  # @line L4
        omega += w[t] * d  # @line L5
    return omega  # @line L6


def crb(n: int, snr: float) -> float:
    """The Cramer-Rao bound on the variance of an unbiased estimate of omega (rad^2 per sample^2)."""
    return 6.0 / (snr * n * (n * n - 1))
test_kay_frequency_estimator.py 83 lines
# SPDX-License-Identifier: MIT
# Part of https://algorithm.tamnd.com/kay-frequency-estimator/. The authors are listed in prov/9RD1BP.jsonl.

import cmath
import math
import random

from kay_frequency_estimator import crb, kay_estimate, kay_weights


def tone(rng, n, omega, snr=None, phi=0.0):
    sd = math.sqrt(0.5 / snr) if snr else 0.0
    return [cmath.exp(1j * (omega * t + phi)) + (complex(rng.gauss(0, sd), rng.gauss(0, sd)) if sd else 0j) for t in range(n)]


def blue_weights(n):
    """Reference: C^-1 1 / (1' C^-1 1) for the tridiagonal covariance (2, -1) of the phase differences."""
    m = n - 1
    a = [[2.0 if i == j else (-1.0 if abs(i - j) == 1 else 0.0) for j in range(m)] + [1.0] for i in range(m)]
    for c in range(m):  # Gaussian elimination, no pivoting needed: C is positive definite
        for i in range(c + 1, m):
            f = a[i][c] / a[c][c]
            for j in range(c, m + 1):
                a[i][j] -= f * a[c][j]
    x = [0.0] * m
    for i in range(m - 1, -1, -1):
        x[i] = (a[i][m] - sum(a[i][j] * x[j] for j in range(i + 1, m))) / a[i][i]
    s = sum(x)
    return [v / s for v in x]


def test_weights_are_the_blue_weights() -> None:
    for n in (2, 3, 5, 8, 17, 32, 64):
        w = kay_weights(n)
        assert abs(sum(w) - 1.0) < 1e-12
        assert max(abs(a - b) for a, b in zip(w, blue_weights(n))) < 1e-12
    # The high-SNR variance of the weighted sum, (N0/2) w' C w, is the CRB.
    for n in (4, 16, 32):
        w = kay_weights(n)
        q = sum(2 * x * x for x in w) - 2 * sum(w[i] * w[i + 1] for i in range(n - 2))
        snr = 10.0
        assert abs(q * 0.5 / snr - crb(n, snr)) < 1e-15
    assert abs(crb(32, 10.0) - 1.833e-5) < 1e-8


def test_exact_without_noise() -> None:
    rng = random.Random(1)
    for _ in range(200):
        n = rng.randrange(2, 40)
        om = rng.uniform(-math.pi, math.pi) * 0.999
        assert abs(kay_estimate(tone(rng, n, om, phi=rng.uniform(-3, 3))) - om) < 1e-9


def test_reaches_the_crb_above_threshold() -> None:
    # N = 32, 500 trials (16000 samples per SNR). Measured MSE / CRB.
    n, trials = 32, 500
    ratio = {}
    for snr_db in (3, 10, 20):
        snr = 10 ** (snr_db / 10)
        rng = random.Random(1)
        se = 0.0
        for _ in range(trials):
            om = rng.uniform(-0.5, 0.5)
            e = kay_estimate(tone(rng, n, om, snr, rng.uniform(-3, 3))) - om
            se += e * e
        ratio[snr_db] = se / trials / crb(n, snr)
    assert 0.85 < ratio[10] < 1.3
    assert 0.85 < ratio[20] < 1.3
    assert ratio[3] > 10  # below the threshold: phase wraps of single differences dominate


def test_better_than_the_unweighted_mean() -> None:
    # Equal weights telescope to (arg-sum of differences)/(N-1): variance about N/6 times worse.
    n, trials, snr = 32, 400, 100.0
    rng = random.Random(2)
    se_k = se_u = 0.0
    for _ in range(trials):
        om = rng.uniform(-0.5, 0.5)
        r = tone(rng, n, om, snr)
        d = [cmath.phase(r[t + 1] * r[t].conjugate()) for t in range(n - 1)]
        se_k += (kay_estimate(r) - om) ** 2
        se_u += (sum(d) / (n - 1) - om) ** 2
    assert se_u > 3 * se_k

History

Before Kay, Steven Tretter showed in 1985 that, at high SNR, the phase of a noisy tone is a line plus white Gaussian noise, so linear regression on the unwrapped phase reaches the Cramer-Rao bound, but unwrapping fails easily. Steven Kay published his estimator in 1989 in the IEEE Transactions on Acoustics, Speech, and Signal Processing. He used phase differences, which need no unwrapping, and the weights of the best linear unbiased estimator for their correlated noise, and he explained with the same analysis why the linear prediction estimator does not reach the bound. The estimator is used in burst modem frequency recovery and in measurement instruments, and its threshold behavior led to many variants with smoothed differences.

References

  1. S. Kay, A fast and accurate single frequency estimator, IEEE Transactions on Acoustics, Speech, and Signal Processing 37(12), 1989, pages 1987 to 1990, the weighted phase averager with parabolic weights, its high-SNR variance at the Cramer-Rao bound, and its relation to the linear prediction estimator
  2. Umberto Mengali and Aldo N. D'Andrea, Synchronization Techniques for Digital Receivers, Plenum Press, 1997, Chapter 3 (carrier frequency recovery with linear modulations, data-aided feedforward estimators)
  3. Steven M. Kay, Fundamentals of Statistical Signal Processing, Volume I: Estimation Theory, Prentice Hall, 1993, Chapter 3 (the Cramer-Rao lower bound, including the bound for the parameters of a sinusoid)

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
  • a scene preset for the example