TExcelTAD
Home › Fourier Analysis & Signals › Reconstruction and Sinc Interpolation

Reconstruction and Sinc Interpolation

Turning samples back into a continuous signal is the job of a digital-to-analog converter, and also of every "resize image" or "change sample rate" function. The ideal method is sinc interpolation; practical systems use cheaper approximations (hold, linear, cubic, windowed sinc), each a different low-pass filter with different errors.

Interpolation is convolution

All the common methods have the same form: place a copy of an interpolation kernel kk at each sample, scaled by the sample value, and add them:

xr(t)=∑nx[n] k ⁣(t−nTsTs)x_r(t) = \sum_n x[n]\, k\!\left(\frac{t - nT_s}{T_s}\right)
Method Kernel k(u)k(u) Frequency response
zero-order hold 1 on [0,1)[0, 1) sinc⁡\operatorname{sinc}, with a half-sample delay
linear triangle Λ(u)\Lambda(u) sinc⁡2\operatorname{sinc}^2
ideal sinc⁡(u)\operatorname{sinc}(u) rectangle (perfect low-pass)

The kernel's spectrum shows how well it removes the spectral copies created by sampling while keeping the original band flat.

python · try it yourself
import numpy as np

fs = 10.0
f0 = 3.2                                         # a tone at 0.32 of the sampling rate
n = np.arange(-400, 401)
samples = np.sin(2 * np.pi * f0 * n / fs)
t = np.linspace(0, 2, 801)
true = np.sin(2 * np.pi * f0 * t)

u = fs * t[:, None] - n[None, :]                 # distance from each sample, in sample units
hold = np.sum(samples * ((u >= 0) & (u < 1)), axis=1)
linear = np.sum(samples * np.clip(1 - np.abs(u), 0, None), axis=1)
ideal = np.sum(samples * np.sinc(u), axis=1)
for name, r in (("zero-order hold", hold), ("linear", linear), ("sinc", ideal)):
    print(f"{name:16s} RMS error {np.sqrt(np.mean((r - true) ** 2)):.4f}")
zero-order hold  RMS error 0.7249
linear           RMS error 0.2416
sinc             RMS error 0.0009

For a tone this close to the Nyquist frequency, hold and linear interpolation are poor; the sinc sum is accurate (its error here comes only from truncating the sum).

Why not always use sinc?

The sinc kernel decays only like 1/u1/u, so each output value needs many samples, and it is non-causal (it uses future samples). Practical resamplers multiply the sinc by a window, keeping 8 to 64 taps (a windowed sinc, Lanczos kernel), or use polyphase filters. Image software offers "nearest", "bilinear", "bicubic" and "Lanczos" for the same reason.

python · try it yourself
import numpy as np

fs = 10.0
f0 = 3.2
n = np.arange(-400, 401)
samples = np.sin(2 * np.pi * f0 * n / fs)
t = np.linspace(0, 2, 801)
true = np.sin(2 * np.pi * f0 * t)
u = fs * t[:, None] - n[None, :]

for a in (2, 3, 8, 32):
    lanczos = np.where(np.abs(u) < a, np.sinc(u) * np.sinc(u / a), 0.0)
    r = np.sum(samples * lanczos, axis=1)
    print(f"Lanczos with a = {a:2d} ({2 * a} taps): RMS error {np.sqrt(np.mean((r - true) ** 2)):.4f}")
Lanczos with a =  2 (4 taps): RMS error 0.1320
Lanczos with a =  3 (6 taps): RMS error 0.0328
Lanczos with a =  8 (16 taps): RMS error 0.0028
Lanczos with a = 32 (64 taps): RMS error 0.0002

In software

  • scipy.signal.resample (FFT-based, effectively sinc for periodic signals), resample_poly (polyphase windowed sinc), and scipy.interpolate for general interpolation; Pillow's Image.resize(..., Image.LANCZOS) for images.
  • Audio DACs oversample (upsample digitally by 4 to 64 times with a good filter) so that the final analog filter can be simple.

Exercises

Exercise 1: Hold's frequency response

The zero-order hold has kernel Π(u−1/2)\Pi(u - 1/2). What is its gain at the Nyquist frequency, relative to DC?

Answer

sinc⁡(1/2)=2/π≈0.637\operatorname{sinc}(1/2) = 2/\pi \approx 0.637, about −3.9-3.9 dB.

Solution

The kernel's transform (in units of the sampling rate) is sinc⁡(f/fs)\operatorname{sinc}(f/f_s) times a delay phase. At f=fs/2f = f_s/2 this is sin⁡(π/2)/(π/2)=2/π\sin(\pi/2)/(\pi/2) = 2/\pi, so DACs often apply a compensating boost.

Exercise 2: Kernel requirements

Why must an interpolation kernel satisfy k(0)=1k(0) = 1 and k(n)=0k(n) = 0 for other integers nn?

Answer

So that the interpolated curve passes exactly through the samples.

Solution

At t=mTst = mT_s the sum is ∑nx[n]k(m−n)\sum_n x[n] k(m - n), which equals x[m]x[m] for every input only if k(0)=1k(0) = 1 and kk vanishes at the other integers. Sinc, triangle and Lanczos all satisfy this; a Gaussian kernel does not, so it smooths rather than interpolates.

Exercise 3: A slower tone (code)

Repeat the first example with f0=0.5f_0 = 0.5 Hz (far below the Nyquist frequency). How does linear interpolation do now?

Answer

Its RMS error falls to about 0.006, roughly 40 times smaller than for the 3.2 Hz tone.

Solution
python · try it yourself
import numpy as np
fs, f0 = 10.0, 0.5
n = np.arange(-400, 401)
samples = np.sin(2 * np.pi * f0 * n / fs)
t = np.linspace(0, 2, 801)
u = fs * t[:, None] - n[None, :]
linear = np.sum(samples * np.clip(1 - np.abs(u), 0, None), axis=1)
print(round(float(np.sqrt(np.mean((linear - np.sin(2 * np.pi * f0 * t)) ** 2))), 4))
0.0064

Simple interpolators are fine for heavily oversampled signals; they fail near the Nyquist frequency.

Test yourself

Linear interpolation corresponds to convolving the samples with…

  1. a sinc
  2. a triangle
  3. a Gaussian
  4. a delta

Why is the ideal sinc kernel rarely used directly?

  1. it is inaccurate
  2. it decays slowly and needs future samples
  3. it is not band-limited
  4. it only works for images

Which interpolator does NOT pass exactly through the samples?

  1. linear
  2. sinc
  3. Lanczos
  4. Gaussian smoothing