---
title: Fractional delay and interpolation
heading: Delaying by part of a sample with linear, Lagrange and all-pass interpolators, and the Farrow structure
eyebrow: S7 · Lesson 11 · Fractional delay and interpolation
xp: 50
---

::: hook
Many systems need a signal's value between its samples: a modem's timing recovery tracks a symbol clock that never lines up with the sampler, an audio resampler converts between rates with no simple ratio, a digital beamformer delays each element by a fraction of a sample. The ideal answer, a shifted sinc, is infinitely long. Practical interpolators are short filters whose coefficients depend on the fraction d, from the two-tap linear interpolator to Lagrange polynomials and all-pass designs, and how flat and how well delayed their response stays decides how much oversampling a system needs.
:::

::: play
- The curves are the magnitude responses of a linear (2-tap) and a cubic Lagrange (4-tap) interpolator set to the hardest fractional delay, halfway between samples.
- Linear interpolation's response is $\cos(\omega/2)$: 0.44 dB down at 0.2π and 3 dB down at π/2.
- The cubic's response stays within 0.03 dB to 0.2π: two more taps buy more than a factor of ten in accuracy at low frequencies.
:::

```sim plot
view: graph
x: w = 0.01..1
y: H = -10..1 dB
plot: 20*log(cos(pi*w/2)) | label: linear, d = ½
plot: 20*log(abs(2*(9/16*cos(pi*w/2) - 1/16*cos(3*pi*w/2)))) | label: cubic Lagrange, d = ½
```

::: predict
## A tone at 0.2π is delayed by half a sample with linear interpolation. How much is it attenuated, and how much with a cubic Lagrange interpolator?

- [ ] Neither attenuates it
- [x] About 0.44 dB with linear, 0.03 dB with cubic
- [ ] About 3 dB with both
- [ ] About 6 dB with linear, 1 dB with cubic

::: reveal
Linear interpolation averages the two neighbors: $|H| = \cos(0.1\pi) = 0.951$, −0.44 dB. The cubic, $[-\frac{1}{16}, \frac{9}{16}, \frac{9}{16}, -\frac{1}{16}]$, gives $2(\frac{9}{16}\cos(0.1\pi) - \frac{1}{16}\cos(0.3\pi)) = 0.996$, −0.03 dB. Both are exact at DC; the higher order stays flat further.
:::
:::

::: nameit
## The ideal

A delay of D samples (D not an integer) has $H(e^{j\omega}) = e^{-j\omega D}$ and impulse response $h[n] = \text{sinc}(n - D) = \frac{\sin\pi(n - D)}{\pi(n - D)}$: infinite and noncausal, so it is approximated.

## Lagrange interpolators

Fitting a polynomial of order N through N + 1 samples and evaluating it at D gives the FIR coefficients
$$h[n] = \prod_{k = 0, k \ne n}^{N}\frac{D - k}{n - k}, \qquad n = 0, \dots, N$$
exact for polynomials up to order N and maximally flat at ω = 0. Accuracy is best with D near the middle, (N − 1)/2 < D < (N + 1)/2.
- N = 1 (**linear**): h = [1 − d, d], with $|H|^2 = (1 - d)^2 + d^2 + 2d(1 - d)\cos\omega$; at d = ½, $|H| = \cos(\omega/2)$.
- N = 3 (**cubic**), D = 1.5: h = $[-\frac{1}{16}, \frac{9}{16}, \frac{9}{16}, -\frac{1}{16}]$.

## The Farrow structure

Each Lagrange coefficient is a polynomial in d, so the output is $y = \sum_{m=0}^{N}c_m d^m$ with each $c_m$ the output of a fixed FIR filter. The fraction d can change every sample at the cost of evaluating one polynomial by Horner's rule: ideal for arbitrary-ratio resampling and timing recovery.

## All-pass (Thiran) delays

