Author Topic: True RMS conversion problem  (Read 6712 times)

0 Members and 1 Guest are viewing this topic.

Offline fcbTopic starter

  • Super Contributor
  • ***
  • Posts: 2174
  • Country: gb
  • Test instrument designer/G1YWC
    • Electron Plus
True RMS conversion problem
« on: February 12, 2017, 07:02:09 pm »
I'm sure I'm being thick here - just can't quite see where I'm going wrong.

I'm building a sampling system that has an input of 50Hz (sinewave) and is sampling at 3906.125Hz, each sample is squared and then added to an accumulator.  After 256 samples (3 and a bit cycles of the sinewave) I take the squareroot of the average. Repeat etc...

I've built this in a spreadsheet (to prove the maths before I transfer it to my prototype) and get the following results (with an input peak magnitude of +/-1000):
0-255 samples: 708.604
256-511 samples: 700.058
512-767 samples: 718.744

I reckon I should get 707.106 for all???
PDF of the spreadsheet attached.
https://electron.plus Power Analysers, VI Signature Testers, Voltage References, Picoammeters, Curve Tracers.
 

Offline 2N3055

  • Super Contributor
  • ***
  • Posts: 8829
  • Country: hr
Re: True RMS conversion problem
« Reply #1 on: February 12, 2017, 07:16:50 pm »
For periodic signals, you should detect zero cross, and use only full signal periods.  It will give consistent measurements..
"Just hard work is not enough - it must be applied sensibly."
Dr. Richard W. Hamming
 

Offline fcbTopic starter

  • Super Contributor
  • ***
  • Posts: 2174
  • Country: gb
  • Test instrument designer/G1YWC
    • Electron Plus
Re: True RMS conversion problem
« Reply #2 on: February 12, 2017, 07:23:54 pm »
Thanks 2N3055,

So I'm not missing something then.

The solution sounds like a nightmare - I'd have to find a reliable way of detecting zero crossing and then adjusting the window size to suit.

How does my 34465A work - I didn't think it had a true rms converter IC inside?  There's no sign that it 'syncs' up to the incoming waveform - what about energy meters?

https://electron.plus Power Analysers, VI Signature Testers, Voltage References, Picoammeters, Curve Tracers.
 

Offline Kleinstein

  • Super Contributor
  • ***
  • Posts: 17280
  • Country: de
Re: True RMS conversion problem
« Reply #3 on: February 12, 2017, 07:54:16 pm »
If the window is longer, it gets less critical to have exact multiples of a full period. One can also ease it a little at the end by giving lower weight to some of the first and last samples.

In real life it also helps to correct the DC offset of the ADC. This can still be done in real time, no need to save all the data, just an additional calculation of the average too. At the low end there is also the systems own noise that could be subtracted from the power data.

The DMMs usually check for the zero crossings and use full period lengths - it could be in sync with the power line though. It is a little more effort but gets more accurate results. For the energy meters, they might just use a very long window.
 

Offline capsicum

  • Contributor
  • Posts: 34
  • Country: us
Re: True RMS conversion problem
« Reply #4 on: February 12, 2017, 08:17:53 pm »
For steady frequencies, you don't need a zero cross, you only need samples that are multiples of 0.5 cycle periods. At 60hz a sample would be any multiple of 1/120 second. It does not matter where in the cycle the sample starts.

[edited to add] I should point out the above only applies to balanced AC, if there is a DC bias or some very odd asymmetric waveform then it will require full 360 degree cycles, not 180 degree, sample units.
« Last Edit: February 13, 2017, 11:24:21 am by capsicum »
 

Offline KE5FX

  • Super Contributor
  • ***
  • Posts: 2674
  • Country: us
    • KE5FX.COM
Re: True RMS conversion problem
« Reply #5 on: February 12, 2017, 09:04:18 pm »
Not a guru at this type of measurement, but I'd be tempted to do it in the frequency domain with a (suitably windowed) FFT. 

The rule about taking measurements at zero crossings really applies to basic waveform shapes only, like sinusoids and square waves.  It minimizes uncertainty because that's where the slew rate is highest.  But it's not hard to imagine a waveform whose dominant frequency is nowhere near the zero-crossing rate, and/or whose fastest rate of change is nowhere near the zero crossing.
« Last Edit: February 12, 2017, 09:05:54 pm by KE5FX »
 

Offline fcbTopic starter

  • Super Contributor
  • ***
  • Posts: 2174
  • Country: gb
  • Test instrument designer/G1YWC
    • Electron Plus
