School/□ Systems Engineer/Week 27
Signals meet hardware: the cycle budget
A 48 MHz microcontroller sounds fast. Can it filter a 48 kHz audio signal in real time? It depends on one division.
About 40 min · 3 goals · Checked by automated tests
1.Why should I care?
This lesson joins the two halves of the term. A real-time signal system has to finish its work on every sample before the next one arrives. Getting the budget wrong means dropped samples, glitches or a control loop that goes unstable.
- ◇ Connect a full pipeline: sensor, anti-aliasing filter, ADC, digital filter, processor.
- ◇ Compute a processor's cycle budget per sample and its load at a given sample rate.
- ◇ Choose sample rate, bit depth and filter to meet a cycle budget.
2.Predict
A 48 MHz processor samples audio at 48 kHz. How many clock cycles does it have per sample?
Guess first. Being wrong here is how the surprise works.
3.Discover
In the Code Lab, simulate the pipeline: generate a 50 Hz sine plus noise sampled at 1 kHz, quantise it to 10 bits, then filter it with a moving average and with a first-order IIR. Count the arithmetic operations per sample for each and compare the outputs.
Can't use the interactive? Read a description instead
In the simulated pipeline, the noisy 50 Hz sine sampled at 1 kHz and quantised to 10 bits is smoothed by both filters. A moving average with a running sum costs about one add and one subtract per sample; the first-order IIR costs two multiplies and one add, and gives a smoother output with a small delay.
4.Checkpoint: guided practice
Check what you just discovered before going on. Hints appear after a wrong answer.
Practice 1
A 16 MHz microcontroller samples at 8 kHz. How many clock cycles are available per sample?
Practice 3
A system is at 95 % load. Which change reduces load WITHOUT reducing the frequencies you can capture?
5.Understand
The pipeline
Sensor → analog anti-aliasing filter → ADC (sample rate fs, N bits) → digital filter → decision or output. Each stage has a requirement from this term: fs > 2 × the highest wanted frequency with margin; N bits for the SNR you need; a filter whose response keeps the band and rejects noise; and a processor that keeps up.
The cycle budget
Cycles available per sample = f_clk / fs. Processor load = fs × (cycles per sample) / f_clk. Keep it well under 100 %, because interrupts, communication and the rest of the program need cycles too. A first-order IIR needs about two multiplies and an add per sample; an M-point moving average can be done with a running sum in constant time.
Trading between stages
A better analog filter lets you sample more slowly, saving cycles. More bits cost ADC time and memory but not filter cycles. An IIR filter gives strong smoothing for a handful of cycles but with phase lag; a long FIR filter has linear phase but costs a multiply per tap. Systems engineering is choosing these together, not one at a time.
Explain it another way
Samples arrive like items on a conveyor belt. You must finish with each one before the next arrives, or they pile up and fall off.
Hard real-time systems guarantee the worst-case execution time per sample, not the average. DMA can move samples from the ADC to memory without using the processor's cycles.
Budget B = f_clk/fs. 16 MHz at 8 kHz: B = 2000 cycles. A 64-tap FIR at 4 cycles per tap uses 256 cycles: 12.8 % load.
Digital hearing aids run filter banks on tiny, low-power processors; their designers balance sample rate, filter length and clock frequency so the battery lasts days, using exactly the P = αCV²f and cycle-budget arithmetic from this term.
Worked example 1
An 80 MHz processor filters a 44.1 kHz signal with a 128-tap FIR at 3 cycles per tap plus 100 cycles of overhead. What is the load?
Cycles per sample = 128 × 3 + 100 = 484. Load = 44,100 × 484 / 80,000,000 ≈ 26.7 %.
Common mix-ups
- If the average load is under 100 %, the system is real-time.
✓ Real-time needs the worst case to fit in every sample period, with margin for everything else.
6.Practice on your own
Practice, not a test. Wrong answers get a hint and another try.
Practice 2 · a fresh version every time
A 8 MHz processor needs 400 clock cycles to filter one sample. Ignoring everything else, what is the highest sample rate it can keep up with, in kHz?
Practice 4 · a fresh version every time
A 48 MHz processor filters a 48 kHz signal, using 400 cycles per sample. What percentage of its cycles does the filter use?
Practice 5 · a fresh version every time
An ideal 16-bit ADC spans 0 to 2.5 V. What is its quantisation step Δ = V / 2^N, in millivolts?
Practice 6 · a fresh version every time
A 13920 Hz sinusoid is sampled at fs = 8000 Hz. At what frequency (between 0 and fs/2) does it appear in the samples, in hertz?
7.Apply it
Term synthesis: specify a vibration monitor for a 0–500 Hz band on a 48 MHz processor. Choose fs, ADC bits (with SNR), an anti-aliasing filter cut-off, a digital filter and its cycle cost, and show the processor load. One page, every number justified.
8.Reflect
Concepts: Digital filters and frequency response · Real-time computing on robots · Sampling rate and aliasing