A first-order all-pass $\frac{a + z^{-1}}{1 + az^{-1}}$ with $a = \frac{1 - D}{1 + D}$ has exactly unity magnitude and a group delay of D at DC. It suits feedback structures such as physical-modeling synthesis, where any magnitude error would accumulate, but it can't be changed abruptly without transients.
:::

::: example
## A system uses linear interpolation and needs the amplitude error at d = ½ below 0.01 dB. Up to what frequency can its signals go?

::: solution
1. **Amplitude limit.** At d = ½ the amplitude is $\cos(\omega/2)$, which must stay above 0.01 dB of loss:
   $$\begin{aligned} \cos(\omega/2) &\ge 10^{-0.01/20} \\ &= 0.99885 \end{aligned}$$
2. **Solve for ω.**
   $$\begin{aligned} \omega/2 &\le 0.0480 \\ \omega &\le 0.096\ \text{rad} \end{aligned}$$
   That's 1.5% of the sample rate.
3. **What it means.** Linear interpolation is accurate only on heavily oversampled signals, which is why high-quality resamplers use long polyphase or high-order Lagrange filters instead.

**Answer:** up to ω = 0.096 rad, 1.5% of the sample rate.
:::
:::

::: example
## Find the first-order Thiran all-pass for a 0.3-sample delay, and check its DC group delay.

::: solution
1. **Coefficient.**
   $$\begin{aligned} a &= \frac{1 - 0.3}{1 + 0.3} \\ &= 0.538 \end{aligned}$$
2. **Group delay.** A first-order all-pass $\frac{a + z^{-1}}{1 + az^{-1}}$ has group delay
   $$\tau(\omega) = \frac{1 - a^2}{1 + 2a\cos\omega + a^2}$$
3. **At DC.**
   $$\begin{aligned} \tau(0) &= \frac{1 - a}{1 + a} \\ &= \frac{0.462}{1.538} \\ &= 0.300\ \text{samples} \end{aligned}$$
   as designed.
4. **Magnitude.** Its magnitude is exactly 1 at every frequency; only the delay varies with frequency.

**Answer:** a = 0.538, and the DC group delay is 0.300 samples.
:::
:::

::: example
## Interpolate cos(0.5πn) at n = 1.5 from its samples at n = 0 to 3, linearly and with the cubic Lagrange filter. How large are the errors?

::: solution
1. **True value.**
   $$\cos(0.75\pi) = -0.707$$
2. **Linear.** From n = 1 and 2:
   $$\begin{aligned} x[1.5] &\approx \tfrac{1}{2}(0 - 1) \\ &= -0.5 \end{aligned}$$
   an error of 0.207.
3. **Cubic.**
   $$\begin{aligned} x[1.5] &\approx -\tfrac{1}{16}(1) + \tfrac{9}{16}(0) \\ &\quad + \tfrac{9}{16}(-1) - \tfrac{1}{16}(0) \\ &= -0.625 \end{aligned}$$
   an error of 0.082.
4. **At a lower frequency.** At a quarter of the sample rate, even the cubic is crude; at 0.05 cycles per sample the errors fall to 0.011 and 0.0002.

**Answer:** against the true −0.707, linear interpolation gives −0.5 (an error of 0.207) and the cubic gives −0.625 (an error of 0.082).
:::
:::

::: check
kind: parts
given: d = 0.25 | 0.5; w = 0.1 | 0.3 | 0.6

## A linear interpolator delays by d = {d} sample; a tone sits at ω = {w}π.

::: part
answer: sqrt((1 - d)^2 + d^2 + 2*d*(1 - d)*cos(w*pi))
tolerance: 0.5%

### What is |H| there?

::: misses
- [any] $|(1 - d) + de^{-j\omega}|$.
:::
:::

::: part
answer: -20*log(sqrt((1 - d)^2 + d^2 + 2*d*(1 - d)*cos(w*pi)))
unit: dB
tolerance: 0.5%

### How much does it attenuate the tone?

