integ_trapez method

Float64List integ_trapez (Float64List array, int firstix, int lastix, double bias, double slope)

Computes a new array representing the integral function of array in the specified range. array represents the values of a function at equidistant x coordinates. firstix first index in array to contribute to integral. lastix last index in array to contribute to integral. Returns the integral function computed from firstix until lastix with index increment 1. The last value in the returned array is the integral value over the entire region.

Integration is performed according to the trapezoidal rule: I ~= (y0/2 + y1 + y2 + y3 + ... + yn-1/2)/h with: n data points y0...yn-1 h = (b-a)/(n-1) (n points, but n-1 intervals of this size)

h = 1 in our case. bias and slope (if their abs. value is > 0) define a linear function whose value at each point is added to the integral function. It can be used as a linear baseline correction of the integral function.

Implementation

Float64List integ_trapez(
    Float64List array, int firstix, int lastix, double bias, double slope) {
  assert(firstix >= 0 && lastix <= (array.length - 1));
  assert(lastix >= 0 && firstix <= (array.length - 1));

  int ixstart = firstix;
  int ixend = lastix;
  if (firstix == lastix) {
    Float64List integFunc = new Float64List(1);
    integFunc[0] = array[firstix];
    return integFunc;
  }

  if (firstix > lastix) // swap to obtain an increasing order
  {
    ixstart = lastix;
    ixend = firstix;
  }

  // As the bias reference we will used the first data point of the region.
  // One could also use an average over several points, however then we would possibly
  // give the user the ability to change this number.
  double biasref = 0.0;
  if (bias.abs() > 0.0000001) {
    biasref = array[firstix];
  }

  double totalBias = biasref * bias;

  Float64List integFunc = new Float64List(ixend - ixstart + 1);
//  double sum = biasref*bias + (ydata[ixstart] + (ixend - ixstart)*slope + ydata[ixend])/2;
  double sum = biasref * bias + array[ixstart] / 2;
  integFunc[0] = sum;

  for (int i = ixstart + 1; i < ixend; i++) {
    sum += totalBias + (i - ixstart) * slope + array[i];
    integFunc[i - ixstart] = sum;
  }

  sum += totalBias + ((ixend - ixstart) * slope + array[ixend]) / 2;
  integFunc[ixend - ixstart] = sum;

  return integFunc;
}