Searching \ for 'RMS Oddity' in subject line. ()
Help us get a faster server
FAQ page: www.piclist.com/techref/index.htm?key=rms+oddity
Search entire site for: 'RMS Oddity'.

Truncated match.
'RMS Oddity'
2000\03\29@062445 by

> SUM=0
> SUMSQ = 0
> For N samples
>     SUM = SUM + sample
>     SUMSQ = SUMSQ + sample^2
> Next
> SUMSQ = SUMSQ - SUM
> SUMSQ/N
> SUMSQ = Sqrt(SUMSQ)

I was wondering if this one is like what I have been doing, at least in some
attempts, and I think the other one is exactly what I was trying to
accomplish

> AC couple sinewave
> Reference AC signal to some known +ve value Xoffs
> Take Xoffs away from each sample before squaring.
This sounds like the part (that I didn't include in my 1.53 ratio pseudocode
because I abandoned it by then thinking it was even less accurate resulting)
where I immediately subtract 128 (2.5v offset) from each sample before
squaring it, or if the result of that subtraction is negative, I subtract
the other from the other...and in my thinking, it should be like having a 0
volt centered sine with + and - 2.5v peaks instead of the actual input sine
with 2.5v center and 0 and 5v peaks.
If the whole power level thing with the extra 2.5v DC reference on the sine
makes a difference in the RMS readings compared with a 0 reference of same
peak to peak range, I think it would be important to note also that my
original sine signal IS a 0 reference one with -2.5 to +2.5 range, and it
only ends up with that 2.5v center reference for the sake of shifting it up
between 0 and 5 so I can get it into the pic with full cycle swing.  I add
the 2.5v DC via op amp non inverting input during some signal conditioning
stages.
So I'm all confused with the numbers I get when I try the RMS code that I
think SHOULD work but doesn't seem to, unless it does and I'm reading the
data wrong.

When Iget time maybe I'll put the actual program on a URL along with some
measurement results and maybe someone can tell me if it's proper and I am
worrying about nothing....although I WOULD like to see the pic showing me
what the meter is showing me because if it IS ac coupled...I would think my
PIC code should still show me the same non-DC rms value after I subtract
that 128 level bias....

1. re your factor of 1.53

Note that 1.53^2 = 2.3409
And sqrt(2.5) = 1.58
Suspicious?
Have you used an oscilloscope to measure the mean value of the offset
sinewaves being fed into the PIC.
Or - written code to determine the mean count over a longish period.
I suspect this means you reference is slightly low and the 128 subtract
count is too high.
B,IMBW! (But, I may be wrong.

I think you should read what I said carefullyish and try it - sorry to sound
imperious but I think it really is what you want.
B,IMBW :-) !

Subtracting 128 is fine IF your offset is exactly half converter range.
IF you have an accurate max converter voltage (and if you are using 5 volt
Vcc as the reference then you have more or less) and if you are shifting the
signals up by EXACTLY half this supply then subtracting 128 and then doing
the squaring should produce exactly the correct result.

The IMPORTANT thing is that you follow the concept that offsetting by a
pedestal voltage (even if only to get the signal in converter range) IS
adding a second signal to your initial signal. As power is proportional to
the square of the voltage (and RMS VOLTAGE is all about POWER) then
increasing part of the signal by Vx and decreasing part by Vx will produce a
net power change as the larger parts will increase by the SQUARE of the
voltage and the smaller parts will reduce by the square so their will be an
imbalance.

eg

say Vsamp+ = 1.5V and 180 degress later Vsamp- = -1.5V
The square in both cases is 2.25

Summation Vsamp+^2 + Vsamp-^2 = 4.5

But - add a +1v pedestal.
Vsamp + = 2.5V
Vsamp- = -0.5V.
Vsamp+ ^2 = 6.25
Vsamp - ^2 = 0.25V

Summation Vsamp+^2 + Vsamp-^2 = 6.5
= a net increase in poer nd therefore RMS value.

