LightLib
PROS library for VEX V5: EKF/MCL localization, RAMSETE path following, high-level chassis API
Loading...
Searching...
No Matches
medianFilter.hpp
Go to the documentation of this file.
1/*
2 * Uses the median filter algorithm from N. Wirth’s book, implementation by N. Devillard.
3 *
4 * This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this
6 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
7 */
8#pragma once
9
11#include <algorithm>
12#include <array>
13#include <cstddef>
14
15namespace okapi {
21template <std::size_t n> class MedianFilter : public Filter {
22 public:
23 MedianFilter() : middleIndex((((n)&1) ? ((n) / 2) : (((n) / 2) - 1))) {
24 }
25
32 double filter(const double ireading) override {
33 data[index++] = ireading;
34 if (index >= n) {
35 index = 0;
36 }
37
39 return output;
40 }
41
47 double getOutput() const override {
48 return output;
49 }
50
51 protected:
52 std::array<double, n> data{0};
53 std::size_t index = 0;
54 double output = 0;
55 const size_t middleIndex;
56
60 double kth_smallset() {
61 std::array<double, n> dataCopy = data;
62 size_t j, l, m;
63 l = 0;
64 m = n - 1;
65
66 while (l < m) {
67 double x = dataCopy[middleIndex];
68 size_t i = l;
69 j = m;
70 do {
71 while (dataCopy[i] < x) {
72 i++;
73 }
74 while (x < dataCopy[j]) {
75 j--;
76 }
77 if (i <= j) {
78 const double t = dataCopy[i];
79 dataCopy[i] = dataCopy[j];
80 dataCopy[j] = t;
81 i++;
82 j--;
83 }
84 } while (i <= j);
85 if (j < middleIndex)
86 l = i;
87 if (middleIndex < i)
88 m = j;
89 }
90
91 return dataCopy[middleIndex];
92 }
93};
94} // namespace okapi
A filter which returns the median value of list of values.
const size_t middleIndex
double filter(const double ireading) override
Filters a value, like a sensor reading.
std::array< double, n > data
double getOutput() const override
Returns the previous output from filter.
double kth_smallset()
Algorithm from N.
@ x
Roll Axis.