Truncated match.
PICList
Thread
'RMS oddity?'
2000\03\29@014726
by
Lorick

I previously tested my little RMS calculation on the analog input and
thought it was working...but I was wrong.
Same story, 255 samples taken, squared, summed, divided by 255, square
rooted.....
I first thought it was right because I was putting in maximum, 5v peak sine
wave, 0v bottom end, and the RMS is supposed to be 1.76v and that's what I
measured. Actually, I think I first got 2.7v using the sum of squares thing
alone but then I modified it to process each sample and full wave rectify it
and remove the DC bias so it would look like a wave with still a 5 volt
swing with 2.5v peaks from the center...and that's when I got the 1.76v
which I expected, but I should have tested it more.
As I lowered the input voltage from the maximum, an error factor increased
and when I was down to 1.2vrms input (by real meter) and kept going lower,
the PIC reading started to go back up....so by the time I was down at near 0
input, I was reading almost 2 volts in the pic....I assumed my full wave
rectifier approach was no good afterall, and the raw unrectified root of the
sum of squares gave 2.7v out when it should have been 1.76 at the peak...so
I did some end point tests.
When I put in 0.5v rms, I measured 0.76vRMS in the PIC....so I divided 0.76
(pic) by 0.5 (expected) and got 1.53 as a ratio. Then I took the initial
measurement and divided 2.7 (pic) by 1.76 (expected) and got the same 1.53.
Magically I discovered a direct relationship between measured and expected
and thought....great all I need to do is divide the incoming "rms"
calculation result by 1.53 and I should have a valid RMS measurement
finally. Then it hit me....how do I do floating point.....but I remembered
the autocodegenerator thing on the piclist math page in the techref area
so I tried it out. I entered that I wanted an 8 bit input and multiplier of
0.65 (1/1.53) and it started barking at me (rrf, rrf, rrf, etc) so I assumed
it was done....I plugged the tweaking algorithm into the end of my RMS thing
and tried it out, it works....
So then finally the question is....does anyone know more on this RMS oddity
I have?
Could this magical 1.53 or 0.65 relationship be due to my midway DC bias
point on the input that properly scales down the high RMS calculations to
what it would be if it were a truly zero centered input? Or is such a
weighting factor always needed in addition to bare sum of square methods for
finding valid RMS results?
Out of interest, here is that portion of code that successfully tweaked the
calculation.
;Portion of pseudocode for RMS calculation
;System consists of analog signal input with its zero
reference at 2.5v (128 out of 255 in 8 bit)
;This method works but it is unknown if it is only for this
custom application of a 2.5v bias or if
;It would always work.
loop Get sample from analog input
Multiply sample by itself (square)
Add sample to accumulator
goto loop (255 times)
Divide accumulator by 255
Find Square Root of division result
Call TWEAK routine for final RMS value of analog input
; Generated by http://www.piclist.com/cgibin/constdivmul.exe (version March 21,
2000)
; Wed Mar 29 05:44:16 2000 GMT
;divide by 1.53 (mul by 0.65) for tweaking RMS calc
; ACC = ACC * 0.653595
;
; ALGORITHM:
; Clear accumulator
; Add input / 2 to accumulator
; Add input / 8 to accumulator
; Add input / 32 to accumulator
; Move accumulator to result
;
; Error in constant approximation : 0.406250, %
; Input: ACC0 (8 bits)
; Output: ACC0 (8 bits)
Tweak movf ACC0,w
clrc
rrf ACC0,f
clrc
rrf ACC0,f
addwf ACC0,f
rrf ACC0,f
clrc
rrf ACC0,f
addwf ACC0,f
rrf ACC0,f
return
2000\03\29@023250
by
Michael RigbyJones