Also - minor pseudo code correction (and I've changed variable bames to
avoid any confusion :-))
NB - this is a single program -

-------------------------------------
SUM=0
SUMSQ = 0

For N samples
SUM = SUM + sample
SUMSQ = SUMSQ + sample^2
Next N

SUMDIV  = SUMSQ/N
RMS        = Sqrt(SUMSQ)
----------------------------------------

>> AC couple sinewave
>> Reference AC signal to some known +ve value Xoffs
>> Take Xoffs away from each sample before squaring.
>This sounds like the part (that I didn't include in my 1.53 ratio
pseudocode
>because I abandoned it by then thinking it was even less accurate
resulting)
>where I immediately subtract 128 (2.5v offset) from each sample before
>squaring it, or if the result of that subtraction is negative, I subtract
>the other from the other...and in my thinking, it should be like having a 0
>volt centered sine with + and - 2.5v peaks instead of the actual input sine
>with 2.5v center and 0 and 5v peaks.

Yep - this is what you want
BUTY
If the code you are using to sqyare is true 2's compelment code then you
shouldn't need to swap the subtraction results if the sample is < 128
However, for unsigned arithmetic the code would look something like

Get Vsample
IF Vsample >=128 then
Vsample = Vsample - 128
ELSE
Vsample = 128 - Vsample
END IF
proceed with sum of squares etc.

The trouble with this is that there is NO ZERO value available
You have a +/- 128 range but no zero code between the 2 halves. This may not
matter but I'd be more comfortable with setting the range to JUST less, say
+/- 127 and allowing a true zero (at binary 128)

>If the whole power level thing with the extra 2.5v DC reference on the sine
>makes a difference in the RMS readings compared with a 0 reference of same
>peak to peak range, I think it would be important to note also that my
>original sine signal IS a 0 reference one with -2.5 to +2.5 range, and it
>only ends up with that 2.5v center reference for the sake of shifting it up
>between 0 and 5 so I can get it into the pic with full cycle swing.  I add
>the 2.5v DC via op amp non inverting input during some signal conditioning
>stages.

WHY you do it is perfectlky acceptable (indeed, necessary) but the PIC he
don't care - he just calls it as he sees it and he sees an extra amount of
power (voltage) being input.

>So I'm all confused with the numbers I get when I try the RMS code that I
>think SHOULD work but doesn't seem to, unless it does and I'm reading the
>data wrong.

Try it, you'll like it (as long as the rest of the code is correct).

>When Iget time maybe I'll put the actual program on a URL along with some
>measurement results and maybe someone can tell me if it's proper and I am
>worrying about nothing....although I WOULD like to see the pic showing me
>what the meter is showing me because if it IS ac coupled

NO - the PIC WAS Ac coupled but you "DC restored" it (or DC de-restired :-)
it) by addingba signal which isn't really there.
We all know WHY you had to do this but you still have to remove it in the
subsequent processing.

...I would think my
>PIC code should still show me the same non-DC rms value after I subtract
>that 128 level bias....

YES!
Agree!
If you do it correctly and IF the actuall offset is exactly 1/2 of Vref.
An easy way to test this.
Write some code to calculate the mean. This is a relatively simple
IF the value you get for mean is NOT 128 then you are in trouble.
IF the result is not 128 BUT is constant over numerous attempts then this is
the value you should be offsetting by.
.
I don't know how you debug this sort of thing but I find it REAAAALY useful
to send the results to a serial routine (bit bashed one is fine) and display
it on a PC. The brain (mine anyway) is very very slow but impressively
pattern parallel).

Russell McMahon
_____________________________

- http://www.easttimor.com
Updated regularly:
100,000 refugees STI LL in  West Timor face starvation!

- http://www.sudan.com
And you think Kosovo and Chechnya are bad!

What can one man* do?
Help the hungry at no cost to yourself!
at  http://www.thehungersite.com/

