calculateBaselineFHR method

int calculateBaselineFHR(
  1. List<int> beats,
  2. int size
)

Calculates the baseline fetal heart rate (FHR).

beats - The list of beats. size - The size of the list.

Returns the baseline FHR as an integer.

Implementation

int calculateBaselineFHR(List<int> beats, int size) {
  //assert size > 15 : "Input size insufficient!" ;

  //List<double> beats = convertBPMArrayListArray(beatsBPM);
  //size = beats.length;
  int noOfSamplesPerMinute = 60;
  int minutes = (size / noOfSamplesPerMinute).truncate();
  List<int?> beatsBpm = List.filled(size, null, growable: false);
  double meanHr = 0, meanHrLb = 0, meanHrUb = 0;
  List<double?> variabilityHr = List.filled(minutes, null, growable: false);
  List<double?> meanHrList = List.filled(minutes, null, growable: false);
  double min = beats[0].toDouble();
  double max = 0;

  for (int j = 0; j < size; j++) {
    if (beats[j] != 0) {
      beatsBpm[j] = (SIXTY_THOUSAND_MS / beats[j]).truncate();
    } else {
      beatsBpm[j] = 0;
    }
  }

  for (int j = 0; j < minutes; j++) {
    min = 1000000;
    max = 0;
    meanHr = 0;
    for (int i = 0; i < noOfSamplesPerMinute; i++) {
      if (beatsBpm[j * noOfSamplesPerMinute + i]! < min) {
        min = beatsBpm[j * noOfSamplesPerMinute + i]!.toDouble();
      }
      if (beatsBpm[j * noOfSamplesPerMinute + i]! > max) {
        max = beatsBpm[j * noOfSamplesPerMinute + i]!.toDouble();
      }

      meanHr += beatsBpm[j * noOfSamplesPerMinute + i]!;
    }
    meanHr /= noOfSamplesPerMinute;
    variabilityHr[j] = max - min;
    meanHrList[j] = meanHr;
  }

  int howManyLb = 0, howManyUb = 0;

  for (int j = 0; j < minutes; j++) {
    if (variabilityHr[j]! <= (SIXTY_THOUSAND_MS / 25)) {
      meanHrLb += meanHrList[j]!;
      howManyLb++;
    } else {
      meanHrUb += meanHrList[j]!;
      howManyUb++;
    }
  }

  baselineFHR = meanHrLb / howManyLb;
  if (baselineFHR! > 0) {
    baselineFHR = ((baselineFHR! / 5) * 5);
  } else {
    baselineFHR = meanHrUb / howManyUb;
    baselineFHR = ((baselineFHR! / 5) * 5);
  }
  if (baselineFHR! > 0 && SIXTY_THOUSAND_MS / baselineFHR! < 300) {
    return (SIXTY_THOUSAND_MS / baselineFHR!).truncate();
  } else {
    return 0;
  }
}