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?