part 0 5686 bytes
<P><FONT SIZE=2 FACE="Arial">I previously tested my little RMS calculation on the analog input and</FONT>
<BR><FONT SIZE=2 FACE="Arial">thought it was working...but I was wrong.</FONT>
</P>
<P><FONT SIZE=2 FACE="Arial">Same story, 255 samples taken, squared, summed, divided by 255, square</FONT>
<BR><FONT SIZE=2 FACE="Arial">rooted.....</FONT>
</P>
<P><FONT SIZE=2 FACE="Arial">I first thought it was right because I was putting in maximum, 5v peak sine</FONT>
<BR><FONT SIZE=2 FACE="Arial">wave, 0v bottom end, and the RMS is supposed to be 1.76v and that's what I</FONT>
<BR><FONT SIZE=2 FACE="Arial">measured. Actually, I think I first got 2.7v using the sum of squares thing</FONT>
<BR><FONT SIZE=2 FACE="Arial">alone but then I modified it to process each sample and full wave rectify it</FONT>
<BR><FONT SIZE=2 FACE="Arial">and remove the DC bias so it would look like a wave with still a 5 volt</FONT>
<BR><FONT SIZE=2 FACE="Arial">swing with 2.5v peaks from the center...and that's when I got the 1.76v</FONT>
<BR><FONT SIZE=2 FACE="Arial">which I expected, but I should have tested it more.</FONT>
</P>
<P><FONT SIZE=2 FACE="Arial">As I lowered the input voltage from the maximum, an error factor increased</FONT>
<BR><FONT SIZE=2 FACE="Arial">and when I was down to 1.2vrms input (by real meter) and kept going lower,</FONT>
<BR><FONT SIZE=2 FACE="Arial">the PIC reading started to go back up....so by the time I was down at near 0</FONT>
<BR><FONT SIZE=2 FACE="Arial">input, I was reading almost 2 volts in the pic....I assumed my full wave</FONT>
<BR><FONT SIZE=2 FACE="Arial">rectifier approach was no good afterall, and the raw unrectified root of the</FONT>
<BR><FONT SIZE=2 FACE="Arial">sum of squares gave 2.7v out when it should have been 1.76 at the peak...so</FONT>
<BR><FONT SIZE=2 FACE="Arial">I did some end point tests.</FONT>
</P>
<P><FONT SIZE=2 FACE="Arial">When I put in 0.5v rms, I measured 0.76vRMS in the PIC....so I divided 0.76</FONT>
<BR><FONT SIZE=2 FACE="Arial">(pic) by 0.5 (expected) and got 1.53 as a ratio. Then I took the initial</FONT>
<BR><FONT SIZE=2 FACE="Arial">measurement and divided 2.7 (pic) by 1.76 (expected) and got the same 1.53.</FONT>
</P>
<P><FONT SIZE=2 FACE="Arial">Magically I discovered a direct relationship between measured and expected</FONT>
<BR><FONT SIZE=2 FACE="Arial">and thought....great all I need to do is divide the incoming "rms"</FONT>
<BR><FONT SIZE=2 FACE="Arial">calculation result by 1.53 and I should have a valid RMS measurement</FONT>
<BR><FONT SIZE=2 FACE="Arial">finally. Then it hit me....how do I do floating point.....but I remembered</FONT>
<BR><FONT SIZE=2 FACE="Arial">the autocodegenerator thing on the piclist math page in the techref area</FONT>
<BR><FONT SIZE=2 FACE="Arial">so I tried it out. I entered that I wanted an 8 bit input and multiplier of</FONT>
<BR><FONT SIZE=2 FACE="Arial">0.65 (1/1.53) and it started barking at me (rrf, rrf, rrf, etc) so I assumed</FONT>
<BR><FONT SIZE=2 FACE="Arial">it was done....I plugged the tweaking algorithm into the end of my RMS thing</FONT>
<BR><FONT SIZE=2 FACE="Arial">and tried it out, it works....</FONT>
</P>
<P><FONT SIZE=2 FACE="Arial">So then finally the question is....does anyone know more on this RMS oddity</FONT>
<BR><FONT SIZE=2 FACE="Arial">I have?</FONT>
<BR><FONT SIZE=2 FACE="Arial">Could this magical 1.53 or 0.65 relationship be due to my midway DC bias</FONT>
<BR><FONT SIZE=2 FACE="Arial">point on the input that properly scales down the high RMS calculations to</FONT>
<BR><FONT SIZE=2 FACE="Arial">what it would be if it were a truly zero centered input? Or is such a</FONT>
<BR><FONT SIZE=2 FACE="Arial">weighting factor always needed in addition to bare sum of square methods for</FONT>
<BR><FONT SIZE=2 FACE="Arial">finding valid RMS results?</FONT>
</P>
</UL>
<P><FONT COLOR="#0000FF" SIZE=2 FACE="Arial">No magic about it. For a *sinusoidal* waveform, the ratio of peak to peak amplitude to RMS is the square root of 2 i.e: 1.41. Your 1.53 is probably due to measurement error or an offset on your waveform. If your input is always going to have the same waveform, then you can use this method. However, if you start putting square waves etc into your circuit, it will give large errors.</FONT></P>
<P><FONT COLOR="#0000FF" SIZE=2 FACE="Arial">Mike</FONT>
</P>
</BODY>
</HTML>
</xhtml>
2000\03\29@025400
by
Spehro Pefhany

