A1147 of 55
0 XP
  1. Hook
  2. Play
  3. Predict
  4. Name it
  5. Check
  6. Explain

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 11+(f/fc)2; the dashed box is the ideal filter that would pass the same white-noise power.
  • The box's width, π2fc, is the filter's noise bandwidth: 57% wider than its 3 dB corner, because of the long skirt.
  • Change fc: 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?

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 N samples divides the noise variance by N: np.convolve(x, np.ones(N) / N, mode="valid").
  • A one-pole low-pass, yn=ayn−1+(1−a)xn, passes a fraction 1−a1+a of white noise's variance; it's the discrete version of an RC filter, with time constant about 11−a samples.

Estimating a spectrum

A single periodogram, |Xk|2N from one FFT, scatters by 100% around the true density. Averaging M periodograms of separate segments (Bartlett's method) reduces the scatter to about 1M.

Synchronous averaging

If a signal repeats exactly and you can trigger on it, averaging M records keeps the signal and divides the noise's RMS by M: a 20 dB SNR gain for M=100.

Worked example

A one-pole filter yn=0.9yn−1+0.1xn smooths unit white noise. What output RMS should you measure?

Show the solution
  1. Output variance. 1−a1+a=0.11.9=0.0526 an RMS of 0.229.
  2. Equivalent moving average. A moving average with the same noise reduction would need 10.0526=19points
  3. 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
  1. One record. One record has amplitude SNR 0.1.
  2. Average M records. That divides the noise by M, so the SNR is 0.1M: 0.1M=10needsM=10000
  3. With 64 records. The noise falls to 18 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 yn=0.9yn−1+(1−0.9)xn. 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 yn=0.9yn−1+0.1xn (a plain loop is fine). Set var_out to the variance of the output after the first 1000 samples, and formula to 1−a1+a.

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 |Xk|2256 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?

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.