-
Notifications
You must be signed in to change notification settings - Fork 0
/
accumulator.cpp
77 lines (68 loc) · 1.54 KB
/
accumulator.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
#include "accumulator.h"
#include <QDebug>
Accumulator::Accumulator()
{
meanAccumulator = 0;
n = 0;
dispAccumulator = 0;
force_learn = false;
frame_counter = 0;
}
void Accumulator::forceLearn()
{
frame_counter = 0;
force_learn = true;
}
void Accumulator::needForceLearn()
{
frame_counter++;
if (frame_counter == MAX_FRAME_AMOUNT)
{
force_learn = false;
}
}
float Accumulator::accumulate(int next)
{
needForceLearn();
float mean = 0;
float disp = 0;
if (n!=0)
{
mean = meanAccumulator / (float) n;
disp = dispAccumulator / (float) n;
}
qDebug()<<mean<<" "<<disp;
int diff = (int)(mean - next);
if (diff*diff < disp || n<MAX_FRAME_AMOUNT/2 || force_learn)
{
int newMeanValue = meanAccumulator + next;
uint newDispValue = dispAccumulator + diff*diff;
if (n == MAX_FRAME_AMOUNT)
{
// normalizing accumulators
newMeanValue = (int) ((float)(newMeanValue) / (n + 1) * n);
newDispValue = (uint) ((float)(newDispValue) / (n + 1) * n); // TODO: is this a correct normalization?
}
else
{
n++;
}
meanAccumulator = newMeanValue;
dispAccumulator = newDispValue;
return false;
}
return true;
}
void Accumulator::reset()
{
meanAccumulator = 0;
n = 0;
dispAccumulator = 0;
force_learn = false;
frame_counter = 0;
}
float Accumulator::getDisp()
{
if (n == 0) return 0.0F;
return dispAccumulator / (float) n;
}