ADC: Calculating RMS of a DC Biased signal.

28 posts / 0 new
Author
Message

Hi folks,

Just looking for a wee bit of advice.
I wish to build an energy monitor, with WiFi and MQTT and to do so I'm going to use the LinkIt Smart 7688 DUO, mainly because I have some lying around, but also because it has an ATMEGA32U4 slave processor on it that can do real-time tasks.

So I thought I'd use Efergy Energy Monitor clamps - again because I happen to have them lying around.

I will DC Bias the signal from the clamp, so that the full wave falls within the ADC range.

However, I have a question - what is the best (Not necessarily easiest!) way to calculate the RMS of the incoming signal, given that it will be DC biased?

Should I just subtract the mid-point from all the samples, and thereby convert all the ADC readings to signed? And then do the RMS calculations from there? Would that be the correct approach?

SpiderKenny
@spiderelectron
www.spider-e.com

Last Edited: Wed. Nov 8, 2017 - 04:04 PM

Should I just subtract the mid-point from all the samples

Conceptually, that would work fine...

But mathematically, if you calculate the "Area under the curve" for 1/2 a period, or for a full period, and do a single subtraction for the DC offset the end result will be the same.

That assumes, or course, that the DC offset is stable, and that you aren't changing that value within the time span of a single period.

I don't have a M32U4 data sheet handy, but I'll also mention that on some micros there is an op-amp within the micro's ADC front end option.

With that hardware option you could have the ADC do a differential (possibly gained) measurement, and automatically subtract the DC offset in the hardware.

That would work if you have a fixed DC source that you are using to add the DC offset that you could also feed to the "-" input of the ADC's op-amp.

(Note that it is a differential of two positive with respect to ground voltages.)

If the sensor is providing the DC offset, then one could feed the signal through a very low, low pass filter, to obtain the DC offset signal.

JC

if you calculate the "Area under the curve" for 1/2 a period, or for a full period

You assume OPs signal is periodic, and period can be deduced, which is very optimistic (else, RMS should be calculated explicitely, which takes much more time to converge, if it is possible, i.e if offset is known).

Well if we assume (we all know what happens, but ignoring that) that we are talking about household AC, sine wave, then sample to find peak to peak values, and then divide by 1.414 to get RMS value.

You will need to also get the RMS voltage as well, (I recommend you use a transformer to lower the voltage and provide isolation), do the same as above but multipling by xformer ratio to get Vrms,

then Power(average) = Vrms * Irms

Are you talking about single phase AC or multi-phase AC (more complicated).

How accurate do you want to be?

Jim

Interests: Ham Radio, Solar power, futures & currency trading - whats yours?

SpiderKenny wrote:
RMS of the incoming (sic) signal

Depends what you mean by, "incoming"?

The "incoming" signal at the ADC?

Or the "incoming" signal into the clamp?

I guess the latter - you want the RMS of whatever it is that this system is monitoring?

Think about it from first principles: RMS is about getting the POWER in a signal:

• For a steady-state DC signal, that's easy - it's constant;
• For anything else, you have to do sums.

Since the contribution of the DC is constant, you can simply subtract it at the end from your calculation.

Are you doing a proper RMS calculation, or - as Jim suggested - just applying the simplifying assumption of a sinusiod ... ?

Top Tips:

1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...

You bet domestic AC is sinusoidal, without any proof. If some (gaussian, symetrical : it remains a very optimistic bet  during thunderstorms) noise is added, this method will (very slowly, I agree in the gaussian case) diverge.

The idea of sampling at high speed and throwing away any value that is not  extreme leads to a huge waste of data : I bet there are cycles enough to sample at, say, 1 ms rate , compute the square of the value, accumulate it ... according to the definition (which does not imply any unverified hypothesis).

Edited : read awneil post. I might be interesting to have in quasi real time differnce between the best "proper RMS" and one of the worst estimations (under usual circumstances, should not differ that much; detecting unusual circumstances might be funny and maybe useful).

Last Edited: Wed. Nov 8, 2017 - 06:35 PM

Thanks for the replies so far - I had a feeling it wouldn't be dead simple.

Yes it is mains AC at 50 Hz, and fairly sinusoidal.

I'll check out the suggestion of using the built in op-amp (if there is one), failing that, the offset will be provided by simply connecting one side of the clamp (transformer) to the midpoint of a simple voltage divider, the other side will be the signal fed to the ADC across a suitable burden resistor.

SpiderKenny
@spiderelectron
www.spider-e.com

