Algorithm Search⌘K

Algorithmcore

Farrow interpolator

Also called Farrow structure, Farrow filter.

A polynomial interpolation filter. Fixed FIR filters give the coefficients of a polynomial in the fractional delay mu, and Horner's rule evaluates it, so a new delay costs no new filter design.

Time
O(N D K) totalO(D K) per_output
Space
O(D) extra
Where
D = the degree of the polynomial; K = the number of taps per filter; N = the number of output samples
Properties
deterministiclinearonlinepasses through samples
Origin
C. W. Farrow, 1988

The Farrow interpolator computes the value of a sampled signal between its samples, at a fractional delay μ\mu that can change at every output. It writes the interpolated value as a polynomial in μ\mu, y(m+μ)=∑i=0Dci[m] μiy(m + \mu) = \sum_{i=0}^{D} c_i[m]\,\mu^i, where each coefficient ci[m]c_i[m] is the output of a fixed FIR filter on the input around the basepoint mm. Horner's rule then evaluates the polynomial. A change of delay needs no new filter taps, only a new value of μ\mu. In all-digital receivers the sampling clock is free-running, and the timing loop or estimator gives the strobe times as mk+μkm_k + \mu_k. The Farrow interpolator produces the strobes for the Gardner detector, the Mueller and Müller loop and the Oerder and Meyr estimator. The two standard coefficient sets are cubic Lagrange interpolation through four samples and the piecewise parabolic interpolator of Erup, Gardner and Harris.

In plain words

A receiver often takes samples at moments that do not match the moments it needs. To get a value between two samples, it fits a smooth curve through a few nearby samples and reads the curve at the wanted point. The Farrow method does the fit with a few fixed filters. Each filter gives one number that describes the curve. To read the curve at a point, it only needs the position of that point between the samples. So the receiver can move the reading point at every step for very little work, and it never has to design a new filter.

The problem

The input is a sequence x[n]=f(n)x[n] = f(n), sampled on a grid of spacing Ts=1T_s = 1. A second input is a sequence of output times tkt_k, each split as tk=mk+μkt_k = m_k + \mu_k with the basepoint mk=⌊tk⌋m_k = \lfloor t_k\rfloor and the fractional interval μk∈[0,1)\mu_k \in [0, 1). The output is yk≈f(tk)y_k \approx f(t_k) for each kk. The interpolator is a linear combination of the samples near mkm_k whose weights depend on μk\mu_k. Its quality is the error ∣yk−f(tk)∣|y_k - f(t_k)| for band-limited ff, which depends on the frequency response of the equivalent continuous filter. For a timing loop, the times come from a numerically controlled oscillator with a step of W=Ti/TsW = T_i/T_s input samples.

The algorithm

  1. FARROW(x, m, mu, b[0..D][0..3])
  2. 1w <- (x[m-1], x[m], x[m+1], x[m+2])
  3. 2c[i] <- sum_j b[i][j] * w[j] for i <- 0 to D ▷ D+1 fixed FIR filters
  4. 3y <- c[D]
  5. 4for i <- D - 1 downto 0 do
  6. 5y <- y * mu + c[i] ▷ Horner's rule
  7. 6return y
  8. RESAMPLE(x, times, b)
  9. for each t in times do
  10. 7m <- floor(t)
  11. 8mu <- t - m
  12. 9append FARROW(x, m, mu, b) to the output

Line L1 takes four samples around the basepoint. For cubic Lagrange interpolation (D=3D = 3) the coefficient rows are

b=(0100−13−121−1612−1120−1612−1216).b = \begin{pmatrix} 0 & 1 & 0 & 0 \\ -\frac13 & -\frac12 & 1 & -\frac16 \\ \frac12 & -1 & \frac12 & 0 \\ -\frac16 & \frac12 & -\frac12 & \frac16 \end{pmatrix}.

For the piecewise parabolic interpolator (D=2D = 2) with parameter α\alpha they are (0,1,0,0)(0, 1, 0, 0), (−α,α−1,α+1,−α)(-\alpha, \alpha - 1, \alpha + 1, -\alpha) and (α,−α,−α,α)(\alpha, -\alpha, -\alpha, \alpha). With α=12\alpha = \frac12 all taps are multiples of 12\frac12, so the filters need only shifts and additions. Line L2 is the filter bank. When several outputs share the same basepoint, as in upsampling, line L2 runs once for them. Lines L3 to L5 evaluate the polynomial.

