Measure Harmonics using Emonlib

Can anyone suggest code using Emonlib.cpp that can calculate the number of zero crossings within one fundamental wavelength, and enable that variable to be used for display?

The reasoning is if the fundamental uses 2 zero crosses/wavelength then the 2nd,3rd etc harmonics will each have their own signature of zero crossings.

That is, 2nd harmonic has 4 zero crossings, 3rd harmonic has 6, 4th harmonic has 8 and so on.

This should give a rough idea how clean the sinewave is.

I am only looking at the Voltage for now




emjay's picture

Re: Measure Harmonics using Emonlib


Your logic is true only when the harmonics are filtered out and looked at separately. The observed waveform is the algebraic sum of the fundamental and those harmonics - since the fundamental dominates for realistic waveforms, the zero crossings correspond only to the fundamental.

The discrete Fourier transform can do what you want.  Free running the ADC and collecting a buffer full of samples spanning an integral number of cycles would be representative, assuming that the harmonic distortion is changing relatively slowly.  This would require considerable RAM and processing power though.

Out of interest, why would this measurement be of interest?  From the grid side, it is not controllable and if you have a local generating capability, it is controllable only by the choice of inverter.




dBC's picture

Re: Measure Harmonics using Emonlib

It looks like somebody had a crack at it a couple of years back:

I'm not sure if that code ever got published.

To help visualise emjay's response, below are some pictures of my most distorted circuit (the lights circuit with a bunch of compact fluros on).  This is current (red trace) not voltage, and it's unlikely your voltage signal would ever get anywhere near this distorted.  But even with a signal that distorted, there's not a whole lot you can deduce from the zero-crossings... although it does manage 3 zero-crossings per half-cycle (just).


emjay's picture

Re: Measure Harmonics using Emonlib


A truly ugly waveform !  I doubt in aggregate that meets the IEC 61000-2-2 family of standards.  The LED equivalent mains drivers often seem better, possibly since the designs are more recent (with an eye on compliance), though there are equally surprising waveforms from some "China Specials".  Note how the current has that low level anti-phase segment, so you are getting some of the power back you put in  ;-)

dBC's picture

Re: Measure Harmonics using Emonlib

Yes, as my ageing compact fluros die, I'm replacing them with LEDs.  The pics below are from the same circuit with only the LED lights on, and you can see it's much nicer (power factor up from 0.59 to 0.87).

Robert Wall's picture

Re: Measure Harmonics using Emonlib

I've used a sound card and a spreadsheet to analyse harmonics, but definitely not in real time and not with an Atmel 328P. EmonLib is a non-starter.

dBC: "I'm not sure if that code ever got published." I don't think it did. I cannot recollect seeing it.

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.