apply method

double apply(
  1. double input
)
override

Implementation

double apply(double input) {
  dataList.add(input);
  output = input;
  output = waveletFilter.applyFilter(input);
  if (output.isNaN) {
    nanCnt += 1;
    return 0;
  } else {
    sample++;
    outSample++;
    dataOrg.add(dataList[sample]);
    newData.add(output);
    output2 = svFilter.applySmoothing(output);
    smoothedData.add(output2);
    output3 = removeR.movingAvFilt(output2);
    movingFiltered.add(output3);
    output4 = removeR.lowpassFilt(output3);
    lpFiltered.add(output4);
    if (lpFiltered.length > 8) {
      lpFiltered2.add(output4);
      if (lpFiltered2.length == 500) {
        delta = detectR.getMedian(lpFiltered2.sublist(0, 500)) +
            detectR.meanAbsoluteDeviation(lpFiltered2.sublist(0, 500));
        detectR.inputDelta(delta);
      }

      if (lpFiltered2.length > 500) {
        rInd = detectR.findPeaks(lpFiltered2[outSample]);
      }

      if (rInd > 0) {
        searchBool = true;
        rCand = rInd + 500;
      } else {
        hr = 0;
      }
      if (searchBool) {
        if (outSample - rCand > 41) {
          searchBool = false;
          rInd2 = detectR.adjustPeaks(
              dataOrg.sublist(rCand - 37, rCand), rCand - 37);

          rDetected = rInd2;
          if (rPrev > 0) {
            hr = 60 / ((rDetected - rPrev) / fs);
          }
          rPrev = rDetected;

          diffSeg = [];
          for (var i = rInd2 + 8 - 30; i < rInd2 + 8 + 30; i++) {
            diffSeg.add(smoothedData[i + 1] - smoothedData[i]);
          }
          qsCand = detectR.getWidth(diffSeg, rInd2 + 8 - 30);

          for (var i = qsCand[0]; i < qsCand[1] + 1; i++) {
            smoothedData[i] = dataOrg[i - 8];
            diffData[i - 8] = 0;
          }

          if (qsCand[0] - 8 >= 500) {
            thresholds = removeR.calculateThreshold(
                diffData, qsCand[0] - 8 - 500, qsCand[0] - 8);
            removeBool = removeR.removeResidualQrs(
                diffData[qsCand[0] - 8 - 1], thresholds[0], thresholds[1]);

            if (removeBool) {
              for (var i = qsCand[0] - 4; i < qsCand[0]; i++) {
                smoothedData[i] = dataOrg[i - 8];
              }
              removeBool = false;
              qsCand[0] = qsCand[0] - 4;
            }

            thresholds = removeR.calculateThreshold(
                diffData, qsCand[1] - 8 - 500, qsCand[1] - 8);
            removeBool = removeR.removeResidualQrs(
                diffData[qsCand[1] - 8 - 1], thresholds[0], thresholds[1]);

            if (removeBool) {
              for (var i = qsCand[1] + 1; i < qsCand[1] + 5; i++) {
                smoothedData[i] = dataOrg[i - 8];
              }
              removeBool = false;
              qsCand[1] = qsCand[1] + 4;
            }

            var sm = 0;
            for (var i = qsCand[0] - 1; i < qsCand[0] + 2; i++) {
              seg = smoothedData.sublist(
                  qsCand[0] - 2 + sm, qsCand[0] + sm + 1);
              newSig = 1 / 3 * seg.reduce((a, b) => a + b);
              smoothedData[i] = newSig;
              sm++;
            }

            sm = 0;
            for (var i = qsCand[1] - 1; i < qsCand[1] + 2; i++) {
              seg = smoothedData.sublist(
                  qsCand[1] - 2 + sm, qsCand[1] + sm + 1);
              newSig = 1 / 3 * seg.reduce((a, b) => a + b);
              smoothedData[i] = newSig;
              sm++;
            }
            filterBool = true;
          }
        }
      }
      diffData.add(dataOrg[outSample] - smoothedData[sample]);
    }

    if (sample > 150) {
      if (filterBool) {
        return smoothedData[sample + 8 - 143];
      } else {
        return dataOrg[sample - 143];
      }
    } else {
      return 0;
    }
  }
}