Example

Take x[m−1..m+2]=1,3,2,0x[m-1..m+2] = 1, 3, 2, 0 and μ=0.4\mu = 0.4. With the cubic rows, line L2 gives c=(3,0.16667,−1.5,0.33333)c = (3, 0.16667, -1.5, 0.33333). Lines L3 to L5 compute:

step value
L3: y=c3y = c_3 0.333330.33333
L5: yμ+c2y\mu + c_2 −1.36667-1.36667
L5: yμ+c1y\mu + c_1 −0.38-0.38
L5: yμ+c0y\mu + c_0 2.8482.848

The same value comes from the Lagrange weights at μ=0.4\mu = 0.4, which are −0.064,0.672,0.448,−0.056-0.064, 0.672, 0.448, -0.056. The parabolic interpolator with α=12\alpha = \frac12 gives c=(3,1,−2)c = (3, 1, -2) and y=3+0.4−0.32=3.08y = 3 + 0.4 - 0.32 = 3.08.

For a sinusoid of unit amplitude at frequency ν\nu, the largest error over all phases and over a grid of 2001 values of μ\mu was (computed), with the theoretical bound 3128(2πν)4\frac{3}{128}(2\pi\nu)^4 of the cubic:

ν\nu (cycles per sample) cubic parabolic, α=12\alpha = \frac12 cubic bound
0.050.05 2.26×10−42.26 \times 10^{-4} 0.01190.0119 2.28×10−42.28 \times 10^{-4}
0.10.1 3.54×10−33.54 \times 10^{-3} 0.04190.0419 3.65×10−33.65 \times 10^{-3}
0.250.25 0.1160.116 0.06070.0607 0.1430.143

At four samples per symbol, a raised cosine signal with β=12\beta = \frac12 has no energy above ν=(1+β)/8=0.1875\nu = (1 + \beta)/8 = 0.1875 cycles per sample, so the cubic error is small for the signals of a timing loop.

Correctness

Claim. With the cubic rows, the output is the unique polynomial of degree at most 3 through the four samples, read at μ\mu. If ff has four continuous derivatives, the error is at most 3128max⁡∣f′′′′∣\frac{3}{128}\max|f''''| for μ∈[0,1)\mu \in [0, 1). The parabolic interpolator is exact for polynomials of degree at most 1 and passes through the samples, but not for degree 2: on x[n]=n2x[n] = n^2 at μ=12\mu = \frac12 it returns 00 instead of 14\frac14 when α=12\alpha = \frac12.

Key steps. By Horner's rule, y=∑jx[m−1+j] ℓj(μ)y = \sum_j x[m - 1 + j]\,\ell_j(\mu) with ℓj(μ)=∑ibijμi\ell_j(\mu) = \sum_i b_{ij}\mu^i. Each column of the cubic rows is the coefficient vector of the Lagrange basis polynomial for the node j−1j - 1, so the output is the interpolating polynomial. The Lagrange remainder is f′′′′(ξ)24(μ+1)μ(μ−1)(μ−2)\frac{f''''(\xi)}{24}(\mu + 1)\mu(\mu - 1)(\mu - 2), and the product has its largest magnitude on [0,1][0, 1] at μ=12\mu = \frac12, where it is 916\frac{9}{16}. The full proof is in the proofs file. The error bound is a theorem. The sinusoid errors are measurements.

