12 bit resolution low pass filter

Hi there, dumb questions from a newbie.

  1. 12 bit resolution
    In EmonLib.cpp I see the following code (lines 111, 113):

Blockquote
line 111: offsetV = offsetV + ((sampleV-offsetV)/1024);
line 113: offsetI = offsetI + ((sampleI-offsetI)/1024);
line 193: offsetV = offsetV + ((sampleV-offsetV)/1024);

The above code applies a “low pass filter” (I’m not pretending to know what this is).

I’m a bit suspicious for the 1024 value: should this be 4096 for a 12 bit resolution ADC? Many places in the code use ADC_COUNTS which translates to ADC_BITS<<1 (power of 2), for 12-bit ADC this results to 4096. The 1024 could be just a “sliding” window in this filter so it’s just coincidence, but i’m just curious.

  1. readVCC
    some boards (ESP8266) has a readVCC() function that returns the power: why dont we use that in readVCC() function? ESP32 does not have such a function, but there is a rom_phy_get_vdd33() , there are most likely other ways as well

Like I said, more questions out of ignorance.

Google is your friend here.

No, it’s nothing to do with the ADC resolution, it’s the filter time constant. It just so happened that a value round about that number seemed good enough, so one that could be done with a bit shift seemed a good idea at the time.

However, with experience of doing it a different way, that filter doesn’t seem to be the best. The way it’s done in emonLibCM seems to offer a worthwhile improvement in performance.

Initially, in early versions of emonLib, the offset arising from the input bias voltage was removed by a high pass filter.
This, while effective, could not be satisfactorily initialised, with the consequence that a large spike would appear in the output as the filter settled, requiring the output to be inhibited for the first few sampling periods. This filter was replaced by straight arithmetic subtraction of the offset, the actual offset value being obtained by a low pass filter, which could be initialised to the expected value. However, this still allowed a small degree of ripple to enter the readings.
However, it is not necessary to remove the offset immediately. If it can be assumed that the offset remains stable over the sampling period, it can be removed after sampling is complete. To do this in a practical manner, the nominal offset is removed by subtracting half the ADC count (simply to reduce the size of the numbers) and the average input value is obtained by totalling the samples and dividing by the sample count. The rms of the combined signal plus the residual offset is obtained in the usual way, by multiplying each sample by itself and accumulating the total. The true rms average is then obtained from

[rms of signal+offset]= √ (signal2 + offset2 ).

This calculation is used for voltage and current. For the real power calculation, it is only necessary to subtract the product of voltage offset and current offset (the ‘offset power’) from the average power (the average of the instantaneous powers).

And neither does the Atmel 328P - which is why it’s done the way it is, for battery power anyway. The perceived problem with the '328P is the uncertainty in the initial value of the internal reference, which is thought to cast doubt on the long-term accuracy, so the nominal value of the supply regulator is used for the emonTx and emonPi.