## Convolution, anyone?
Jun 22, 2016

Right now, the JeeLabs Energy Monitor only tracks and reports three mains pulse counters here at JeeLabs. The smart meter’s P1 serial data source will be added soon, but there have been issues with reading it out with only a 3.3V power source - some more experimentation is needed to sort this out.

The next task will be to read out 3 current transformers using the STM32F103’s 12-bit ADC, as well as the AC mains signal, to be able to perform lots of multiplication w/ sums and estimate the real and reactive power consumption for each channel.

As described recently, AC mains can be read via the AC power supply transformer, using only the negative-going cycles.

This means that we must *fill in* the missing half of the input waveform, and
this in turn requires detecting where the half wave gets cut off during
rectification.

One way to calculate this is would be with convolution, a fascinating Digital Signal Processing technique related to (auto-) correlation and the Fourier Transform.

I’m still in learning mode and trying to wrap my head around all this stuff, but one way of looking at convolution, is that it takes a (long/continuous) signal and compares it to a “shorter” signal while shifting that signal along on the time line. The resulting stream of values will then correspond to the how “similar” the two are.

If we take the 7-point sequence **(-1 -1 -1 -1 +1 +1 +1)** and apply it to our
rectified wave, we get a nice peak where the waveform is flattening out. Here as
zoomed-in detail:

No peak on the negative side of the wave:

And here’s an *artist’s impression* of what we’re after:

The solid blue line is the input wave, the solid red line was manually added to turn it all into a full waveform: it’s a flipped-and-shifted version of the blue line. It all relies on identifying the flat segments so that we can switch over cleanly and remove them.

That’s where convolution could be used: the dotted lines are the convolution with that same 7-point signal, one for detecting the start of the flat segment, another (reversed) sequence for detecting the segment’s end. As you can see, both dotted lines will peak right next to these transitions, which can then be identified algorithmically.

This is only a very first (but important) step in the whole processing task. We’ll also need to measure the (quasi-fixed) phase shifts present in each of the current transformers as well as the AC power transformer and correct for each of them (by shifting the acquired data around a bit).

Note that this process will also let us determine all the 50 Hz voltage waveform zero crossings.

PS. Here is a small 20 mV peak-to-peak 50 Hz sinewave, as acquired by the ADC:

As you can see there’s a fair bit of noise, but keep in mind that this is a very small signal. For larger currents, signal levels up to 3 Vpp can be measured, so this is less than 1% of full-scale.

There’s probably no need to clean this up: the noise should cancel out in the final multiply-accumulate step, i.e. when V (volt) times I (amp) produces W (true power).

Please keep in mind that a lot of what I’ve described above is uncharted
territory for me - *I’m still reading up and learning…*

For comments, visit the forum.