Open FFBoard
Open source force feedback firmware
FastAvg.h
Go to the documentation of this file.
1
/*
2
* FastAvg.h
3
*
4
* Created on: Sept 28, 2021
5
* Author: Vincent Manoukian + Yannick Richter
6
*/
7
8
#ifndef FAST_AVERAGE_H_
9
#define FAST_AVERAGE_H_
10
11
#include <
FFBoardMain.h
>
12
13
template
<
class
T,std::
size_t
LEN>
14
class
FastAvg
{
15
16
public
:
17
FastAvg
(){};
18
~FastAvg
(){};
19
20
__attribute__
((optimize(
"-Ofast"
)))
21
void addValue(T value) {
22
23
// Add the new value
24
samples
[
currentIndex
] = value;
25
sumOfSamples
+= value;
26
27
// remove the previous value
28
int
indexRemove = (
currentIndex
+ 1) % (LEN+1);
29
sumOfSamples
-=
samples
[indexRemove];
30
31
// prepare the next add
32
currentIndex
= indexRemove;
33
}
34
35
T
getAverage
() {
36
return
sumOfSamples
/ LEN;
37
}
38
39
T
process
(
float
value) {
40
addValue(value);
41
return
sumOfSamples
/ LEN;
42
}
43
44
void
clear
() {
45
memset(
samples
,0,LEN*
sizeof
(T));
46
sumOfSamples
= 0;
47
}
48
49
private
:
50
T
samples
[LEN+1] = {
static_cast<
T
>
(0)};
51
int
currentIndex
= 0;
52
T
sumOfSamples
= 0;
53
54
};
55
62
template
<
class
T>
63
class
FastMovingAverage
{
64
public
:
65
FastMovingAverage
(int32_t len = 0) :
fixedLen
(len > 0 ? len : INT32_MAX),
count
(0){};
66
~FastMovingAverage
(){};
67
68
void
clear
(){
69
curAvg
= 0;
70
count
=0;
71
}
75
T
getAndReset
(){
76
T t =
curAvg
;
77
clear
();
78
return
t;
79
}
80
84
T
getAverage
(){
85
return
curAvg
;
86
}
90
T
addValue
(T v){
91
if
(
count
<
fixedLen
)
92
count
++;
93
curAvg
+= (v -
curAvg
)/
count
;
94
return
curAvg
;
95
}
96
private
:
97
T
curAvg
= 0;
98
const
int32_t
fixedLen
;
99
int32_t
count
= 0;
100
};
101
102
#endif
FFBoardMain.h
FastAvg
Definition:
FastAvg.h:14
FastAvg::sumOfSamples
T sumOfSamples
Definition:
FastAvg.h:52
FastAvg::getAverage
T getAverage()
Definition:
FastAvg.h:35
FastAvg::samples
T samples[LEN+1]
Definition:
FastAvg.h:50
FastAvg::clear
void clear()
Definition:
FastAvg.h:44
FastAvg::__attribute__
__attribute__((optimize("-Ofast"))) void addValue(T value)
Definition:
FastAvg.h:20
FastAvg::currentIndex
int currentIndex
Definition:
FastAvg.h:51
FastAvg::~FastAvg
~FastAvg()
Definition:
FastAvg.h:18
FastAvg::process
T process(float value)
Definition:
FastAvg.h:39
FastAvg::FastAvg
FastAvg()
Definition:
FastAvg.h:17
FastMovingAverage
Definition:
FastAvg.h:63
FastMovingAverage::clear
void clear()
Definition:
FastAvg.h:68
FastMovingAverage::FastMovingAverage
FastMovingAverage(int32_t len=0)
Definition:
FastAvg.h:65
FastMovingAverage::fixedLen
const int32_t fixedLen
Definition:
FastAvg.h:98
FastMovingAverage::curAvg
T curAvg
Definition:
FastAvg.h:97
FastMovingAverage::~FastMovingAverage
~FastMovingAverage()
Definition:
FastAvg.h:66
FastMovingAverage::count
int32_t count
Definition:
FastAvg.h:99
FastMovingAverage::getAverage
T getAverage()
Definition:
FastAvg.h:84
FastMovingAverage::addValue
T addValue(T v)
Definition:
FastAvg.h:90
FastMovingAverage::getAndReset
T getAndReset()
Definition:
FastAvg.h:75
Firmware
FFBoard
Inc
FastAvg.h
Generated by
1.9.2