TUIO C++ Developer API
OneEuroFilter.h
1 /* reacTIVision tangible interaction framework
2  Copyright (C) 2005-2017 Martin Kaltenbrunner <martin@tuio.org>
3  Based on an example by Nicolas Roussel <nicolas.roussel@inria.fr>
4 
5  This program is free software; you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation; either version 2 of the License, or
8  (at your option) any later version.
9 
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License
16  along with this program; if not, write to the Free Software
17  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  */
19 
20 #ifndef ONEEUROFILTER
21 #define ONEEUROFILTER
22 
23 #include <stdexcept>
24 #include <cmath>
25 
26 typedef double TimeStamp; // in seconds
27 static const TimeStamp UndefinedTime = -1.0;
28 
29 namespace TUIO {
30 
31  class LowPassFilter {
32 
33  public:
34 
35  bool initialized;
36  double lastRawValue,lastResult;
37 
38  LowPassFilter(double initval=0.0) {
39 
40  lastRawValue = lastResult = initval;
41  initialized = false;
42  }
43 
44  double filter(double value, double alpha);
45  };
46 
47  // -----------------------------------------------------------------
48 
49  class OneEuroFilter {
50 
51  double freq;
52  double mincutoff;
53  double beta;
54  double dcutoff;
55  LowPassFilter *x;
56  LowPassFilter *dx;
57  TimeStamp lasttime;
58 
59  double alpha(double cutoff);
60 
61  public:
62 
63  OneEuroFilter(double f, double mc=1.0, double b=0.0, double dc=1.0) {
64 
65  if (f<=0) throw std::range_error("freq should be >0");
66  else freq = f;
67  if (mc<=0) throw std::range_error("mincutoff should be >0");
68  else mincutoff = mc;
69  if (b<=0) throw std::range_error("beta should be >0");
70  else beta = b;
71  if (dc<=0) throw std::range_error("dcutoff should be >0");
72  else dcutoff = dc;
73 
74  x = new LowPassFilter(alpha(mincutoff));
75  dx = new LowPassFilter(alpha(dcutoff));
76  lasttime = UndefinedTime;
77  }
78 
79  ~OneEuroFilter(void) {
80  delete x;
81  delete dx;
82  }
83 
84  double filter(double value, TimeStamp timestamp=UndefinedTime);
85 
86  };
87 
88 }
89 
90 #endif
Definition: OneEuroFilter.h:49
Definition: OneEuroFilter.h:31
Definition: FlashSender.h:166