Project 2: The Moving Average Filter (Removing the Noise)
Build a moving average filter that smooths noisy signals and reveals the underlying trend.
Project Overview
| Attribute | Value |
|---|---|
| Difficulty | Level 1: Beginner |
| Time Estimate | 1 evening |
| Main Language | C |
| Alternative Languages | Python, Rust, C++ |
| Knowledge Area | Time-domain filtering |
| Tools | Audacity or CSV plotting tool |
| Main Book | “The Scientist and Engineer’s Guide to DSP” by Steven W. Smith |
What you’ll build: A filter that replaces each sample with the average of the last N samples.
Why it teaches DSP: This is the simplest real filter. It shows how smoothing is just a sliding window sum.
Core challenges you’ll face:
- Choosing window size and understanding its tradeoffs
- Managing sample history in a fixed buffer
- Measuring how smoothing affects sharp transitions
Real World Outcome
You will feed a noisy signal in and get a smoother signal out. You should be able to visualize how spikes are reduced and how the signal lags behind sudden changes.
Example Output:
$ ./moving_avg --window 8 --input noisy.csv --output smooth.csv
Input samples: 2000
Window size: 8
Wrote smoothed output to smooth.csv
Verification steps:
- Plot input and output on the same chart
- Confirm noise is reduced and peaks are rounded
The Core Question You’re Answering
“How does a simple average become a filter, and what do I lose when I smooth a signal?”
This project makes the cost of smoothing obvious: less noise but also less detail and added delay.
Concepts You Must Understand First
Stop and research these before coding:
- Sliding windows
- How do you update a rolling average without recomputing the whole sum?
- Book Reference: “The Scientist and Engineer’s Guide to DSP” Ch. 15
- Filter delay
- Why does a moving average introduce a lag?
- Book Reference: “Understanding Digital Signal Processing” by Richard G. Lyons, Ch. 6
- Noise vs. signal
- What counts as noise and what counts as signal?
- Book Reference: “The Scientist and Engineer’s Guide to DSP” Ch. 2
Questions to Guide Your Design
- Window size selection
- How will you choose the window size for different noise levels?
- How will you quantify the smoothing effect?
- Streaming vs. batch
- Can your filter work in streaming mode, one sample at a time?
- What state must be stored between samples?
Thinking Exercise
Manual Windowing
Take the sample sequence below and compute the moving average with window size 4:
Sequence: 2, 4, 8, 4, 2, 0, 2, 4
Questions while working:
- Where does the output begin (after how many samples)?
- Which features are smoothed out completely?
- What happens if the window size is 2 vs 8?
The Interview Questions They’ll Ask
Prepare to answer these:
- “Why does a moving average act as a low-pass filter?”
- “How does window size affect frequency response?”
- “What is filter delay and why does it happen?”
- “How can you compute a running average efficiently?”
- “When would you avoid a moving average filter?”
Hints in Layers
Hint 1: Starting Point Keep a fixed-size queue of recent samples.
Hint 2: Next Level Maintain a running sum so each new sample is O(1).
Hint 3: Technical Details When a new sample arrives, add it to the sum and subtract the oldest sample.
Hint 4: Tools/Debugging Plot the filtered output over the noisy input to verify smoothing and delay.
Books That Will Help
| Topic | Book | Chapter |
|---|---|---|
| Moving average filters | “The Scientist and Engineer’s Guide to DSP” by Steven W. Smith | Ch. 15 |
| Filter delay | “Understanding Digital Signal Processing” by Richard G. Lyons | Ch. 6 |
| Noise fundamentals | “The Scientist and Engineer’s Guide to DSP” by Steven W. Smith | Ch. 2 |
Implementation Hints
- Use a circular buffer to store the last N samples.
- Document the effective delay so users interpret results correctly.
- Keep numeric types stable to avoid accumulating error.
Learning Milestones
- First milestone: You can smooth a noisy sequence with a fixed window.
- Second milestone: You can explain how window size changes the output.
- Final milestone: You can describe why this filter attenuates high frequencies.