Digital integrator

I have been implementing a control software where I need to calculate a magnetic flux based on the measurement of the phase voltages of a three phase grid (basically three sinewaves) according to the equation $$\psi = \int_0^t u(\tau)\mathrm{d}\tau$$.

It seemed to be a pretty simple task at first glance. So I have written following piece of code (trapezoidal integration rule) and I have passed perfect sinawave

#define PI 3.14
#define N 64

float x, y;
for(int k = 0; k < N; k++){
x = sin(k*2*PI/N);
y = integrate(1.0, x);
}

float integrate(float T, float xk)
{
static float intk_1 = 0; // I(k-1)
static float xk_1 = 0; // x(k-1)

float intk = intk_1 + T/2*(xk_1 + xk);
intk_1 = intk;
xk_1 = xk;

return intk;
}

I have realized that integration outcome depends on the time instant where the integration starts. For example in case the integration starts at the time instant where the phase voltage has zero value the integrator output has large offset which occurs already in ideal case i.e. perfect analog channels, no noise and so on. In case the integration starts at the time instant where the phase voltage has its positive maximum the integrator output in ideal case doesn't have the offset. Another complications occurs in case I will use real analog channels with offsets and noise.

What I have been looking for is some robust integration method which will be immune mainly against the offsets so that I can calculate the the magnetic flux according to the above mentioned equation. One idea which I have is to use some digital filter with appropriate frequency characteristics. But I am not sure whether it is good idea. Can anybody help?

• I'm a bit rusty solving Laplace equations, but from what I've tried so far, the constant will be maximal if your starting phase is 0 degrees or 180 degrees.
– Ben
May 12 '21 at 12:00
• @Ben thank you very much for your reaction. Do you think the idea with the digital filter is wrong way? May 12 '21 at 12:33
• I don't know why you worry about this phenomenon. If your integrator is used in a control loop, the offset will cancel itself out anyway...
– Ben
May 12 '21 at 12:34
• I am very sorry but this idea is not clear for me. Please can you clarify me that? May 12 '21 at 14:42
• You asked a question about a 3-phase PLL, right? There are 2 integrators in a typical 3-phase PLL. Try implementing a 3-phase PLL and try varying the starting phase of the input signal and try to infer the effect on the transient on your PLL. TL,DR : Your PLL is a closed-loop system, the initial conditions of your integrators will decay out pretty quickly depending on the bandwidth of your PLL.
– Ben
May 12 '21 at 14:47

I'm a bit rusty with solving equations using the Laplace transform so please be indulgent if I made a mistake.

$$x = sin(\omega t + \theta)$$ and you want to find $$y = \int{sin(\omega t + \theta)dt}$$ You can rewrite x as $$x = a\sin(\omega t) + b\cos(\omega t)$$

In Laplace

$$y(s) = \frac{1}{s} \{\frac{a\omega + bs}{s^2 + \omega^2}\}$$

Using partial fraction expansion we get

$$y(s) = \frac{a}{\omega s} + \frac{b}{s^2 + \omega^2} - \frac{\frac{as}{\omega}}{s^2+\omega^2}$$

Going back in the time domain we get

$$y(t) = \frac{a}{\omega} + \frac{b}{\omega} sin(\omega t) - \frac{a}{\omega} cos(\omega t)$$

since a is related to your initial phase $$a = cos(\theta)$$, that's why you get an offset.