Infuse-IoT SDK API 0.0.1
A Scalable Open Source RTOS
Loading...
Searching...
No Matches
statistics.h
Go to the documentation of this file.
1
9
10#ifndef INFUSE_SDK_INCLUDE_INFUSE_MATH_STATISTICS_H_
11#define INFUSE_SDK_INCLUDE_INFUSE_MATH_STATISTICS_H_
12
13#include <stdint.h>
14#include <string.h>
15
16#ifdef __cplusplus
17extern "C" {
18#endif
19
31
35 int64_t v;
37 int64_t w;
39 int32_t m;
41 int32_t p;
43 uint32_t n;
44};
45
51static inline void statistics_reset(struct statistics_state *state)
52{
53 memset(state, 0x00, sizeof(*state));
54}
55
62void statistics_update(struct statistics_state *state, int32_t value);
63
71static inline float statistics_mean(const struct statistics_state *state)
72{
73 if (state->n == 0) {
74 return 0.0f;
75 }
76
77 /* Equation 8 */
78 return (float)state->m + ((float)state->p / state->n);
79}
80
88static inline float statistics_variance(const struct statistics_state *state)
89{
90 if (state->n < 2) {
91 return 0.0f;
92 }
93 float mean_error = statistics_mean(state) - state->m;
94
95 /* Equation 9 */
96 return (float)state->v + ((float)state->w / (float)(state->n - 1)) -
97 (state->n * mean_error * mean_error / (state->n - 1));
98}
99
113static inline int32_t statistics_mean_rough(const struct statistics_state *state)
114{
115 return state->m;
116}
117
133static inline uint64_t statistics_variance_rough(const struct statistics_state *state)
134{
135 if (state->n < 2) {
136 return 0;
137 }
138 return state->v + (state->w / (state->n - 1));
139}
140
144
145#ifdef __cplusplus
146}
147#endif
148
149#endif /* INFUSE_SDK_INCLUDE_INFUSE_MATH_STATISTICS_H_ */
static int32_t statistics_mean_rough(const struct statistics_state *state)
Compute the rough mean of the statistics object.
Definition statistics.h:113
static float statistics_mean(const struct statistics_state *state)
Compute the mean of the statistics object.
Definition statistics.h:71
static uint64_t statistics_variance_rough(const struct statistics_state *state)
Compute the rough variance of the statistics object.
Definition statistics.h:133
void statistics_update(struct statistics_state *state, int32_t value)
Update the statistics object with a new sample.
static void statistics_reset(struct statistics_state *state)
Reset statistics object.
Definition statistics.h:51
static float statistics_variance(const struct statistics_state *state)
Compute the variance of the statistics object.
Definition statistics.h:88
Running statistics state.
Definition statistics.h:33
int64_t v
Variance approximation.
Definition statistics.h:35
uint32_t n
Sequence count.
Definition statistics.h:43
int32_t m
Mean approximation.
Definition statistics.h:39
int64_t w
Variance correction factor.
Definition statistics.h:37
int32_t p
Mean fractional accumulation.
Definition statistics.h:41