Algorithm Search⌘K

Algorithmcore

Early-late gate synchronizer

Also called early-late gate, early-late timing detector.

A timing loop that compares the energy of the matched filter output a little before and a little after the sampling instant, and moves the instant toward the side with more energy.

Time
O(N) worst
Space
O(1) extra
Where
N = the number of symbols
Properties
carrier phase invariantdeterministicnon data aidedonline
Origin
Marvin K. Simon, 1970

The early-late gate synchronizer is a feedback loop for symbol timing recovery. The output of the matched filter has its largest energy at the correct sampling instant, and the energy curve is symmetric around that point. So the loop takes two extra samples, one a time dd before the current instant (early) and one a time dd after it (late), and uses the difference of their squared magnitudes as the timing error. A loop filter and an accumulator, as in a phase-locked loop, move the instant until the two energies balance. The detector needs no decisions and no carrier phase. For raised cosine pulses its mean output is β2sin⁡(2πd)sin⁡(2πΔ)\frac{\beta}{2}\sin(2\pi d)\sin(2\pi\Delta) for the timing error Δ\Delta, so it works only with excess bandwidth β>0\beta > 0, and it gives no error signal at all when d=T/2d = T/2. The same early-late structure tracks a spreading code in the delay-locked loop. The Gardner detector uses fewer samples and has less self-noise, and the Mueller and Muller detector uses decisions at one sample per symbol.

In plain words

Each received pulse rises to a peak and falls again, and the receiver should read it at the peak. The receiver does not know where the peak is, so it takes three readings: one at its current guess, one a little earlier and one a little later. If the later reading is stronger, the peak is later than the guess, and the guess moves forward a small step. If the earlier reading is stronger, the guess moves back. Since one pulse overlaps its neighbors and the data change from symbol to symbol, a single comparison can point the wrong way. The loop takes small steps, so these errors average out over many symbols. The comparison uses only the strength of the signal, so it works before the receiver knows the data or the phase of the carrier.

The problem

Times are in symbol periods, T=1T = 1. The receiver can sample the matched filter output

y(t)=ejθ∑mam g(t−m−τ)+v(t)y(t) = e^{j\theta}\sum_m a_m\,g(t - m - \tau) + v(t)

at any time tt: with an analog-to-digital converter whose clock it controls, or with an interpolator such as the Farrow interpolator on a fixed sample grid. Here gg is the overall pulse (a raised cosine with roll-off β\beta in the analysis), ama_m are unknown independent symbols with Eam=0E a_m = 0 and E∣am∣2=1E|a_m|^2 = 1, θ\theta is an unknown carrier phase, v(t)v(t) is the filtered noise with SNR=Es/N0\mathrm{SNR} = E_s/N_0, and τ\tau is the unknown delay, which may drift slowly. The loop must output estimates τk\tau_k such that the strobes y(k+τk)y(k + \tau_k) are taken at the pulse peaks.

The algorithm

  1. EARLY-LATE-GATE(y, N, Kp, Ki, tau0, d)
  2. 1tau <- tau0; s <- 0
  3. 2for k <- 0 to N - 1 do
  4. 3early <- |y(k + tau - d)|^2
  5. 4late <- |y(k + tau + d)|^2
  6. 5e <- late - early ▷ positive: the strobe is early
  7. 6est[k] <- tau; strobe[k] <- y(k + tau)
  8. 7s <- s + Ki * e ▷ loop filter
  9. 8tau <- tau + Kp * e + s ▷ timing accumulator
  10. 9return est, strobe

Lines L3 and L4 take the early and late samples at a distance dd from the strobe; d=T/4d = T/4 gives the largest slope. Line L5 is the square-law detector. Line L6 is the on-time sample that goes to the detector of the data. Lines L7 and L8 are the loop of the PLL, with the phase replaced by the delay: Ki=0K_i = 0 gives a first-order loop, which follows a constant delay, and Ki>0K_i > 0 gives a second-order loop, which also follows a clock frequency offset (a delay that grows linearly). For a first-order loop with noise bandwidth BLTB_L T, the gain is Kp=4BLT/((1+2BLT)KD)K_p = 4B_LT/\bigl((1 + 2B_LT)K_D\bigr), with the detector slope KD=πβsin⁡(2πd)K_D = \pi\beta\sin(2\pi d) at unit symbol energy.

