1€ filter for Arduino 1.0.0
Algorithm to filter noisy signals for high precision and responsiveness.
Loading...
Searching...
No Matches
1euroFilter.h
1/* -*- coding: utf-8 -*-
2 *
3 * 1euroFilter.h -
4 *
5 * Authors:
6 * Nicolas Roussel (nicolas.roussel@inria.fr)
7 * Géry Casiez https://gery.casiez.net
8 *
9 * Copyright 2019 Inria
10 *
11 * BSD License https://opensource.org/licenses/BSD-3-Clause
12 *
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions are met:
15 *
16 * 1. Redistributions of source code must retain the above copyright notice, this list of conditions
17 * and the following disclaimer.
18 *
19 * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions
20 * and the following disclaimer in the documentation and/or other materials provided with the distribution.
21 *
22 * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or
23 * promote products derived from this software without specific prior written permission.
24
25 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
26 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
27 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
29 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
31 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 *
33 */
34
35#if (ARDUINO >= 100)
36#include <Arduino.h>
37#else
38#include <WProgram.h>
39#endif
40
41#include <inttypes.h>
42
43#ifndef ONEEUROFILTER
44#define ONEEUROFILTER
45
46// -----------------------------------------------------------------
47// Utilities
48
49typedef float TimeStamp ; // in seconds
50
51static const TimeStamp UndefinedTime = -1.0 ;
52
53// -----------------------------------------------------------------
54
55#define DEFAULT_FREQUENCY 120 // [Hz] default frequency of your incoming noisy data
56#define DEFAULT_MINCUTOFF 1.0 // [Hz] needs to be tuned
57#define DEFAULT_BETA 0 // Needs to be tuned
58#define DEFAULT_DCUTOFF 1.0 // [Hz] should not be changed
59
61
62 float y, a, s ;
63 bool initialized ;
64
65 void setAlpha(float alpha);
66
67public:
68
69 LowPassFilter(float alpha, float initval=0.0) ;
70
71 float filter(float value) ;
72
73 float filterWithAlpha(float value, float alpha) ;
74
75 bool hasLastRawValue(void) ;
76
77 float lastRawValue(void) ;
78
79 float lastFilteredValue(void) ;
80
81} ;
82
83// -----------------------------------------------------------------
84
86
87 float freq ;
88 float mincutoff ;
89 float beta_ ;
90 float dcutoff ;
91 LowPassFilter *x ;
92 LowPassFilter *dx ;
93 TimeStamp lasttime ;
94
95 // implicitly implemented, not to be used
97 OneEuroFilter& operator=(const OneEuroFilter&);
98
99 float alpha(float cutoff) ;
100
101 void setDerivateCutoff(float dc) ;
102
103public:
104
105 explicit OneEuroFilter(float freq = DEFAULT_FREQUENCY,
106 float mincutoff=DEFAULT_MINCUTOFF, float beta_=DEFAULT_BETA, float dcutoff=DEFAULT_DCUTOFF) ;
107
115 void begin(float freq,
116 float mincutoff=1.0, float beta_=0.0, float dcutoff=1.0) ;
117
122 void setFrequency(float f) ;
123
128 void setMinCutoff(float mc) ;
129
134 void setBeta(float b) ;
135
142 float filter(float value, TimeStamp timestamp=UndefinedTime) ;
143
144 ~OneEuroFilter(void) ;
145
146} ;
147
148#endif
Definition: 1euroFilter.h:60
Definition: 1euroFilter.h:85
void setMinCutoff(float mc)
Sets the filter min cutoff frequency.
void begin(float freq, float mincutoff=1.0, float beta_=0.0, float dcutoff=1.0)
Creates the filter and set its parameters.
void setBeta(float b)
Sets the Beta parameter.
void setFrequency(float f)
Sets the frequency of the signal.
float filter(float value, TimeStamp timestamp=UndefinedTime)
Filter the noisy signal.