In measuring True RMS voltage and current, waveforms from nonlinear loads like rectifiers chop up the current and cause noise on the voltage. In me asuring with an A/D converter of a microcontroller, we need to figure out z ero crossing points in order to avoid truncation errors in the data to defi ne the start and stop of each cycle. That is challenging with a lot of har monics and noise that cause tons of false zero crossings.
Does anyone have any experience in doing something similar to this and how did you deal with the zero crossing noise?
Yes, harmonics make it harder to synchronize to some fundamental frequency.
No, you don't have to measure synchronously to some fundamental frequency.
No, looking for zerocrossing points is not the best way to synchronize to some fundamental frequency.
No, you don't have to synchronize at all. In fact, unless you know that you are looking at a signal that is dominated by a single tone at a constant frequency, you probably should not synchronize.
And your worst problems are going to be due to issues that you didn't even mention.
What exactly are you trying to measure? Power line voltage? Some other lowfrequency power? Audio? Anything at all?
As JL said, sampling, squaring, lowpass filtering, and squarerooting is a good general technique. The amount of lowpass filtering you need is usually disappointingly large  getting the RMS power of noise just takes a lot of time.
If you're making some sort of power line monitoring equipment, and if you don't mind if it doesn't work well when powered from gensets and 3rd world grids, then sample the noise over 100ms. That's exactly 5 cycles of 60Hz, and 6 cycles of 50 Hz. Better yet, give it ramps on each end so that it has zeros at 50 and 60Hz, but filters the edges better.
If it's for power line measurement and you want to accurately measure gen sets and stuff, and for some reason you don't like Mr. Larkin's suggestion of just lowpassing the squared wave, then get lots of samples per cycle (which you need to do anyway, to catch all the harmonics). Low pass or bandpass filter so that you capture everything up to about 65Hz; you should get a cleanenough sine wave that you can phase lock to the power line. Then phase lock using a multiplying phase detector (and if you don't know what a multiplying phase detector is, ASK). DON'T use the filtered version for your measurement  you're just using that for synchronization.
You didn't ask about how fast to sample. The answer is  fast. You need to sample fast enough so that the squared version of your waveform does not suffer from significant aliasing. That means that if your frequency of interest in the nonsquared signal is f_i, you need to sample fast enough to capture the signal at 2 * f_i that you'll be generating in the squaring operation. I'd probably use f_s = 5 * f_i as an absolute minimum sampling rate, and I wouldn't hesitate to go to ten times if my hardware would allow it.

Tim Wescott
Control system and signal processing consulting
s like rectifiers chop up the current and cause noise on the voltage. In measuring with an A/D converter of a microcontroller, we need to figure out zero crossing points in order to avoid truncation errors in the data to de fine the start and stop of each cycle. That is challenging with a lot of h armonics and noise that cause tons of false zero crossings. I am attemptin g to measure AC power line voltages and current.
Actually it specifically needs to work with gensets, and gensets that are b eing loaded by nonlinear loads like VFDs.

