00001 /* 00002 * This file is part of ALVAR, A Library for Virtual and Augmented Reality. 00003 * 00004 * Copyright 2007-2012 VTT Technical Research Centre of Finland 00005 * 00006 * Contact: VTT Augmented Reality Team <alvar.info@vtt.fi> 00007 * <http://www.vtt.fi/multimedia/alvar.html> 00008 * 00009 * ALVAR is free software; you can redistribute it and/or modify it under the 00010 * terms of the GNU Lesser General Public License as published by the Free 00011 * Software Foundation; either version 2.1 of the License, or (at your option) 00012 * any later version. 00013 * 00014 * This library is distributed in the hope that it will be useful, but WITHOUT 00015 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 00016 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License 00017 * for more details. 00018 * 00019 * You should have received a copy of the GNU Lesser General Public License 00020 * along with ALVAR; if not, see 00021 * <http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html>. 00022 */ 00023 00024 #ifndef FILTER_H 00025 #define FILTER_H 00026 00033 #include "Alvar.h" 00034 #include <algorithm> 00035 #include <deque> 00036 #include <vector> 00037 #include <cmath> 00038 00039 namespace alvar { 00040 00079 class ALVAR_EXPORT Filter { 00080 protected: 00081 double value; 00082 public: 00084 Filter(); 00086 double get() const { return value; } 00088 operator double () { return get(); } 00090 virtual double next(double y) = 0; 00092 virtual void reset() = 0; 00093 }; 00094 00106 class ALVAR_EXPORT FilterAverage : public Filter { 00107 protected: 00108 unsigned int count; 00109 unsigned int window_size; 00110 std::deque<double> buffer; 00111 void push_to_buffer(double y); 00112 public: 00113 FilterAverage(int size=3) { setWindowSize(size); } 00114 void setWindowSize(int size) { window_size=size; count=0; } 00115 int getWindowSize() { return window_size; } 00116 int getCurrentSize() { return (int) buffer.size(); } 00117 double operator= (double _value) { return next(_value); } 00118 virtual double next(double y); 00119 virtual void reset(); 00120 double deviation() const; 00121 }; 00122 00133 class ALVAR_EXPORT FilterMedian : public FilterAverage { 00134 std::vector<double> sort_buffer; 00135 public: 00136 FilterMedian(int size=3) { setWindowSize(size); } 00137 void setWindowSize(int size) { 00138 FilterAverage::setWindowSize(size); 00139 sort_buffer.resize(size); 00140 } 00141 double operator= (double _value) { return next(_value); } 00142 virtual double next(double y); 00143 }; 00144 00159 class ALVAR_EXPORT FilterRunningAverage : public Filter { 00160 protected: 00161 double alpha; 00162 bool breset; 00163 public: 00164 FilterRunningAverage(double _alpha=0.5) { breset=true; setAlpha(_alpha); } 00165 void setAlpha(double _alpha) { alpha=std::max(std::min(_alpha,1.0),0.0); } 00166 double getAlpha() { return alpha; } 00167 double operator= (double _value) { return next(_value); } 00168 virtual double next(double y); 00169 virtual void reset(); 00170 }; 00171 00187 class ALVAR_EXPORT FilterDoubleExponentialSmoothing : public FilterRunningAverage { 00188 protected: 00189 double gamma; 00190 double slope; 00191 public: 00192 FilterDoubleExponentialSmoothing(double _alpha=0.5, double _gamma=1.0) : FilterRunningAverage(_alpha) { 00193 setGamma(_gamma); 00194 } 00195 void setGamma(double _gamma) { gamma=std::max(std::min(_gamma,1.0),0.0); } 00196 double getGamma() { return gamma; } 00197 double operator= (double _value) { return next(_value); } 00198 virtual double next(double y); 00199 }; 00200 00205 template <class F> 00206 class ALVAR_EXPORT FilterArray { 00207 protected: 00208 double *tmp; 00209 std::vector<F> arr; 00210 public: 00211 FilterArray(int size) { 00212 tmp = NULL; 00213 SetSize(size); 00214 } 00215 ~FilterArray() { 00216 delete [] tmp; 00217 } 00218 size_t GetSize() { 00219 return arr.size(); 00220 } 00221 void SetSize(size_t size) { 00222 if (tmp) delete [] tmp; 00223 tmp = new double[size]; 00224 arr.resize(size); 00225 } 00226 F &operator[](size_t i) { 00227 return arr[i]; 00228 } 00229 const double *as_double_array(size_t start_i=0) { 00230 for (size_t i=0; i<arr.size(); i++) { 00231 tmp[i] = arr[i]; 00232 } 00233 return &(tmp[start_i]); 00234 } 00235 }; 00236 00237 } // namespace alvar 00238 00239 #endif