Resampling and interpolation

Changing the sampling rate without inventing or folding frequencies — low-pass before you downsample, interpolate with care when you upsample.

What you'll learn
  • Downsample without aliasing by filtering first
  • Compare linear, cubic and spline interpolation
  • Know when to reach for resample, interp1 or dynamic time warping

Signals recorded at different rates have to be brought to one rate before they can be compared. Going down loses information; going up has to make some up. Both are easy to get subtly wrong.

Downsampling: filter first

downsampling.m builds a 100 Hz signal — a smooth Laplace-shaped bump plus a 15 Hz sine — and keeps every fourth sample, for a new rate of 25 Hz and a new Nyquist frequency of 12.5 Hz. The 15 Hz sine is above it, so it aliases (lesson 2) to \(25 - 15 = 10\) Hz. The fix is to low-pass filter to below the new Nyquist frequency before throwing samples away:

signal_dsB = signal(1:dnsampleFactor:end);              % bad: just decimate
fkern = fir1(14*newSrate/2, (newSrate/2)/(srate/2));    % 175-order low-pass at 12.5 Hz
fsignal = filtfilt(fkern,1,signal);
signal_dsG = fsignal(1:dnsampleFactor:end);             % good: filter, then decimate
Power spectra on a log scale. The original shows a peak at 15 Hz. Naive downsampling shows a spurious peak at 10 Hz; filtering first leaves no peak there.
A NumPy port of downsampling.m. Naive decimation folds the 15 Hz sine to 10 Hz; filtering first leaves 3 million times less power there.

The script ends with MATLAB’s shortcut, resample(signal, p, q) with [p,q] = rat(newSrate/srate), which designs the anti-aliasing filter itself; scipy.signal.resample_poly is the Python equivalent.

Upsampling and interpolation

Going up means estimating values between samples. Interpolation/1_linear_cubic.py compares the two most common choices on twelve points of \(\cos(x^2/3 + 4)\):

f1 = interp1d(x, y, kind='linear')
f2 = interp1d(x, y, kind='cubic')
xnew = np.linspace(0, 4, 30)

Linear interpolation joins the dots with straight lines: it never overshoots, but its slope jumps at every sample. Cubic interpolation fits piecewise cubics with continuous slope and curvature, so it follows a smooth signal far better — and can overshoot near sharp changes. (The script’s second figure evaluates both interpolants only at the original points, where they agree with the data by construction; the third figure, on xnew, is the informative one.) 2_splines.py and 3_rbf.py extend this to smoothing splines and radial basis functions, which also work for scattered, irregularly spaced data — the case resample_irregular_sampled_data.m handles.

Two other tools in Resampling/ deserve a mention: spectral_interpolation.m fills a gap by averaging the spectra of the stretches on either side and adding a straight line between the gap’s endpoints, which keeps the gap’s texture instead of drawing a bare line through it. As written it reads the post-gap stretch as signal(boundryPnts(2)+1 : boundryPnts(1)+diff(boundryPnts)+1), which is 6501:6501 — a single sample — so only the pre-gap spectrum really counts; the end index should start from boundryPnts(2). Finally, dynamic_time_warp.m aligns two signals that run at varying speeds by warping their time axes against each other.

Try it yourself. Make a 100 Hz signal with a 15 Hz sine, downsample by 4 with x[::4] and with scipy.signal.decimate(x, 4), and compare their FFTs. Then upsample the decimated signal back with scipy.signal.resample_poly(y, 4, 1) and measure how close it gets to the original below 12.5 Hz.

View source on GitHub →