A11 · Lesson 47 · Lab: noise in code
Generating, filtering, averaging and measuring noise
Lessons 32 and 33, and 40 to 42, described noise by its statistics: its RMS, its autocorrelation, its power spectral density, and how filtering and averaging change them. In this lab you'll generate white Gaussian noise and measure its crest factor, smooth it with a moving average and a one-pole filter and check the output noise against the formulas, estimate its spectrum by averaging periodograms, and pull a weak repeated signal out of noise by averaging triggered records.
- The solid curve is a one-pole low-pass's power gain ; the dashed box is the ideal filter that would pass the same white-noise power.
- The box's width, , is the filter's noise bandwidth: 57% wider than its 3 dB corner, because of the long skirt.
- Change : both scale together. The ratio is fixed by the filter's shape.
Predict
White noise of RMS 1 passes through a 16-point moving average. What RMS comes out?
1/4. Each output is the mean of 16 independent samples, so its variance is and its RMS . Successive outputs share 15 of their 16 samples, so they're strongly correlated, but each one alone has that spread.
Name it
White noise
rng.normal(0, sigma, n) gives independent Gaussian samples: white noise, with equal power at every frequency up to half the sample rate.
Filtering
- A moving average of samples divides the noise variance by :
np.convolve(x, np.ones(N) / N, mode="valid"). - A one-pole low-pass, , passes a fraction of white noise's variance; it's the discrete version of an RC filter, with time constant about samples.
Estimating a spectrum
A single periodogram, from one FFT, scatters by 100% around the true density. Averaging periodograms of separate segments (Bartlett's method) reduces the scatter to about .
Synchronous averaging
If a signal repeats exactly and you can trigger on it, averaging records keeps the signal and divides the noise's RMS by : a 20 dB SNR gain for .
Worked example
A one-pole filter smooths unit white noise. What output RMS should you measure?
Show the solution
- Output variance. an RMS of 0.229.
- Equivalent moving average. A moving average with the same noise reduction would need
- Why it's popular. The one-pole filter gets it with one multiply-add per sample and one stored value, which is why it's the standard cheap smoother in firmware.
Answer you should measure an output RMS of 0.229.
Worked example
A 0.1 V repeated pulse sits in 1 V RMS of noise. How many triggered records must be averaged to see it with an SNR of 10 in amplitude?
Show the solution
- One record. One record has amplitude SNR 0.1.
- Average records. That divides the noise by , so the SNR is :
- With 64 records. The noise falls to V, an SNR of 0.8: the pulse becomes visible but not clean.
Answer 10 000 records.
Check
White noise of RMS 1 passes through a 16-point moving average. What RMS comes out?
Check
Unit white noise passes through . What RMS comes out?
Check
Generate 10⁵ samples of unit white Gaussian noise (np.random.default_rng(1)) and set crest to the largest absolute value divided by the RMS.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
Pass 10⁶ unit white noise samples (np.random.default_rng(2)) through a 16-point moving average, and set ratio to the input RMS divided by the output RMS.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
Filter 10⁵ unit white noise samples (np.random.default_rng(3)) with (a plain loop is fine). Set var_out to the variance of the output after the first 1000 samples, and formula to .
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
Estimate the spectrum of unit white noise by averaging periodograms: split 102 400 samples (np.random.default_rng(4)) into 400 segments of 256, compute for each with np.fft.fft, and average. Set level to the mean over all bins and scatter to the standard deviation across bins divided by the mean.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
A pulse of 0.1 V (a half sine over 100 samples) repeats in records of unit white noise. Average 64 triggered records (np.random.default_rng(5)) and set noise_left to the RMS of the average minus the pulse.
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 averaging triggered records improve the SNR, while averaging adjacent samples of one record would also blur the signal?
Triggered averaging lines up records so the signal is in the same place every time: averaging leaves it unchanged, while the noise, independent from record to record, is divided by in RMS. Averaging adjacent samples within one record also reduces noise, but it treats the signal as constant over the averaging window, so any detail shorter than the window is smeared: it's a low-pass filter. The trigger is what makes the difference: it gives you many independent looks at the same moment of the signal, rather than one look at neighboring moments.
Practice
Keep practicing
4 kinds of problem on this topic, dealt one at a time with new numbers every time, for as long as you like.