-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathewmaT.cpp
More file actions
41 lines (35 loc) · 996 Bytes
/
ewmaT.cpp
File metadata and controls
41 lines (35 loc) · 996 Bytes
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
#include "ewmaT.h"
template <typename T>
ewmaT<T>::ewmaT(T alpha, unsigned int alphaScale) {
init(alpha, alphaScale, 0);
this->hasInitial = false;
}
template <typename T>
ewmaT<T>::ewmaT(T alpha, unsigned int alphaScale, T initialOutput) {
init(alpha, alphaScale, initialOutput);
}
template <typename T>
void ewmaT<T>::init(T alpha, unsigned int alphaScale, T initialOutput) {
this->alpha = alpha;
this->alphaScale = alphaScale;
this->outputScaled = initialOutput * alphaScale;
this->hasInitial = true;
}
template <typename T>
void ewmaT<T>::reset() {
this->hasInitial = false;
}
template <typename T>
T ewmaT<T>::filter(T input) {
if (hasInitial) {
outputScaled = alpha * input + (alphaScale - alpha) * outputScaled / alphaScale;
} else {
outputScaled = input * alphaScale;
hasInitial = true;
}
return output();
}
template <typename T>
T ewmaT<T>::output() {
return (outputScaled + alphaScale / 2) / alphaScale;
}