(* - or woman, child or internet enabled intelligent entity :-))

Could it be that 1.53 is about pi/2?

I doubt it.

Lorick,

I'm not going to attempt to speculate or rationalize your observations. What I
think would be beneficial is if you answered these questions for us:

1) What's the bandwidth of the signal whose RMS value you seek?

2) What is its worst case peak-to-peak amplitude?

3) What is the minimal value you wish to accurately measure?

4) You describe a full wave rectifier. How is this implemented?

5) Is there any additional circuitry that hasn't been mentioned?

6) What are you using for the A/D converter?

7) What is the sample rate?

It's probably obvious why the answers to these questions are important. However,
let me point out a few observations.

At one point you discussed full wave rectifiers. Keep in mind that a passive
full wave rectifier has two forward diode voltage drops. This severly affects
the low amplitude signals. It's possible to design an active full wave rectifier
that has negligible forward voltage drop. However, if you really want to measure
the RMS value then the FWR will introduce non-linearities if more than one
frequency is present.

Knowing the frequency band of your signal is also important. We're all familiar
more or less with the concept aliasing. (That is, the sample rate of the a/d
converter is too slow for the signal.) However, for RMS calculations like for
FFT calculations, the inverse problem exists as well. If you over sample your
signal and your sample window is (consequently) too short then you'll also have
errors in your RMS calculation. For example, let's say you take 100 samples over
100 milliseconds and that you wish to measure the RMS value of a 1 Hz sine wave.
The sample window is only covering 1/10 of the period of the 1 Hz sine wave. As
a result, the RMS value you measure will oscillate (but it should be constant).
There are ways of getting around this (e.g. solutions like periodograms for
FFT's - where the outputs of your algorithm pass through a subsequent
algorithm/filter).

Russel discussed at some length the effects of DC in the presence of your
signal. Good. But I'm wondering how you are interfaced to the signal. If you're
using a uni-polar A/D converter then it's going to be necessary to bias the AC
signal. (If you don't bias the AC signal then the negative portions of the
signal cannot be measured.) It's not too important what the DC bias is as long
as you 1) know it's value 2) the peaks of the AC signal don't exceed the DC
bias.

Now, introducing a DC bias to an AC signal presumes that you're not interested
in DC or that you don't care if your AC signal get's driven by your DC bias. For
example, most often the DC biasing circuit AC-couples your signal to your
system. AC-coupling means there's a capacitor in series with the signal - DC
can't get through. If you want to measure the DC component as well, then you can
remove the capacitor, however the DC bias will now be driving your
sensor/signal. Perhaps this is okay - no one can tell until we know your signal
source.

Hopefully these comments will give you some ideas to ponder.

Scott

Lorick,
The method you are using will not give the proper RMS value
for a very simple reason: your "zero" point is not actually

You are making the assumption that you can use +2.5 volts
as your reference "zero point". That will only work if
you have insured that the **physical** input does in fact have
a +2.5 volt value when the system input is 0 volts.

To accomplish this you need a circuit such as the following:

O +5v
|
|
\
/ 10k
1uf    \
/
||    |
||    |
|
\
/ 10k
\
/
|
|
O Ground

The RMS method now involves the following steps:
2) (a) If reading is +2.5, store it as 0
(b) If reading is > +2.5, store it as Value-2.5
(c) If reading is < +2.5, store it as +2.5-Value

the following algorithm assumes that all input
values have been "normalized" as in step (2) above!

SQSUM=0     ;initialize to 0
N=0         ;N will hold the number of samples
MAX_N=100   ;this should be adjusted to allow several
; complete cycles!
FOR N=1 to MAX_N step 1
Convert to Normalized Value  ;see step (2) above
Square Normalized Value
SQSUM=SQSUM+Normalized Value ;the S part of RMS!
NEXT N

