** Dangerous assertion mode which may result in egg on face on: The answer to your problem is that you don't fully understand RMS voltage. Your code may or may not be working correctly! ** Dangerous assertion mode which may result in egg on face off (whew!!!) Because: Imagine you are applying the following voltages to a 1 ohm resistor. Power dissipation will be Vrms^2/R = Vrms^2/1 = Vrms^2 1. Apply:1 volt DC Power = 1^2 = 1 watt 2. Apply 2 Vdc Power = 2^2 = 4 watt you get the idea. 3. Now apply a 1 volt peak to peak sine wave CENTRED ON GROUND. ie peak positive level is +1v relative to ground and peak negative value of -1 volt relative to ground. We would call this a 1 volt sine wave. If you apply it to an AC meter it will measure 1 VAC The MEAN voltage is 0v due to equal negative and positive excursions. You KNOW that this has an RMS value of 0.7071 volt. and if you manually calculate the RMS voltage you will indeed get 0.7071 volt rms. If you apply this voltage to a resistor with 1 end connected to ground and the other end being "waved" by +/- 1 volt AC then you will get 0.5 watts of heating which is what you expect as 0.7071^2 = 0.5. OK so far. 4. NOW apply an AC signal which is still 2vp-p BUT the negative limit is now 0v and the positive peak is +2v. To do this you have added a "pedestal" of +1 volt. The MEAN voltage has risen to 1 volt (draw a picture). The effective heating power has changed due to this pedestal. When the sine wave is at its peak excursion the voltage is now 2v and the peak instantaneous power dissipation is 4 watts (not 1 watt as before). Effectively you are applying a separate 1 volt DC signal plus a 1v AC signal. The effective power dissipation from this would be 1 watt from the DC and 0.5 watt from the AC = 1.5 watt total Lets ee what figures we get from an RMS calculation. The offset voltage is V= sin(theta)+1 Do an RMS calculation over N samples (I used 256 in an Excel spreadsheet). Sum squares Divide by 256 Take square root Vrms = 1.224 volts. Looks very strange, doesn't it Not a factor of 1.414 or .7071 or whatever. BUT Square it!!! 1.224^2 = 1.498 = 1.5 watt allowing for truncation errors Which IS the predicted POWER in the 1 ohm resistor. What you are doing by using a zero references sine wave is adding a DC level The reason you get the "correct" value with an AC meter is that it is AC coupled AND is only measuring the AC component. Lets see if we can convert to the value you really want. Vrms actual = (Vrms of AC component) + DC offset. For an X volt AC signal this is X/1.414 ac + Xdc Power = (X/1,414)^2 + X^2 = X^2/2 + X^2 = 1.5 X^2 For a zero referenced Y volt sine wave (no DC component) Vrms to cause this would be (Y/1.414)^2 = 1.5X^2 Y^2 = 3 .X^2 Y = 1.732 X Disappointingly, this is NOT the factor of 1.53 that you report (you Now I'm *fairly* sure that a little more beating would produce the same result as you get (assuming your code is OK) but I'm also *fairly* sure that the above pretty fairly reflects what's going on. CERTAINLY (he said boldly) adding the DC pedestal must be allowed for. THE EASY WAY ******************* Wot, they cried, you mean we didn't have to go through all this !!!!!!!! Nope :-) Just subtract the pedestal. But what Is the meaning of life, sorry, the pedestal, they all said. "Believe .." / whoops / the pedestal is the MEAN value SO Something like SUM=0 SUMSQ = 0 For N samples SUM = SUM + sample SUMSQ = SUMSQ + sample^2 Next SUMSQ = SUMSQ - SUM SUMSQ/N SUMSQ = Sqrt(SUMSQ) Check my logic - seems OK - done on fly after looooong day. THE EVEN EASIER ANALOG WAY :-) ******************************************** AC couple sinewave Reference AC signal to some known +ve value Xoffs Take Xoffs away from each sample before squaring. Bingo. An accurate knowledge of the value of Xoffs is required. Really hope this helps (or I've wasted everyone's time :-)) regards. Russell McMahon _____________________________ - www.easttimor.com Updated regularly: 100,000 refugees STILL in West Timor face starvation! - 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 :-)) -----Original Message----- From: Lorick To: PICLIST@MITVMA.MIT.EDU Date: Wednesday, 29 March 2000 18:48 Subject: RMS oddity? >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 auto-code-generator 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 mid-way 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 www.piclist.com/cgi-bin/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 >