At 01:47 AM 3/29/00 0500, you wrote:
>I previously tested my little RMS calculation on the analog input and
>thought it was working...but I was wrong.
>
>Same story, 255 samples taken, squared, summed, divided by 255, square
>rooted.....
What scaling are you using? I presume you are calculating the input
as abs(adreading  0x80), so 2.50 V in is zero.. then you are doing
32 bit math to calculate the values... so an RMS input of 2.5V (either 0
or 5V DC input) would give you 128 each reading, squared and summed to
4177920 (0x3FC000), you divide by 255 to get 16384 (0x4000), then square
root it to get 128 (0x80) again.. (worst case input). That is, for a
2.50V RMS input (0V or 5VDC), you will get 0x80 output.
If I presume you are using scaling so that 255 = 0xFF = 5.00 V, then the
output display should be about right. BTW, why are you using 255 samples?
It's a lot easier to divide by 256 (just discard the lowest byte).
If it doesn't work as above then there is probably a problem somewhere that
could well come back to bite you. Suggest you track it down.
Best regards,
=======================================
Spehro Pefhany "it's the network..." "The Journey is the reward"
spam_OUTspeffTakeThisOuTinterlog.com Info for manufacturers: http://www.trexon.com
Embedded software/hardware/analog Info for designers: http://www.speff.com
Contributions invited>The AVRgcc FAQ is at:
http://www.bluecollarlinux.com
=======================================
2000\03\29@030842
by
Lorick
part 0 16 bytes
</xhtml>
2000\03\29@032601
by
Lorick

> What scaling are you using? I presume you are calculating the input
> as abs(adreading  0x80), so 2.50 V in is zero..
Either that, or close to it is what I used to be doing but I still got error
so now I'm
just taking the input as it is with the 0x80 zero instead of subtracting
0x80. I was
taking the hump above 0x80 and shifting it straight down below 0x80, and the
hump
that was already below 0x80 I inverted so that I would have what looks like
a
full wave rectifier output from 0 to 0x80 and I thought that would work like
a sine
wave from 2.5 to +2.5v once squared, but I had weird error factors. Maybe
I could
look at those factors again but I'm running out of time. I witnessed my
readings go down from
maximum to about 1.2v RMS (real level) and then when I'd continue to lower
the rms input,
the Pic would start reading higher values again and I peaked at almost 2v
reading on the pic
when I was at 0v real input....and the highest I'm even putting in is 1.76
rms to begin with.
> then you are doing
> 32 bit math to calculate the values... so an RMS input of 2.5V (either 0
> or 5V DC input) would give you 128 each reading, squared and summed to
> 4177920 (0x3FC000), you divide by 255 to get 16384 (0x4000), then square
> root it to get 128 (0x80) again.. (worst case input). That is, for a
> 2.50V RMS input (0V or 5VDC), you will get 0x80 output.
That's how it was set up with my inverted and shifted hump story rectifier,
yes.
But the results didn't look right and I did test my rectifier routine
manually with forced
inputs and it did what it should have functionally...etc.
> If I presume you are using scaling so that 255 = 0xFF = 5.00 V, then the
> output display should be about right.
That's my scale, 0 to 5v peak input, 0 to 255 adc results, 128 zero
line...So what I can't figure out is
if I am having a zero line where 2.5v or 0v input get converted to 128, how
does it work
out to an accurate rms final result in the end? Once and for all, if I
follow this method,
should I get true RMS output by (I have said this so many times I'm starting
to hate it)
taking samples, subracting the 128 reference from samples greater than 128
and subtracting the
samples from 128 for those less than reference (rectify?), squaring the
rectified sample, summing it
with other samples thus far, dividing by total samples taken, then just
square rooting and nothing
further?
> BTW, why are you using 255 samples?
> It's a lot easier to divide by 256 (just discard the lowest byte).
I knew I'd seen that somewhere but I couldn't remember the specifics so I
did it the long way and
chose 255 because it was the 8 bit max....I'd forgotten the shortcut in
dividing by 256 so I went
through all the trouble of getting a 24 bit by 16 bit division routine. So
does discarding the lowest byte
mean that if I have a 24 bit sample accumulator, all I do is take the upper
16 bits, ignore the lower 8 bits,
and call that my 16 bit divided answer?
2000\03\29@034918
by
Spehro Pefhany

