A5 · Lesson 47 · Lab: integrating data
Energy, charge and RMS from a list of samples
A power meter doesn't know the formula for the power it's measuring; it has a list of samples. Charge from a current log, energy from a power log and RMS from a voltage record are all integrals of data. In this lab you'll write the trapezoid and Simpson rules for sampled data, handle samples that aren't evenly spaced, let the computer decide how many slices it needs, and measure an RMS from one period of a distorted wave.
- A clipped sine wave, , over one period. Clipping flattens its tops at .
- Lower the clip level. The readout is its RMS, computed by integrating exactly; the sine's own RMS is 0.707.
- How much does clipping at 0.9 lower the RMS? At 0.5?
Predict
A current is logged at uneven times: 0, 0.1, 0.3 and 0.6 s. Can the trapezoid rule still find the charge?
Yes. Each trapezoid is , whatever its width. Simpson's rule, in its usual form, needs equal pairs of slices; the trapezoid rule works on any grid, which is why it's the default for logged data.
Name it
Rules for samples
For samples spaced apart:
- trapezoid: ;
- Simpson ( even): .
For uneven spacing, add the trapezoids one at a time: .
Letting the computer choose
When you can evaluate the function anywhere, start with a few slices and double until two successive trapezoid results and agree to within a tolerance. Their difference estimates the error, and (Richardson's correction) is usually far more accurate than either.
RMS from samples
: square the samples, integrate over a whole number of periods, divide by the time, take the root. For a clipped sine wave of peak 1 clipped at , the exact mean square is with , a handy check.
Worked example
A current is logged as 0 A at 0 s, 2 A at 0.1 s, 3 A at 0.3 s and 1 A at 0.6 s. How much charge passed?
Show the solution
- Widths. The slices are 0.1 s, 0.2 s and 0.3 s wide.
- Add the trapezoids with their own widths.
Answer 1.2 C passed.
Worked example
Integrate on by doubling until two trapezoid results agree to .
Show the solution
- Double . and so on; the differences shrink by about 4 each time.
- Stop when two agree. They first agree to at against 256, with .
- Compare with Richardson's correction. From and alone it already gives 0.746 826: a tiny fraction of the work for the same accuracy.
Answer the results first agree to at , giving 0.746 824.
Check
A current reads 0 A, then 2 A after 0.2 s, then 3 A after a further 0.5 s. What charge passed?
Write units the way you normally would: 1.5 C, 1500 mC and 0.0015 kC are the same answer.
Check
A unit sine wave is clipped at 0.7. What is its RMS?
Check
Write trap(y, h) and simpson(y, h) for equally spaced samples (Simpson needs an even number of slices, so an odd number of samples). Test them on samples of over , whose integral is 2.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
Write trap_xy(x, y) for samples at uneven times. Set Q to the charge in the starter's current log number k = 1.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
Write auto_trap(f, a, b, tol): start with 1 slice, double the number of slices until two successive trapezoid results differ by less than tol, and return (estimate, n), where the estimate is the Richardson value and n the larger slice count. Set r = auto_trap(lambda x: math.exp(-x*x), 0, 1, tol).
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
Sample one period of a unit sine clipped at Ac = 0.7 with 2001 points, and set rms to its RMS using Simpson's rule on . Set exact from the mean-square formula in the reading.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
Write romberg(f, a, b, m): build a triangle of estimates. Row starts with the trapezoid rule on slices; each next entry is . Return the last entry of row . Set r = romberg(lambda x: math.exp(-x*x), 0, 1, 5).
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Explain why
Why is the trapezoid rule the default for logged data, even though Simpson's rule is more accurate for smooth functions?
Simpson's rule gets its accuracy by fitting a parabola through each pair of equal slices, which needs evenly spaced samples, an even number of slices, and a function smooth enough that a parabola describes it. Logged data often fails all three: loggers skip or bunch samples, records end at an odd count, and real signals have kinks, switching edges and noise. The trapezoid rule needs none of that: each slice uses only its own two points and its own width, so it works on any spacing, and its error behaves sensibly at kinks. With noisy data, the accuracy is usually limited by the noise and the sampling rate, not by the rule, so the trapezoid rule's simplicity costs little.
Practice
Keep practicing
7 kinds of problem on this topic, dealt one at a time with new numbers every time, for as long as you like.