True RMS conversion problem - Page 1
Forum section: electronics / projects

author: fcb
author_rank: Super Contributor
timestamp: February 12, 2017, 07:02:09 pm
content: 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.604256-511 samples: 700.058512-767 samples: 718.744I reckon I should get 707.106 for all???PDF of the spreadsheet attached.

author: 2N3055
author_rank: Super Contributor
timestamp: February 12, 2017, 07:16:50 pm
content: For periodic signals, you should detect zero cross, and use only full signal periods. It will give consistent measurements..

author: fcb
author_rank: Super Contributor
timestamp: February 12, 2017, 07:23:54 pm
content: 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?

author: Kleinstein
author_rank: Super Contributor
timestamp: February 12, 2017, 07:54:16 pm
content: 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.

author: capsicum
author_rank: Contributor
timestamp: February 12, 2017, 08:17:53 pm
content: 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.

author: KE5FX
author_rank: Super Contributor
timestamp: February 12, 2017, 09:04:18 pm
content: 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.

author: fcb
author_rank: Super Contributor
timestamp: February 12, 2017, 09:10:46 pm
content: 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.

author: bktemp
author_rank: Super Contributor
timestamp: February 12, 2017, 09:31:27 pm
content: Quote from: KE5FX on February 12, 2017, 09:04:18 pmUsing 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.htmlIt does not give the highest precision because of the resampling, but it keeps everything simple.

author: 2N3055
author_rank: Super Contributor
timestamp: February 12, 2017, 09:36:26 pm
content: Take a look at Swerleins algorithm....Lots to learn from that document...

author: Benta
author_rank: Super Contributor
timestamp: February 12, 2017, 10:44:58 pm
content: 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..

author: snarkysparky
author_rank: Frequent Contributor
timestamp: February 13, 2017, 12:32:09 pm
content: each samplex = adc_reading();y = x^2yav = yav + K*(y-yav)when you want the resultYrms = 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.

author: fcb
author_rank: Super Contributor
timestamp: February 15, 2017, 09:13:10 pm
content: 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.

author: mikerj
author_rank: Super Contributor
timestamp: February 15, 2017, 10:05:45 pm
content: 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.

author: Kalvin
author_rank: Super Contributor
timestamp: February 15, 2017, 10:33:28 pm
content: 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 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.

author: macboy
author_rank: Super Contributor
timestamp: February 17, 2017, 02:55:48 pm
content: Quote from: Kleinstein on February 12, 2017, 07:54:16 pmThis is definitely best.Quote from: Kleinstein on February 12, 2017, 07:54:16 pmThis is called windowing and it can be quite effective, but it adds substantial complexity to the calculations.Quote from: Kleinstein on February 12, 2017, 07:54:16 pmNever 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 }\$

author: Kalvin
author_rank: Super Contributor
timestamp: February 17, 2017, 04:02:11 pm
content: 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 fromhttp://www.analog.com/media/en/technical-documentation/data-sheets/AD736.pdfFor 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.

author: Kalvin
author_rank: Super Contributor
timestamp: February 18, 2017, 01:48:11 pm
content: Here's a simple code for computing the RMS of the  signal using the exponential averaging.Code:#!/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 mathtav=200 # Exponential averaging time constant .Fsin = 50.0 # Sine signal frequency .Fs = 3906.125 # Sampling rate .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.for n in range(2048):x = math.sin(n * omega2pi) # Compute x.rms_sum = rms_sum + tau*(x**2 - rms_sum) # Update the RMS accumulator.rms = math.sqrt(rms_sum) # Compute the RMS, rms.err = 100.0 * (rms - rms_sin) / rms_sin # Compute error %.print n, x, rms, err