Arduino Libraries
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
lowpass.hpp
Go to the documentation of this file.
1 // Author: Mario S. Könz <mskoenz@gmx.net>
2 // Date: 23.07.2013 05:09:59 EDT
3 // File: lowpass.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 __LOWPASS_HEADER
12 #define __LOWPASS_HEADER
13 
14 #include "realization.hpp"
15 #include "../../util/mean_trait.hpp"
16 
17 
18 namespace ustd {
19  template<typename T, unsigned N, typename _base = identity_filter<T> >
20  class lowpass_filter: public _base {
21  typedef typename util::mean_trait<T>::type mean_type;
22  typedef _base base;
23  public:
24  //------------------- ctors -------------------
25  lowpass_filter(): val_() {
26  }
27  lowpass_filter(lowpass_filter const & arg): val_(arg.val_) {
28  }
29  lowpass_filter(T const & arg): val_(arg) {
30  }
31  //------------------- ops -------------------
32  lowpass_filter & operator=(T const & in) {
33  base::operator=(in);
34  val_ = base::value();
35  return (*this);
36  }
37  lowpass_filter & operator<<(T const & in) {
38  base::operator<<(in);
39  val_ = ((N - 1) * val_ + base::value()) / N;
40  return (*this);
41  }
42  //------------------- converter -------------------
43  T value() const {
44  return lowpass_impl<mean_type, T>();
45  }
46  operator T() const {
47  return value();
48  }
49  private:
50  //------------------- impl -------------------
51  template<typename M, typename U>
52  typename enable_if<is_same<M, U>::value, M>::type lowpass_impl() const {
53  return val_;
54  }
55  template<typename M, typename U>
56  typename enable_if<not is_same<M, U>::value, M>::type lowpass_impl() const {
57  return val_ + 0.5; //round for integer types
58  }
59  private:
60  mean_type val_;
61  };
62  //------------------- spez for N == 1 -------------------
63  EMPTY_FILTER(lowpass, 1)
64  //------------------- realization for filter -------------------
65  template<typename T, typename _base, unsigned N>
66  struct realization<T, tag::lowpass<N>, _base> {
68  };
69 }//end namespace ustd
70 #endif //__LOWPASS_HEADER
S & operator<<(S &os, oss_class< D, max_buf > const &arg)
Definition: serializer.hpp:158
Definition: type_traits.hpp:228
lowpass_filter(lowpass_filter const &arg)
Definition: lowpass.hpp:27
lowpass_filter & operator=(T const &in)
Definition: lowpass.hpp:32
lowpass_filter(T const &arg)
Definition: lowpass.hpp:29
#define EMPTY_FILTER(NAME, VALUE)
Definition: realization.hpp:22
Definition: realization.hpp:19
ustd::conditional< detail::use_double_identifier< T >::value, double, T >::type type
Definition: mean_trait.hpp:44
lowpass_filter & operator<<(T const &in)
Definition: lowpass.hpp:37
Definition: lowpass.hpp:20
lowpass_filter()
Definition: lowpass.hpp:25
lowpass_filter< T, N, _base > type
Definition: lowpass.hpp:67
T value() const
Definition: lowpass.hpp:43