-
Notifications
You must be signed in to change notification settings - Fork 13
/
c_median.h
133 lines (108 loc) · 3.17 KB
/
c_median.h
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
/***************************************************
Copyright (C) 2016 Steffen Ochs
The elements of this program were taken from the work of
AUTHOR: Rob dot Tillaart at gmail dot com
PURPOSE: RunningMedian library for Arduino 0.1.13
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
HISTORY: Please refer Github History
****************************************************/
#define MEDIAN_SIZE 16 // Einfluss auf Batterie-Simulation
uint16_t _cnt;
uint16_t _idx;
boolean _sorted;
int _ar[MEDIAN_SIZE];
uint16_t _p[MEDIAN_SIZE];
//++++++++++++++++++++++++++++++++++++++++++++++++++++++
// add Value to Buffer
void median_add(int value) {
_ar[_idx++] = value;
if (_idx >= MEDIAN_SIZE) _idx = 0; // wrap around
if (_cnt < MEDIAN_SIZE) _cnt++;
_sorted = false;
}
void median_clear()
{
_cnt = 0;
_idx = 0;
_sorted = false;
for (uint8_t i = 0; i< MEDIAN_SIZE; i++) _p[i] = i;
}
/*
//++++++++++++++++++++++++++++++++++++++++++++++++++++++
// sort Buffer
void median_sort() {
// bubble sort with flag
for (uint8_t i = 0; i < _cnt-1; i++) {
bool flag = true;
for (uint8_t j = 1; j < _cnt-i; j++) {
if (_ar[_p[j-1]] > _ar[_p[j]]) {
uint8_t t = _p[j-1];
_p[j-1] = _p[j];
_p[j] = t;
flag = false;
}
}
if (flag) break;
}
_sorted = true;
}
//++++++++++++++++++++++++++++++++++++++++++++++++++++++
// get Median from Buffer
double median_get()
{
if (_cnt > 0)
{
if (_sorted == false) median_sort();
if (_cnt & 0x01) return _ar[_p[_cnt/2]];
else return (_ar[_p[_cnt/2]] + _ar[_p[_cnt/2 - 1]]) / 2;
}
return NAN;
}
double median_getHighest()
{
if (_cnt > 0)
{
if (_sorted == false) median_sort();
return _ar[_p[_cnt-1]];
}
return NAN;
}
*/
double median_average()
{
if (_cnt > 0)
{
double sum = 0;
for (uint8_t i = 0; i < _cnt; i++) sum += _ar[i];
return sum / _cnt;
}
return NAN;
}
//++++++++++++++++++++++++++++++++++++++++++++++++++++++
// Temp Buffer
void mem_add(float value, int i) {
ch[i].ar[ch[i].idx++] = value;
if (ch[i].idx >= MEM_SIZE) ch[i].idx = 0; // wrap around
if (ch[i].cnt < MEM_SIZE) ch[i].cnt++;
}
void mem_clear(int i) {
ch[i].cnt = 0;
ch[i].idx = 0;
}
float mem_a(int i) {
if (ch[i].cnt > 0) {
float sum = 0;
for (uint8_t j = 0; j < ch[i].cnt; j++) sum += ch[i].ar[j];
return (sum / (float) ch[i].cnt);
}
return INACTIVEVALUE;
}