calculateHR method

int calculateHR(
  1. double input,
  2. bool targetLeadState
)

Implementation

int calculateHR(
  double input,
  bool targetLeadState,
) {
  // target_leadState : false --> detached
  var hr_result = -1; // -1 : peak is not detected
  var output1 = 0.0;
  var output2 = 0.0;

  if (targetLeadState) {
    dataList.add(input);
    //modified by HJK
    // if (dataList.length > LP_WINDOW) {
    //   dataList.removeAt(0);
    // }

    output1 = removeR.movingAvFilt(input);
    movingFiltered.add(output1);
    output2 = removeR.lowpassFilt(output1);
    lpFiltered.add(output2);

    if (lpFiltered.length > MAX_LIST_LEN) {
      rInd = detectR.findPeaks(lpFiltered[lpFiltered.length - 1]);

      //modified by HJK
      if (rInd > 0) {
        //for index adjustment
        rInd += 500;
        rInd = detectR.adjustPeaks(
          dataList.getRange(rInd - 37, rInd).toList(),
          rInd - 37,
        );

        peakExist = detectR.checkPeaks(
          dataList.getRange(rInd - 37, rInd).toList(),
        );
      }

      if (rInd > 0 && peakExist == true) {
        searchBool = true;
      }

      if (searchBool) {
        searchBool = false;
        notDetectedCnt = 0;

        //modified HJK
        //calculate HR
        r_detected = rInd;
        if (r_prev > 0) {
          hrB = (60.0 * fs / (r_detected - r_prev)).round();
          // hr_result = hrB;
        }

        r_prev = r_detected;

        //modified HJK
        if (hrB > 0) {
          hrList.add(hrB.toDouble());
          hr_result = calculateMedian(hrList).toInt();
        }

        if (hrList.length >= 5) {
          hrList.removeAt(0);
        }
      } else {
        notDetectedCnt++;
        if (notDetectedCnt > SEC4) {
          // 1000sample = 4sec
          notDetectedCnt = 0;
          hr_result = 0;
        }
      }
    } else if (lpFiltered.length == MAX_LIST_LEN) {
      delta = detectR.getMedian(lpFiltered.sublist(0, MAX_LIST_LEN)) +
          detectR.meanAbsoluteDeviation(
            lpFiltered.sublist(0, MAX_LIST_LEN),
          );
      detectR.inputDelta(delta);
    }
  } else {
    // lead is not attached
    notDetectedCnt2++;
    if (notDetectedCnt2 > fs) {
      // 1 sec
      // 1sec
      hr_result = 0;
      notDetectedCnt2 = 0;
    } else {
      hr_result = -1;
    }
  }

  return hr_result;
}