::: misses
- [any] $-20\log_{10}|H|$.
:::
:::
:::

::: check
kind: parts
given: e = 0.001 | 0.01 | 0.1

## Linear interpolation at d = ½ must stay within {e} dB.

::: part
answer: 2*acos(10^(-e/20))/(2*pi)*100
unit: %
tolerance: 0.5%

### Up to what fraction of the sample rate may the signal go?

::: misses
- [any] Solve $\cos(\omega/2) = 10^{-e/20}$ for ω, then divide by 2π.
:::
:::
:::

::: check
kind: parts
given: D = 1.2 | 1.4 | 1.7

## A cubic Lagrange interpolator (taps at n = 0 to 3) delays by D = {D}.

::: part
answer: (D - 1)*(D - 2)*(D - 3)/((0 - 1)*(0 - 2)*(0 - 3))
tolerance: 0.5%

### What is h[0]?

::: misses
- [any] $\prod_{k \ne 0}\frac{D - k}{0 - k}$.
:::
:::

::: part
answer: D*(D - 2)*(D - 3)/((1 - 0)*(1 - 2)*(1 - 3))
tolerance: 0.5%

### What is h[1]?

::: misses
- [any] $\prod_{k \ne 1}\frac{D - k}{1 - k}$.
:::
:::
:::

::: check
kind: parts
given: D = 0.1 | 0.4 | 0.75

## A first-order Thiran all-pass must delay by {D} sample at DC.

::: part
answer: (1 - D)/(1 + D)
tolerance: 0.5%

### What is its coefficient a?

::: misses
- [(1 + D)/(1 - D)] That would put the pole outside the circle.
- [any] $(1 - D)/(1 + D)$.
:::
:::

::: part
answer: (1 - ((1 - D)/(1 + D))^2)/(1 - 2*(1 - D)/(1 + D) + ((1 - D)/(1 + D))^2)
tolerance: 0.5%

### What group delay does it have at ω = π?

::: misses
- [D] Its delay varies with frequency; evaluate $\frac{1 - a^2}{1 + 2a\cos\omega + a^2}$ at π.
- [any] $\frac{1 - a^2}{(1 - a)^2} = \frac{1 + a}{1 - a}$.
:::
:::
:::

::: check
kind: parts
given: w = 0.1 | 0.2 | 0.4

## The cubic Lagrange interpolator at D = 1.5 passes a tone at ω = {w}π.

::: part
answer: 2*(9/16*cos(w*pi/2) - 1/16*cos(3*w*pi/2))
tolerance: 0.5%

### What is its amplitude there?

::: misses
- [cos(w*pi/2)] That's linear interpolation.
- [any] $2(\frac{9}{16}\cos\frac{\omega}{2} - \frac{1}{16}\cos\frac{3\omega}{2})$.
:::
:::
:::

::: check
kind: choice

## Why does a timing-recovery loop in a modem use a Farrow interpolator rather than a bank of precomputed fractional-delay filters?

- [ ] The Farrow structure is more accurate than any filter
- [x] The fraction d changes continuously as the loop tracks the symbol clock; the Farrow structure computes the output for any d by evaluating a polynomial whose coefficients come from fixed filters, without storing or switching coefficient sets
- [ ] A Farrow interpolator needs no multiplications
- [ ] Precomputed filters can't delay by less than a sample

::: misses
- [1] It implements the same polynomial interpolators; its advantage is flexibility.
- [3] It needs N + 1 fixed filters plus the polynomial evaluation.
- [4] They can; they just can't change d smoothly without a huge table.
:::
:::

::: check
kind: derivation
vars: d = 0..1, w = 0..3.14, D = 0.01..2

## A linear interpolator delays by d; separately, a first-order Thiran all-pass is designed for a delay D.

::: step
answer: sqrt((1 - d)^2 + d^2 + 2*d*(1 - d)*cos(w))

### What is the linear interpolator's |H| at frequency w?
:::