MEAN_OF_SQSUM=SQSUM/N  ;the M part of RMS!
RMS=SQRT(MEAN_OF_SQSUM)

In this case the RMS is the RMS value over the
period covered by the time it took to accumulate

To get the proper RMS of at repetitive waveform
the time period for MAX_N number of ADC samples
should be an integer multiple (1 2 3 4 etc) of
the period of the waveform if you want maximum
accuracy.

Note that if you sample for a long enough time
and that covers many cycles of the waveform, then
you can achieve very good accuracy even if you
don't sample an exact integer multiple. That is
because the total sample base is so large that
the error becomes an insignificant part of the
total.

I hope this helps to clear up some things as regards
measuring RMS with a PIC.

If you want to measure DC RMS or AC + DC RMS, then
you are best off setting your "zero point" at TRUE
ZERO. The input must be "precision rectified". In
that case use an opamp precision rectifier circuit
to do the rectification. When using a precision
rectifier at the input you don't need the circuit
shown above, and you can skip step (2), since
the precision rectification will already "normalize"
the input.

Fr. Tom McGahee

{Original Message removed}
At 11:29 AM 3/29/00 -0500, you wrote:
>Lorick,
>The method you are using will not give the proper RMS value
>for a very simple reason: your "zero" point is not actually

My assumption: he is using a circuit like this:

0 Vdd
|
R1     (floating input voltage)
|         in (-)    in (+)
----------0         0---------------> to PIC A/D input
|
R2
|
0V

Where R1 = R2  (and they are accurate resistors)
and R1||R2 << max input impedance for PIC A/D inputs.

In this case (and assuming the input signal is floating
wrt the PIC supplies), his circuit is fine, and will
work with AC or DC, but is pretty limited in dynamic
range with such a low resolution (7-bit effectively,
since one bit is wasted doing the rectification in
software). So, measurements on lower voltage or
high crest factor waveforms will likely be inaccurate.

The requirement for a strange BF adjustment is prob'ly
an indication of a software problem somewhere if the
assumptions are correct.

Best regards,

=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
Spehro Pefhany --"it's the network..."            "The Journey is the reward"
speffinterlog.com             Info for manufacturers: http://www.trexon.com
Embedded software/hardware/analog  Info for designers:  http://www.speff.com
Contributions invited->The AVR-gcc FAQ is at:
http://www.bluecollarlinux.com
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

On Wed, 29 Mar 2000 06:25:27 -0500 Lorick <lorickAIR.ON.CA> writes:
> > SUM=0
> > SUMSQ = 0
> > For N samples
> >     SUM = SUM + sample
> >     SUMSQ = SUMSQ + sample^2
> > Next
> > SUMSQ = SUMSQ - SUM
> > SUMSQ/N
> > SUMSQ = Sqrt(SUMSQ)
>
> I was wondering if this one is like what I have been doing, at least
> in some
> attempts, and I think the other one is exactly what I was trying to
> accomplish

Why is the sumsq=sumsq-sum there?

Harold

FCC Rules Online at http://hallikainen.com/FccRules
Lighting control for theatre and television at http://www.dovesystems.com

________________________________________________________________
YOU'RE PAYING TOO MUCH FOR THE INTERNET!
Juno now offers FREE Internet Access!
Try it today - there's no risk!  For your FREE software, visit:
dl.http://www.juno.com/get/tagj.

----------
> From: Lorick <lorickAIR.ON.CA>
> To: PICLISTMITVMA.MIT.EDU
> Subject: RMS Oddity
> Date: Wednesday, March 29, 2000 3:25 AM
>
>> SUM=0
>> SUMSQ = 0
>> For N samples
>>     SUM = SUM + sample
>>     SUMSQ = SUMSQ + sample^2
>> Next
>> SUMSQ = SUMSQ - SUM
>> SUMSQ/N
>> SUMSQ = Sqrt(SUMSQ)
>

Shouldn't the last 3 lines be something like this? :

