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 at each sample, scaled by the sample value, and add them:
| Method | Kernel | Frequency response |
|---|---|---|
| zero-order hold | 1 on | , with a half-sample delay |
| linear | triangle | |
| ideal | 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.
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 , 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.
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), andscipy.interpolatefor general interpolation; Pillow'sImage.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 . What is its gain at the Nyquist frequency, relative to DC?
Answer
, about dB.
Solution
The kernel's transform (in units of the sampling rate) is times a delay phase. At this is , so DACs often apply a compensating boost.
Exercise 2: Kernel requirements
Why must an interpolation kernel satisfy and for other integers ?
Answer
So that the interpolated curve passes exactly through the samples.
Solution
At the sum is , which equals for every input only if and 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 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
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…
- a sinc
- a triangle
- a Gaussian
- a delta
Why is the ideal sinc kernel rarely used directly?
- it is inaccurate
- it decays slowly and needs future samples
- it is not band-limited
- it only works for images
Which interpolator does NOT pass exactly through the samples?
- linear
- sinc
- Lanczos
- Gaussian smoothing