rm_control
Loading...
Searching...
No Matches
one_euro_filter.h
Go to the documentation of this file.
1/*******************************************************************************
2 * BSD 3-Clause License
3 *
4 * Copyright (c) 2021, Qiayuan Liao
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are met:
9 *
10 * * Redistributions of source code must retain the above copyright notice, this
11 * list of conditions and the following disclaimer.
12 *
13 * * Redistributions in binary form must reproduce the above copyright notice,
14 * this list of conditions and the following disclaimer in the documentation
15 * and/or other materials provided with the distribution.
16 *
17 * * Neither the name of the copyright holder nor the names of its
18 * contributors may be used to endorse or promote products derived from
19 * this software without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE
25 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
28 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 *******************************************************************************/
33
34//
35// Created by liucong on 2020/12/5.
36//
37
38#pragma once
39
40#include <cmath>
41
42template <typename T>
43class OneEuroFilter
44{
45public:
46 OneEuroFilter(double _freq, T _mincutoff, T _beta, T _dcutoff)
47 : freq(_freq), mincutoff(_mincutoff), beta(_beta), dcutoff(_dcutoff)
48 {
49 firsttime = true;
50 x_prev = 0;
51 hatxprev = 0;
52 dhatxprev = 0;
53 filtered_val = 0;
54 };
55
56 ~OneEuroFilter() = default;
57
58 void input(T input_value)
59 {
60 T dx = 0;
61 if (!firsttime)
62 dx = (input_value - x_prev) * freq;
63 if (firsttime)
64 dhatxprev = dx;
65 T edx = alpha(dcutoff, freq) * dx + (1 - alpha(dcutoff, freq)) * dhatxprev;
66 dhatxprev = edx;
67 T cutoff = mincutoff + beta * std::abs(static_cast<double>(edx));
68
69 if (firsttime)
70 hatxprev = input_value;
71 filtered_val = alpha(cutoff, freq) * input_value + (1 - alpha(cutoff, freq)) * hatxprev;
72 hatxprev = filtered_val;
73 firsttime = false;
74 };
75
76 T alpha(T cutoff, double freq)
77 {
78 T tau = 1.0 / (2 * M_PI * cutoff);
79 T te = 1.0 / freq;
80 return 1.0 / (1.0 + tau / te);
81 }
82
84 {
85 return filtered_val;
86 };
87
88 void clear()
89 {
90 firsttime = true;
91 x_prev = 0;
92 hatxprev = 0;
93 dhatxprev = 0;
94 };
95
96private:
97 double freq;
98 bool firsttime;
99 T mincutoff, beta, dcutoff;
100 T x_prev, dhatxprev, hatxprev;
101 T filtered_val;
102};
Definition filters.h:227
void clear()
Definition one_euro_filter.h:88
T alpha(T cutoff, double freq)
Definition one_euro_filter.h:76
void input(T input_value)
Definition one_euro_filter.h:58
T output()
Definition one_euro_filter.h:83
~OneEuroFilter()=default
OneEuroFilter(double _freq, T _mincutoff, T _beta, T _dcutoff)
Definition one_euro_filter.h:46