MEAN = SUM/N
MSE = SUMSQ/N - MEAN^2
RMSE = SQRT(MSE)

This should give the RMS deviation from the mean (DC value),
which sounds like what you want.

> worrying about nothing....although I WOULD like to see the pic showing me
> what the meter is showing me because if it IS ac coupled...I would think
my
> PIC code should still show me the same non-DC rms value after I subtract
> that 128 level bias....

> Could it be that 1.53 is about pi/2?
> I doubt it.
With a margin of error, it comes close but I can't see why dividing by it
would fix my trouble either way...

> 1) What's the bandwidth of the signal whose RMS value you seek?
A 330Hz constant nice lab function generator going through a 3rd order op
amp filter whereby the DC bias of 2.5v is introduced for the a/d to like it.

> 2) What is its worst case peak-to-peak amplitude?
Before filter, from function generator is 0 to +/- 2.5v sine.
After filter, 0 to 5v minus losses with 2.5v center.  The filter is
calculated (and tested/verified) for
no attenuation or amplification in the pass band, and just to attenuate from
that point on outside the
pass band, so my 2.5v peak input is always at dead center of the filter pass
band for these tests and the output is always nicely from 0 to almost 5 with
the input from -2.5 to 2.5

> 3) What is the minimal value you wish to accurately measure?
This isn't too critical.  At one point I was considering ignoring anything
below 0.5v RMS but it's just arbitrary in the end.  As long as I can even
just get a valid window, all I need in the end is to be able to get some
region where I can break it into 8 steps and always be certain that anything
between the 2 end point voltages will be accurately measured in RMS and I
can divide this region into 8 equal steps and tell which of 8 increments I'm
at with a valid input range to give the RMS output between the working end
points that I can measure wth success.  If I find that I can only get
good results between 0.9vrms and 1.7vrms, I will only make use of that and
have 0.1v RMS intervals to look at.
All I'm doing is using the function generator as a manually set input
sensor, so if I decide to mimic a sensor reading of half scale, and I know
my valid RMS input voltages are from 0.9 to 1.7v RMS, I will set the
amplitude to give me 1.3v RMS (half way along my 8 partition scaling) and
the PIC hopefully would read the same 1.3v RMS that I'm putting in and
output that indeed this sensor is showing half of its range and thus I
should perform a certain task, etc.

There's no real world use yet where I have to consider varying conditions
and impure signal inputs, losses, etc. I just need a nice theoretical RMS
measurement on this lab sine wave.

> 4) You describe a full wave rectifier. How is this implemented?
That's my little software 128 of 255 bias line that I subtract from any
input greater than 128 (or so) or subtract inputs lower than 128 from 128
and if I am thinking right, I turn the 0 to 5v swinging sine wave into two
identical series positive humps from 0 to 128 of 255.  Or close to it.

> 5) Is there any additional circuitry that hasn't been mentioned?
Just that filter which I have been neglecting.  While I make all these
observations, I always have the filter output on the scope with DC coupling
so I can see its real swinging range not zero centered, and I do verify that
it goes 0 to 5v peak to peak or whatever the case, so I consider it to be
somewhat of an ideal input signal and speak of sine waves in pure context.
Looks nice and pure on the display at least. I don't know if I'm neglecting
anything.

> 6) What are you using for the A/D converter?
The 16F876 analog input with VDD/VSS references and only considering 8 of
the 10 bits.  I wonder if it matters which way I justify the reading and
truncate it....

> 7) What is the sample rate?
That's something I've been trying to decide how to determine.  How would I?
I still haven't had time to look into it with the data sheets and I know now
that I won't have time for at least a month.