There is an *old* Atmel app note titled 'energy meter', or some such.  I can't remember the actual app note number.  It uses a simple digital filter to remove any dc bias from the readings which eliminates any need for offset adjustments.  The RMS calculations themselves are simple and straightforward.

Tom Pappano
Tulsa, Oklahoma

tpappano wrote:
There is an *old* Atmel app note

AVR1631

Interests: Ham Radio, Solar power, futures & currency trading - whats yours?

Wow ! Those Efergy current transformers are cheap.

But I have a couple of points on the design.

1. Putting a dc bias onto your current clamps doesn't seem like a good thing to do. I don't think they like it one bit.
2. The majority of equipment these days most definitely will not have a sinusoidal current waveform.
3. The dynamic range of current measurement is large. Think about a new Level VI PSU (few mA) versus something like a kettle. 10 bits of ADC may not be enough.

-- Nigel

PS This topic rings a bell with me (in general that is - not specifically the devices you've chosen), I think it's come up on freaks before.

N.Winterbottom wrote:

I think it's come up on freaks before.

I agree, and the cynical answer would have been : look at the topic title; look at wikipedia : OP lives in England and each and everything -s-he needs is in wikipedia (if bias is known, one just has to substract its square ).

Doc JC and you taught me a lot, as I did not think of floating point errors (would occur, if one waits for convergence) and wide dynamics. Perhaps wide dynamics issue can be solved by buying an external ADC, with 16-24 bits....

Doc JC told, and it is true, that a fast  (an infinite improvement upon waiting until it converges)  and exact solution can be reached quickly if signal is periodic. As some equipments are not linear at all (LCD bulbs, switching power supplies), detecting period (and betting it is 50-60 hz) seemed abnsurd for me. If OP has an optocoupler and is very courageous , he might use the voltage -more sinusoidal than current, except for kettles maybe-  (in UK, except for storms, it may be periodic; cheap generators do not know trigonometry)and detect, after low pass filtering, zero up|down wards crossing might give a good estimation of the period (50 Hz in France can vary by 10%, IIRC : only the daily average  must be 50 HZ, else  some clocks would drift -2hrs/day...- ).

Last Edited: Fri. Nov 10, 2017 - 03:24 PM

OP Does not live in England, and telling me to look it up in WikiPedai is just arrogant and insulting.

I didn't ask for help on building an energy monitor, I only asked for advice on removing a DC offset from ADC values. You guys expanded the subject to suit your own gloating needs.

SpiderKenny
@spiderelectron
www.spider-e.com

My signal processing maths is a bit hazy but what about...

In the analogue domain we would remove the DC bias from a waveform by passing it through a C, with an R the other side to 0V. This forms an RC HPF (single pole?). Is it not possible to take a waveform that has been sampled with a DC bias and pass that through a software HPF (single pole?)?

"This forum helps those that help themselves."

"How have you proved that your chip is running at xxMHz?" - Me

"If you think you need floating point to solve the problem then you don't understand the problem. If you really do need floating point then you have a problem you do not understand." - Heater's ex-boss

Well, wikipedia can be found, too, in turkey bombed -seem to like that : matter of religion- Arkansas https://www.avrfreaks.net/forum/faa-looking-after-pilot-drops-turkeys-arkansas-festival (60 Hz) and in Ulster (50hz)....

OK Thanks for all the suggestions, (and all the off-topic speculation too!)

Here's what I think am going to do:-

The waveform will be DC Biased to 50% of the ADC full-count. (If we are using, eg, 10-bit ADC, 0-1023, then mid point is ~512)

Each ADC count will be converted to signed int, and have 512 subtracted from it.

This will create values from -512 to 511 which can easily be used to calculate the RMS.

If I use an external 12 or 16 bit ADC then the midpoint value will be adjusted accordingly.

And just for the reference, I am in Scotland, not England. Different countries, you see.

SpiderKenny
@spiderelectron
www.spider-e.com

Where is Scotland?

North of England 8)

Tom Pappano
Tulsa, Oklahoma

That's a great question, geographically we are "north of England".

Politically we are a "country" but we are also part of the UK, which in itself is part of the EU. We have our own Language, and our own Government, but we also have political responsibility to the UK parliament. It's a mess really.

There's also Great Britain, which is technically just the name of the Island that we live on, but in Sports we run for "Team GB" - which is just wrong. But we can't use "UK" for sports because Norther Ireland, which is part of the UK, also compete in sports independently as "NI"..

