RMS Value of a Sine Wave with a DC Offset - Page 1
Forum section: electronics / projects

author: Red_Micro
author_rank: Regular Contributor
timestamp: August 31, 2020, 05:40:23 am
content: To find the RMS of sine wave with a DC offset I've seen that I could do three things:1- Subtract the DC value from each sample2- Use a software high pass filter3- Use the formula attached which I think is a derivation from point 1.https://masteringelectronicsdesign.com/how-to-derive-the-rms-value-of-a-sine-wave-with-a-dc-offset/Which of the three points will be more optimal considering a low cost 32 bit microcontroller with no FPU?Background: I'm setting up an ADC triggered each ms with a timer interrupt, or 1 kHz sampling frequency to read a 50/60 Hz AC signal and calculate its RMS with 1% accuracy. Also I'm using DMA. I need to find the RMS in a single cycle (20 ms or 16.67 ms). (Is 1 kHz enough?) This mean that for 50Hz I will take 20 samples per cycle and for 60 Hz 16 samples. So I need to fill the DMA buffer with these few samples.Any guidance is appreciated.

author: Berni
author_rank: Super Contributor
timestamp: August 31, 2020, 06:06:39 am
content: Let me guess you are trying to measure power on mains.I did that with a 16bit PIC before (that means no FPU) without any issues. Forgot some of the details but pretty sure i did all of it in fixed point math by simply using large variables. To remove the DC offset of my ADC pretty sure i just used a simple low pass filter (likely a power of 2 based LERP feeding back into itself since its so cheap to compute) to find the DC offset and subtract it away before the RMS and power math.

author: Fixpoint
author_rank: Regular Contributor
timestamp: September 14, 2020, 04:57:35 pm
content: Quick sidenote: it is unlikely you need to do any fixpoint math. That was necessary in the 80s, but today's MCUs are so fast that you would only need to do that if you have very specific high-performance requirements. So, you probably could just use regular floating point math software emulation courtesy of your compiler.

author: Kleinstein
author_rank: Super Contributor
timestamp: September 14, 2020, 06:48:24 pm
content: Which way to separate the DC part depends on the relative size. If there is a lot of DC (e.g. the zero in the center of the range), it could be better to subtract the DC part. With only a small offset, it works better using the formulae to subtract it, as one does not need to store all the data and have the DC level from the actual data.As the raw ADC values are integer, one can do the squares and sums in integer. Once calculating the final result, there is no problem using floating point, as there may be no good square root for integers anyway.1 kHz sampling is kind of slow motion for a modern µC. So one could do it all with floats, but no real not need for this, especially with so few samples.1 kSPS may be good enough, but it would limit the BW quite a bit. So why not use faster sampling (e.g. some 50 kSPS) so one has the option to have a proper anti-aliasing filter, so that the bandwidth is defined. It also depends one the ADC the µC offers.I have used software RMS with an 8 bit AVR with some 20 kSPS sampling and it did not need a really fast clock.

author: Berni
author_rank: Super Contributor
timestamp: September 15, 2020, 05:23:08 am
content: Yeah you can do it in floating point just fine on a reasonably grunty MCU.Its just that i am used to avoiding floating point from slow old 8bit MCUs. So i tend to develop things to use fixed point if i can, this also includes tricks for avoiding integer division like bitshifts, getting remainders with bitmasks etc... Can really make for a difference of magnitude in speed when the thing has to run in loops.Yes you can't avoid that dreaded square root at the end of a RMS calculation, but this is only calculated once per measurement window, the operations done to individual samples are just multiply and add, so very easy to make that part fixed point.You can also run the ADC faster and then downsample to a low sample rate using averaging. This is very computationally cheep (one add operation per sample). Mostly useful for squeezing better noise and ENOB performance out of a crappy ADC. Tends to clean up the data a lot and might make a 12bit ADC perform like its a 14bit one.

author: Fixpoint
author_rank: Regular Contributor
timestamp: September 15, 2020, 07:14:11 am
content: Quote from: Berni on September 15, 2020, 05:23:08 amLuckily there are clever methods how to calculate square roots very quickly with just integer arithmetics (without LUTs), both in the floating point and fixpoint situation.

author: Renate
author_rank: Super Contributor
timestamp: September 15, 2020, 11:27:43 am
content: Huh, what's all this talk of separating the DC and AC?(Or did you give that up and I didn't notice?)You're sampling a voltage at some interval.Square that value, it's the (relative) instantaneous power.Average that any way you like, windowed, long term, whatever.Take the square root, done.Note: That formula is really only useful for chalkboard calculation.