> 8) What is your application?
I guess I covered that with the lab input and  8 range decision making
output based on what the input falls between...
What I am actually doing with the numbers is calculating sides of a
triangle.  I am taking two analog inputs and trying to get RMS.  they are
both sine waves and different amplitudes.  The two RMS values indicate two
sides of a triangle and the 3rd side is a program constant.  Based on the 2
sides measured, I am doing some add/subtract/multiply/divide triangle
formula magic and trying to calculate a certain other side length of a
triangle within the bigger triangle....I did have the thing properly
calculating one test, but as I changed the two input amplitudes, the error
in the calculation increased and I traced it back to the RMS calculation
which worked perfectly at maximum swing input, but got more erroneous as the
voltage lowered.

> At one point you discussed full wave rectifiers. Keep in mind that a
passive
> full wave rectifier has two forward diode voltage drops. This severly
affects
> the low amplitude signals. It's possible to design an active full wave
rectifier
> that has negligible forward voltage drop. However, if you really want to
measure
> the RMS value then the FWR will introduce non-linearities if more than one
> frequency is present.
If I would have used hardware I was considering the op amp precision full
wave rectifier but I thought subtracting
the bias in software would work too. And the code was short that I came up
with.

> If you over sample your
> signal and your sample window is (consequently) too short then you'll also
have
> errors in your RMS calculation. For example, let's say you take 100
samples over
> 100 milliseconds and that you wish to measure the RMS value of a 1 Hz sine
wave.
> The sample window is only covering 1/10 of the period of the 1 Hz sine
wave. As
> a result, the RMS value you measure will oscillate (but it should be
constant).
I guess I should figure out how to determine my sampling rate and then
figure out if it's too fast.
I don't see it being too slow at all.

>  I'm wondering how you are interfaced to the signal. If you're
> using a uni-polar A/D converter then it's going to be necessary to bias
the AC
> signal.
Indeed I am.

> Now, introducing a DC bias to an AC signal presumes that you're not
interested
> in DC or that you don't care if your AC signal get's driven by your DC
bias.
I don't need the DC any more than to bias for full reading ability and then
Id' like it extracted.

>On Wed, 29 Mar 2000 06:25:27 -0500 Lorick <lorickAIR.ON.CA> writes:

Actually, it was me

{Quote hidden}

This is THE crucial step which corrects for the RMS value of the DC offset
which is what is causing the problem he has (according to me anyway :-))
Try the algorithm with a non varying input - ie no sine wave. Without this
line you get the RMS value of the "pedestal" that the sine wave is sitting
on.
WITH this line you get 0 which is what you want.
This method is arguably better than just taking off the binary value of the
offset as it automatically tracks the TRUE value of the offset which may not
be the hald suply which it is assumed to be.

RM

> work with AC or DC, but is pretty limited in dynamic
> range with such a low resolution (7-bit effectively,
> since one bit is wasted doing the rectification in
> software). So, measurements on lower voltage or
> high crest factor waveforms will likely be inaccurate.

Sounds like what I was observing...I was fine with full input, but the lower
I made the amplitude, the more inaccurate it was.  So why are the lower

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

>Sounds like what I was observing...I was fine with full input, but the lower
>I made the amplitude, the more inaccurate it was.  So why are the lower

The ADC has larger error (percentage wise) as the signal gets smaller,
since it measures in fixed steps.

Otherwise, you may have a small DC offset.

-----BEGIN PGP SIGNATURE-----
Version: PGPfreeware 6.5.2 for non-commercial use <http://www.pgp.com>

iQA/AwUBOOLBF4FlGDz1l6VWEQJ/XgCffJ+RX6LaeYQk5iZLaPYTjM1oMzsAoLvJ
eDxzgx+iJg15MJqp40NeD1gQ
=Dsll
-----END PGP SIGNATURE-----

Lorick,

This thing has no doubt got you seriously brain battered by now,
but don't give up and we'll forgive you for not replying to individual
messages.

A tip for checking your timing is pulse a spare output port each
output for each new final RMS calculation. Look at these on your
scope, overlay with input sinewave if necessary.

