modm API documentation
median.hpp
1 /*
2  * Copyright (c) 2009-2010, Fabian Greif
3  * Copyright (c) 2010, Martin Rosekeit
4  * Copyright (c) 2012, Niklas Hauser
5  *
6  * This file is part of the modm project.
7  *
8  * This Source Code Form is subject to the terms of the Mozilla Public
9  * License, v. 2.0. If a copy of the MPL was not distributed with this
10  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
11  */
12 // ----------------------------------------------------------------------------
13 
14 #ifndef MODM_FILTER_MEDIAN_HPP
15 #define MODM_FILTER_MEDIAN_HPP
16 
17 #include <stdint.h>
18 
19 namespace modm
20 {
21  namespace filter
22  {
23  /**
24  * \brief Median filter
25  *
26  * Calculates the median of a input set. Useful for eliminating spikes
27  * from the input. Adds a group delay of N/2 ticks for the signal.
28  *
29  * Implementation are available for N = 3, 5, 7 and 9. To find
30  * the median the signal values will be partly sorted, but only as much
31  * as needed to find the median.
32  *
33  * \code
34  * // create a new filter for five samples
35  * modm::filter::Median<uint8_t, 5> filter;
36  *
37  * // append new signal values
38  * filter.append(10);
39  * filter.append(10);
40  * filter.append(20);
41  *
42  * // calculate the median
43  * filter.update();
44  *
45  * output = filter.getValue();
46  * \endcode
47  *
48  * \tparam T Input type
49  * \tparam N Number of samples
50  *
51  * \ingroup modm_math_filter
52  */
53  template<typename T, int N>
54  class Median
55  {
56  public:
57  /**
58  * \brief Constructor
59  *
60  * \param initialValue Value will be set for the complete
61  * input buffer.
62  */
63  Median(const T& initialValue = 0);
64 
65  /// Append new value
66  void
67  append(const T& input);
68 
69  /// calculate median
70  void
71  udpate();
72 
73  /// Get median value
74  const T
75  getValue() const;
76  };
77  }
78 }
79 
80 #include "median_impl.hpp"
81 
82 #endif // MODM_FILTER_MEDIAN_HPP
const T getValue() const
Get median value.
void udpate()
calculate median
void append(const T &input)
Append new value.
Median(const T &initialValue=0)
Constructor.
Median filter.
Definition: median.hpp:54