Understanding EmonLib Power Factor (phase difference) calculation

Hello. Good day.

Before using EmonLib, I used to write the code to Sample a Voltage cycle, Sample a Current cycle, find the peak for both and measure the time difference for both, so I can then, calculate the power factor to then calculate real power.
I read the EmonLib code few times and I’m not able to completely understand how the Power Factor is calculated. I don’t see anywhere where the SIN or phase is compared.

I read the page below but still not making much sense.

I understand that the RMS Current and Voltage are calculated the squaring each sample and then taking the square root of the averaged values.

On the Emonlib.cpp I can find this block:

  //Calculation power values
  realPower = V_RATIO * I_RATIO * sumP / numberOfSamples;
  apparentPower = Vrms * Irms;
  powerFactor=realPower / apparentPower;

And SumP is calculated on the loop at this part:

    // F) Instantaneous power calc
    instP = phaseShiftedV * filteredI;          //Instantaneous Power
    sumP +=instP;                               //Sum

I’ll be glad if someone can detail how the offsetV, filteredV, offsetI, filteredI, phaseShiftedV, PHASECAL and lastFilteredV are used in the calculation and how to determine the Power Factor.

Thanks a lot.

1 Like

What you appear to have measured is the phase angle between voltage and current. Power Factor = cos (φ) is a simplification. It is only true for pure sine waves, such as the ones in text books; and these are very rarely found in the real world.

The formal and correct definition of power factor is the ratio of real power to apparent power, and that is the sum that emonLib does. It calculates real power as the average of the instantaneous powers of the many samples, and apparent power as the product of the root mean square averages of voltage and current.

The phase and timing correction has nothing directly to do with power factor. The correction is necessary because transformers have phase errors, and there is a time difference between the voltage and current samples. The algorithm effectively shifts the voltage wave to correct those errors. Uncorrected, a phase/timing error will give an incorrect value of power factor.

You’ll need to read the code and figure out for yourself what those variables are. If you look at the mathematical operations that create them, it should become clear. You could also look at the other background articles in the Learn section, that describe how the signals from the sensors are conditioned to make them suitable as inputs to the ADC, and are then processed.

[By the way, it is not helpful to give a silly answer for your Country. Because electrical systems differ from country to country, we can help you better when we know what your power system is likely to be.]

Hello Robert.

Thanks for your reply and thanks for the explanation.

If I understood you correctly, the Real power calculated by the EmonLIb should be accurate for any type of waveform, as it integrates the values of each sample. OffsetV and OffsetI are the sample values, centered on 0 (instead of ADC middle value), removing the positive offset. PHASECAL variable is used to “compensate” for transformer and sampling time difference between V and I. I’ll study further to understand better how the voltage wave is shifted to correct the phase errors.

In my scenario, Main voltage is typical 127V (phase + neutral) with 60Hz.

Thanks a lot.

Yes, that is correct - up to a point. Very steep edges (= high order harmonics) will not be measured correctly, but it is antisocial, and in many countries illegal, to generate high levels of high order harmonics, so the error will be small (even if they get through the current transformer).
And, irrespective of the rms value, the current peak must remain within the measuring range of the input.