My Project
Filter.h
Go to the documentation of this file.
1 /*
2  * This file is part of ALVAR, A Library for Virtual and Augmented Reality.
3  *
4  * Copyright 2007-2012 VTT Technical Research Centre of Finland
5  *
6  * Contact: VTT Augmented Reality Team <alvar.info@vtt.fi>
7  * <http://www.vtt.fi/multimedia/alvar.html>
8  *
9  * ALVAR is free software; you can redistribute it and/or modify it under the
10  * terms of the GNU Lesser General Public License as published by the Free
11  * Software Foundation; either version 2.1 of the License, or (at your option)
12  * any later version.
13  *
14  * This library is distributed in the hope that it will be useful, but WITHOUT
15  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
16  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
17  * for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public License
20  * along with ALVAR; if not, see
21  * <http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html>.
22  */
23 
24 #ifndef FILTER_H
25 #define FILTER_H
26 
33 #include "Alvar.h"
34 
35 #include <algorithm>
36 #include <cmath>
37 #include <deque>
38 #include <vector>
39 
40 namespace alvar {
41 
80 class ALVAR_EXPORT Filter
81 {
82 protected:
83  double value;
84 
85 public:
87  Filter();
89  double
90  get() const
91  {
92  return value;
93  }
95  operator double()
96  {
97  return get();
98  }
100  virtual double next(double y) = 0;
102  virtual void reset() = 0;
103 };
104 
116 class ALVAR_EXPORT FilterAverage : public Filter
117 {
118 protected:
119  unsigned int count;
120  unsigned int window_size;
121  std::deque<double> buffer;
122  void push_to_buffer(double y);
123 
124 public:
125  FilterAverage(int size = 3)
126  {
127  setWindowSize(size);
128  }
129  void
130  setWindowSize(int size)
131  {
132  window_size = size;
133  count = 0;
134  }
135  int
136  getWindowSize()
137  {
138  return window_size;
139  }
140  int
141  getCurrentSize()
142  {
143  return (int)buffer.size();
144  }
145  double
146  operator=(double _value)
147  {
148  return next(_value);
149  }
150  virtual double next(double y);
151  virtual void reset();
152  double deviation() const;
153 };
154 
165 class ALVAR_EXPORT FilterMedian : public FilterAverage
166 {
167  std::vector<double> sort_buffer;
168 
169 public:
170  FilterMedian(int size = 3)
171  {
172  setWindowSize(size);
173  }
174  void
175  setWindowSize(int size)
176  {
177  FilterAverage::setWindowSize(size);
178  sort_buffer.resize(size);
179  }
180  double
181  operator=(double _value)
182  {
183  return next(_value);
184  }
185  virtual double next(double y);
186 };
187 
202 class ALVAR_EXPORT FilterRunningAverage : public Filter
203 {
204 protected:
205  double alpha;
206  bool breset;
207 
208 public:
209  FilterRunningAverage(double _alpha = 0.5)
210  {
211  breset = true;
212  setAlpha(_alpha);
213  }
214  void
215  setAlpha(double _alpha)
216  {
217  alpha = std::max(std::min(_alpha, 1.0), 0.0);
218  }
219  double
220  getAlpha()
221  {
222  return alpha;
223  }
224  double
225  operator=(double _value)
226  {
227  return next(_value);
228  }
229  virtual double next(double y);
230  virtual void reset();
231 };
232 
249 {
250 protected:
251  double gamma;
252  double slope;
253 
254 public:
255  FilterDoubleExponentialSmoothing(double _alpha = 0.5, double _gamma = 1.0)
256  : FilterRunningAverage(_alpha)
257  {
258  setGamma(_gamma);
259  }
260  void
261  setGamma(double _gamma)
262  {
263  gamma = std::max(std::min(_gamma, 1.0), 0.0);
264  }
265  double
266  getGamma()
267  {
268  return gamma;
269  }
270  double
271  operator=(double _value)
272  {
273  return next(_value);
274  }
275  virtual double next(double y);
276 };
277 
282 template <class F>
283 class ALVAR_EXPORT FilterArray
284 {
285 protected:
286  double * tmp;
287  std::vector<F> arr;
288 
289 public:
290  FilterArray(int size)
291  {
292  tmp = NULL;
293  SetSize(size);
294  }
295  ~FilterArray()
296  {
297  delete[] tmp;
298  }
299  size_t
300  GetSize()
301  {
302  return arr.size();
303  }
304  void
305  SetSize(size_t size)
306  {
307  if (tmp)
308  delete[] tmp;
309  tmp = new double[size];
310  arr.resize(size);
311  }
312  F &
313  operator[](size_t i)
314  {
315  return arr[i];
316  }
317  const double *
318  as_double_array(size_t start_i = 0)
319  {
320  for (size_t i = 0; i < arr.size(); i++) {
321  tmp[i] = arr[i];
322  }
323  return &(tmp[start_i]);
324  }
325 };
326 
327 } // namespace alvar
328 
329 #endif
This file defines library export definitions, version numbers and build information.
Class for handling an array of filtered values.
Definition: Filter.h:284
FilterAverage provides an average filter
Definition: Filter.h:117
virtual void reset()
Reset the filter state.
virtual double next(double y)
Update the value. All inherited classes need to update value in next().
FilterDoubleExponentialSmoothing provides an weighted running average filter
Definition: Filter.h:249
virtual double next(double y)
Update the value. All inherited classes need to update value in next().
Filter is pure virtual class describing the basic virtual interface for all filters
Definition: Filter.h:81
virtual void reset()=0
Reset the filter state.
virtual double next(double y)=0
Update the value. All inherited classes need to update value in next().
Filter()
Constructor.
double get() const
Get the latest value.
Definition: Filter.h:90
FilterMedian provides an median filter
Definition: Filter.h:166
virtual double next(double y)
Update the value. All inherited classes need to update value in next().
FilterRunningAverage provides an weighted running average filter
Definition: Filter.h:203
virtual void reset()
Reset the filter state.
virtual double next(double y)
Update the value. All inherited classes need to update value in next().
Main ALVAR namespace.
Definition: Alvar.h:174