There may be other reasons where synchronization is needed, such as to sync hronize the transfer between two sources, but that appears to be a separate problem. I probably need to also compute phase angle at some point as a f uture addon so that needs to be considered.
The actual sampling rate does not have to be very high, if you can wait for the result for a few seconds.
The sampling pulse to the S&H circuit must be narrow (hundreds of microseconds) but the actual A/D sampling rate could be about the same (but not exactly the same) as the mains frequency. With a sampling pulse every 361 degrees of mains frequency, it would take about 7 seconds at 50 Hz and 6 seconds at 60 Hz to reconstruct a repetitive waveform with 1 degree resolution. Taking a few samples every mains cycle or reducing time resolution, would drop the averaging time considerably.
You've got a lot of good reasons to synchronize, then.
My kneejerk first candidate for synchronizing this would be to use a multiplying phase detector: generate a sine wave at the frequency that I think I'm looking at, and multiply that by the incoming signal. If the incoming signal is exactly 90 degrees off from my reference sine wave, then I have zero phase error. Then I'd steer the frequency of that generated sine wave using my measured phase error (in other words, I'd make a phase locked loop). The fact that it's a genset whose frequency may be varying complicates things, particularly if you want this to work with any genset, no matter how badly the frequency fluctuates.
I had said lowpass and then use a multiplying phase detector  I was wrong. It's all the fault of trying to be sensible before I've consumed anything with caffeine that day. At any rate, if you run the phase detection over one cycle of your reference sine wave that'll automatically act like the filtering you want.
Zero crossing after filtering may be adequate, particularly if the sampling rate is high, but there's an uncertainty about exactly where the zero crossing occurred unless you interpolate the zero crossing between input samples. With the multiplying phase detector approach, you'll always nail the phase dead on as long as the frequency isn't varying too much. (Paradoxically, the problem of the indeterminate zero crossing is actually improved if the source has some phase jitter  the jitter in the actual phase tends to randomize the quantization due to sampling, which makes it less of a problem).
Either of these techniques will probably work best if you're willing to vary the speed of your ADC sampling so that you always have an integer number of samples per cycle on the AC line. Varying your sampling rate like that is a pain in the behind, but it's often less so than trying to compensate for a whole measurement system that's centered around one cycle of some incoming AC.

Tim Wescott
Control system and signal processing consulting
As the OP, I realized that I should probably more clearly state my objectives so as to make it easier for you to focus your advice.
I am measuring 3Phase Volts and 3Phase Amps
It must yield a 'True RMS' measurement accurate to 1% of full scale for voltage (say 480V), and 2% of full scale current (5A).
The current sensing will use standard 5A secondary Current Transformers across a precision shunt resistor.
The processor available is a 32 bit ARM Cortex with builtin ADC (12Bit).
I expect substantial harmonics and possibly conducted/radiated noise from nonlinear loads such as VFDs. Measurement up to the 10th harmonic is desirable.
I don't need especially speed response of the Volts and Amps values, substantial delay is acceptable up to perhaps a second or two.
It must work with enginedriven generator outputs which may not be 'stiff' power supplies compared to the grid. However, usually the outputs will be reasonably well regulated with electronic governors and automatic voltage regulators.
Although its not part of my initial design, I do anticipate adding phase angle to complete KW and PowerFactor and to possibly trigger inphase synchronization of two AC sources.
Although there is a fairly high power CPU available, lots of other activities will be happening at the same time such as network communications, supervisory functions, time delay logic, processing of sensors, etc. This is happening within the framwork of a basic preemptive RTOS (FreeRTOS).
It is desirable to do as much of the RMS processing and filter in software so as to keep the hardware generic.
Since 6 pulse rectifiers (without smoothing) tend to have quite long conducting angles, the peak/average ratio is pretty moderate, compared to single phase rectifier with a large capacitor, thus less problems with the dynamic range.
The peak current handling (full scale) capacity needs to be many times the average ratings, if big single phase electronic loads without PFC are expected.
So at least 2030 sample points are needed. To get those points during a single 60 Hz cycle, a sampling rate above
2000 Hz would be sufficient.
If the generator frequency is sufficiently stable during that one second (mechanical inertia), you could spread out those 2030 sample points all over the 1 s period, thus less than one sample/cycle. Taking a few asynchronous samples every cycle and the measurement will also follow subsecond speed fluctuation. Some odd sampling frequency, such as 379 Hz should work with 50/60 Hz with some governor tolerances, without falling to sync.
While real power [kW] and power factor [0..1] make sense for any kind of waveform, the phase angle is relevant only for linear (RCL) loads, not for nonlinear loads, such as the VFD input rectifiers.
I think that you should forget the issue of trying to measure phase angles at the moment and study other methods to measure the RMS current. The real power can be obtained by multiplying the voltage and current samples obtained out and averaging the result.
Doing a three lamp phase synchronizer in software ?? I still do not think that you need to know the phase angle.
He needs to be above Nyquist _after_ squaring. That means he needs to be able to capture up to the 20th harmonic. So, greater than 40 samples per cycle. I'd probably sample at no less than 80 samples per cycle, and even then I'd test to make sure that it was enough sampling to insure accuracy.
So 4800Hz, at least. Fortunately, that's probably not much for his processor.
I'm just contrary today, but if processor speed is an issue, capture a vector of samples from one or two cycles of the input wave, then process at leisure.

Tim Wescott
Control system and signal processing consulting
I don't think that's the case, because otherwise Rayleigh's theorem wouldn't work for DFTs, and it does.
Squaring the samples does mean that the transform turns into its own autocorrelation, so that it's twice as wide and there is all sorts of aliasing happening.
The trick is that the average power measurement is taking place at the zero frequency sample, where (assuming the Nyquist criterion was satisfied at sampling time) the smearing doesn't quite reach.
You must be right, because I distinctly remember proving Parseval's (AKA Raylieig's) theorem, but I can't quite wrap my head around your answer just yet...

