bcLinear method

int bcLinear (Float64List array, double offset, int nBaselinePoints)

Subtracts a straight line from array constructed as follows:

  1. Two regions of length nBaselinePoints in array are considered. The first region starts at index offset of array, the second one ends at array.length-offset (i.e. is at near the end of array).
  2. In both regions, the average of the data are computed. The resulting values define a straigt line which is subtracted from array

Implementation

static int bcLinear(Float64List array, double offset, int nBaselinePoints) {
  if (array.length <= 10 * nBaselinePoints) {
    nBaselinePoints = array.length ~/ 10;
    if (nBaselinePoints == 0) nBaselinePoints = 1;
  }

  int start1 = (array.length * offset).round();
  if (start1 < 0 || start1 >= array.length) start1 = 0;

  int end1 = start1 + nBaselinePoints;
  if (end1 <= start1 || end1 >= array.length) end1 = start1;

  int start2 =
      (array.length - array.length * offset - nBaselinePoints).round();
  if (start2 < 0 || start2 >= array.length) start2 = array.length;

  int end2 = start2 + nBaselinePoints;
  if (end2 <= start2 || end2 >= array.length) end2 = array.length - 1;

  double mean1 = Sigma.meanValue(array, start1, end1);
  double mean2 = Sigma.meanValue(array, start2, end2);

  // if the means are the same, just do an offset correction
  if ((mean1 - mean2).abs() < Sigma.sigma(array, start1, end1)) {
    if (start1 + end1 >= array.length) return -1;
    bcOffset(array, false, start1, end1);
    return 0;
  }

  // used equations:
  //  y = m*x + b
  //  mean1 = m*x1 + b
  //  mean2 = m*x2 + b
  //  mean2 - mean1 = m*x2 - m*x1
  //  m = (mean2 - mean1)/(x2 - x1);
  //  b = mean1 - m*x1

  int x1 = start1 + nBaselinePoints ~/ 2;
  int x2 = start2 + nBaselinePoints ~/ 2;
  double m = (mean2 - mean1) / (x2 - x1);
  double b = mean1 - m * x1;

  for (int i = 0; i < array.length; i++) {
    array[i] -= m * i + b;
  }
  return 0;
}