# How to minimize RMS calculation error due to sampling?

Say I have a sinusoidal signal of 49.93 Hz, which is sampled at 16 kHz. A period of the input signal would then contain 16000/49.93 = 320.448 samples. Naturally, the number of samples has to be an integer, so it is rounded to 320 samples. Say the RMS value of the continuous input wave is 100 V (volts). When I calculate the RMS value of the 320 samples, the result is 99.91407 V. The error introduced is then 99.91407V - 100V = -0.08593V.

What would be the best way to minimize this error?

• Hi! What's the number of quantization bits? Or, do you assume ideal infinite resolution samples? And, is the frequency of the sine wave known and fixed? Sep 4 at 12:58
• Hi! In the actual HW implementation, it the ADC resolution is 20 bits. However, I am running tests with python on a PC, which uses 64bit floating point representation (equivalent to C's double, i guess..). The frequency of the waveform is known, but not fixed. The frequency varies from about 48Hz to 52Hz. [I edited this comment, as in the previous version I had erroneously written that the frequency could be considered constant] Sep 4 at 13:13

There's many ways to approach this, but the classical "estimation of power in a system having data on harmonic oscillations" would be too just apply a quadrature mixture to baseband to it and take the RMS of the result.

Things would be easy if you knew the phase of your oscillation. If you knew the phase, you could multiply with an amplitude-one oscillation of the same phase, and get a new oscillation at twice the frequency, and at 0 frequency. You'd then eliminate the higher frequency component through filtering, and just look at the amplitude of the 0-frequency component.

Problem: you don't know the phase, usually, nor do you know the frequency that exactly.

There's two possible approaches: in the first, you just rely on the fact that analytical signals to real harmonic signals have constant complex envelope, and in the second you just recover phase and frequency.

# first approach

Calculate a cos and a -sin of the same frequency with amplitude 1/2, multiply each with the observed signal, apply a low-pass filter to eliminate anything above say, half the original frequency (and especially at twice the original frequency). You get two new signals, one from the cosine-branch and one from the sine-branch of your processing. Use the cosine branch as real part and the sine branch as imaginary part.

Notice how the amplitude of the two parts depends on the phase of your original signal, but that the magnitude of the complex number that they form together is constant.

So, you just take the magnitude square of the complex signal you formed and get the square off your amplitude. Apply the mean and the root of it to that.

# second approach

You could also just train an oscillator to have the same phase and frequency as the observed signal, and use that to multiply, then filter away higher frequency components to get rid of the double frequency.

That would be a Phase-Locked Loop (PLL) approach.

• Hi Markus Müller. Thank you immensely for you detailed answer. This RMS calculation will be implemented in a microcontroller operating in real-time, so there are limitations on the ammount of calculations required to get to the answer. In any case, the system already has a PLL running, which is used to determine the frequency. Through your answered I realized I can just use this PLL to also determine the RMS of the fundamental waveform. I will run some tests with it to check the error produced. Sep 4 at 14:00
• Note that neither method requires you to sum up over a full period, so computational load can be quite limited. There's people solving much more involved special estimation problems at higher sampling rates on microcontrollers, do don't worry too much about having to calculate a sine and a cosine a couple thousand times per second. Sep 4 at 14:03
• I still need to determined the true RMS, though (containing frequency components up to the Nyquist frequency). For that, I still intend to apply the RMS calculation to the samples of a period. In order to reduce the error of that, I am now experimenting with a correction factor obtained as a function of the ratio of the float and integer number of samples (in the example, the ratio between 320.448 and 320). Sep 4 at 14:04
• A third approach is to capture many more samples than one period, assuming the signal is stationary long enough to do that (and if the actual frequency is not known apriori, this would be best approach to minimize noise and error as it gets to the fundamental relationship between frequency resolution and total time duration.). Decimating first can reduce the processing load if we know the frequency is so low. Sep 4 at 14:04
• @VicLo: how good does your RMS estimate need to be? Currently you are getting an SNR > 60 dB which isn't horrible. At these levels you may need to look at other noise sources (quantization, analog noise, additive signals, etc) as well. The RMS calculation may not be your biggest problem. Sep 4 at 14:52

Here are some more ideas.

1. If your frequency is not drifting to much you can easily choose a better frame size. In your example you get and error of about -60dB for a frame size of 320 or 321. The "correct" value would be 320.45. If you simply use $$320+321 = 641$$ the error drops below -80dB. That would be an effective frame size of 320.5. If you use $$11*320+9*321=6409$$ the error drops below -110 dB. Obviously you need to adapt the frame size as the frequency changes but that's not that hard to do.
2. The easiest way is to simply square the signal and lowpass filter it. The squared signal has the (desired) RMS at DC plus a second spectral line at twice the signal frequency. The lowpass filter removes this line and allows dialing in the trade-off between accuracy and "agility", i.e. how fast it reacts to changes. For example, a 6th order butterworth at 5 Hz will reduce the error to a whopping -160dB but it takes about 2 seconds to get there.