Someone has said you don't need to be above the nyquist sample
freq for what you are doing. In my opinion though (a resaonably
humble one), such a scheme would work only if your sampling was
truly random, ie. irregular sample times, and you had a
considerable time period to average a large number of results. In
other words it probably doesn't suit the way you are doing it right
now, you really do need many samples for each cycle of the input
waveform. Could get into this deeper but won't just yet.

The rest of your method is good so the above is the only area I can
think of that really needs checking out. To be doubly sure you
know what you are doing see if my steps below are what you
expect/see.

Input waveform is sine wave 330Hz from -2.5 to +2.5V.
This can be stated as 2.5Vpk, 5.0Vpk-pk or 1.77VRMS

-Do your software "full wave rectification" and offset removal (use
either fixed offest of 127 or dynamic value as in other suggestions.)

-Result is a series of zero referenced positive half cycles with 127
peak value, (7 bits).

-Sum the squares of 256 consecutive samples, divide by 256, take
the square root. The result will be 89 or 90 for your sinewave. A
square wave input would measure 127 (provided of course that you
used the same 2.5V DC offset and the squarewave goes from 0 to
5V). This is the RMS value.

What would you expect from a DC input? =>

Vin=0.0V, result = 127
Vin=1.0V, result = 76
Vin=2.0V, result = 25
Vin=2.5V, result = 0
Vin=3.0V, result = 26
Vin=4.0V, result = 77
Vin=5.0V, result = 128

Brent Brown
Electronic Design Solutions
16 English Street
Hamilton, New Zealand
Ph/fax: +64 7 849 0069
Mobile: 025 334 069
eMail:  brent.brownclear.net.nz

>The requirement for a strange BF adjustment is prob'ly
>an indication of a software problem somewhere if the
>assumptions are correct.

remember that the RMS value is related to the area between the curve of the
waveform and the zero line. hence when you impose the ac signal on a dc offset,
you are measuring on the "opposite side" of the waveform for half of the signal
to what you were before the dc offset, and the rest of the time you have
considerable more area under the waveform than before the dc offset. This is
probably why the 1.53 factor appears in the calculation, and is within the
formula accuracy presented by someone else, when related to the a/d accuracy.

> remember that the RMS value is related to the area between the curve of
the
> waveform and the zero line. hence when you impose the ac signal on a dc
offset,
> you are measuring on the "opposite side" of the waveform for half of the
signal
> to what you were before the dc offset, and the rest of the time you have
> considerable more area under the waveform than before the dc offset. This
is
> probably why the 1.53 factor appears in the calculation, and is within the
> formula accuracy presented by someone else, when related to the a/d
accuracy.

Does this suggest that it would be useful to manipulate one of the 2 halves
of the waveform in some manner (whatever that may be, inverting it, flipping
it, whatever applies) to get an accurate reading?

I considered that there should be no trouble with just shifting the top half
down since it was always on the positive half of the cycle to begin with,
but I wondered if there was some symmetry issue with the bottom half..  I
forget what I had tried but my end result was that instead of attempting to
get a processed waveform that had two positive humps shifted down by 128, I
tried an experiment with the one positive hump shifted down from above the
128 line, and the other hump that was originally between 0 and 128, I left
it as it was instead of inverting it to get another positive hump.  I did
get a different reading but it was still wrong at least in that experiment,
which might have been wrong in other ways too.  I think the error was
greater than when I inverted the lower hump to make two positive humps.

another couple of items to consider for your circuit, first use an op amp as a
full wave rectifier, to make your waveform go from 0 to Vcc instead of +/-0.5Vcc
on a pedestal. This will get rid of one of your problems - subtracting the
pedestal voltage from your calculation. Also it will give you twice the accuracy
in your a/d converter as you no longer need a sign bit.

the other item to consider is to make the sample rate a number which causes
rejection of 50/60Hz ripple, to make it easier to remove miscellaneous mains
incoming signal, but such noise has a habit of finding its way into the system
by all sorts of nefarious routes. If you select a sample frequency that allows
it to reject both 50 and 60 Hz, then it makes it easier to "internationalise"

