-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMedianFilter.h
More file actions
72 lines (60 loc) · 3.61 KB
/
MedianFilter.h
File metadata and controls
72 lines (60 loc) · 3.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
/* MedianFilter.h - Median Filter
Copyright 2012, Adam Cooper */
/* ***************************** LICENCE ************************************
* This file is part of LibSimpleFilters Arduino library. *
* (each component of the library is licenced separately) *
* *
* MedianFilter is free software: you can redistribute it and/or modify *
* it under the terms of the GNU Lesser General Public License as published *
* by the Free Software Foundation, either version 3 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU Lesser General Public License for more details. *
* *
* You should have received a copy of the GNU Lesser General Public License *
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
****************************************************************************/
#ifndef MEDIAN_FILTER_H
#define MEDIAN_FILTER_H
#include "Arduino.h"
#define MEDIAN_MAX_LEN 25 //!< Maximum number of samples, as specified by the length parameter in the constructor. Absolute max is 256.
/*! This filter calculates the median of a set number of previous values.
This is able to suppress sudden impules/glitches while still allowing step changes to be preserved, although delayed.
Longer buffers will reject longer outlier signals.\n
See http://en.wikipedia.org/wiki/Median_filter (also http://medim.sth.kth.se/6l2872/F/F7-1.pdf)\n
To use: create an instance of the filter and submit new readings using update().
Readings should be sampled at regular (i.e. equal) time intervals.
@brief A finite length median filter. */
class MedianFilter{
public:
/*! Create the filter with specified parameters.
@param length The number of samples to take into account with a maximum specified by MEDIAN_MAX_LEN. This should be odd; even values will be siltently increased by 1.
@param burnIn Whether to initialise the filter on first reading such that the output = the input after that reading.
Otherwise the output is as if the input had just been turned on with previous zero readings. */
MedianFilter(int length, boolean burnIn);
/*! Submit a new measurement to the filter.\n
Readings should be sampled at regular (i.e. equal) time intervals.
@param newVal The new value.
@returns The filter output. */
int update(int newVal);
/*! Get the previously-submitted values. This is a "circular buffer" so the current pointer must be obtained using getLastIndex()
@param[out] values A buffer of length specified by the length parameter in the constructor. */
void getHistory(int values[]);
/*! What was the last index used in the returned buffer from getHistory()?
@returns The index to the value submitted by the last update() */
int getLastIndex();
private:
//constructor parameters
boolean _burnIn;
int _length;
boolean _updated;//has the filter received any data yet
int _values[MEDIAN_MAX_LEN];
byte _sortList[MEDIAN_MAX_LEN]; //sorted order of entries in _values
int _index;// pointer into _values[]
//common code used by both update() methods
void accumulate(int newVal);
};
#endif