ProtoTracer  1.0
Real-time 3D rendering and animation engine
Loading...
Searching...
No Matches
RunningAverageFilter.tpp
Go to the documentation of this file.
1#pragma once
2
3template<size_t memory>
4RunningAverageFilter<memory>::RunningAverageFilter() {
5 gain = 0.1f;
6 currentAmount = 0;
7}
8
9template<size_t memory>
10RunningAverageFilter<memory>::RunningAverageFilter(float gain) {
11 this->gain = gain;
12 currentAmount = 0;
13}
14
15template<size_t memory>
16void RunningAverageFilter<memory>::SetGain(float gain) {
17 this->gain = gain;
18}
19
20template<size_t memory>
21float RunningAverageFilter<memory>::Filter(float value) {
22 float sum = 0.0f;
23 float avg = 0.0f;
24 float gainInverse = (1.0f - gain);
25
26 if (currentAmount < memory) {
27 values[currentAmount++] = value;
28 } else {
29 for (uint8_t i = 0; i < memory - 1; i++) {
30 values[i] = values[i + 1];
31 }
32
33 values[memory - 1] = value;
34 }
35
36 for (uint8_t i = 0; i < currentAmount; i++) {
37 sum += values[i];
38 }
39
40 if (currentAmount > 0) {
41 avg = sum / currentAmount;
42 }
43
44 return (gain * value) + (gainInverse * avg);
45}