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
- Model
- Real RAM
- Properties
- deterministicefficient above thresholdfeedforward
- Evidence
- Proof sketchCode in 1 languages
- Origin
- Steven M. Kay, 1989
The Kay frequency estimator finds the frequency of one complex tone in white noise from samples. It computes the phase difference of each pair of consecutive samples and takes a weighted average with the parabolic weights . 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 ; 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 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 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 , , where the amplitude , the phase and the frequency in radians per sample are unknown, and is white, . In a receiver, 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 . Output .
The algorithm
KAY(r[0..N-1])- 1
w[t] <- 6 (t + 1) (N - 1 - t) / (N (N^2 - 1)) for t = 0..N-2 - 2
omega <- 0 - 3
for t <- 0 to N - 2 do - 4
d <- arg(r[t+1] * conj(r[t])) ▷ phase difference in (-pi, pi] - 5
omega <- omega + w[t] * d - 6
return 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 . Line L1 gives a parabola that is largest in the middle. The weights sum to 1; the frequency in cycles per sample is .
Example
, , , dB (noise seed 8). The weights are .
| (line L4) | |||
|---|---|---|---|
| 0 | |||
| 1 | |||
| 2 | |||
| 3 | |||
| 4 |
The last sample is . The estimate is , an error of . The bound is , a standard deviation of , so this error is typical. The single differences range from to , which shows how noisy each one is.
With and 500 random tones per point (seed 1, uniform in ), 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 |
Above about 10 dB the estimator is at the bound; between 5 and 10 dB it leaves it fast. At and 10 dB the bound is rad. 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 exactly for (theorem). In the high-SNR model, where the phase noise of each sample is and no difference wraps, the differences are with tridiagonal covariance, the weights are the best linear unbiased weights, and the variance is the CRB (a closed form under assumptions).
Key steps. . For the covariance , is the negative second difference of , which is the constant times the scale, so , the generalized least squares weights of the Gauss-Markov theorem. Then . The tests check the weights against a direct solution of .
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 , , , with , , and white; . Lines L1 to L5 form with and
Write and , so .
Lemma 1 (the weights sum to one). .
Proof. .
Theorem 1 (exact without noise). If for all , then .
Proof. , and since , for every . By Lemma 1, .
The high-SNR model. Write with , white. Then modulo , with . To first order, the are independent with . If no difference wraps, that is for all , then
so with and , where is the tridiagonal matrix with on the diagonal and next to it. This is a linear model with one unknown and correlated noise.
Theorem 2 (the weights are the BLUE). In this model, is the best linear unbiased estimator of from , and its variance is
which is the Cramer-Rao bound for with unknown amplitude and phase.
Proof. By the Gauss-Markov theorem for correlated noise (generalized least squares), the BLUE of in is , so its weight vector is the unique vector proportional to that sums to . It is enough to show that is a constant vector. Set , which agrees with . Row of is proportional to , the negative second difference of . As , the second difference is for every , so
for every , including the first and the last rows, where the missing neighbor is or . With Lemma 1, is the BLUE weight vector. Its variance is . The Cramer-Rao bound for the frequency of a complex tone with unknown amplitude and phase in complex white Gaussian noise is , and .
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 wraps when is close to , and the noise of has standard deviation for every . So the probability of a wrap per difference depends on the per-sample SNR and on , and does not fall as grows. Below a threshold SNR, wrapped differences add errors of size about , and the mean square error rises far above the bound. This is a rule, and the tests measure it.
Complexity. Line L1 computes weights, and lines L3 to L5 run times with one complex product and one angle each. The total is time; the weights can be computed on the fly, so the extra space is .
Complexity
complex products, angles and additions: time in the real RAM, with 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 , whatever is. A wrap of a single difference adds an error of about . So the threshold does not fall as grows, unlike the periodogram peak, whose threshold depends on (a rule).
- Full range. It estimates any , but near 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 .
- Not robust to fading. It assumes a constant amplitude. Deep fades make some differences pure noise.
Variants and improvements
- Unweighted mean. Equal weights give , which, without wraps, is and uses only two samples: its variance is about times the bound. The tests show it.
- Linear regression on the unwrapped phase. Tretter's estimator fits a line to after unwrapping. It has the same high-SNR variance, but needs a correct phase unwrapping.
- Smoothed or lagged differences. Averaging over a few terms before the angle, or using differences at lag , 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 and divide by , which reduces the range to .
- 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 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 , the frequency is , and it must stay in .
- Mixing units. The output is in radians per sample. Divide by for hertz.
- Comparing with the wrong bound. The CRB 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.
# 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
- 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
- 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)
- 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.
| Ladder | Level | What the next level needs |
|---|---|---|
| Human proof | H1 sketch |
|
| Lean | L0 no Lean part |
|
| Code (python) | I0 not checked yet |
|
| Visual | V0 none |
|
| Writing | W2 basic |
|
Tag 9RD1BP Edit this page History Report an error Markdown