I am building a home power meter to use in my house. I sent my board to be manufactured that's why at the moment I am simulating an AC power line and applying an FFT in order to calculate all the parameters.
The parameters I need to calculate are voltage and currents (RMS), apparent power($V_{rms} . I_{rms}$), active power($\sum_0^N V_{real} . I_{real} + V_{imag} . I_{imag}$), power factor ($\frac{ActivePower}{ApparentPower} = cos \theta$), where $\theta$ is the angle between voltage and current and total harmonic distortion (maybe even the rms values of the first 10 harmonics).
In order to measure the current I am using a TC which inserts a phase lag to the current in respect to the voltage, which at high power factors is not a problem, but the lower the power factor gets the error in calculations to the real values is pretty high.
Then I remembered from my studying days. The FFT has a shift theorem , which means that I can add a phase lag to my sistem by multiplying it by $e^{(-\Delta j)}$, which should theoretically fix the phase lag inserted by the current transformer.
The phase theorem states that the DFT of a shifted signal is the same as the DFT of the original signal multiplied by a delay given by $e^{-jw_k\Delta}$: $$ DFT_k[SHIFT_{\Delta}(x)] = e^{-jw_k\Delta}X(k) $$
The thing is that I can't reproduce this in my code. I use: $\Delta = - 2 . \pi . j . N$ ; where N is the number of samples but it ends up adding more phase lag, I have even tried multiplying the current instead of the voltage but the result is the same, so there is certainly something wrong.
My parameters:
AC Line frequency: 60Hz
Voltage: 127Vrms
Current: up to 10Arms (testing with 5)
Sampling frequency : 8640Hz
Number of samples per full wave: 144
Phase lag inserted by current transformer: 6° (With 144 points per wave I can get a resolution of 2.5°)
Also, here's my code
PS: Finished Formatting
I'm using a STM32f407 microcontroller running at 168MHz with an embedded DSP coprocessor, which allows me to perform up to 2 simultaneous 16 bit MAC operations. It also has about 512kib of flash and about 100kib of RAM
Modified my source code and something worked
This subtitle is horrible but it's the best I've got now. I modified my code in the line where $V=V.correction$, I modified to $V=np.abs(V),correction$ and the phase shift started working. I just decided to try it, because of a hunch and it started working but I don't know why. And also, I couldn't insert the desired phase shift with the equation above, I tweaked it manually, which is NOT ok, it needs to make sense (the final value in order to add a phase shift of 6° was -2.15j)