Bruno Allaire-Lemay
/
APP1test
df
Fork of APP1 by
Diff: Utility.cpp
- Revision:
- 18:a21199781d20
- Parent:
- 15:b38d9d210e32
- Child:
- 21:a111be2582be
--- a/Utility.cpp Tue Jan 17 06:03:41 2017 +0000 +++ b/Utility.cpp Tue Jan 17 19:45:02 2017 +0000 @@ -1,6 +1,7 @@ #include "Utility.hpp" #include "mbed.h" - +#include <stdlib.h> +#include <list> #include <cmath> namespace utility @@ -56,4 +57,28 @@ wait(0.2); } } + + MovingAverageFilter::MovingAverageFilter(const int subSize): + subsetSize(subSize) + { + for(int i=0; i<subSize; i++) + { + subset.push_front(0); + } + } + + int MovingAverageFilter::calculate(int newValue) + { + subset.push_front(newValue); + subset.pop_back(); + int result = 0; + + for (std::list<int>::iterator it=subset.begin(); it != subset.end(); ++it) + { + result += *it; + } + result = result / subsetSize; + + return result; + } } \ No newline at end of file