-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
histogram.cpp
434 lines (356 loc) · 7.51 KB
/
histogram.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
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
//
// FILE: Histogram.cpp
// AUTHOR: Rob Tillaart
// DATE: 2012-11-10
// VERSION: 0.3.7
// PURPOSE: Histogram library for Arduino
// URL: https://github.com/RobTillaart/Histogram
#include "histogram.h"
Histogram::Histogram(const uint16_t length, float *bounds)
{
_bounds = bounds;
_length = length + 1;
_data = (int32_t *) malloc((_length) * sizeof(int32_t));
if (_data != NULL)
{
clear();
}
else
{
_length = 0;
}
_count = 0;
_status = HISTO_OK;
_maxBucket = 2147483647;
}
Histogram::~Histogram()
{
if (_data != NULL)
{
free(_data);
}
}
// resets all counters to value (default 0)
uint8_t Histogram::clear(int32_t value)
{
for (uint16_t i = 0; i < _length; i++)
{
_data[i] = value;
}
_count = 0;
_status = HISTO_OK;
if (value == _maxBucket) _status = HISTO_FULL;
return _status;
}
uint8_t Histogram::setBucket(const uint16_t index, int32_t value)
{
_data[index] = value;
_status = HISTO_OK;
if (value == _maxBucket) _status = HISTO_FULL;
return _status;
}
// adds a new value to the histogram - increasing
uint8_t Histogram::add(const float value)
{
if (_length == 0)
{
_status = HISTO_ERR_LENGTH;
return _status;
}
uint16_t index = find(value);
if (_data[index] == _maxBucket)
{
_status = HISTO_ERR_FULL;
return _status;
}
_data[index]++;
_count++;
_status = HISTO_OK;
if (_data[index] == _maxBucket) _status = HISTO_FULL;
return _status;
}
// adds a new value to the histogram - decreasing
uint8_t Histogram::sub(const float value)
{
if (_length == 0)
{
_status = HISTO_ERR_LENGTH;
return _status;
}
uint16_t index = find(value);
if (_data[index] == -_maxBucket)
{
_status = HISTO_ERR_FULL;
return _status;
}
_data[index]--;
_count++;
_status = HISTO_OK;
if (_data[index] == _maxBucket) _status = HISTO_FULL;
return _status;
}
uint8_t Histogram::status()
{
return _status;
}
// number of buckets
uint16_t Histogram::size()
{
return _length;
}
// number of values added to all buckets
uint32_t Histogram::count()
{
return _count;
}
// returns the count of a bucket
int32_t Histogram::bucket(const uint16_t index)
{
if (index > _length) return 0;
return _data[index];
}
// returns the relative frequency of a bucket
float Histogram::frequency(const uint16_t index)
{
if ((_count == 0) || (_length == 0)) return NAN;
if (index > _length) return 0; // differs from PMF()
return (1.0 * _data[index]) / _count;
}
// EXPERIMENTAL
// returns the probability of the bucket of a value
float Histogram::PMF(const float value)
{
if ((_count == 0) || (_length == 0)) return NAN;
uint16_t index = find(value);
return (1.0 * _data[index]) / _count;
}
// EXPERIMENTAL
// returns the cumulative probability of
// values <= value
float Histogram::CDF(const float value)
{
if ((_count == 0) || (_length == 0)) return NAN;
// TODO: could be done in one loop?
uint16_t index = find(value);
int32_t sum = 0;
for (uint16_t i = 0; i <= index; i++)
{
sum += _data[i];
}
return (1.0 * sum) / _count;
}
// EXPERIMENTAL
// returns the value of the original array for
// which the CDF is at least probability.
// must start at 0.
float Histogram::VAL(const float probability)
{
if ((_count == 0) || (_length == 0)) return NAN;
float prob = probability;
if (prob < 0.0) prob = 0.0;
else if (prob > 1.0) prob = 1.0;
prob *= _count;
int32_t sum = 0;
for (uint16_t i = 0; i < _length; i++)
{
sum += _data[i];
if ((sum >= prob) && (i < (_length - 1)))
{
return _bounds[i];
}
}
return INFINITY;
}
int32_t Histogram::sum()
{
int32_t _sum = 0;
for (uint16_t i = 0; i < _length; i++)
{
_sum += _data[i];
}
return _sum;
}
// returns the bucket number for value
// - binary search, more memory ; faster for #buckets > 20 ?
// uint16_t Histogram::find(const float value)
// {
// if (_length <= 0) return -1;
// uint16_t low = 0, high = _length;
// uint16_t mid;
// while (high - low > 1)
// {
// mid = (low + high)/2;
// if (_bounds[mid] > value)
// {
// high = mid;
// }
// else
// {
// low = mid;
// }
// }
// if (_bounds[mid] > value) return mid;
// return _length - 1;
// }
// returns the bucket number for value
// must start at 0.
uint16_t Histogram::find(const float value)
{
if (_length <= 0) return -1;
for (uint16_t i = 0; i < (_length - 1); i++)
{
if (_bounds[i] >= value)
{
return i;
}
}
return _length - 1;
}
// returns the (first) index of the bucket with minimum value.
uint16_t Histogram::findMin()
{
if (_length <= 0) return -1;
uint16_t index = 0;
uint16_t n = _length;
while (n > 1)
{
n--;
if (_data[n] < _data[index]) index = n;
}
return index;
}
// returns the (first) index of the bucket with maximum value.
uint16_t Histogram::findMax()
{
if (_length <= 0) return -1;
uint16_t index = 0;
uint16_t n = _length;
while (n > 1)
{
n--;
if (_data[n] > _data[index]) index = n;
}
return index;
}
// returns the number of buckets with an exact level.
uint16_t Histogram::countLevel(const int32_t level)
{
if (_length <= 0) return -1;
uint16_t buckets = 0;
uint16_t n = _length;
while (n > 0)
{
n--;
if (_data[n] == level) buckets++;
}
return buckets;
}
// returns the number of buckets above a certain level.
uint16_t Histogram::countAbove(const int32_t level)
{
if (_length <= 0) return -1;
uint16_t buckets = 0;
uint16_t n = _length;
while (n > 0)
{
n--;
if (_data[n] > level) buckets++;
}
return buckets;
}
// returns the number of buckets below a certain level.
uint16_t Histogram::countBelow(const int32_t level)
{
if (_length <= 0) return -1;
uint16_t buckets = 0;
uint16_t n = _length;
while (n > 0)
{
n--;
if (_data[n] < level) buckets++;
}
return buckets;
}
///////////////////////////////////////////////////
//
// experimental
//
// use with care
float Histogram::saturation()
{
return (1.0 * _count) / _length;
}
// experimental use with care
int32_t Histogram::getMaxBucket()
{
return _maxBucket;
}
void Histogram::setMaxBucket(int32_t value)
{
_maxBucket = value;
}
//////////////////////////////////////////////////////////////
//
// DERIVED CLASS - HISTOGRAM16
//
Histogram16::Histogram16(const uint16_t length, float *bounds) : Histogram(length, bounds)
{
_bounds = bounds;
_length = length + 1;
_data = (int16_t *) malloc((_length) * sizeof(int16_t));
if (_data != NULL)
{
clear();
}
else
{
_length = 0;
}
_count = 0;
_status = HISTO_OK;
_maxBucket = 32767;
}
Histogram16::~Histogram16()
{
if (_data) free(_data);
}
uint8_t Histogram16::setBucket(const uint16_t index, int16_t value)
{
_data[index] = value;
_status = HISTO_OK;
if (value == _maxBucket) _status = HISTO_FULL;
return _status;
}
//////////////////////////////////////////////////////////////
//
// DERIVED CLASS - HISTOGRAM8
//
Histogram8::Histogram8(const uint16_t length, float *bounds) : Histogram(length, bounds)
{
_bounds = bounds;
_length = length + 1;
_data = (int8_t *) malloc((_length) * sizeof(int8_t));
if (_data != NULL)
{
clear();
}
else
{
_length = 0;
}
_count = 0;
_status = HISTO_OK;
_maxBucket = 127;
}
Histogram8::~Histogram8()
{
if (_data) free(_data);
}
uint8_t Histogram8::setBucket(const uint16_t index, int8_t value)
{
_data[index] = value;
_status = HISTO_OK;
if (value == _maxBucket) _status = HISTO_FULL;
return _status;
}
// -- END OF FILE --