::: step
answer: (1 - D)/(1 + D)

### What is the Thiran coefficient?
:::

::: step
answer: (1 - ((1 - D)/(1 + D))^2)/(1 + 2*(1 - D)/(1 + D)*cos(w) + ((1 - D)/(1 + D))^2)

### What is its group delay at frequency w?
:::

::: right
Right. The linear interpolator errs in magnitude, the all-pass only in delay.
:::
:::

::: check
kind: python

## Write `lagrange(N, D)`: the N + 1 Lagrange coefficients for a delay of D samples, and `interp(x, n0, D, N)`: the value at n0 + D from the samples x[n0], …, x[n0 + N].

```python starter
def lagrange(N, D):
    return [1.0] + [0.0] * N

def interp(x, n0, D, N):
    return x[n0]
```

```python solution
def lagrange(N, D):
    h = []
    for n in range(N + 1):
        p = 1.0
        for k in range(N + 1):
            if k != n:
                p *= (D - k) / (n - k)
        h.append(p)
    return h

def interp(x, n0, D, N):
    h = lagrange(N, D)
    return sum(h[k] * x[n0 + k] for k in range(N + 1))
```

```python tests
import math
assert lagrange(3, 1.5) == [-0.0625, 0.5625, 0.5625, -0.0625], "Cubic at D = 1.5: −1/16, 9/16, 9/16, −1/16."
assert lagrange(1, 0.3) == [0.7, 0.3] or all(abs(a - b) < 1e-12 for a, b in zip(lagrange(1, 0.3), [0.7, 0.3])), "Linear: 1 − d and d."
x = [n ** 3 - 2 * n for n in range(6)]
assert abs(interp(x, 1, 1.25, 3) - (2.25 ** 3 - 4.5)) < 1e-9, "A cubic interpolator is exact on a cubic polynomial."
w = 0.1 * math.pi
s = [math.cos(w * n) for n in range(4)]
assert abs(interp(s, 0, 1.5, 3) - math.cos(1.5 * w)) < 3e-4, "Within 0.0002 at 0.05 cycles per sample."
```

::: right
Right. Exactness on polynomials is what makes Lagrange interpolators maximally flat at DC.
:::
:::

::: explain
## Why do fractional-delay interpolators work well only for signals well below the Nyquist frequency?

::: model
An interpolator estimates values between samples by fitting a smooth curve, a low-order polynomial for Lagrange filters, through nearby samples. A signal far below the Nyquist frequency changes little between samples and looks locally like a polynomial, so the fit is accurate. As the frequency rises, the signal wiggles more between samples and a short polynomial can't follow it, so the interpolated values lose amplitude and pick up errors; at half the sampling rate a half-sample delay is impossible to get right with any short filter. Systems that need accurate fractional delays therefore oversample, or use long interpolation filters.
:::
:::

::: practice
::: check
given: d = 0.3 | 0.5; w = 0.2 | 0.5
answer: sqrt((1 - d)^2 + d^2 + 2*d*(1 - d)*cos(w*pi))
tolerance: 0.5%

## Linear interpolator |H| for d = {d} at {w}π?

::: misses
- [any] $|(1 - d) + de^{-j\omega}|$.
:::
:::

::: check
given: D = 0.2 | 0.4 | 0.5 | 0.6 | 0.8 | 1.2
answer: (1 - D)/(1 + D)
tolerance: 0.5%

## Thiran coefficient for a {D}-sample delay?

::: misses
- [any] $(1 - D)/(1 + D)$.
:::
:::

::: check
given: D = 1.2 | 1.3 | 1.4 | 1.5 | 1.6 | 1.75
answer: D*(D - 1)*(D - 3)/((2 - 0)*(2 - 1)*(2 - 3))
tolerance: 0.5%

## Cubic Lagrange h[2] for D = {D}?

::: misses
- [any] $\prod_{k \ne 2}\frac{D - k}{2 - k}$.
:::
:::
:::