Tim Wescott
Control system and signal processing consulting
I am reviewing an existing software design from another engineer so I needed to spend some time trying to understand what is currently being done. This design works but has problems with current sensing not working or working inaccurately when presented with nonlinear VFD loads.
The basic sequence is as follows. Each RMS calculation is tored in a ring buffer in FIFO fashion:
Calculate average RMS current (Ph A, B, and C) Wait 200ms while doing other systems tasks
Calculate average RMS Voltage (Ph A, B, and C) Wait 200ms while doing other systems tasks
Calculate average RMS current (Ph A, B, and C) Wait 200ms while doing other systems tasks
Calculate average RMS Voltage (Ph A, B, and C) Wait 200ms while doing other systems tasks
Calculate average RMS current (Ph A, B, and C) Wait 200ms while doing other systems tasks
Calculate average RMS Voltage (Ph A, B, and C) Wait 200ms while doing other systems tasks
Calculate average RMS current (Ph A, B, and C) Wait 200ms while doing other systems tasks
Calculate average RMS Voltage (Ph A, B, and C) Wait 200ms while doing other systems tasks
Calculate average RMS current (Ph A, B, and C) Wait 200ms while doing other systems tasks
Calculate average RMS Voltage (Ph A, B, and C)
The averages of the above 5 calculations is taken and presented to the display screen and monitoring system.
Each RMS calculation consists of:
1) Each 1/2 waveform being sampled at 21us between each sample.
2) Slope detection and zero crossing detection is used to start and stop the sampling and to identify "tops" and "bottoms" of the waveforms.
3) The tops and bottoms are sampled in an alternating fashion for each RMS calculation above. Hence, we might sample a top and then approx
200ms later start sampling a bottom.
4) When two tops and two bottoms are sampled (equivalent to 2 cycles), then these are used to calculuate RMS and is fed to the ring buffer above.
5) The microcontroller has a hardware feature for detecting zero crossings which is used at the basis of the zero crossing detection using interrupts. We also do some additional checks to verify monotonicity on the result of the zero crossing to try to verify that its legitimate and not "noise".
6) There does not appear to be any low pass filtering in the digital domain. There is some hardware filtering; a second order filter that starts to roll off quickly after the 10th harmonic (assuming 60hz fundamental).
I know the devil is in the details and I've probably left out some important ones that I have yet to discover but I am wondering if anyone has any general comments or critique on this overall approach?
It strikes me that:
a) The sampling rate seems unnecessarily high. Its sort of like we can vastly oversampling with LONG gaps between sampling runs. b) Splitting the waveforms into tops and bottoms seems complicated and subject to error in the presence of harmonics. i.e. how do we know we are on a positive slope of the fundamental and not the positive slope of a double dip bump due to harmonics? c) There could be things changing in between the long gaps of sampling runs. For example the frequency shifts between the time we sampled the top and later sampled a bottom. d) the zero crossing detection in the microcontroller is a bit of a black box for me. Its not clear to me these are designed to work on noisy waveforms.
The best way to meter a variable frequency is to pass the sample data (E*I products for power, E^2 or I^2 for RMS) through a lowpass filter, to get a tapered windowing function. Sample rate should be fairly high to avoid wobbles in the filtered data, but the amplitude of higher harmonics is usually low, and (if the line voltage is basically sinusoidal) there's no power in the current harmonics.
This is like the recent discussion about measuring the realtime efficiency of a switching regulator. A rectangular averaging window makes ugly power values unless the window is synchronized to the waveform, but a smarter windowing function (easily implemented by an IIR filter) smooths things much better.
A high sample rate avoids folding harmonics back into the filter passband, which is a Nyquist sort of statement. There might be an argument for nonperiodic sampling.
AC line measurements are usually so messy that a little measurementtomeasurement noise is expected, so if the sample rate and filtering aren't extreme, nobody will notice. Where you get verified is on energy measurement, and that's averaged over minutes.

John Larkin Highland Technology Inc
www.highlandtechnology.com jlarkin at highlandtechnology dot com
ElectronDepot website is not affiliated with any of the manufacturers or service providers discussed here.
All logos and trade names are the property of their respective owners.