# 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.
https://learn.openenergymonitor.org/electricity-monitoring/ctac/explanation-of-the-phase-correction-algorithm

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.