fork download
  1. struct exp_smoothing {
  2. float alpha; // smoothing parameter (1 = identity mapping)
  3. float last; // state
  4.  
  5. float operator()(float x) {
  6. return last += (x - last) * alpha;
  7. }
  8. };
  9.  
  10. template<class Filter, class Iterator>
  11. Filter apply_inplace(Filter flt, Iterator beg, Iterator end) {
  12. while (beg != end) {
  13. *beg = flt(*beg);
  14. ++beg;
  15. }
  16. return flt;
  17. }
  18.  
  19. #include <iostream>
  20. #include <vector>
  21.  
  22. int main() {
  23. std::vector<float> signal = {0,1,2,3,4,5,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6};
  24. exp_smoothing flt = {
  25. 0.5f, // alpha
  26. 0.0f // last
  27. };
  28. flt = apply_inplace(flt,signal.begin(),signal.end());
  29. for (float x : signal) {
  30. std::cout << x << '\n';
  31. }
  32. }
  33.  
Success #stdin #stdout 0s 3428KB
stdin
Standard input is empty
stdout
0
0.5
1.25
2.125
3.0625
4.03125
5.01562
5.50781
5.75391
5.87695
5.93848
5.96924
5.98462
5.99231
5.99615
5.99808
5.99904
5.99952
5.99976
5.99988
5.99994
5.99997