-
Notifications
You must be signed in to change notification settings - Fork 12
/
GaussianAverage.h
110 lines (82 loc) · 2.1 KB
/
GaussianAverage.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
/*
GaussianAverage.h - A moving average object with Gaussians
This class was made to simplify complex filters.
Applying moving average with Gaussians is now just a few lines.
For instructions, go to https://github.com/ivanseidel/Gaussian
Created by Ivan Seidel Gomes, July, 2013.
Released into the public domain.
*/
#ifndef GaussianAverage_h
#define GaussianAverage_h
/*
!!!!!!!!!!!!!!! ATENTION !!!!!!!!!!!!!!!!!!!
This class REQUIRES LikedList Library.
You also need to INCLUDE LinkedList on your code.
For more information go to:
https://github.com/ivanseidel/LinkedList
*/
#include "LinkedList.h"
class GaussianAverage: public Gaussian
{
protected:
// Linked list that will hold past Gaussians
LinkedList<Gaussian> *gaussians;
// Number of gaussians to keep record of
int n;
// Flag that indicates if a process MUST be done
bool isCached;
// Only a temporary helper, to minimize memory usage
Gaussian avg;
public:
/*
Creates a new Gaussian Moving Average that
keeps track of _n last Gaussians
*/
GaussianAverage(int _n = 4): Gaussian(){
gaussians = new LinkedList<Gaussian>();
n = _n;
isCached = false;
}
/*
Adds a new Gaussian to the FIFO
*/
virtual void add(Gaussian _gaus){
// Notify that we have changed things
isCached = false;
while(gaussians->size() >= n)
gaussians->shift();
gaussians->add(_gaus);
}
/*
Adds a new Gaussian to the FIFO with the overloaded '+='
*/
virtual void operator+=(Gaussian _gaus){
add(_gaus);
}
/*
Adds a new Gaussian to the FIFO with the overloaded '+='
*/
virtual void operator+=(double _mean){
add(Gaussian(_mean));
}
/*
Return the average of all current Gaussians
*/
virtual Gaussian process(){
// Checks if the process is already cached
if(!isCached){
avg = gaussians->get(0);
for(int i = 1; i < gaussians->size(); i++){
Gaussian tmp = gaussians->get(i);
// This will enable quadratic importance for newer samples
// tmp.variance = tmp.variance / (i+1.0);
avg = avg + tmp;
}
mean = avg.mean;
variance = avg.variance;
isCached = true;
}
return *this;
}
};
#endif