Example

Take QPSK data (seed 1), β=0.5\beta = 0.5, the true delay τ=0.3\tau = 0.3, no noise and no carrier phase. The gate half-width is d=14d = \frac14, so KD=π/2K_D = \pi/2, and BLT=0.01B_LT = 0.01 gives Kp=0.024965K_p = 0.024965 with Ki=0K_i = 0. The loop starts at τ0=0\tau_0 = 0:

kk τk\tau_k early (L3) late (L4) eke_k (L5) τk+1\tau_{k+1} (L8)
0 00 0.2999490.299949 0.9935280.993528 0.6935790.693579 0.0173160.017316
1 0.0173160.017316 0.8807890.880789 1.0564031.056403 0.1756150.175615 0.0217000.021700
2 0.0217000.021700 0.0214930.021493 0.9933030.993303 0.9718100.971810 0.0459620.045962

The mean error at Δ=0.3\Delta = 0.3 is 0.52sin⁡(0.6π)=0.2378\frac{0.5}{2}\sin(0.6\pi) = 0.2378, but single values range from 0.180.18 to 0.970.97 in these three steps: the data pattern decides each one. The error τ−τk\tau - \tau_k is 0.2170.217 at k=10k = 10, 0.1610.161 at 2525, 0.0460.046 at 5050, −0.016-0.016 at 100100 and −0.011-0.011 at 200200. After that the loop does not settle to a point. It jitters around the delay with an rms error of 0.0160.016 and peaks of 0.040.04, without noise, because the detector has self-noise. For alternating data am=(−1)ma_m = (-1)^m the self-noise is zero, and the tests check that the loop then stays at τ\tau to 10−910^{-9}.

In noise at Es/N0=10E_s/N_0 = 10 dB with BLT=0.01B_LT = 0.01, the loop form of the MCRB is BLT/(4π2ξ SNR)=2.66×10−4B_LT/(4\pi^2\xi\,\mathrm{SNR}) = 2.66 \times 10^{-4}, with ξ=0.0951727\xi = 0.0951727 for β=0.5\beta = 0.5. Three runs of 2400 symbols (seeds 7, 8 and 9) measured a variance of 1.951.95 to 2.792.79 times this bound (measurement).

Correctness

Claim. For raised cosine pulses and independent zero-mean data, the mean of eke_k with τk\tau_k held fixed is β2sin⁡(2πd)sin⁡(2πΔ)\frac{\beta}{2}\sin(2\pi d)\sin(2\pi\Delta) with Δ=τ−τk\Delta = \tau - \tau_k, for every carrier phase and every stationary noise. So the loop has its stable points at Δ∈Z\Delta \in \mathbb{Z} and behaves near lock as a PLL with detector gain KD=πβsin⁡(2πd)K_D = \pi\beta\sin(2\pi d).

Key steps. The mean of ∣y(t)∣2|y(t)|^2 is ∑mg(t−m−τ)2\sum_m g(t - m - \tau)^2 plus the noise power, because the cross terms between symbols have mean zero. By Poisson summation, this folded energy of a raised cosine is 1−β4+β4cos⁡(2π(t−τ))1 - \frac{\beta}{4} + \frac{\beta}{4}\cos(2\pi(t - \tau)): only the spectral lines at 00 and ±1\pm1 survive, since ∣g∣2|g|^2 has a band below 2 cycles per symbol. The late sample minus the early sample removes the constant and the noise power and leaves the sine. The full proof is in the proofs file.

The S-curve is a closed form under assumptions: raised cosine pulses, independent data, and τk\tau_k constant while the mean is taken. The jitter of the example is a measurement.

Proof: The square-law early-late gate has the S-curve (beta/2) sin(2 pi d) sin(2 pi Delta) for raised cosine pulses, so the loop locks at the true delayfull

