C11 · Lesson 27 · Measurement lab
Statistics, uncertainty and RMS in Python
Modern measurements end up as lists of numbers, from a data logger, a scope's memory or a script that steps an instrument through settings. Turning those numbers into results with honest uncertainties is a few lines of code, and writing them yourself makes clear what the instrument's built-in "mean", "std dev" and "RMS" readouts actually compute. In this lab you'll write three: the mean and its standard error, uncertainty propagation through any formula, and the true RMS and crest factor of a sampled waveform.
- A pulse train of height A and duty cycle D, sampled. The readouts are what your
rms_and_crestwill return: the true RMS, , and the crest factor, . - Shrink the duty cycle: the RMS falls slowly while the crest factor climbs. A meter rated for a crest factor of 3 misreads pulses below about 11% duty.
- The dashed line is the RMS level: the DC voltage that would heat a resistor equally.
Predict
Four readings are 1, 2, 3 and 4 V. What is the standard error of their mean?
The sample standard deviation (dividing by N − 1 = 3) is 1.29 V, and the standard error is V. Dividing by N instead of N − 1 would understate the spread of so few readings.
Name it
Mean and standard error
For N readings, the mean is , the sample standard deviation is
(N − 1 because the mean itself was estimated from the data), and the standard error of the mean is .
Propagation by computer
For a result with independent uncertainties , the quadrature rule of lesson 2 needs the partial derivatives. A program can estimate each one numerically with a central difference,
with a small step h scaled to , and then combine . It works for any formula, however messy.
True RMS and crest factor
For samples , the RMS is over a whole number of periods, and the crest factor is the largest |v| over the RMS. That's exactly what a true-RMS meter or a scope's RMS measurement computes.
Worked example
A resistance is found as V/I with V = 5.00 ± 0.02 V and I = 2.000 ± 0.010 mA. What does numerical propagation give?
Show the solution
- Sensitivity to V.
- Sensitivity to I.
- Combine in quadrature. so
- Compare with lesson 2. That's 16 Ω on 2500 Ω, matching lesson 2's 0.64%.
Answer R = 2500 ± 16 Ω.
Worked example
Samples of a 5 V pulse train with 10% duty: what are the RMS and crest factor?
Show the solution
- RMS.
- Crest factor.
Answer 1.58 V RMS, and a crest factor of 3.16.
Check
A set of 5 readings has a sample standard deviation of 1. What is the standard error of their mean?
Check
What is the RMS of a pulse train of height 2 V and duty cycle 0.25?
Write units the way you normally would: 1.5 V, 1500 mV and 0.0015 kV are the same answer.
Check
Write mean_and_error(xs): return the mean of the list xs and its standard error, using the sample standard deviation (divide by N − 1).
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
Write propagate(f, x, sx): f is a function of len(x) arguments, x the measured values and sx their standard uncertainties. Return the propagated uncertainty of f, using central-difference derivatives and quadrature.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
Write rms_and_crest(v): return the true RMS of the samples v and the crest factor (largest |v| over the RMS).
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 the sample standard deviation divide by N − 1 instead of N?
The deviations are measured from the sample's own mean, which is pulled toward the samples themselves: the sum of squared deviations from the sample mean is always a little smaller than from the true mean. Dividing by N would therefore underestimate the true spread, noticeably so for small samples. One degree of freedom has been used up estimating the mean, leaving N − 1 independent deviations, and dividing by N − 1 corrects the bias exactly on average.
Practice
Keep practicing
5 kinds of problem on this topic, dealt one at a time with new numbers every time, for as long as you like.