Electronic – RMS measurement/calculation On-the-fly

rms

I have an SAR ADC to measure the RMS value of an AC signal. My present method is to use a timer to trigger the conversion and store the conversion result. When I've sampled a whole cycle of points, I subtract the DC level (the average of all points) from the signal then use the well-known RMS algorithm to get the RMS value.

I wonder if there is any method to "distribute" the calculation work to samples. For example, if I need to calculate the mean value of the cycled signal, I can add the conversion result to a global variable "sum" after a conversion is done. When the conversion of the last sample is done, I only need to divide the "sum" by the sample count. In this method, I only need one global variable, there's no need to store all the samples. But for my RMS measurement, what is difficult for me is that I need to subtract the DC level, but the DC level can only be gotten after I've done a whole cycle sampling.

I think you can understand me. Any suggestion is appreciated.


Update:

Thanks for all you who have gave me answers and suggestions. And some answers are acceptable for me. After some searching and thinking, I've an ideal too. But not very sure.

As known,

$$
V_{RMS(total)} = \sqrt{V_{D}^2 + V_{1RMS}^2 + V_{2RMS}^2 + \cdots + V_{nRMS}^2}
$$

Assume a perfect whole cycle sampling, and omit the higher harmonics,

$$
V_{1RMS} = \sqrt{V_{RMS(total)}^2 – V_{D}^2}
$$

So, I can use running-averaging to get \$V_{D}\$, and running-rms to get \$V_{RMS(total)}\$ (without removing the DC), then finally do a subtraction and sqrt to get the RMS value of the fundamental wave signal. Certainly, there will some error when my sampling is not a perfect whole cycle one.

I need someone to confirm this, thanks.

Best Answer

Keep the data stored in a circular buffer, and keep a pointer to the current value. Keep a running squared total. At each sample, subtract the squared value that "falls off" your buffer, and add the squared value that adds on. To get the new sum squared, its then two squarings, and add and a subtract, regardless of how big your buffer is. You're still on the hook for the square root, though.

For efficiency in the modulo math for the circular buffer, I suggest a buffer size of a power of 2.