0

I may be trying something which may not be possible. I am still trying to figure out how to get proper answers.

I use the HRV Analysis Python package and have set up an API layer that calculates the HRV along with all the Time and Frequency domain values.

I record a minute of RR intervals from a BLE device and send that array to the API for calculations, which continues for some time. A good, almost accurate reading for HRV analysis is about 3 minutes.

I now have a request to calculate the LF (one of the Frequency domain values) in real time almost. So I need to do the calculations on the flutter/dart written mobile device itself.

Are real-time calculations even possible? I am trying to do this using Lomb-Scargle method. I have distilled down the following class by looking up the web and using GitHub Copilot. As you can see, almost everywhere, I need a "List of nnIntervals"

import 'dart:math';

class LombScargle {
  final List<double> times;
  final List<double> values;
  List<double> frequencies;
  List<double> power;

  LombScargle(List<double> nnIntervals)
      : times = List.generate(nnIntervals.length, (i) => i.toDouble()),
        values = nnIntervals {
    // Calculate the frequencies to use in the Lomb-Scargle calculation
    frequencies = _generateFrequencies(nnIntervals.length);

    // Calculate the power spectrum using the Lomb-Scargle method
    power = _calculatePowerSpectrum();
  }

  List<double> _generateFrequencies(int n) {
    List<double> freqs = [];
    for (int i = 0; i < n; i++) {
      freqs.add(i / n);
    }
    return freqs;
  }

  List<double> _calculatePowerSpectrum() {
    double mean = values.reduce((a, b) => a + b) / values.length;

    List<double> sinValues = [];
    List<double> cosValues = [];
    for (int i = 0; i < times.length; i++) {
      sinValues.add(sin(2 * pi * frequencies[i] * times[i]));
      cosValues.add(cos(2 * pi * frequencies[i] * times[i]));
    }

    List<double> powers = [];
    for (int i = 0; i < frequencies.length; i++) {
      double sinSum = sinValues.reduce((a, b) => a + b * values[i]);
      double cosSum = cosValues.reduce((a, b) => a + b * values[i]);
      double num = 0.5 * (sinSum * sinSum + cosSum * cosSum);
      double denom = sinValues.reduce((a, b) => a + b * b) *
          cosValues.reduce((a, b) => a + b * b);
      powers.add(num / denom);
    }

    return powers;
  }
}

What are the possibilities I am just going about the wrong way? Any suggestions here?

Vivian Lobo
  • 583
  • 10
  • 29

0 Answers0