Project 12: Resampling & Interpolation (The Shape Shifter)
Build a resampler that converts audio between sample rates without distortion.
Project Overview
| Attribute | Value |
|---|---|
| Difficulty | Level 2: Intermediate |
| Time Estimate | Weekend |
| Main Language | C |
| Alternative Languages | Python, Rust, C++ |
| Knowledge Area | Sample rate conversion |
| Tools | Audio player, plotting tool |
| Main Book | “The Scientist and Engineer’s Guide to DSP” by Steven W. Smith |
What you’ll build: A tool that converts signals from one sample rate to another with minimal artifacts.
Why it teaches DSP: Resampling forces you to understand interpolation, anti-aliasing, and sample timing.
Core challenges you’ll face:
- Designing an interpolation filter
- Avoiding aliasing when downsampling
- Maintaining timing accuracy over long signals
Real World Outcome
You will convert a 48kHz audio file into 44.1kHz and verify that the output sounds correct and has the expected duration.
Example Output:
$ ./resample --input song_48k.wav --from 48000 --to 44100 --output song_44k.wav
Input samples: 2,880,000
Output samples: 2,646,000
Wrote resampled audio to song_44k.wav
Verification steps:
- Compare the length of the output to expected duration
- Check for aliasing artifacts in the spectrum
The Core Question You’re Answering
“How can I change a signal’s sample rate without changing its pitch or introducing artifacts?”
This project shows why sample timing is as important as sample values.
Concepts You Must Understand First
Stop and research these before coding:
- Interpolation
- Why is connecting the dots between samples non-trivial?
- Book Reference: “The Scientist and Engineer’s Guide to DSP” by Steven W. Smith, Ch. 16
- Anti-aliasing filters
- Why must you filter before downsampling?
- Book Reference: “Understanding Digital Signal Processing” by Richard G. Lyons, Ch. 10
- Rational resampling
- How do you convert between rates that are not multiples?
- Book Reference: “The Scientist and Engineer’s Guide to DSP” by Steven W. Smith, Ch. 16
Questions to Guide Your Design
- Resampling method
- Will you use simple linear interpolation or a windowed-sinc filter?
- How will you trade quality for speed?
- Timing accuracy
- How will you avoid drift over long files?
- How will you handle fractional sample positions?
Thinking Exercise
Rate Conversion Math
If you convert from 48kHz to 44.1kHz, what is the resampling ratio? How many output samples correspond to 1 second?
Questions while working:
- Why is 44.1kHz not an integer divisor of 48kHz?
- What does that imply for interpolation?
The Interview Questions They’ll Ask
Prepare to answer these:
- “Why do you need an anti-aliasing filter before downsampling?”
- “What is the difference between interpolation and resampling?”
- “How do you convert between two non-integer-related sample rates?”
- “What artifacts appear if your interpolation is poor?”
- “Why does resampling require precise timing?”
Hints in Layers
Hint 1: Starting Point Start with linear interpolation and listen for artifacts.
Hint 2: Next Level Add a low-pass filter before downsampling to avoid aliasing.
Hint 3: Technical Details Use a fractional phase accumulator to track sample positions.
Hint 4: Tools/Debugging Compare spectra before and after resampling to detect aliasing.
Books That Will Help
| Topic | Book | Chapter |
|---|---|---|
| Interpolation | “The Scientist and Engineer’s Guide to DSP” by Steven W. Smith | Ch. 16 |
| Anti-aliasing | “Understanding Digital Signal Processing” by Richard G. Lyons | Ch. 10 |
| Resampling ratios | “The Scientist and Engineer’s Guide to DSP” by Steven W. Smith | Ch. 16 |
Implementation Hints
- Start with a high-quality reference file to detect artifacts.
- Measure exact output length to confirm timing accuracy.
- Keep the filter design modular for experimentation.
Learning Milestones
- First milestone: You can resample with correct duration.
- Second milestone: You can explain why anti-aliasing is required.
- Final milestone: You can reduce artifacts with better interpolation.