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
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
32 /* Variance approximation */
33 int64_t v;
34 /* Variance correction factor */
35 int64_t w;
36 /* Mean approximation */
37 int32_t m;
38 /* Mean fractional accumulation */
39 int32_t p;
40 /* Sequence count */
41 uint32_t n;
42};
43
49static inline void statistics_reset(struct statistics_state *state)
50{
51 memset(state, 0x00, sizeof(*state));
52}
53
60void statistics_update(struct statistics_state *state, int32_t value);
61
69static inline float statistics_mean(const struct statistics_state *state)
70{
71 if (state->n == 0) {
72 return 0.0f;
73 }
74
75 /* Equation 8 */
76 return (float)state->m + ((float)state->p / state->n);
77}
78
86static inline float statistics_variance(const struct statistics_state *state)
87{
88 if (state->n < 2) {
89 return 0.0f;
90 }
91 float mean_error = statistics_mean(state) - state->m;
92
93 /* Equation 9 */
94 return (float)state->v + ((float)state->w / (float)(state->n - 1)) -
95 (state->n * mean_error * mean_error / (state->n - 1));
96}
97
111static inline int32_t statistics_mean_rough(const struct statistics_state *state)
112{
113 return state->m;
114}
115
131static inline uint64_t statistics_variance_rough(const struct statistics_state *state)
132{
133 if (state->n < 2) {
134 return 0;
135 }
136 return state->v + (state->w / (state->n - 1));
137}
138
143#ifdef __cplusplus
144}
145#endif
146
147#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:111
static float statistics_mean(const struct statistics_state *state)
Compute the mean of the statistics object.
Definition statistics.h:69
static uint64_t statistics_variance_rough(const struct statistics_state *state)
Compute the rough variance of the statistics object.
Definition statistics.h:131
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:49
static float statistics_variance(const struct statistics_state *state)
Compute the variance of the statistics object.
Definition statistics.h:86
Definition statistics.h:31
int64_t v
Definition statistics.h:33
uint32_t n
Definition statistics.h:41
int32_t m
Definition statistics.h:37
int64_t w
Definition statistics.h:35
int32_t p
Definition statistics.h:39