Times are in symbol periods, T=1T = 1. The matched filter output is y(t)=ejθ∑mamg(t−m−τ)+v(t)y(t) = e^{j\theta}\sum_m a_m g(t - m - \tau) + v(t), where gg is a raised cosine pulse with roll-off 0<β<10 < \beta < 1, the data ama_m are independent with Eam=0E a_m = 0 and E∣am∣2=1E|a_m|^2 = 1, θ\theta is any carrier phase and vv is noise independent of the data. Line L6 strobes at tk=k+τkt_k = k + \tau_k. Lines L3 and L4 take the early sample y(tk−d)y(t_k - d) and the late sample y(tk+d)y(t_k + d), and line L5 forms ek=∣y(tk+d)∣2−∣y(tk−d)∣2e_k = |y(t_k + d)|^2 - |y(t_k - d)|^2. Write Δ=τ−τk\Delta = \tau - \tau_k for the timing error. The S-curve is the mean of eke_k over data and noise with τk\tau_k held fixed.

Lemma 1 (folded energy of the raised cosine). For every real xx,

∑m∈Zg(x+m)2=1−β4+β4cos⁡(2πx).\sum_{m \in \mathbb{Z}} g(x + m)^2 = 1 - \frac{\beta}{4} + \frac{\beta}{4}\cos(2\pi x).

Proof. The function h=g2h = g^2 is integrable and its Fourier transform is H=G∗GH = G * G, where G(f)G(f) is the raised cosine spectrum. GG is 11 for ∣f∣≤1−β2|f| \le \frac{1-\beta}{2}, 12(1+cos⁡πβ(∣f∣−1−β2))\frac12\bigl(1 + \cos\frac{\pi}{\beta}(|f| - \frac{1-\beta}{2})\bigr) for 1−β2<∣f∣≤1+β2\frac{1-\beta}{2} < |f| \le \frac{1+\beta}{2}, and 00 beyond. By the Poisson summation formula, ∑mh(x+m)=∑nH(n)ej2πnx\sum_m h(x + m) = \sum_n H(n) e^{j2\pi n x}. HH is zero for ∣f∣>1+β|f| > 1 + \beta, and 1+β<21 + \beta < 2, so only n∈{−1,0,1}n \in \{-1, 0, 1\} remain. First, H(0)=∫G(f)2df=(1−β)+2∫0β14(1+cos⁡πvβ)2dv=1−β+3β4=1−β4H(0) = \int G(f)^2 df = (1 - \beta) + 2\int_0^\beta \frac14\bigl(1 + \cos\frac{\pi v}{\beta}\bigr)^2 dv = 1 - \beta + \frac{3\beta}{4} = 1 - \frac{\beta}{4}. Second, H(1)=∫G(f)G(1−f)dfH(1) = \int G(f) G(1 - f) df, and the product is nonzero only for f=12+uf = \frac12 + u with ∣u∣≤β2|u| \le \frac{\beta}{2}. There G(12+u)=12(1−sin⁡πuβ)G(\frac12 + u) = \frac12(1 - \sin\frac{\pi u}{\beta}) and G(12−u)=12(1+sin⁡πuβ)G(\frac12 - u) = \frac12(1 + \sin\frac{\pi u}{\beta}), so the product is 14cos⁡2πuβ\frac14\cos^2\frac{\pi u}{\beta} and its integral is β8\frac{\beta}{8}. H(−1)=H(1)H(-1) = H(1) because GG is even. So the sum is 1−β4+2⋅β8cos⁡(2πx)1 - \frac{\beta}{4} + 2\cdot\frac{\beta}{8}\cos(2\pi x). □\square

Lemma 2 (mean energy at an offset). Without noise, E∣y(tk+x)∣2=1−β4+β4cos⁡(2π(x−Δ))E|y(t_k + x)|^2 = 1 - \frac{\beta}{4} + \frac{\beta}{4}\cos(2\pi(x - \Delta)). With noise, a constant E∣v(t)∣2E|v(t)|^2 is added if vv is stationary.

