AccelerationFeatures.fromAccelerometerReadings constructor

AccelerationFeatures.fromAccelerometerReadings(
  1. List<UserAccelerometerEvent> readings
)

Implementation

factory AccelerationFeatures.fromAccelerometerReadings(
  List<UserAccelerometerEvent> readings,
) {
  final n = readings.length;

  final List<num> xList = readings.map((event) => event.x).toList();
  final List<num> yList = readings.map((event) => event.y).toList();
  final List<num> zList = readings.map((event) => event.z).toList();

  final xStatistics = xList.statistics;
  final yStatistics = yList.statistics;
  final zStatistics = zList.statistics;

  final xStats = Stats(xList);
  final yStats = Stats(yList);
  final zStats = Stats(zList);

  var features = AccelerationFeatures()
        ..count = n
        // mean
        ..xMean = xStatistics.mean
        ..yMean = yStatistics.mean
        ..zMean = zStatistics.mean
        // std dev
        ..xStd = xStatistics.standardDeviation
        ..yStd = yStatistics.standardDeviation
        ..zStd = zStatistics.standardDeviation
        //min
        ..xMin = xStatistics.min
        ..yMin = yStatistics.min
        ..zMin = zStatistics.min
        // max
        ..xMax = xStatistics.max
        ..yMax = yStatistics.max
        ..zMax = zStatistics.max
        //max-min diff
        ..xMaxMinDiff = xStatistics.max - xStatistics.min
        ..yMaxMinDiff = yStatistics.max - yStatistics.min
        ..zMaxMinDiff = zStatistics.max - zStatistics.min
        // median
        ..xMedian = xStatistics.median
        ..yMedian = yStatistics.median
        ..zMedian = zStatistics.median
        // energy
        ..xEnergy = xStatistics.squaresSum / n
        ..yEnergy = yStatistics.squaresSum / n
        ..zEnergy = zStatistics.squaresSum / n
      //
      ;

  // positive count
  features.xPosCount = xList.where((x) => x > 0).length;
  features.yPosCount = yList.where((y) => y > 0).length;
  features.zPosCount = zList.where((z) => z > 0).length;

  // negative count
  features.xNegCount = xList.where((x) => x < 0).length;
  features.yNegCount = yList.where((y) => y < 0).length;
  features.zNegCount = zList.where((z) => z < 0).length;

  // inter-quartile range
  features.xIqr = xStats.quartile3 - xStats.quartile1;
  features.yIqr = yStats.quartile3 - yStats.quartile1;
  features.zIqr = zStats.quartile3 - zStats.quartile1;

  // values above mean
  features.xAboveMean = xList.where((x) => x > xStatistics.mean).length;
  features.yAboveMean = yList.where((y) => y > yStatistics.mean).length;
  features.zAboveMean = zList.where((z) => z > zStatistics.mean).length;

  // average absolute diff (AAD)
  // median abs dev (MAD)
  // average results
  // signal magnitude area (SMA)
  List<num> ar = [];
  num xSma = 0, ySma = 0, zSma = 0;
  List<num> xAad = [], yAad = [], zAad = [];
  List<num> xMad = [], yMad = [], zMad = [];
  for (var r in readings) {
    xAad.add((r.x - xStatistics.mean).abs());
    yAad.add((r.y - yStatistics.mean).abs());
    zAad.add((r.z - zStatistics.mean).abs());

    xMad.add((r.x - xStatistics.median).abs());
    yMad.add((r.y - yStatistics.median).abs());
    zMad.add((r.z - zStatistics.median).abs());

    ar.add(math.sqrt(math.pow(r.x, 2) + math.pow(r.y, 2) + math.pow(r.z, 2)));

    xSma += r.x.abs() / n;
    ySma += r.y.abs() / n;
    zSma += r.z.abs() / n;
  }
  features.xAad = xAad.mean;
  features.yAad = yAad.mean;
  features.zAad = zAad.mean;
  features.xMad = xMad.median;
  features.yMad = yMad.median;
  features.zMad = zMad.median;
  features.avgResultAcceleration = ar.mean;
  features.signalMagnitudeArea = xSma + ySma + zSma;

  return features;
}