At 03:26 AM 3/29/00 0500, you wrote:
>Once and for all, if I follow this method,
>should I get true RMS output by (I have said this so many times I'm starting
>to hate it)
>taking samples, subracting the 128 reference from samples greater than 128
>and subtracting the
>samples from 128 for those less than reference (rectify?), squaring the
>rectified sample, summing it
>with other samples thus far, dividing by total samples taken, then just
>square rooting and nothing further?
Yes. There is nothing wrong with your algorithm.
Have a look again at my analysis of the worstcase (0xFF and 0x00) inputs
which represent an output of 0x80, which you should display as 2.50 volts
(RMS).
It sounds like you have a problem somewhere. I'll bet it won't give
accurate results with DC inputs. 8( If it does, that may be a hint as
to where the problem is.
Good luck, and Best regards,
=======================================
Spehro Pefhany "it's the network..." "The Journey is the reward"
.....speffKILLspam@spam@interlog.com Info for manufacturers: http://www.trexon.com
Embedded software/hardware/analog Info for designers: http://www.speff.com
Contributions invited>The AVRgcc FAQ is at:
http://www.bluecollarlinux.com
=======================================
2000\03\29@042731
by
Lorick
> It sounds like you have a problem somewhere. I'll bet it won't give
> accurate results with DC inputs. 8( If it does, that may be a hint as
> to where the problem is.
That's another good question...how should an RMS algorithm handle dc inputs?
I've read in this list that the RMS of DC should be the DC value, but I am
confusing this with the fact that the RMS meter shows 0 on a dc
signal...both seem
right so I'm assuming it's a different theory for each reason.
2000\03\29@050109
by
Spehro Pefhany

At 04:27 AM 3/29/00 0500, you wrote:
>> It sounds like you have a problem somewhere. I'll bet it won't give
>> accurate results with DC inputs. 8( If it does, that may be a hint as
>> to where the problem is.
>That's another good question...how should an RMS algorithm handle dc inputs?
The RMS value of a DC voltage +V is the DC voltage. It has the same RMS
voltage as a square wave alternating between +V and V. If you think of
RMS voltage as DC voltage that would cause the equivalent heating in a
fixed resistor, it might be easier (or not?). Anyway, it's an easy
second data point for you.
>I've read in this list that the RMS of DC should be the DC value, but I am
>confusing this with the fact that the RMS meter shows 0 on a dc
>signal...both seem right so I'm assuming it's a different theory for each
>reason.
Probably, the RMS meter is ACcoupled. However, this is not very good
in some real cases, since it clearly will read incorrectly if there is an
AC voltage with a DC voltage superimposed. Real RMS voltmeters often do
this and also they may have accuracy problems with input waveforms that
have large crest factors. Yours cannot handle waveforms that go beyond
+/2.5V and will read low. So, a voltage that is a 10% duty cycle 5V
pulse (RMS value is 1.58V) will read as 0.79V (50% error).
Best regards,
=======================================
Spehro Pefhany "it's the network..." "The Journey is the reward"
speffKILLspaminterlog.com Info for manufacturers: http://www.trexon.com
Embedded software/hardware/analog Info for designers: http://www.speff.com
Contributions invited>The AVRgcc FAQ is at:
http://www.bluecollarlinux.com
=======================================
2000\03\29@052227
by
Brent Brown

> That's another good question...how should an RMS algorithm handle dc inputs?
> I've read in this list that the RMS of DC should be the DC value, but I am
> confusing this with the fact that the RMS meter shows 0 on a dc
> signal...both seem right so I'm assuming it's a different theory for
> each reason.
Some meters have two ranges: RMS and RMS + DC. The theory
says that RMS includes DC. In practice however there can be
reasons to want to exclude the DC component and measure it
seperately, thus the confusion.
Following is the description that best helps me remember what
RMS is: "the value of DC that would produce the same heating
effect" (given a resistive load)
Persist with the theory until it works like the scientists say it does
because they are often right. I haven't followed this thread for a
while, but are you able to rule out aliasing effects due to sample
rate?
Brent Brown
Electronic Design Solutions
16 English Street
Hamilton, New Zealand
Ph/fax: +64 7 849 0069
Mobile: 025 334 069
eMail: .....brent.brownKILLspam.....clear.net.nz
2000\03\29@060127
by
Lorick
> while, but are you able to rule out aliasing effects due to sample
> rate?
I've discounted the possibility but I guess there's always room for it. I'm
sampling a 330 Hz sine with a 4MHz crystal and I'm just blindly assuming I'm
way above 2.2x for Nyquist.
2000\03\29@060954
by
Michael RigbyJones
part 0 2357 bytes
<P><FONT COLOR="#0000FF" SIZE=2 FACE="Arial">The RMS meter is probably only suitable for measuring AC signals as it I suspect it will be AC coupled.</FONT>
</P>
<P><FONT COLOR="#0000FF" SIZE=2 FACE="Arial">Mike</FONT>
</P>
<UL>
<P><FONT SIZE=1 FACE="Arial">{Original Message removed}
2000\03\29@074750
by
hgraf
> That's another good question...how should an RMS algorithm handle
> dc inputs?
> I've read in this list that the RMS of DC should be the DC value,
> but I am
> confusing this with the fact that the RMS meter shows 0 on a dc
> signal...both seem
> right so I'm assuming it's a different theory for each reason.
It should be the DC value, however many RMS meters capacitivly couple the
input so DC shows 0V. TTYL
2000\03\29@132511
by
Harold M Hallikainen

On Wed, 29 Mar 2000 06:01:10 0500 Lorick <EraseMElorickspam_OUTTakeThisOuTAIR.ON.CA> writes:
> > while, but are you able to rule out aliasing effects due to sample
> > rate?
>
> I've discounted the possibility but I guess there's always room for
> it. I'm
> sampling a 330 Hz sine with a 4MHz crystal and I'm just blindly
> assuming I'm
> way above 2.2x for Nyquist.
As I mentioned before, I don't believe Nyquist is a concern unless you
are trying to reconstruct the waveform. A RANDOM undersampling of the
waveform should be fine. There is a concern over sample time, but I don't
think this is a concern at audio frequencies with the PIC A/D.
I believe that as long as the sample frequency is not a harmonic of the
signal being measured, you'll get a correct RMS with ANY sample rate. You
could even take 256 samples at one per second.
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.
2000\03\29@132515
by
Harold M Hallikainen

I don't think you should need any "magic number" in the RMS
calculations. The DC offset is obviously a problem. It can be subtracted
out before doing the square. If the square routine assumes positive
numbers, you can check for negative and two's complement before doing the
square, allowing you to stick with positive numbers.
I don't recall your exact application, but is the incoming waveform
approximately symmetrical? If so, you could just chop off the negative
half of the incoming waveform by NOT biasing the A/D input up to Vcc/2.
Put in a series current limit resistor and the input clamp diode will get
rid of the negative half of the waveform. Do the RMS calculation and
multiply the result by two (since you've thrown out the negative half of
the waveform). One way to do that would be to collect 512 samples but
divide by 256.
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.
2000\03\29@184927
by
Lorick
> I don't recall your exact application, but is the incoming
waveform
> approximately symmetrical?
It's exactly symmetrical. If all else fails I will try chopping the bottom
half and multiplying the result by 2.
I've read so many things that right now I can't even remember what steps to
take next before reading messages again.
I think I'm going to strip the program down yet gain and start building it
up from scratch just making a one channel RMS voltmeter with the bias line
removal and see if it works again....if not, take some measurements and post
the code and the results somewhere and reference the url here and see what's
wrong with it.
More... (looser matching)
 Last day of these posts
 In 2000
, 2001 only
 Today
 New search...