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.
- Solves
- Sample-rate conversion
- 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
- Model
- Real RAM
- Properties
- deterministiclinearonlinepasses through samples
- Evidence
- Proof sketchCode in 1 languages
- Origin
- C. W. Farrow, 1988
The Farrow interpolator computes the value of a sampled signal between its samples, at a fractional delay that can change at every output. It writes the interpolated value as a polynomial in , , where each coefficient is the output of a fixed FIR filter on the input around the basepoint . Horner's rule then evaluates the polynomial. A change of delay needs no new filter taps, only a new value of . In all-digital receivers the sampling clock is free-running, and the timing loop or estimator gives the strobe times as . 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 , sampled on a grid of spacing . A second input is a sequence of output times , each split as with the basepoint and the fractional interval . The output is for each . The interpolator is a linear combination of the samples near whose weights depend on . Its quality is the error for band-limited , 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 input samples.
The algorithm
FARROW(x, m, mu, b[0..D][0..3])- 1
w <- (x[m-1], x[m], x[m+1], x[m+2]) - 2
c[i] <- sum_j b[i][j] * w[j] for i <- 0 to D ▷ D+1 fixed FIR filters - 3
y <- c[D] - 4
for i <- D - 1 downto 0 do - 5
y <- y * mu + c[i] ▷ Horner's rule - 6
return y RESAMPLE(x, times, b)for each t in times do- 7
m <- floor(t) - 8
mu <- t - m - 9
append FARROW(x, m, mu, b) to the output
Line L1 takes four samples around the basepoint. For cubic Lagrange interpolation () the coefficient rows are
For the piecewise parabolic interpolator () with parameter they are , and . With all taps are multiples of , 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 and . With the cubic rows, line L2 gives . Lines L3 to L5 compute:
| step | value |
|---|---|
| L3: | |
| L5: | |
| L5: | |
| L5: |
The same value comes from the Lagrange weights at , which are . The parabolic interpolator with gives and .
For a sinusoid of unit amplitude at frequency , the largest error over all phases and over a grid of 2001 values of was (computed), with the theoretical bound of the cubic:
| (cycles per sample) | cubic | parabolic, | cubic bound |
|---|---|---|---|
At four samples per symbol, a raised cosine signal with has no energy above 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 . If has four continuous derivatives, the error is at most for . The parabolic interpolator is exact for polynomials of degree at most 1 and passes through the samples, but not for degree 2: on at it returns instead of when .
Key steps. By Horner's rule, with . Each column of the cubic rows is the coefficient vector of the Lagrange basis polynomial for the node , so the output is the interpolating polynomial. The Lagrange remainder is , and the product has its largest magnitude on at , where it is . 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 on a grid of spacing . Line L1 takes the window . Line L2 computes for , and lines L3 to L5 compute by Horner's rule. Lines L7 and L8 split an output time into and .
Lemma 1 (Horner's rule). Lines L3 to L5 return .
Proof. Let be the value after the step with index , so . By induction, : the step multiplies each term by and adds the next coefficient. After steps the sum is .
Theorem 1 (cubic Lagrange). With the rows of CUBIC_LAGRANGE,
the output is the unique polynomial of degree at most 3 that takes the values at .
Proof. By Lemma 1, with . Column of holds the coefficients of the Lagrange basis polynomial for the node : , , , . Each is at its own node and at the other three, so 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.
Corollary 1. The output is exact when is a polynomial of degree at most 3. At it returns , and the four weights sum to for every .
Theorem 2 (error bound). If has four continuous derivatives on and there, then for ,
Proof. The first inequality is the standard remainder of Lagrange interpolation: for some in the interval, with the nodes . For the second, put . Then and , so the product is in absolute value. Both factors are nonnegative and decrease in , so the maximum is at : . And .
For , . This is a theorem; how close the bound is for a given is a measurement, which the page reports.
Theorem 3 (piecewise parabolic form). With the rows , and , for every , the output returns at , a constant input gives the same constant, and a linear input gives exactly. The output is continuous in : the value at in window equals the value at in window .
Proof. At , . Rows 1 and 2 sum to zero, so a constant gives . For around , and , so ; linearity and a shift give the general line. At , , which is the value at in the next window.
The parabolic form is not exact for a quadratic when : for at the taps are on the values , and the output is instead of . Cubic Lagrange has the taps there and returns . Its value is a frequency response that Erup, Gardner and Harris chose for modems, with a cost of only two multiplications for and after shared additions. The tests check Theorem 1 against a direct Lagrange sum to , Corollary 1 on random cubics, Theorem 2 on a sinusoid, and Theorem 3.
Complexity
In the real RAM, line L2 costs multiply-adds with taps, and lines L3 to L5 cost multiply-adds. So one output costs 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. outputs cost , and the extra space is for the coefficients. A filter whose taps are designed for each new would need evaluations of a -dependent kernel per output instead. The hardware cost is fixed filters and multipliers by .
Properties
- Linear and time-varying. For fixed it is an FIR filter. Its continuous impulse response , with , is piecewise polynomial on . At its values are for the cubic and for the parabolic with .
- Passes through the samples. At the output is 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 , so the interpolator does not modulate a flat signal.
- Delay in one parameter. The filters do not depend on . 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 . Erup, Gardner and Harris studied how changes the performance in a modem. The value is the cheap choice.
- Polyphase interpolators. A bank of fixed filters, one per quantized value of , is the alternative. It needs memory for every phase but no multiplication by .
- 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 to with . Using 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 of the amplitude. The table shows that the cubic is not always better than the parabolic there.
- Mixing the timing estimate and . A timing estimate in symbols must be converted to input samples, and its integer part goes into the basepoint. A carry from to 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.
# 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
- 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
- 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
- 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
- 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.
| 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 9JVEXY Edit this page History Report an error Markdown