Arduino Libraries
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
ema_module.hpp
Go to the documentation of this file.
1 // Author: Mario S. Könz <mskoenz@gmx.net>
2 // Date: 23.07.2013 05:01:24 EDT
3 // File: ema_module.hpp
4 
5 /* This program is free software. It comes without any warranty, to
6  * the extent permitted by applicable law. You can redistribute it
7  * and/or modify it under the terms of the Do What The Fuck You Want
8  * To Public License, Version 2, as published by Sam Hocevar. See
9  * http://www.wtfpl.net/ or COPYING for more details. */
10 
11 #ifndef __EMA_MODULE_HEADER
12 #define __EMA_MODULE_HEADER
13 
14 //exponentially moving average
15 
16 #ifndef Arduino_h
17  #include <cmath>
18 #endif
19 
20 #include "module.hpp"
21 #include "../../util/mean_trait.hpp"
22 
23 namespace ustd {
24  namespace detail {
25  template<int N>
26  struct requirement<tag::ema<N> > {
27  typedef util::list_end type;
28  };
29  }//end namespace detail
30 
31  template<typename T, typename _base, int N>
32  class module<T, tag::ema<N>, _base>: public _base {
33  typedef _base base;
34  typedef typename util::mean_trait<T>::type mean_type;
35  public:
36  //------------------- ctor -------------------
37  module(): base(), sum_() { //just heuristics for now
38  }
39  //------------------- ops -------------------
40  void operator<<(T const & in) {
41  base::operator<<(in);
42  sum_ = ((N-1) * (sum_)) / N + in;
43  }
44  //------------------- fct -------------------
45  T ema() const {
46  return ema_impl<mean_type, T>();
47  }
48  void clear() {
49  base::clear();
50  sum_ = mean_type();
51  }
52  private:
53  //------------------- impl -------------------
54  template<typename M, typename U>
55  typename enable_if<is_same<M, U>::value, M>::type ema_impl() const {
56  return sum_ / N;
57  }
58  template<typename M, typename U>
59  typename enable_if<not is_same<M, U>::value, M>::type ema_impl() const {
60  return round(sum_ / N); //round for integer types
61  }
62  private:
63  mean_type sum_;
64  };
65 }//end namespace ustd
66 
67 #endif //__EMA_MODULE_HEADER
S & operator<<(S &os, oss_class< D, max_buf > const &arg)
Definition: serializer.hpp:158
void clear()
Definition: ema_module.hpp:48
T ema() const
Definition: ema_module.hpp:45
Definition: type_traits.hpp:228
module()
Definition: ema_module.hpp:37
Definition: module.hpp:33
ustd::conditional< detail::use_double_identifier< T >::value, double, T >::type type
Definition: mean_trait.hpp:44
void operator<<(T const &in)
Definition: ema_module.hpp:40
Definition: meta_list.hpp:22