Proof. y(tk+x)=ejθ∑mamg(k−m+x−Δ)y(t_k + x) = e^{j\theta}\sum_m a_m g(k - m + x - \Delta). The factor ejθe^{j\theta} has modulus one. Expand ∣⋅∣2|\cdot|^2 and take the mean: the cross terms E[amam′‾]E[a_m \overline{a_{m'}}] are zero for m≠m′m \ne m', so the mean is ∑mg(k−m+x−Δ)2\sum_m g(k - m + x - \Delta)^2. Lemma 1 with the integer shift kk gives the formula. The noise is independent of the data and has zero mean, so its cross terms with the signal vanish too. □\square

Theorem 1 (S-curve). For every gate half-width dd and every carrier phase θ\theta,

E[ek]=β2sin⁡(2πd)sin⁡(2πΔ).E[e_k] = \frac{\beta}{2}\sin(2\pi d)\sin(2\pi\Delta).

The slope at Δ=0\Delta = 0 is KD=πβsin⁡(2πd)K_D = \pi\beta\sin(2\pi d). It is largest at d=14d = \frac14 and zero at d=12d = \frac12.

Proof. By Lemma 2 with x=dx = d and x=−dx = -d, the constant terms and the noise power cancel, and

E[ek]=β4(cos⁡(2π(d−Δ))−cos⁡(2π(d+Δ)))=β4⋅2sin⁡(2πd)sin⁡(2πΔ).E[e_k] = \frac{\beta}{4}\bigl(\cos(2\pi(d - \Delta)) - \cos(2\pi(d + \Delta))\bigr) = \frac{\beta}{4}\cdot 2\sin(2\pi d)\sin(2\pi\Delta).

The derivative in Δ\Delta at 00 is πβsin⁡(2πd)\pi\beta\sin(2\pi d). □\square

Theorem 2 (lock points). For 0<d<120 < d < \frac12, the mean error is zero at Δ∈12Z\Delta \in \frac12\mathbb{Z}. The points Δ∈Z\Delta \in \mathbb{Z} are stable for the update τk+1=τk+Kpek+sk\tau_{k+1} = \tau_k + K_p e_k + s_k with Kp>0K_p > 0, and the points Δ∈12+Z\Delta \in \frac12 + \mathbb{Z} are unstable. So, in the mean, the loop locks at τk=τ\tau_k = \tau modulo one symbol.

Proof. sin⁡(2πΔ)\sin(2\pi\Delta) is zero exactly at Δ∈12Z\Delta \in \frac12\mathbb{Z}. Near Δ=0\Delta = 0, E[ek]≈KDΔE[e_k] \approx K_D\Delta with KD>0K_D > 0, so a positive error (the receiver is early) gives a positive step of τk\tau_k, which reduces Δ\Delta. Near Δ=12\Delta = \frac12 the slope is −KD<0-K_D < 0 and the step moves the error away. The linear analysis of the phase-locked loop, with the phase replaced by 2πτ2\pi\tau, gives convergence for 0<KpKD<20 < K_p K_D < 2 in the first-order loop and for the Jury conditions in the second-order loop. □\square

Theorem 3 (exact lock on alternating data). If am=(−1)ma_m = (-1)^m and there is no noise, then ek=sin⁡(2πd)sin⁡(2πΔ)e_k = \sin(2\pi d)\sin(2\pi\Delta) exactly, for every kk, so τk=τ\tau_k = \tau is an exact fixed point of the loop.

Proof. By Poisson summation, ∑m(−1)mg(t−m)=∑nG(n+12)ej2π(n+12)t\sum_m (-1)^m g(t - m) = \sum_n G(n + \frac12) e^{j2\pi(n + \frac12)t}. Only n=0n = 0 and n=−1n = -1 fall in the band, and G(±12)=12G(\pm\frac12) = \frac12 for every β\beta, so y(t)=±ejθcos⁡(π(t−τ))y(t) = \pm e^{j\theta}\cos(\pi(t - \tau)). Then ∣y(tk+x)∣2=cos⁡2(π(k+x−Δ))=12(1+cos⁡(2π(x−Δ)))|y(t_k + x)|^2 = \cos^2(\pi(k + x - \Delta)) = \frac12\bigl(1 + \cos(2\pi(x - \Delta))\bigr), and the same subtraction as in Theorem 1 gives the result. At Δ=0\Delta = 0 the error is 00, and with s=0s = 0 lines L7 and L8 leave τk\tau_k unchanged. □\square

The S-curve is a closed form under assumptions: raised cosine pulses, independent zero-mean data, and τk\tau_k constant while the mean is taken. With random data the error eke_k also has a part that depends on the data pattern and is not zero at Δ=0\Delta = 0 (self-noise), so the loop jitters around the lock point even without noise. Theorem 3 shows that this part is zero for alternating data. The tests check Theorem 1 by averaging eke_k over 1280 random QPSK symbols at three offsets, and Theorem 3 to 10−910^{-9}.

Complexity

Each symbol needs three samples of yy (lines L3, L4 and L6), two squared magnitudes of two real multiplications and one addition each, one subtraction, and the four operations of lines L7 and L8. With an interpolator of KK taps, each sample costs KK more multiply-adds, so a symbol costs 3K+O(1)3K + O(1) operations. The loop is O(1)O(1) per symbol and O(N)O(N) for NN symbols in the real RAM, with O(1)O(1) extra space besides the output. It is sequential, like every feedback loop.

Properties

  • Non-data-aided and phase-free. The detector uses only ∣y∣2|y|^2, so a carrier phase or a slow frequency offset does not change it. Timing can be recovered before the carrier.
  • Needs excess bandwidth. The slope πβsin⁡(2πd)\pi\beta\sin(2\pi d) is proportional to the roll-off β\beta. For β→0\beta \to 0 the energy of the signal no longer varies with tt on average, and the detector has no mean output.
  • Self-noise. At Δ=0\Delta = 0 the mean is zero, but each eke_k depends on the neighboring symbols, so the loop jitters even without noise. The jitter falls with the loop bandwidth.
  • Mean offset in the loop (measurement). In the runs of the tests and the example, the mean of τk−τ\tau_k - \tau was about 1.05 BLT1.05\,B_LT without noise: 0.01050.0105 at BLT=0.01B_LT = 0.01 and 0.00260.0026 at BLT=0.0025B_LT = 0.0025, over three seeds each. A likely cause is that τk\tau_k depends on past symbols that also enter eke_k through the pulse tails (heuristic). The offset shrinks with the bandwidth.
  • Gain depends on amplitude. KDK_D scales with the square of the signal amplitude, so the loop bandwidth changes with the received level unless an automatic gain control keeps it fixed.

Variants and improvements

  • Absolute-value gate. e=∣y(t+d)∣−∣y(t−d)∣e = |y(t + d)| - |y(t - d)|. Simon analyzed this form in 1970 and found it less noisy than two other bit synchronizers of that time.
  • Derivative form. As d→0d \to 0, (∣y(t+d)∣2−∣y(t−d)∣2)/(2d)→2Re⁡{y(t)‾ y′(t)}(|y(t + d)|^2 - |y(t - d)|^2)/(2d) \to 2\operatorname{Re}\{\overline{y(t)}\,y'(t)\}. This is the non-data-aided maximum likelihood timing detector at low SNR. It needs a derivative filter instead of two extra samples.
  • Decision-directed gate. e=Re⁡{a^k‾ (y(t+d)−y(t−d))}e = \operatorname{Re}\{\overline{\hat a_k}\,(y(t + d) - y(t - d))\} uses the decision a^k\hat a_k. It removes most of the self-noise at high SNR, but it needs the carrier phase.
  • Integrate-and-dump gates. For rectangular pulses, analog bit synchronizers integrate the signal over an early half and a late half of the symbol and compare the magnitudes. This is the classic form in telemetry receivers.
  • Delay-locked loop. The same early-late structure on a known spreading code tracks the code phase; see delay-locked loop.
  • Fewer samples. The Gardner detector needs two samples per symbol, and the Mueller and Muller detector needs one.

When to use it

Use an early-late gate when the receiver has three samples per symbol or a continuous-time signal, the pulses have a clear excess bandwidth, and the timing must be found before the carrier. It is easy to build in analog hardware, which is why older bit synchronizers used it. In a digital modem at two samples per symbol, the Gardner detector does the same job with less self-noise. At high SNR with a known carrier phase, a decision-directed detector is more accurate.

Pitfalls

  • Gate half-width d=T/2d = T/2. For raised cosine pulses, sin⁡(2πd)=0\sin(2\pi d) = 0 and the square-law gate has no restoring force. Use dd near T/4T/4.
  • Wrong sign of the update. The error is positive when the strobe is early. If the update subtracts it, the loop locks at the worst point, half a symbol from the peak.
  • Gain not divided by KDK_D. The slope depends on β\beta, dd and the signal power. Using a fixed KpK_p for another roll-off or another level changes the loop bandwidth.
  • Too wide a loop. The self-noise grows with BLTB_LT. A loop that is fast enough for acquisition may be too noisy for tracking; switch to a narrower bandwidth after lock.
  • Small roll-off. At β=0.1\beta = 0.1 the slope is a fifth of that at β=0.5\beta = 0.5, and the self-noise is relatively larger. Narrowband systems need a data-aided or decision-directed detector.

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.

early_late_gate_synchronizer.pyI0RawGitHub
# SPDX-License-Identifier: MIT
# Part of https://algorithm.tamnd.com/early-late-gate-synchronizer/. The authors are listed in prov/Z897RY.jsonl.

"""The early-late gate symbol synchronizer, in its square-law (non-data-aided) form.

Times are in symbol periods T. The receiver has a sampler y(t) for the matched
filter output: an ADC with a controlled clock, or an interpolator on a fixed
sample grid. The symbols sit at t = k + tau for an unknown delay tau. For the
estimate tau_k, the gate takes the early sample y(k + tau_k - d) and the late
sample y(k + tau_k + d) and forms

    e_k = |y(k + tau_k + d)|^2 - |y(k + tau_k - d)|^2.

If the receiver samples too early (tau_k < tau), the late sample is nearer the
peak of the pulse and e_k > 0. A first- or second-order loop moves tau_k:

    s <- s + ki e_k,   tau_{k+1} = tau_k + kp e_k + s.

For raised cosine pulses with roll-off beta and i.i.d. zero-mean data with
E|a|^2 = 1, the mean of e_k is (beta/2) sin(2 pi d) sin(2 pi delta), where
delta = tau - tau_k. The slope at lock is pi beta sin(2 pi d): it is largest
at d = 1/4 and it is zero at d = 1/2.
"""

import math
from typing import Callable

Sampler = Callable[[float], complex]


def detector(sample: Sampler, t: float, d: float = 0.25) -> float:
    """The early-late error at the strobe time t, with the gate half-width d."""
    late = sample(t + d)  # @line L4
    early = sample(t - d)  # @line L3
    return abs(late) ** 2 - abs(early) ** 2  # @line L5


def s_curve(delta: float, beta: float, d: float = 0.25) -> float:
    """Mean detector output for raised cosine pulses and i.i.d. data, E|a|^2 = 1."""
    return 0.5 * beta * math.sin(2 * math.pi * d) * math.sin(2 * math.pi * delta)


def gain_for_bandwidth(bl_t: float, kd: float) -> float:
    """kp of a first-order loop with noise bandwidth B_L T for the detector slope kd."""
    return 4.0 * bl_t / ((1.0 + 2.0 * bl_t) * kd)


def early_late_gate(
    sample: Sampler,
    n: int,
    kp: float,
    ki: float = 0.0,
    tau0: float = 0.0,
    d: float = 0.25,
) -> tuple[list[float], list[complex]]:
    """Run the loop over n symbols. Return the estimates tau_k and the strobes y(k + tau_k)."""
    tau, s = tau0, 0.0  # @line L1
    taus: list[float] = []
    strobes: list[complex] = []
    for k in range(n):  # @line L2
        t = k + tau
        taus.append(tau)
        strobes.append(sample(t))  # @line L6
        e = detector(sample, t, d)  # @line L3
        s += ki * e  # @line L7
        tau += kp * e + s  # @line L8
    return taus, strobes  # @line L9
test_early_late_gate_synchronizer.py 146 lines
# SPDX-License-Identifier: MIT
# Part of https://algorithm.tamnd.com/early-late-gate-synchronizer/. The authors are listed in prov/Z897RY.jsonl.

import cmath
import math
import random

from early_late_gate_synchronizer import detector, early_late_gate, gain_for_bandwidth, s_curve

BETA = 0.5
TAU = 0.3
QPSK = [cmath.exp(1j * (math.pi / 4 + m * math.pi / 2)) for m in range(4)]


def rc(t, b):
    """The raised cosine pulse with roll-off b and T = 1."""
    if abs(t) < 1e-12:
        return 1.0
    if abs(abs(t) - 1 / (2 * b)) < 1e-9:
        return math.pi / 4 * math.sin(math.pi / (2 * b)) / (math.pi / (2 * b))
    return math.sin(math.pi * t) / (math.pi * t) * math.cos(math.pi * b * t) / (1 - (2 * b * t) ** 2)


def rrc(t, b):
    """The root raised cosine pulse with roll-off b and T = 1."""
    if abs(t) < 1e-12:
        return 1 - b + 4 * b / math.pi
    if abs(abs(t) - 1 / (4 * b)) < 1e-9:
        return b / math.sqrt(2) * ((1 + 2 / math.pi) * math.sin(math.pi / (4 * b)) + (1 - 2 / math.pi) * math.cos(math.pi / (4 * b)))
    return (math.sin(math.pi * (1 - b) * t) + 4 * b * t * math.cos(math.pi * (1 + b) * t)) / (math.pi * t * (1 - (4 * b * t) ** 2))


def make_sampler(a, tau, n0=0.0, seed=0, theta=0.0, span=4, reach=8):
    """Matched filter output y(t) = e^(j theta) sum a_k g(t - k - tau) + v(t), g raised cosine.

    The noise is v(t) = sum_n w_n rrc(t - n h) with h = 1/2 and w_n i.i.d.
    CN(0, h n0). Since rrc has bandwidth (1 + beta)/2 < 1/(2h), v is stationary
    with autocorrelation n0 g(t - s): the matched filter output of white noise
    of density n0 (up to the cut of rrc at span symbols).
    """
    rot = cmath.exp(1j * theta)
    h = 0.5
    n_lo = int(-6 / h)
    w = None
    if n0 > 0:
        rng = random.Random(seed)
        sd = math.sqrt(h * n0 / 2)
        w = [complex(rng.gauss(0, sd), rng.gauss(0, sd)) for _ in range(int((len(a) + 12) / h))]
    reach_w = int(span / h)

    def sample(t):
        k0 = int(round(t - tau))
        acc = 0j
        for k in range(max(0, k0 - reach), min(len(a), k0 + reach + 1)):
            acc += a[k] * rc(t - k - tau, BETA)
        acc *= rot
        if w is not None:
            c = int(round(t / h))
            for n in range(c - reach_w, c + reach_w + 1):
                acc += w[n - n_lo] * rrc(t - n * h, BETA)
        return acc

    return sample


def qpsk(n, seed):
    rng = random.Random(seed)
    return [rng.choice(QPSK) for _ in range(n)]


def test_exact_lock_on_alternating_data():
    # With a_k = (-1)^k the signal is cos(pi (t - tau)) and the error is
    # sin(2 pi d) sin(2 pi (tau - tau_k)) exactly, so the loop has no self-noise.
    a = [(-1) ** k + 0j for k in range(400)]
    sample = make_sampler(a, TAU)
    taus, _ = early_late_gate(sample, 360, kp=0.1)
    for t in taus[300:]:
        assert abs(t - TAU) < 1e-9


def test_detector_mean_matches_s_curve():
    a = qpsk(1300, 2)
    sample = make_sampler(a, TAU)
    for delta in (-0.3, 0.1, 0.2):
        es = [detector(sample, k + TAU - delta) for k in range(10, 1290)]
        mean = sum(es) / len(es)
        # The standard error of the mean is about 0.02 here.
        assert abs(mean - s_curve(delta, BETA)) < 0.06


def test_gate_half_width_one_half_gives_no_error_signal():
    a = qpsk(1300, 3)
    sample = make_sampler(a, TAU)
    es = [detector(sample, k + TAU - 0.2, d=0.5) for k in range(10, 1290)]
    assert abs(sum(es) / len(es)) < 0.04
    assert abs(s_curve(0.2, BETA, d=0.5)) < 1e-15


def test_carrier_phase_does_not_change_the_loop():
    a = qpsk(300, 4)
    t1, _ = early_late_gate(make_sampler(a, TAU), 250, kp=0.05)
    t2, _ = early_late_gate(make_sampler(a, TAU, theta=1.1), 250, kp=0.05)
    assert max(abs(x - y) for x, y in zip(t1, t2)) < 1e-9


def test_random_data_noiseless_settles_near_true_delay():
    a = qpsk(1700, 5)
    kd = math.pi * BETA * math.sin(2 * math.pi * 0.25)
    kp = gain_for_bandwidth(0.0025, kd)
    taus, _ = early_late_gate(make_sampler(a, TAU), 1650, kp=kp)
    tail = taus[800:]
    mean = sum(tail) / len(tail)
    assert abs(mean - TAU) < 0.006
    assert max(abs(t - TAU) for t in tail) < 0.015


def test_second_order_loop_tracks_clock_offset():
    # A sampler whose symbols drift by 1e-3 T per symbol: tau grows like a ramp.
    a = [(-1) ** k + 0j for k in range(700)]
    drift = 1e-3

    def sample(t):
        k0 = int(round(t))
        acc = 0j
        for k in range(max(0, k0 - 16), min(len(a), k0 + 17)):
            acc += a[k] * rc(t - k - TAU - drift * k, BETA)
        return acc

    taus, _ = early_late_gate(sample, 600, kp=0.1, ki=0.005)
    for k in range(450, 600):
        assert abs(taus[k] - (TAU + drift * k)) < 2e-3


def test_noise_variance_against_mcrb():
    snr = 10.0
    bl_t = 0.01
    xi = 1.0 / 12.0 + BETA ** 2 * (0.25 - 2.0 / math.pi ** 2)
    mcrb = bl_t / (4 * math.pi ** 2 * xi * snr)
    a = qpsk(2400, 6)
    sample = make_sampler(a, TAU, n0=1.0 / snr, seed=7)
    kd = math.pi * BETA
    taus, _ = early_late_gate(sample, 2350, kp=gain_for_bandwidth(bl_t, kd))
    tail = taus[400:]
    mean = sum(tail) / len(tail)
    var = sum((t - mean) ** 2 for t in tail) / len(tail)
    assert 1.0 <= var / mcrb <= 4.0

History

Range trackers in radar keep a gate centered on an echo pulse by comparing the energy in an early half gate and a late half gate, and early bit synchronizers for digital telemetry used the same idea. Marvin K. Simon, at the Jet Propulsion Laboratory, published a nonlinear analysis of the absolute-value early-late gate bit synchronizer in 1970, with its S-curve and its steady-state timing error. With digital modems, the gate became a few lines of code on interpolated samples, as above, and later detectors such as Gardner's (1986) reduced the number of samples per symbol.

References

  1. M. K. Simon, Nonlinear analysis of an absolute value type of an early-late gate bit synchronizer, IEEE Transactions on Communication Technology 18(5), 1970, pages 589 to 596, the absolute-value early-late gate, its S-curve and its steady-state timing error by the Fokker-Planck method
  2. John G. Proakis and Masoud Salehi, Digital Communications, fifth edition, McGraw-Hill, 2008, Section 5.3 (symbol timing estimation, with the early-late gate synchronizer as a non-decision-directed method)
  3. Umberto Mengali and Aldo N. D'Andrea, Synchronization Techniques for Digital Receivers, Plenum Press, 1997, Chapter 7 (timing recovery in baseband transmission) and Chapter 8 (timing recovery with linear modulations)

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