A4 Python for engineers · Practice
Filtering signals in code
Problems dealt one at a time, with new numbers every time. The kinds you miss come back more often, and you can keep going as long as you like.
Check
Write moving_average(x, N) without np.convolve, using a cumulative sum: with c = np.cumsum(np.insert(x, 0, 0)), the sum of x[i:i + N] is c[i + N] - c[i]. Return the len(x) - N + 1 window averages. Set y = moving_average(x, N) for the starter's samples and N = 8.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
Sample a 1 V sine at f = 3 Hz for 2 s at fs = 1000 Hz, pass it through an N-point moving average (N = 10, mode "valid"), and set measured to the output's amplitude, half its peak-to-peak over the last second. Set formula to from the lesson.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
Write smooth(x, alpha) returning the exponentially smoothed sequence, starting from y = x[0]. Feed it a unit step (100 zeros then 400 ones) sampled every 1 ms with alpha = 0.05. Set n63 to the number of samples after the step until the output first reaches 0.632, and tau_ms to the formula in ms.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
Write median_filter(x, w) returning the median of each window of w = 3 samples (a list w - 1 shorter than x), and use it on the starter's readings, which include some single-sample glitches. Set clean to the result and worst to its largest distance from 1.0.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
A signal is sampled at 1200 Hz and carries 50 Hz hum. How many samples long must a moving average be for its first null to fall on the hum?
Check
An exponential smoother runs every 0.1 ms with = 0.05. What's its time constant?
Write units the way you normally would: 1.5 s and 1500 ms are the same answer.
Check
Set y to the N-point moving average (N = 4) of [2, 4, 6, 8, 10, 12, 14, 16] with np.convolve in "valid" mode.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
Starting from y = 0, apply exponential smoothing with alpha = 0.3 to five samples of 1.0 and set y to the final output.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
A kind of problem counts as solid once you've got it right on the first try twice in a row. Back to the lesson · Practice every topic in A4