summaryrefslogtreecommitdiff
path: root/firmware/os/algos/calibration/sample_rate_estimator/sample_rate_estimator.c
blob: 5ab7dd83adcabc8e7c5e99764356202495a0f1de (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
/*
 * Copyright (C) 2018 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "calibration/sample_rate_estimator/sample_rate_estimator.h"

#include <string.h>

#include "common/math/macros.h"
#include "chre/util/nanoapp/assert.h"

// Helper function used to reset the sampling rate estimator accumulator.
static void sampleRateEstimatorResetAccumulator(
    struct SampleRateEstimator* sample_rate_estimator) {
  sample_rate_estimator->last_timestamp_nanos = 0.0f;
  sample_rate_estimator->interval_accumulator_nanos = 0.0f;
  sample_rate_estimator->num_intervals_collected = 0;
}

void sampleRateEstimatorInit(struct SampleRateEstimator* sample_rate_estimator,
                             size_t num_intervals_to_collect,
                             float max_interval_sec) {
  CHRE_ASSERT_NOT_NULL(sample_rate_estimator);
  memset(sample_rate_estimator, 0, sizeof(struct SampleRateEstimator));
  sample_rate_estimator->mean_sampling_rate_estimate_hz =
      SAMPLE_RATE_ESTIMATOR_INVALID_SAMPLE_RATE_HZ;
  sample_rate_estimator->num_intervals_to_collect = num_intervals_to_collect;
  sample_rate_estimator->max_interval_nanos =
      max_interval_sec * SEC_TO_NANOS(1);
}

float sampleRateEstimatorGetHz(
    struct SampleRateEstimator* sample_rate_estimator) {
  sample_rate_estimator->new_sampling_rate_estimate_ready = false;
  return sample_rate_estimator->mean_sampling_rate_estimate_hz;
}

void sampleRateEstimatorUpdate(
    struct SampleRateEstimator* sample_rate_estimator,
    uint64_t timestamp_nanos) {
  // Resets the current interval capture and returns if:
  //   1. A bad timestamp was received (i.e., time not monotonic).
  //   2. 'last_timestamp_nanos' is zero. NOTE: 'last_timestamp_nanos' = 0
  //      indicates that the first complete time interval has not been captured
  //      yet (so, set it and return).
  if (timestamp_nanos <= sample_rate_estimator->last_timestamp_nanos ||
      sample_rate_estimator->last_timestamp_nanos == 0) {
    sample_rate_estimator->last_timestamp_nanos = timestamp_nanos;
    return;
  }

  // Computes the current sampling interval. This conversion will be very fast
  // for intervals less than ~4.3 seconds (i.e., 2^32 nano-seconds).
  const float next_interval_nanos = floatFromUint64(
      timestamp_nanos - sample_rate_estimator->last_timestamp_nanos);

  // Helps prevent corruption of the estimator when there are gaps in the input
  // sampling intervals greater than 'max_interval_nanos' (i.e., intermittant
  // periods where there are no input timestamps).
  if (next_interval_nanos >= sample_rate_estimator->max_interval_nanos) {
    // Resets the estimator and returns.
    sampleRateEstimatorResetAccumulator(sample_rate_estimator);
    return;
  }

  // Accumulates the next sampling interval.
  sample_rate_estimator->interval_accumulator_nanos += next_interval_nanos;
  sample_rate_estimator->last_timestamp_nanos = timestamp_nanos;
  sample_rate_estimator->num_intervals_collected++;

  // If the number of collected time intervals exceed the target number, then
  // this computes a new sample rate estimate.
  if (sample_rate_estimator->num_intervals_collected >
      sample_rate_estimator->num_intervals_to_collect) {
    sample_rate_estimator->mean_sampling_rate_estimate_hz =
        sample_rate_estimator->num_intervals_collected *
        (SEC_TO_NANOS(1) / sample_rate_estimator->interval_accumulator_nanos);

    // Sets the polling flag to indicate that a new estimate is ready.
    sample_rate_estimator->new_sampling_rate_estimate_ready = true;

    // Resets the estimator variables.
    sampleRateEstimatorResetAccumulator(sample_rate_estimator);
  }
}