1. Home
  2. Signal processing

Signal processing · rate conversion

Resampling by Rational Factors

Eigenfield editorial team ·

Resampling changes the number of samples per second without changing what the signal looks like. When the new rate is a ratio of two whole numbers, the job splits into three plain steps: insert zeros to raise the rate by an integer factor L, smooth with a low-pass filter, then keep every M-th sample. The result has rate fs · L/M. Both factors have to be integers, and the code that mishandles them is usually the code in which they were typed as fractions or real numbers. Rate conversion is a routine job in signal processing.

Reducing the ratio

Start by reducing the fraction. Converting 44100 Hz to 48000 Hz needs L/M = 48000/44100. The greatest common divisor is 300, so L = 160 and M = 147. Check: 44100 × 160 / 147 = 48000. The reduction matters because the intermediate rate is fs · L = 7,056,000 Hz here, and an unreduced pair such as 480/441 would raise it threefold higher for no reason.

Going the other way, 48000 to 44100, swaps the roles: L = 147, M = 160. A down factor, often called ndown in code, has to be an integer, and the same is true of the up factor.

The three steps

First, upsample by L: place L − 1 zeros between neighbors. The spectrum then contains L copies of the original, the images.

Second, low-pass filter at the lower of the two Nyquist limits. In units of the intermediate rate, the cutoff is π/max(L, M) radians per sample. It removes the images from the upsampling step and, in the same pass, protects against aliasing from the downsampling step. The filter gain must be L, because inserting zeros divided the signal’s amplitude by L. Filter design details are on the analog filter design page, and the weights of a long finite filter come from a tapered sinc function.

Third, downsample by M: keep entries 0, M, 2M, and so on, discarding the rest.

A small worked case

Convert a signal sampled at 8000 Hz to 12000 Hz, so L/M = 3/2. Take three samples, x = [0, 3, 6], which sample the straight line 3t at the times 0, 1, 2 (in units of the old sampling period). Upsampling by 3 gives [0, 0, 0, 3, 0, 0, 6, 0, 0]. An interpolating filter that follows straight lines fills the zeros and gives [0, 1, 2, 3, 4, 5, 6] at spacing 1/3; the last two zeros cannot be filled without extrapolating past the final sample. Keeping every second entry yields [0, 2, 4, 6].

Those four values sit at the times 0, 2/3, 4/3 and 2, a spacing of 2/3 of the old period, which is 1/12000 s. Each equals 3t at its own time: 0, 2, 4, 6. The new record holds 4 samples where the old one held 3 over the same span of two periods.

A linear interpolator is the cheapest filter. It is exact for this ramp and adequate for slowly varying data, but it attenuates highs noticeably. For audio-quality work a long windowed-sinc filter is used instead, and its length grows in proportion to max(L, M).

Efficiency

Computing all L·N intermediate samples and then discarding most of them is wasteful. The standard remedy is the polyphase structure: split the filter into L sub-filters, each producing one of the L output phases, and evaluate only the samples that survive the downsampling. The cost falls to roughly the filter length per output sample divided by L. Whatever the implementation, the result is identical to the direct three-step form.

Edge effects and axes

The filter needs data on both sides of each output sample, and near the ends it lacks it. Output values within half a filter length of either end are therefore unreliable, which is where padding or a stated trim belongs. The new time grid is t(m) = m · M/(L · fs) and building it follows the time axis page, with the duration of the record unchanged. If the filter is applied by direct summation, see convolution of vectors for the length and offset rules.

Pseudocode

function resample(x, L, M, h):       # h: low-pass, gain L, cutoff pi/max(L,M)
    u = zeros(L * length(x))
    u[0 :: L] = x                    # insert L-1 zeros between samples
    v = conv(u, h)
    return v[0 :: M]                 # keep every M-th sample
Back to top