S7 · Lesson 51 · Project
Converting 32 kHz audio to 48 kHz with a polyphase 3/2 resampler, from the prototype filter to an efficient implementation verified against the direct method
Every time audio moves between a 44.1 kHz file, a 48 kHz interface and a 16 kHz voice link, a sample-rate converter runs. This project builds one for a rational ratio, 3/2, which takes 32 kHz audio to 48 kHz. You'll design the single lowpass filter that does both the interpolation and the anti-aliasing, write the obvious implementation, which stuffs zeros, filters at three times the rate and throws most of the results away, and then the polyphase version, which computes only the outputs that are kept and never touches a stuffed zero. The tests require the two to agree to rounding error and the converted tone to keep its pitch and level.
Predict
The direct 3/2 converter filters at 96 kHz and keeps every second output. What fraction of its multiplications are wasted?
Five sixths. Of every three samples the filter sees at 96 kHz, two are zeros, so two thirds of its products are zero by construction; and of the outputs it computes, the downsampler keeps only every second one. The useful work is 1/3 × 1/2 = 1/6 of the total. The polyphase form does only that sixth: each kept output uses only the taps that line up with real input samples.
Name it
The prototype
One lowpass serves both purposes: removing the images that upsampling by L creates (cutoff π/L) and preventing the aliasing that downsampling by M would cause (cutoff π/M). So its cutoff is , measured at the upsampled rate, and its gain is L, to restore the amplitude that zero stuffing divides by L. A Kaiser window with β = 8 gives about 80 dB of stopband.
The direct method
Insert L − 1 zeros after each input sample, convolve with h, keep every Mth output. Simple, and the reference against which the fast version is checked.
The polyphase method
The output sample m is , where v is the zero-stuffed input, nonzero only when is a multiple of L. With and , only the taps meet real samples:
Each output uses one polyphase branch, N/L taps, chosen by the phase p, which cycles through L values. The cost is N/L multiplications per output, against N per upsampled sample for the direct method.
Worked example
A 96-tap prototype converts 32 kHz to 48 kHz. Compare the multiplications per output sample.
Show the solution
- Direct. The filter runs at 96 kHz, 96 multiplications per upsampled sample, and two upsampled samples are computed per kept output:
- Polyphase. Each output uses one branch of 96/3 = 32 taps: 32 per output, a sixth of the direct count.
- Per second. At 48,000 outputs per second: multiplications per second for each audio channel.
Answer 192 multiplications per output directly, against 32 in polyphase form.
Worked example
Which taps does output m = 5 use in the 3/2 converter, and which input samples?
Show the solution
- Output 5. so taps h[1], h[4], h[7], … meet x[3], x[2], x[1], ….
- Output 6. Here mM = 12, p = 0 and b = 4: taps h[0], h[3], h[6], … with x[4], x[3], ….
- The phase cycle. The phase p runs 0, 2, 1, 0, 2, 1, …, cycling through all three branches.
Answer output 5 uses taps h[1], h[4], h[7], … on inputs x[3], x[2], x[1], ….
Check
Step 1. Write prototype(L, M, N, beta): the converter's lowpass, with cutoff π/max(L, M) and gain L.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
Step 2. Write resample_direct(x, L, M, h): zero-stuff, filter, keep every Mth sample.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
Step 3. Write resample_poly(x, L, M, h): the same result from only the taps that meet real samples.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Explain why
Why does the polyphase converter give exactly the same output as the direct one, not an approximation?
The direct method computes every output of a convolution between the filter and a signal that is zero at all but every Lth position. Each of those outputs is a sum of products, and every product that involves a stuffed zero contributes exactly nothing. The polyphase method computes the same sums but skips those zero products, using only the taps that line up with real samples, and it only computes the outputs that the downsampler would keep. Nothing that could change the result is left out; only arithmetic whose answer is known in advance to be zero, or that would be thrown away. The two methods are algebraically identical, which is why the test can demand agreement to rounding error.
Practice
Keep practicing
3 kinds of problem on this topic, dealt one at a time with new numbers every time, for as long as you like.