detectTroughs method

List<int> detectTroughs(
  1. List<double> data,
  2. List<DateTime> timestamps,
  3. double threshold,
  4. double minTroughDistanceSec,
)

Detects troughs in the data with a debounce period.

data - List of double values to analyze. timestamps - List of timestamps corresponding to the data. threshold - Maximum value to consider as a trough. minTroughDistanceSec - Minimum time between troughs in seconds. Returns a list of indices where troughs are detected.

Implementation

List<int> detectTroughs(List<double> data, List<DateTime> timestamps, double threshold, double minTroughDistanceSec) {
  List<int> troughs = [];
  for (int i = 1; i < data.length - 1; i++) {
    if (data[i] < threshold && data[i] < data[i - 1] && data[i] < data[i + 1]) {
      if (troughs.isEmpty ||
          timestamps[i].difference(timestamps[troughs.last]).inMilliseconds / 1000 > minTroughDistanceSec) {
        troughs.add(i);
      }
    }
  }
  return troughs;
}