When we drive our cars in Europe we must display either "GB" or "SCO" on the numberplate, but GB is just plain wrong, because what happens to people who live on the Islands, like Skye and Orkney? Technically they are not part of Great Britain.....

I think explains it best: https://www.youtube.com/watch?v=rNu8XDBSn10

No wonder we Scottish folks have an identity crisis and want (45% of us anyway) independence. But having said that, the independence campaigners want us to be independent of Westminster (England) politics, but remain part of the EU. ARRRRGGGGGGHHHHHH!!!!!

SpiderKenny
@spiderelectron
www.spider-e.com

@dbrion0606

SpiderKenny
@spiderelectron
www.spider-e.com

dbrion0606 wrote:
Where is Scotland?

tpappano wrote:
North of England 8)

Not to be confused with The North of England ... !

Top Tips:

1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...

Is my Omericon that good?

If you had asked "Belgium", may be I would have felt insulted (but I know belgien people tell the same funny stories about french silliness), but Belgium makes the same best french    freedom fries in the world (there shoul be a tunnel between Dover and Ostende) and that tobacco is very cheap (when Matesa get deported out of France, they buy cigarettes in Belgien, 150 km away... and come back legally into france to sell them)

OTOH, I asked wikipedia about the greates Scott Iread, RL Stevenson: in Scottland, he suffered from loung disease and boredom, he went to the poorest part of France, Vivarais, and travelled with a 4 legged donkey. He did not suffer from long desease anymore.

Then he went to eden like islands, Samoa, with an  Omerican ... and lost his life.

From http://www.gutenberg.org/files/5... RL Stevenson wrote in Traves with Donkey in the Cevennesl :

I am worthy the name of an Englishman, and it goes against my conscience to lay my hand rudely on a female.

"Wha's like us? Damn few and they're a'died"

Translation:

"Who is there like us? Very few, and they are all dead."

SpiderKenny
@spiderelectron
www.spider-e.com

For 60 Hz, I always use a 4 or 16 point Walsh function. 4 point Walsh functions need 240 Hz sample rate, 16 point Walsh function use a 960 Hz sample rate. These are band pass filters hence any DC offset is automatically removed.  The beauty of Walsh function digital filters is that samples are either added or subtracted. The following equations are for a 4 point Walsh function.

CAL = x0 - x1 - x2 + x3

SAL = x0 + x1 - x2 - x2

RMS = sqrt(CAL*CAL + SAL*SAL)

then Power(average) = Vrms * Irms

Well, to make it better, you should use the real-time instantaneous values, or meas the phase difference to account for the power factor.  For example a pure inductor would generate both Vrms & Irms non-zero readings, but a pure inductor uses zero real power.  This because their (ideal) instantaneous I & V values are always 90 degrees shifted.

When in the dark remember-the future looks brighter than ever.

The simplicity of the Walsh algorithm described in my previous posting is not realized until you understand the true implications. x0, x1, x2, and x3 are the present and three previous samples of the input signal sampled at a rate of 240Hz. The CAL result is the real part of an complex value and the SAL result is the imaginary part. Taking the arctangent of the SAL/CAL gives the phase of the voltage.  The Vrms and Vphase are updated at the 240Hz sampling rate.  Now this phase is pretty much meaningless UNTIL you apply the same algorithm to the current. Now the phase angle between voltage and current is simply the difference. To compute the real power, P = Vrms*Irms*cos(Vphase - Iphase). To compute the reactive power, just replace cos with sin. Historically, a multi million dollar company (Schweitzer Engineering) got its start in the mid 70's applying this algorithm to power system protective relays. This allowed my friend Ed Schweitzer to not only calculate the magnitude of the fault current but also the distance to the fault location.  Those interested in applications of DSP to power can read my PhD dissertation.

There is even a way to speed up this algorithm.  Just save the present CAL squared value and use it in the next iteration as the SAL squared value. Hence, for the memory of one variable, three add operations and one multiply operation is eliminated.  The reason that this works is that delaying the CAL value by one sample period is essentially the same as a 90 degree phase shift making it the imaginary part of the complex variable. For those who would like to see the ugly math, here is a place to start: http://dergipark.gov.tr/download.... Enjoy!

...posting is not realized until you understand the true implications

Do you have an example with some actual numbers...your explanation is a "bit" lacking.

When in the dark remember-the future looks brighter than ever.

There is a way to calculate RMS with offset, by subtracting the mean once. On the fly calculations involves sums and sums of squares. N cycle update involves subtraction, square root and scale.
Hint: standard deviation.

It all starts with a mental vision.