-
Notifications
You must be signed in to change notification settings - Fork 38
/
stat.cpp
59 lines (52 loc) · 1.49 KB
/
stat.cpp
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
#include "stat.h"
int32_t EMP_AVERAGE::average(int32_t value) {
uint8_t round_v = emp_k >> 1;
update(value);
return (emp_data + round_v) / emp_k;
}
void EMP_AVERAGE::update(int32_t value) {
uint8_t round_v = emp_k >> 1;
emp_data += value - (emp_data + round_v) / emp_k;
}
int32_t EMP_AVERAGE::read(void) {
uint8_t round_v = emp_k >> 1;
return (emp_data + round_v) / emp_k;
}
//------------------------------------------ class HISTORY ----------------------------------------------------
void HISTORY::update(uint16_t item) {
if (len < H_LENGTH) {
queue[len++] = item;
} else {
queue[index ] = item;
if (++index >= H_LENGTH) index = 0; // Use ring buffer
}
}
uint16_t HISTORY::read(void) {
uint8_t i = H_LENGTH - 1;
if (index)
i = index - 1;
return queue[i];
}
uint16_t HISTORY::average(void) {
uint32_t sum = 0;
if (len == 0) return 0;
if (len == 1) return queue[0];
for (uint8_t i = 0; i < len; ++i) sum += queue[i];
sum += len >> 1; // round the average
sum /= len;
return uint16_t(sum);
}
float HISTORY::dispersion(void) {
if (len < 3) return 1000;
uint32_t sum = 0;
uint32_t avg = average();
for (uint8_t i = 0; i < len; ++i) {
int32_t q = queue[i];
q -= avg;
q *= q;
sum += q;
}
sum += len << 1;
float d = (float)sum / (float)len;
return d;
}