I think Alan suggested the right way to do it. Since you are using the
opamp to drag the AC to 0V-2.5V-5V range, you can rectify the
signal instead and get the ground level unchanged - for RMS the
polarity is irrelevant. Shifting the ground level as you do (by adding
2.5V) often causes errors and I always try to avoid that. Another plus
is that you'll have that +1 bit resolution and you'll be able to measure
RMS of AC+DC signals as long as the peaks don't exceed 5V.

The drawback of this circuit is that you need negative supply but that is
not that hard, you can generate low current  -Vcc from +5V easily.

Another thing you could do - do 256 samples and get them out from PIC
somehow (ie. use PC serial port) and analyze the data with Excell.
You shoud be able to track down any oddity or bugs or whatever makes

----------
> From: Alan B Pearce <A.B.PearceRL.AC.UK>
> To: PICLISTMITVMA.MIT.EDU
> Subject: Re: RMS Oddity
> Date: 30 March, 2000 11:09
>
> another couple of items to consider for your circuit, first use an op amp
as a
> full wave rectifier, to make your waveform go from 0 to Vcc instead of
+/-0.5Vcc
> on a pedestal. This will get rid of one of your problems - subtracting
the
> pedestal voltage from your calculation. Also it will give you twice the
accuracy
> in your a/d converter as you no longer need a sign bit.
>
> the other item to consider is to make the sample rate a number which
causes
> rejection of 50/60Hz ripple, to make it easier to remove miscellaneous
mains
the
> incoming signal, but such noise has a habit of finding its way into the
system
> by all sorts of nefarious routes. If you select a sample frequency that
allows
> it to reject both 50 and 60 Hz, then it makes it easier to
"internationalise"

On Wed, 29 Mar 2000, Lorick wrote:

>From what you've described, most of what everyone's been suggesting is
correct. I can a couple of things that you may wish to watch out for.

The 'window' time, or the time over which you sample the signal, needs to be
infinity for an RMS calculation. Obviously that condition can't be met. So be
aware that sampling for a finite time can introduce errors. As an extreme
example, let's consider your 300hz sinewave. This has a period of 1/300 or 3.33
mSec. If you sample for a quarter of the period, or 208 uSec, it's probably
obvious that you'll introduce an error. However, if your sampling window is not
even multiple of half of the period or (N * 1.6667mSec) you'll still introduce
an error in the 'true' rms value. Compare the results for a window that is 1
cycle wide versus one that is 1 and a quarter cycles.

There are two ways to minimize this error. If you know that you're signal is
predominately 300Hz plus a "little noise", then you can sample for integer
number of half periods of 300Hz. But if the frequency changes, then you need to
adjust the sampling window. Yuk. Second, you can make the window as "long as
possible" and then combine the results of successive RMS calculations.  For
example, if you sampled about 5 cycles (but not exactly 5) you'll get a small
error (smaller than if you sampled over about 1 cycle). Now if you average two
consecutive runs through the RMS algorithm you'll get a better result. But,
you'll still have an error. However, you can concatenate the consecutive
calculations before performing the square root. This will, over time, converge
to a more accurate result.

Also, the hard coded DC value of 128 should, as others have suggested, be
subtracted from your data stream prior to squaring. There are two observations
here. As (I believe it waw Russel) suggested, you may improve on the estimation
of the DC component by explicitly measuring it. This estimate may be improved in
analogous manner to above by combining results of several passes through the
algorithm. Second, if the DC term is really just '128', then the 8-bit squaring
operation can be turned into a 7-bit one and computed in 31 cycles.

There are a few more suggestions I have, but I don't have the time now to
elaborate...

Scott

More... (looser matching)
- Last day of these posts
- In 2000 , 2001 only
- Today
- New search...