int i1 = 0, i2 = 0; // left window
int j1 = 0, j2 = 0; // right window
double sum = 0; // sum of products
double count = 0; // number of products
IDoubleArray cumdata = cumulate(data);
double maxtime = time.get(time.size() - 1);
// as long as there is enough space:
while (maxtime - time.get(i1) >= tau)
{
// move end of left window
i2 = i1;
while (time.get(i2) - time.get(i1) <= averageWidth && i2 < time.size())
{
i2++;
if (i2 >= time.size())
{
break;
}
}
// move beginning of right window
while (time.get(j1) - time.get(i1) < tau
&& maxtime - time.get(j1) >= tau)
{
j1++;
if (j1 >= time.size())
{
break;
}
}
// move end of right window
j2 = j1;
if (j2 < time.size())
{
while (time.get(j2) - time.get(j1) <= averageWidth && j2 < time.size())
{
j2++;
if (j2 >= time.size())
{
break;
}
}
}
/*
if (tau == 103247)
{
System.out.println("("+i1+","+i2+")\t("+j1+","+j2+")");
}
*/
// update autocorrelation
double x1 = (cumdata.get(i2)-cumdata.get(i1))/(i2-i1);
double x2 = (cumdata.get(j2)-cumdata.get(j1))/(j2-j1);
sum += x1*x2;
count += 1;
// shift left window
i1++;