Proof: The cubic Farrow interpolator equals cubic Lagrange interpolation through four samples, with error at most (3/128) max |f''''|, and the parabolic form is exact for linesfull

The input is x[n]=f(n)x[n] = f(n) on a grid of spacing Ts=1T_s = 1. Line L1 takes the window x[m−1],x[m],x[m+1],x[m+2]x[m - 1], x[m], x[m + 1], x[m + 2]. Line L2 computes ci=∑j=03bij x[m−1+j]c_i = \sum_{j=0}^{3} b_{ij}\,x[m - 1 + j] for i=0,…,Di = 0, \ldots, D, and lines L3 to L5 compute y=(⋯(cDμ+cD−1)μ+⋯ )μ+c0y = (\cdots(c_D\mu + c_{D-1})\mu + \cdots)\mu + c_0 by Horner's rule. Lines L7 and L8 split an output time tt into m=⌊t⌋m = \lfloor t\rfloor and μ=t−m∈[0,1)\mu = t - m \in [0, 1).

Lemma 1 (Horner's rule). Lines L3 to L5 return ∑i=0Dciμi\sum_{i=0}^{D} c_i\mu^i.

Proof. Let yry_r be the value after the step with index i=D−ri = D - r, so y0=cDy_0 = c_D. By induction, yr=∑i=D−rDciμi−D+ry_r = \sum_{i=D-r}^{D} c_i\mu^{i-D+r}: the step y←yμ+cD−r−1y \leftarrow y\mu + c_{D-r-1} multiplies each term by μ\mu and adds the next coefficient. After r=Dr = D steps the sum is ∑i=0Dciμi\sum_{i=0}^{D} c_i\mu^i. □\square

Theorem 1 (cubic Lagrange). With the rows of CUBIC_LAGRANGE,

b=(0100−13−121−1612−1120−1612−1216),b = \begin{pmatrix} 0 & 1 & 0 & 0 \\ -\frac13 & -\frac12 & 1 & -\frac16 \\ \frac12 & -1 & \frac12 & 0 \\ -\frac16 & \frac12 & -\frac12 & \frac16 \end{pmatrix},

the output y(μ)y(\mu) is the unique polynomial of degree at most 3 that takes the values x[m−1],x[m],x[m+1],x[m+2]x[m - 1], x[m], x[m + 1], x[m + 2] at μ=−1,0,1,2\mu = -1, 0, 1, 2.

Proof. By Lemma 1, y(μ)=∑jx[m−1+j] ℓj(μ)y(\mu) = \sum_j x[m - 1 + j]\,\ell_j(\mu) with ℓj(μ)=∑ibijμi\ell_j(\mu) = \sum_i b_{ij}\mu^i. Column jj of bb holds the coefficients of the Lagrange basis polynomial for the node j−1j - 1: ℓ0(μ)=−16μ(μ−1)(μ−2)=−13μ+12μ2−16μ3\ell_0(\mu) = -\frac16\mu(\mu - 1)(\mu - 2) = -\frac13\mu + \frac12\mu^2 - \frac16\mu^3, ℓ1(μ)=12(μ+1)(μ−1)(μ−2)=1−12μ−μ2+12μ3\ell_1(\mu) = \frac12(\mu + 1)(\mu - 1)(\mu - 2) = 1 - \frac12\mu - \mu^2 + \frac12\mu^3, ℓ2(μ)=−12(μ+1)μ(μ−2)=μ+12μ2−12μ3\ell_2(\mu) = -\frac12(\mu + 1)\mu(\mu - 2) = \mu + \frac12\mu^2 - \frac12\mu^3, ℓ3(μ)=16(μ+1)μ(μ−1)=−16μ+16μ3\ell_3(\mu) = \frac16(\mu + 1)\mu(\mu - 1) = -\frac16\mu + \frac16\mu^3. Each ℓj\ell_j is 11 at its own node and 00 at the other three, so yy takes the four sample values. A polynomial of degree at most 3 through four distinct points is unique, because the difference of two such polynomials has four roots. □\square

Corollary 1. The output is exact when ff is a polynomial of degree at most 3. At μ=0\mu = 0 it returns x[m]x[m], and the four weights sum to 11 for every μ\mu.

Theorem 2 (error bound). If ff has four continuous derivatives on [m−1,m+2][m - 1, m + 2] and ∣f(4)∣≤M4|f^{(4)}| \le M_4 there, then for 0≤μ≤10 \le \mu \le 1,

∣f(m+μ)−y(μ)∣≤M424 ∣(μ+1)μ(μ−1)(μ−2)∣≤3128M4.|f(m + \mu) - y(\mu)| \le \frac{M_4}{24}\,|(\mu + 1)\mu(\mu - 1)(\mu - 2)| \le \frac{3}{128}M_4.

Proof. The first inequality is the standard remainder of Lagrange interpolation: f(m+μ)−y(μ)=f(4)(ξ)4!∏j(μ−μj)f(m + \mu) - y(\mu) = \frac{f^{(4)}(\xi)}{4!}\prod_{j}(\mu - \mu_j) for some ξ\xi in the interval, with the nodes μj∈{−1,0,1,2}\mu_j \in \{-1, 0, 1, 2\}. For the second, put v=μ−12∈[−12,12]v = \mu - \frac12 \in [-\frac12, \frac12]. Then (μ+1)(μ−2)=v2−94(\mu + 1)(\mu - 2) = v^2 - \frac94 and μ(μ−1)=v2−14\mu(\mu - 1) = v^2 - \frac14, so the product is (94−v2)(14−v2)(\frac94 - v^2)(\frac14 - v^2) in absolute value. Both factors are nonnegative and decrease in v2v^2, so the maximum is at v=0v = 0: 94⋅14=916\frac94\cdot\frac14 = \frac{9}{16}. And 916⋅124=3128\frac{9}{16}\cdot\frac{1}{24} = \frac{3}{128}. □\square

For f(t)=cos⁡(ωt)f(t) = \cos(\omega t), M4=ω4M_4 = \omega^4. This is a theorem; how close the bound is for a given ff is a measurement, which the page reports.

Theorem 3 (piecewise parabolic form). With the rows (0,1,0,0)(0, 1, 0, 0), (−α,α−1,α+1,−α)(-\alpha, \alpha - 1, \alpha + 1, -\alpha) and (α,−α,−α,α)(\alpha, -\alpha, -\alpha, \alpha), for every α\alpha, the output returns x[m]x[m] at μ=0\mu = 0, a constant input gives the same constant, and a linear input x[n]=p+qnx[n] = p + qn gives p+q(m+μ)p + q(m + \mu) exactly. The output is continuous in tt: the value at μ→1\mu \to 1 in window mm equals the value at μ=0\mu = 0 in window m+1m + 1.

Proof. At μ=0\mu = 0, y=c0=x[m]y = c_0 = x[m]. Rows 1 and 2 sum to zero, so a constant gives c1=c2=0c_1 = c_2 = 0. For x[n]=nx[n] = n around m=0m = 0, c1=α+0+(α+1)−2α=1c_1 = \alpha + 0 + (\alpha + 1) - 2\alpha = 1 and c2=−α−0−α+2α=0c_2 = -\alpha - 0 - \alpha + 2\alpha = 0, so y=μy = \mu; linearity and a shift give the general line. At μ=1\mu = 1, y=c0+c1+c2=x[m]+(−α+α)x[m−1]+(α−1−α)x[m]+(α+1−α)x[m+1]+(−α+α)x[m+2]=x[m+1]y = c_0 + c_1 + c_2 = x[m] + (-\alpha + \alpha)x[m - 1] + (\alpha - 1 - \alpha)x[m] + (\alpha + 1 - \alpha)x[m + 1] + (-\alpha + \alpha)x[m + 2] = x[m + 1], which is the value at μ=0\mu = 0 in the next window. □\square

The parabolic form is not exact for a quadratic when α=12\alpha = \frac12: for x[n]=n2x[n] = n^2 at μ=12\mu = \frac12 the taps are −18,58,58,−18-\frac18, \frac58, \frac58, -\frac18 on the values 1,0,1,41, 0, 1, 4, and the output is 00 instead of 14\frac14. Cubic Lagrange has the taps −116,916,916,−116-\frac{1}{16}, \frac{9}{16}, \frac{9}{16}, -\frac{1}{16} there and returns 14\frac14. Its value is a frequency response that Erup, Gardner and Harris chose for modems, with a cost of only two multiplications for c1c_1 and c2c_2 after shared additions. The tests check Theorem 1 against a direct Lagrange sum to 10−1210^{-12}, Corollary 1 on random cubics, Theorem 2 on a sinusoid, and Theorem 3.

Complexity

In the real RAM, line L2 costs (D+1)K(D + 1)K multiply-adds with K=4K = 4 taps, and lines L3 to L5 cost DD multiply-adds. So one output costs O(DK)O(DK) operations, which is 16 plus 3 for the cubic and 12 plus 2 for the parabolic, fewer when the zero and unit taps are skipped. NN outputs cost O(NDK)O(NDK), and the extra space is O(D)O(D) for the coefficients. A filter whose taps are designed for each new μ\mu would need KK evaluations of a μ\mu-dependent kernel per output instead. The hardware cost is fixed filters and DD multipliers by μ\mu.

Properties

  • Linear and time-varying. For fixed μ\mu it is an FIR filter. Its continuous impulse response h(t)h(t), with y(t)=∑nx[n]h(t−n)y(t) = \sum_n x[n]h(t - n), is piecewise polynomial on [−2,2][-2, 2]. At t=−1.5,−0.5,0.5,1.5t = -1.5, -0.5, 0.5, 1.5 its values are −116,916,916,−116-\frac1{16}, \frac9{16}, \frac9{16}, -\frac1{16} for the cubic and −18,58,58,−18-\frac18, \frac58, \frac58, -\frac18 for the parabolic with α=12\alpha = \frac12.
  • Passes through the samples. At μ=0\mu = 0 the output is x[m]x[m] for both coefficient sets.
  • Exact on polynomials. The cubic form is exact on polynomials of degree at most 3. The parabolic form is exact on degree at most 1.
  • Response to a constant. Both sets give the constant back for every μ\mu, so the interpolator does not modulate a flat signal.
  • Delay in one parameter. The filters do not depend on μ\mu. Only the Horner step does.

Variants and improvements

  • Higher degree and more taps. Lagrange interpolation with more points, or polynomial fits to a longer ideal interpolator, give better frequency response at the cost of more filters.
  • Optimized coefficients. Instead of Lagrange coefficients, the rows can be designed to minimize the error over a frequency band. Any piecewise polynomial impulse response has a Farrow form.
  • Parabolic with other α\alpha. Erup, Gardner and Harris studied how α\alpha changes the performance in a modem. The value 12\frac12 is the cheap choice.
  • Polyphase interpolators. A bank of fixed filters, one per quantized value of μ\mu, is the alternative. It needs memory for every phase but no multiplication by μ\mu.
  • Splines. Cubic spline interpolation is smoother but depends on all samples, so it is not a short filter in its exact form.

When to use it

Use the Farrow interpolator when the receiver samples with a free-running clock and the timing must be corrected digitally, when the delay changes at every output, or when the output rate is not a simple ratio of the input rate, as in sample rate conversion by an arbitrary factor. At two to four samples per symbol the cubic or the parabolic form is enough for most modems. When the signal is close to the Nyquist frequency, use more taps or an optimized design.

Pitfalls

  • Wrong basepoint. The window is x[m−1]x[m - 1] to x[m+2]x[m + 2] with μ∈[0,1)\mu \in [0, 1). Using μ∈[−12,12)\mu \in [-\frac12, \frac12) with the same rows reads the curve in the wrong interval.
  • Edges. An output time needs one sample before and two after its basepoint. The implementation only returns times with a full window.
  • Underestimating the error at high frequency. At a quarter of the sample rate the cubic error is about 12%12\% of the amplitude. The table shows that the cubic is not always better than the parabolic there.
  • Mixing the timing estimate and μ\mu. A timing estimate τ^\hat\tau in symbols must be converted to input samples, and its integer part goes into the basepoint. A carry from μ\mu to mm that is lost causes a slip of one sample.

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.

farrow_interpolator.pyI0RawGitHub
# SPDX-License-Identifier: MIT
# Part of https://algorithm.tamnd.com/farrow-interpolator/. The authors are listed in prov/9JVEXY.jsonl.

"""The Farrow interpolator: a polynomial interpolation filter with the fractional delay as one parameter.

The input is a sequence x[n] on a grid of spacing T_s = 1. The output at the
time t = m + mu (basepoint index m, fractional interval 0 <= mu < 1) is

    y(m + mu) = sum_{i=0}^{D} c_i[m] mu^i,   c_i[m] = sum_{n} b[i][n] x[m + n],

where each c_i is the output of a fixed FIR filter b[i]. The polynomial in mu is
evaluated by Horner's rule. Two coefficient sets are given:

- cubic Lagrange (D = 3), taps at n = -1, 0, 1, 2: the polynomial through the
  four samples x[m-1], ..., x[m+2];
- piecewise parabolic (D = 2) of Erup, Gardner and Harris with parameter alpha,
  taps at n = -1, 0, 1, 2.

Both pass through the samples: y(m + 0) = x[m].
"""

import math
from typing import Sequence

# Rows are the filters b[0], ..., b[D]; columns are the taps n = -1, 0, 1, 2.
CUBIC_LAGRANGE: list[list[float]] = [
    [0.0, 1.0, 0.0, 0.0],
    [-1.0 / 3.0, -1.0 / 2.0, 1.0, -1.0 / 6.0],
    [1.0 / 2.0, -1.0, 1.0 / 2.0, 0.0],
    [-1.0 / 6.0, 1.0 / 2.0, -1.0 / 2.0, 1.0 / 6.0],
]


def parabolic(alpha: float = 0.5) -> list[list[float]]:
    """The piecewise parabolic coefficient rows for the parameter alpha."""
    return [
        [0.0, 1.0, 0.0, 0.0],
        [-alpha, alpha - 1.0, alpha + 1.0, -alpha],
        [alpha, -alpha, -alpha, alpha],
    ]


def farrow(x: Sequence[complex], m: int, mu: float, b: list[list[float]] = CUBIC_LAGRANGE) -> complex:
    """The interpolated value at t = m + mu, from the samples x[m - 1], ..., x[m + 2]."""
    window = (x[m - 1], x[m], x[m + 1], x[m + 2])  # @line L1
    c = [sum(row[j] * window[j] for j in range(4)) for row in b]  # @line L2
    y = c[-1]  # @line L3
    for i in range(len(c) - 2, -1, -1):  # @line L4
        y = y * mu + c[i]  # @line L5
    return y  # @line L6


def resample(x: Sequence[complex], times: Sequence[float], b: list[list[float]] = CUBIC_LAGRANGE) -> list[complex]:
    """Values at the given times (in input samples). Each time needs 1 <= t <= len(x) - 3."""
    out: list[complex] = []
    for t in times:
        m = math.floor(t)  # @line L7
        mu = t - m  # @line L8
        out.append(farrow(x, m, mu, b))  # @line L9
    return out


def nco_times(n_in: int, w: float, eta0: float = 0.0) -> list[float]:
    """Output times for the interpolation interval w = T_i / T_s (Gardner 1993 control).

    The first output is at eta0; each next one is w input samples later. Only
    times with a full four-sample window are returned.
    """
    out: list[float] = []
    t = eta0
    while t <= n_in - 3:
        if t >= 1:
            out.append(t)
        t += w
    return out


def impulse_response(t: float, b: list[list[float]] = CUBIC_LAGRANGE) -> float:
    """The continuous impulse response h(t) of the interpolator: y(t) = sum_n x[n] h(t - n)."""
    # y(m + mu) = sum_j sum_i b[i][j] mu^i x[m + j - 1], so h(mu - j + 1) = sum_i b[i][j] mu^i.
    for j in range(4):
        mu = t + j - 1
        if 0.0 <= mu < 1.0:
            return sum(b[i][j] * mu ** i for i in range(len(b)))
    return 0.0
test_farrow_interpolator.py 127 lines
# SPDX-License-Identifier: MIT
# Part of https://algorithm.tamnd.com/farrow-interpolator/. The authors are listed in prov/9JVEXY.jsonl.

import math
import random

from farrow_interpolator import (
    CUBIC_LAGRANGE,
    farrow,
    impulse_response,
    nco_times,
    parabolic,
    resample,
)


def lagrange_direct(x, m, mu):
    """The Lagrange polynomial through (n, x[m + n]) for n = -1, 0, 1, 2, evaluated at mu."""
    nodes = (-1, 0, 1, 2)
    total = 0j
    for i, ni in enumerate(nodes):
        w = 1.0
        for k, nk in enumerate(nodes):
            if k != i:
                w *= (mu - nk) / (ni - nk)
        total += w * x[m + ni]
    return total


def test_exact_on_cubics():
    rng = random.Random(1)
    for _ in range(50):
        p = [complex(rng.uniform(-1, 1), rng.uniform(-1, 1)) for _ in range(4)]

        def f(t):
            return ((p[3] * t + p[2]) * t + p[1]) * t + p[0]

        x = [f(n) for n in range(10)]
        for _ in range(10):
            t = rng.uniform(1, 7)
            m = math.floor(t)
            assert abs(farrow(x, m, t - m) - f(t)) < 1e-9


def test_matches_direct_lagrange():
    rng = random.Random(2)
    x = [complex(rng.gauss(0, 1), rng.gauss(0, 1)) for _ in range(40)]
    for _ in range(200):
        m = rng.randrange(1, 37)
        mu = rng.random()
        assert abs(farrow(x, m, mu) - lagrange_direct(x, m, mu)) < 1e-12


def test_passes_through_samples():
    rng = random.Random(3)
    x = [rng.gauss(0, 1) for _ in range(20)]
    for b in (CUBIC_LAGRANGE, parabolic(0.5), parabolic(0.43)):
        for m in range(1, 17):
            assert abs(farrow(x, m, 0.0, b) - x[m]) < 1e-15


def test_sinusoid_error_within_bound():
    f = 0.05
    w = 2 * math.pi * f
    bound = 3.0 / 128.0 * w ** 4
    x = [math.cos(w * n + 0.4) for n in range(80)]
    worst = 0.0
    for i in range(1, 7600):
        t = 1 + i / 100.0
        m = math.floor(t)
        if m + 2 >= len(x):
            break
        err = abs(farrow(x, m, t - m) - math.cos(w * t + 0.4))
        worst = max(worst, err)
    assert worst <= bound
    assert worst > 0.3 * bound


def test_parabolic_exact_on_lines_and_symmetric():
    rng = random.Random(4)
    for alpha in (0.5, 0.43, 0.0):
        b = parabolic(alpha)
        x = [2.0 - 0.7 * n for n in range(8)]
        for _ in range(20):
            t = rng.uniform(1, 5)
            m = math.floor(t)
            assert abs(farrow(x, m, t - m, b) - (2.0 - 0.7 * t)) < 1e-12
        for _ in range(20):
            t = rng.uniform(-2, 2)
            assert abs(impulse_response(t, b) - impulse_response(-t, b)) < 1e-12


def test_constant_input_gives_constant_output():
    for b in (CUBIC_LAGRANGE, parabolic(0.5), parabolic(0.43)):
        assert abs(sum(b[0]) - 1.0) < 1e-15
        for row in b[1:]:
            assert abs(sum(row)) < 1e-15
        x = [2.5] * 8
        for mu in (0.0, 0.1, 0.5, 0.99):
            assert abs(farrow(x, 3, mu, b) - 2.5) < 1e-14


def test_impulse_response_reproduces_output():
    rng = random.Random(5)
    x = [rng.gauss(0, 1) for _ in range(30)]
    for b in (CUBIC_LAGRANGE, parabolic(0.5)):
        for _ in range(50):
            t = rng.uniform(3, 25)
            m = math.floor(t)
            direct = sum(x[n] * impulse_response(t - n, b) for n in range(len(x)))
            assert abs(farrow(x, m, t - m, b) - direct) < 1e-12
    assert abs(impulse_response(0.0) - 1.0) < 1e-15
    for n in (-2, -1, 1, 2):
        assert abs(impulse_response(float(n))) < 1e-15


def test_resample_with_nco_times():
    f = 0.03
    x = [math.sin(2 * math.pi * f * n) for n in range(200)]
    times = nco_times(len(x), 1.37, 0.2)
    assert times[0] >= 1 and times[-1] <= len(x) - 3
    for a, b in zip(times, times[1:]):
        assert abs(b - a - 1.37) < 1e-9
    out = resample(x, times)
    bound = 3.0 / 128.0 * (2 * math.pi * f) ** 4
    for t, v in zip(times, out):
        assert abs(v - math.sin(2 * math.pi * f * t)) <= bound

History

C. W. Farrow presented the structure at the IEEE International Symposium on Circuits and Systems in 1988 as a continuously variable digital delay element. In 1993 Floyd M. Gardner described how digital modems use an interpolator controlled by a basepoint index and a fractional interval, and Lars Erup, Gardner and Robert A. Harris analysed the cubic Lagrange and the piecewise parabolic Farrow interpolators in the second part of that work. Since then the Farrow structure has been the usual interpolator in all-digital timing recovery and in sample rate converters with an arbitrary ratio.

References

  1. C. W. Farrow, A continuously variable digital delay element, Proceedings of the IEEE International Symposium on Circuits and Systems, 1988, pages 2641 to 2645, the structure of fixed FIR filters whose outputs are combined as a polynomial in the delay
  2. Floyd M. Gardner, Interpolation in digital modems, Part I, Fundamentals, IEEE Transactions on Communications 41(3), 1993, pages 501 to 507, the basepoint index, the fractional interval mu and the control of the interpolator by a numerically controlled oscillator
  3. Lars Erup, Floyd M. Gardner and Robert A. Harris, Interpolation in digital modems, Part II, Implementation and performance, IEEE Transactions on Communications 41(6), 1993, pages 998 to 1008, the cubic Lagrange and the piecewise parabolic Farrow interpolators and their performance
  4. Heinrich Meyr, Marc Moeneclaey and Stefan A. Fechtel, Digital Communication Receivers, Wiley, 1998, Chapter 9 (interpolation and decimation)

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.per_output
  • a full proof of time.total
  • 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