← Back to the notebookPower meter
A working note

Why a normal average does not work

The saved RMS sketch makes the formula happen a little at a time. It starts a squared sum and count at zero, reads a channel, converts the count to input volts, removes its offset and adds the square. After the collection, it divides by that channel's count and takes the root. I like being able to find each operation in the loop. The equation becomes something I can trace rather than a label attached to the final number.

The first problem is with an ordinary average. If an AC signal is properly centred and I average complete cycles, its positive and negative parts can cancel. That doesn't mean there is no signal. It means the particular average has answered a different question from the one I had in mind. Zero can be a completely correct mean and still be a poor description of how large the alternating signal is. I like that this becomes obvious with tiny numbers before I have to think about hundreds of ADC readings.

Take two values, minus 2 and plus 2, in arbitrary units. Add them and divide by two: the ordinary average is zero. Now square them first. Both become 4. Average those squares and I still have 4. Take the square root and I get 2. That’s the root, mean and square in an order I can follow. Squaring has stopped opposite signs cancelling; the mean describes the collection; the square root brings the result back from squared units to the original unit.

The order matters. If I average first and then square, the cancellation has already happened. In the two-value example, zero squared stays zero. If I take the square root before averaging the squares, I'm doing something else again. The name RMS is a compact reminder of the steps, but the example helps me see why the steps can't be rearranged casually. It’s the kind of thing that is easy to write correctly while copying a formula and easy to misunderstand when changing the code later.

A second set is useful because not every signal has equal magnitude at every reading. In this case, the values are minus 2, zero, plus 2 and zero. The squares are 4, zero, 4 and zero. Their mean is 2, so the RMS is the square root of 2, about 1.414. The peak magnitude is still 2, but the RMS is smaller than in the first example. Both sets reach the same extreme values. They don't spend their sampled values in the same way. That tells me why looking only at the biggest number loses information.

There’s a physical reason for squaring in power calculations as well. For a resistor with resistance R, the instantaneous heating power can be written as current squared times R. Opposite current directions don't produce opposite amounts of heating that cancel. If I want a steady current that would produce the same average heating in that resistor, I end up comparing the average of the squares. Taking the square root gives the corresponding current magnitude. That’s a much more useful explanation to me than saying the formula is used because the signal is AC.

Voltage and current have separate collections in this smaller program: 100 ms for voltage, followed by 100 ms for current. Each has its own count and double-precision sum. The later full meter stores paired channel readings and estimates their centres before accumulating calibrated squares. That changes the organisation of the calculation, but not the reason for root, mean and square. I can compare the two structures without assuming that applying the same formula makes their acquisition windows identical.

I have to be careful with that explanation too. It explains a useful meaning of RMS; it doesn't let me describe every load or waveform using any shortcut I remember. The familiar relationship between sine-wave peak and RMS depends on the waveform. The sampled RMS calculation depends on the values I actually include. I want the formula to make me pay more attention to the signal, not give me an excuse to ignore it because I recognise the letters.

Before the squaring, the sensor offset still has to be handled. The ADC may be reading a signal shifted around a midpoint rather than directly around zero. If I square those shifted values, the midpoint becomes part of the result. In a simple example, values slightly above and below 1.65 volts are not the same thing as their small changes relative to 1.65 volts. I need the intended AC part first. Otherwise the program can perform every RMS operation perfectly and report a quantity different from the one I thought I asked for.

I can also see why the sine shortcut should stay separate from the general calculation. A square wave that stays at equal positive and negative magnitudes has an RMS equal to that magnitude. An ideal sine with the same peak has a smaller RMS. The peaks alone don't distinguish them. The values over the interval do. That’s a useful reason to understand the sample-based calculation rather than use a remembered peak-to-RMS ratio for every shape.

The units help me follow the loop. If the values entering it are volts, the squares are square volts. Averaging doesn't change that unit. Taking the square root returns volts. If I begin with raw ADC counts instead, the intermediate result is in counts until I apply the appropriate scale. I don't want to switch between those descriptions in my head without noticing. A variable's name can make the value sound further along in the conversion than it really is.

The sample window is another part of the meaning. A formula applied to a set of values describes that set. If the values miss important parts of a changing signal, the formula doesn't invent the missing information. A small example on paper is deliberately simplified so I can follow the maths. The real program has to collect values in a way that suits the signal. That brings me back to how many readings it takes and when it takes them. RMS isn't separate from sampling just because the equation can be written on one line.

I'm keeping the equation and the loop beside one another. For each step, I want to say what is being accumulated and what the current number means. The code can make a familiar operation harder to recognise because it happens gradually: add one squared value, repeat, divide after the collection. Working through a few artificial values helps me see whether that repeated process matches the equation. It doesn't validate the instrument, but it does let me understand the calculation before combining it with all the other uncertainties.

The simple examples explain why RMS is useful; the actual loops show where that meaning enters the program. Following both gives me a much better explanation than remembering the order of three words.

Leaving this here for next time.All the notes ↗