Re: True RMS conversion problem
« Reply #6 on: February 12, 2017, 09:10:46 pm »
Thanks all,

I'm probably going to extend the sample window period as a short-term measure (probably to 4096 samples), in the longer term I think some sort of zero-crossing variable sample length system will be called for - my superloop runs at 3906Hz, so actively changing the sample frequency is not going to happen.


https://electron.plus Power Analysers, VI Signature Testers, Voltage References, Picoammeters, Curve Tracers.
 

Offline bktemp

  • Super Contributor
  • ***
  • Posts: 1616
  • Country: de
Re: True RMS conversion problem
« Reply #7 on: February 12, 2017, 09:31:27 pm »
Not a guru at this type of measurement, but I'd be tempted to do it in the frequency domain with a (suitably windowed) FFT. 
Using a FFT does not solve the actual problem, because it has the same problems (using a finite number of samples affects the results, that's why you need the windowing function). You can skip the FFT after applying the window function and doing the RMS calculation directly using the windowed samples.
Cheap DMMs use an analogue lowpass filter after the true-rms -> DC converter. You could do the same, using a digital filter for filtering the squared data. Some energy meters work that way (multiplying the voltage and current samples, then lowpass filtering the data for calculating the avarage power or simply integrating the unfiltered data for energy).
Power analysers often use a PLL for generating the ADC sampling clock locked to input voltage frequency.

A simple example how the calculations can be done is this project:
http://elm-chan.org/works/heco/report_e.html
It does not give the highest precision because of the resampling, but it keeps everything simple.
 

Offline 2N3055

  • Super Contributor
  • ***
  • Posts: 8829
  • Country: hr
Re: True RMS conversion problem
« Reply #8 on: February 12, 2017, 09:36:26 pm »
Take a look at Swerleins algorithm....

Lots to learn from that document...
"Just hard work is not enough - it must be applied sensibly."
Dr. Richard W. Hamming
 
The following users thanked this post: KE5FX

Offline Benta

  • Super Contributor
  • ***
  • Posts: 7185
  • Country: de
Re: True RMS conversion problem
« Reply #9 on: February 12, 2017, 10:44:58 pm »
Looking at your spreadsheet-pdf, it's obvious what's happening.
Your are squaring and summing your samples absolutely correctly, but then you're extracting the square root at an arbitrary point in time. What you are simulating is a full cycle plus "something", depending on your sample clock.

.
 

Offline snarkysparky

  • Frequent Contributor
  • **
  • Posts: 441
  • Country: us
Re: True RMS conversion problem
« Reply #10 on: February 13, 2017, 12:32:09 pm »
each sample
   x =  adc_reading();
   y = x^2
   yav = yav + K*(y-yav)


when you want the result
   Yrms =  sqrt(yav)


K between zero and one.  With K toward zero the averaging is slower and the result more steady.  It's a low pass filter on the result of the squaring.
 

Offline fcbTopic starter

  • Super Contributor
  • ***
  • Posts: 2174
  • Country: gb
  • Test instrument designer/G1YWC
    • Electron Plus
Re: True RMS conversion problem
« Reply #11 on: February 15, 2017, 09:13:10 pm »
As an update.

If we do a basic SMR (square-mean-root) on 4096 samples (a little over a second) the worst-case accuracy that can be achieved is <0.07%, which exceeds the 0.5% we're aiming for.

This gets us out of a hole on the current project and buys some time to investigate more accurate ways of doing things - thank you all.
https://electron.plus Power Analysers, VI Signature Testers, Voltage References, Picoammeters, Curve Tracers.
 

Offline 2N3055

  • Super Contributor
  • ***
  • Posts: 8829
  • Country: hr
Re: True RMS conversion problem
« Reply #12 on: February 15, 2017, 09:49:44 pm »
 :-+
"Just hard work is not enough - it must be applied sensibly."
Dr. Richard W. Hamming
 

Offline mikerj

  • Super Contributor
  • ***
  • Posts: 3671
  • Country: gb
Re: True RMS conversion problem
« Reply #13 on: February 15, 2017, 10:05:45 pm »
You probably don't need quite that many samples.  805 samples at 3096.125Hz is almost exactly 13 cycles of 50Hz, the error is about 0.012% of a 50Hz period.
 

Offline Kalvin

  • Super Contributor
  • ***
  • Posts: 2178
  • Country: fi
  • Embedded SW/HW.
Re: True RMS conversion problem
« Reply #14 on: February 15, 2017, 10:33:28 pm »
As snarkysparky wrote above, the exponential filtering is quite handy, and it is used in the audio signal processing. See the block diagram below for the implementation (From the book: "Digital Audio Signal Processing", 2nd. ed. Udo Zölzer). Just take the sqrt from the x[n] in order to get the actual RMS.

Ps. You need to use double precision for the accumulator because squaring will double the bits. For example if your data is 16 bits, squaring will produce 32 bits, so your accumulator needs to be at least 32 bits.

« Last Edit: February 15, 2017, 10:56:57 pm by Kalvin »
 

Offline macboy

  • Super Contributor
  • ***
  • Posts: 2411
  • Country: ca
Re: True RMS conversion problem
« Reply #15 on: February 17, 2017, 02:55:48 pm »
If the window is longer, it gets less critical to have exact multiples of a full period...
This is definitely best.
...One can also ease it a little at the end by giving lower weight to some of the first and last samples.
This is called windowing and it can be quite effective, but it adds substantial complexity to the calculations.

In real life it also helps to correct the DC offset of the ADC. This can still be done in real time, no need to save all the data, just an additional calculation of the average too. At the low end there is also the systems own noise that could be subtracted from the power data.
Never simply subtract the noise from the reading. You can subtract as root sum of squares, since the reading will the be square root of the sum of the squares of the input signal and of the noise. If you do the math you will see that after the reading is >10x the noise floor, the noise doesn't contribute to the reading any longer (this is very unlike a DC offset, which always contributes to the reading).
So
\$V_{RMS_{signal}} = \sqrt{V_{RMS_{signal+noise}}^2 - V_{RMS_{noise}}^2 }\$
« Last Edit: February 17, 2017, 02:59:41 pm by macboy »
 

Offline Kalvin

  • Super Contributor
  • ***
  • Posts: 2178
  • Country: fi
  • Embedded SW/HW.
Re: True RMS conversion problem
« Reply #16 on: February 17, 2017, 04:02:11 pm »
If you cannot make sure that the sampling window is exactly one period of the waveform to be measured, you need to increase the sampling window ie. increase the integration time. For example, the analog RMS-to-DC converters have a minimum integration time around 7 times the lowest frequency to be measured in order to get 1% accuracy.

See Table 6, page 15 from
http://www.analog.com/media/en/technical-documentation/data-sheets/AD736.pdf

For example, the integration time needs to be 360 ms in order to be able to measure the RMS-value of the 20 Hz AC-signal. Thus, for the digital RMS computation for the 50 Hz signal and 3906.125 Hz sample rate, you will need to sample at least 7 * 3906.125 / 50 samples = 546 samples in order to achieve ~ 1% accuracy.
« Last Edit: February 17, 2017, 04:20:06 pm by Kalvin »
 

Offline Kalvin

  • Super Contributor
  • ***
  • Posts: 2178
  • Country: fi
  • Embedded SW/HW.
Re: True RMS conversion problem
« Reply #17 on: February 18, 2017, 01:48:11 pm »
Here's a simple code for computing the RMS of the [sine] signal using the exponential averaging.

Code: [Select]
#!/usr/bin/python
#
# Calculate RMS of the sine signal using exponential averaging.
# Varying the averaging time constant tav, one can adjust how
# fast the RMS will converge to the final value. Faster converge
# will increase the ripple of the computed RMS.
# Author: Kalvin, eevblog member.
#
import math
tav=200         # Exponential averaging time constant [samples].
Fsin = 50.0     # Sine signal frequency [Hz].
Fs = 3906.125   # Sampling rate [Hz].
tau = 1.0/tav   # Exponential averaging time constant.
omega = Fsin/Fs # Normalized sine frequency.
omega2pi = omega * 2.0 * math.pi # Radians / sample.
rms_sin = math.sqrt(2.0) / 2.0   # RMS of the sine wave, 0.7071...
rms_sum = 0     # RMS accumulator, y[n].
for n in range(2048):
    x = math.sin(n * omega2pi)               # Compute x[n].
    rms_sum = rms_sum + tau*(x**2 - rms_sum) # Update the RMS accumulator.
    rms = math.sqrt(rms_sum)                 # Compute the RMS, rms[n].
    err = 100.0 * (rms - rms_sin) / rms_sin  # Compute error %.
    print n, x, rms, err
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf

 

- - -
-->