latest version v1.9 - last update 10 Apr 2010 |
00001 /* 00002 * Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006 00003 * Lehrstuhl fuer Technische Informatik, RWTH-Aachen, Germany 00004 * 00005 * This file is part of the LTI-Computer Vision Library (LTI-Lib) 00006 * 00007 * The LTI-Lib is free software; you can redistribute it and/or 00008 * modify it under the terms of the GNU Lesser General Public License (LGPL) 00009 * as published by the Free Software Foundation; either version 2.1 of 00010 * the License, or (at your option) any later version. 00011 * 00012 * The LTI-Lib is distributed in the hope that it will be 00013 * useful, but WITHOUT ANY WARRANTY; without even the implied warranty 00014 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00015 * GNU Lesser General Public License for more details. 00016 * 00017 * You should have received a copy of the GNU Lesser General Public 00018 * License along with the LTI-Lib; see the file LICENSE. If 00019 * not, write to the Free Software Foundation, Inc., 59 Temple Place - 00020 * Suite 330, Boston, MA 02111-1307, USA. 00021 */ 00022 00023 00024 /*---------------------------------------------------------------- 00025 * project ....: LTI Digital Image/Signal Processing Library 00026 * file .......: ltiKalmanFilter.h 00027 * authors ....: Joerg Zieren 00028 * organization: LTI, RWTH Aachen 00029 * creation ...: 26.3.2001 00030 * revisions ..: $Id: ltiKalmanFilter.h,v 1.8 2006/02/08 11:20:42 ltilib Exp $ 00031 */ 00032 00033 #ifndef _LTI_KALMAN_FILTER_H_ 00034 #define _LTI_KALMAN_FILTER_H_ 00035 00036 #include "ltiIoHandler.h" 00037 #include "ltiVector.h" 00038 #include "ltiMatrix.h" 00039 #include "ltiMatrixInversion.h" 00040 #include "ltiObject.h" 00041 00042 namespace lti { 00043 /** 00044 * A discrete linear Kalman %filter implementation. Kalman filters 00045 * can be used to estimate the state of a dynamic system from noisy 00046 * measurement data. 00047 * 00048 * This implementation uses (at some places) the nomenclature from 00049 * the following tech report: 00050 * 00051 * Theory: "An Introduction to the Kalmen Filter" by Greg Welch and 00052 * Gary Bishop, Department of Computer Science, University of North 00053 * Carolina at Chapel Hill. 00054 * http://www.cs.unc.edu/~welch/kalman/kalmanIntro.html */ 00055 class kalmanFilter : public functor { 00056 public: 00057 00058 /** 00059 * The parameters for the class kalmanFilter. 00060 */ 00061 class parameters : public functor::parameters { 00062 public: 00063 /** 00064 * Default constructor. All initial values (vectors and matrices) 00065 * are initialized empty, i.e. they have 0 or 0x0 dimensions. 00066 */ 00067 parameters(); 00068 00069 /** 00070 * This constructor creates all initial values (vectors and 00071 * matrices) with matching dimensions, containing only zeros. 00072 */ 00073 parameters(int systemDimension, 00074 int measurementDimension, 00075 int controlDimension); 00076 00077 /** 00078 * copy constructor 00079 * @param other the parameters object to be copied 00080 */ 00081 parameters(const parameters& other); 00082 00083 /** 00084 * destructor 00085 */ 00086 ~parameters(); 00087 00088 /** 00089 * returns name of this type 00090 */ 00091 const char* getTypeName() const; 00092 00093 /** 00094 * copy the contents of a parameters object 00095 * @param other the parameters object to be copied 00096 * @return a reference to this parameters object 00097 */ 00098 parameters& copy(const parameters& other); 00099 00100 /** 00101 * copy the contents of a parameters object 00102 * @param other the parameters object to be copied 00103 * @return a reference to this parameters object 00104 */ 00105 parameters& operator=(const parameters& other); 00106 00107 /** 00108 * returns a pointer to a clone of the parameters 00109 */ 00110 virtual functor::parameters* clone() const; 00111 00112 /** 00113 * Check the consistency of the parameters. The parameters are 00114 * consistent if certain dimensions match each other, e.g. the 00115 * dynamicsMatrix must be square and must have as many rows as 00116 * the system state. 00117 * Additional consistency checks are performed for each 00118 * measurement and control vector. 00119 */ 00120 bool consistent() const; 00121 00122 /** 00123 * write the parameters in the given ioHandler 00124 * @param handler the ioHandler to be used 00125 * @param complete if true (the default) the enclosing begin/end will 00126 * be also written, otherwise only the data block will be written. 00127 * @return true if write was successful 00128 */ 00129 virtual bool read(ioHandler& handler,const bool complete=true); 00130 00131 /** 00132 * write the parameters in the given ioHandler 00133 * @param handler the ioHandler to be used 00134 * @param complete if true (the default) the enclosing begin/end will 00135 * be also written, otherwise only the data block will be written. 00136 * @return true if write was successful 00137 */ 00138 virtual bool write(ioHandler& handler, const bool complete=true) const; 00139 00140 # ifdef _LTI_MSC_6 00141 /** 00142 * this function is required by MSVC only, as a workaround for a 00143 * very awful bug, which exists since MSVC V.4.0, and still by 00144 * V.6.0 with all bugfixes (so called "service packs") remains 00145 * there... This method is public due to another bug!, so please 00146 * NEVER EVER call this method directly 00147 */ 00148 virtual bool readMS(ioHandler& handler,const bool complete=true); 00149 00150 /** 00151 * this function is required by MSVC only, as a workaround for a 00152 * very awful bug, which exists since MSVC V.4.0, and still by 00153 * V.6.0 with all bugfixes (so called "service packs") remains 00154 * there... This method is public due to another bug!, so please 00155 * NEVER EVER call this method directly 00156 */ 00157 virtual bool writeMS(ioHandler& handler,const bool complete=true) const; 00158 # endif 00159 00160 // -------------- 00161 // the parameters 00162 // -------------- 00163 00164 /** 00165 * Initial system state (x). This value is used either for 00166 * \f$\hat{x}^{-}\f$ or \f$\hat{x}\f$, depending on whether the 00167 * first update is a measurement or a time update (respectively). 00168 */ 00169 vector<float> initialSystemState; 00170 00171 /** 00172 * matrix that describes system dynamics (A) 00173 */ 00174 matrix<float> dynamicsMatrix; 00175 00176 /** 00177 * matrix that describes influence of control input on 00178 * system state (rarely used at LTI) (B) 00179 */ 00180 matrix<float> controlMatrix; 00181 00182 /** 00183 * matrix that relates the system state to the 00184 * (expected) measurement (H) 00185 */ 00186 matrix<float> measurementMatrix; 00187 00188 /** 00189 * measurement %noise covariance (R) 00190 */ 00191 matrix<float> measurementNoiseCovariance; 00192 00193 /** 00194 * process %noise covariance (Q) 00195 */ 00196 matrix<float> processNoiseCovariance; 00197 00198 /** 00199 * Initial estimate error covariance (P). This value is used either for 00200 * \f$P^{-}\f$ or \f$P\f$, depending on whether the 00201 * first update is a measurement or a time update (respectively). 00202 */ 00203 matrix<float> initialErrorCovariance; 00204 }; 00205 00206 /** 00207 * Default constructor. 00208 */ 00209 kalmanFilter(); 00210 00211 /** 00212 * copy constructor 00213 * @param other the object to be copied 00214 */ 00215 kalmanFilter(const kalmanFilter& other); 00216 00217 /** 00218 * destructor 00219 */ 00220 virtual ~kalmanFilter(); 00221 00222 /** 00223 * returns the name of this type ("kalmanFilter") 00224 */ 00225 virtual const char* getTypeName() const; 00226 00227 /** 00228 * Perform a measurement update using the given measurement, followed 00229 * by a time update with a control vector of zero. This call results 00230 * in a new prediction available via getEstimate. 00231 */ 00232 bool apply(const lti::vector<float>& measurement); 00233 00234 /** 00235 * Perform a measurement update using the given measurement, followed 00236 * by a time update with the given control. This call results 00237 * in a new (a priori) prediction available via getEstimate. 00238 */ 00239 bool apply(const lti::vector<float>& measurement, 00240 const lti::vector<float>& control); 00241 00242 /** 00243 * Perform a time update without control input. This call 00244 * results in a new a priori prediction available via 00245 * getEstimate. 00246 */ 00247 bool apply(); 00248 00249 /** 00250 * Perform a measurement update using the given measurement. This call 00251 * results in a new a posteriori prediction available via 00252 * getEstimateAposteriori. This method is identical to 00253 * apply(const lti::vector<float>&). 00254 */ 00255 bool applyMeasurementUpdate(const lti::vector<float>& measurement); 00256 00257 /** 00258 * Perform a time update using the given control. This call 00259 * results in a new a priori prediction available via 00260 * getEstimate. 00261 */ 00262 bool applyTimeUpdate(const lti::vector<float>& control); 00263 00264 /** 00265 * Perform a time update without control input. This call 00266 * results in a new a priori prediction available via 00267 * getEstimate. 00268 */ 00269 bool applyTimeUpdate(); 00270 00271 /** 00272 * copy data of "other" functor. 00273 * @param other the functor to be copied 00274 * @return a reference to this functor object 00275 */ 00276 kalmanFilter& copy(const kalmanFilter& other); 00277 00278 /** 00279 * returns a pointer to a clone of this functor. 00280 */ 00281 virtual functor* clone() const; 00282 00283 /** 00284 * returns used parameters 00285 */ 00286 const parameters& getParameters() const; 00287 00288 /** 00289 * Get the (a priori) system state estimate (\f$\hat{x}^{-}\f$). 00290 * This is the prediction one would normally be interested in. 00291 */ 00292 const vector<float>& getEstimate() const; 00293 00294 /** 00295 * Get the a posteriori system state estimate (\f$\hat{x}\f$). 00296 * This value is primarily of internal use and not of interest 00297 * to the user. 00298 */ 00299 const vector<float>& getEstimateAposteriori() const; 00300 00301 /** 00302 * Get the a priori error covariance (\f$P^{-}\f$). 00303 */ 00304 const matrix<float>& getErrorCovarianceApriori() const; 00305 00306 /** 00307 * Get the a posteriori error covariance (\f$P\f$). 00308 */ 00309 const matrix<float>& getErrorCovarianceAposteriori() const; 00310 00311 /** 00312 * Reset the filter state (\f$x\f$) and error covariance (\f$P\f$) 00313 * to the initial values stored in the parameters. 00314 */ 00315 void reset(); 00316 00317 protected: 00318 00319 // vectors and matrices that describe this Kalman filter 00320 00321 /** @name Internal State 00322 * These are the internal state members of the Kalman filter, which 00323 * are copied to by calling the copy() member 00324 */ 00325 //@{ 00326 00327 /** 00328 * system state prior to measurement update (\f$\hat{x}^{-}\f$) 00329 */ 00330 vector<float> systemStateApriori; 00331 00332 /** 00333 * system state after measurement update (\f$\hat{x}\f$) 00334 */ 00335 vector<float> systemStateAposteriori; 00336 00337 /** 00338 * matrix of current Kalman gain (K) 00339 */ 00340 matrix<float> kalmanGainMatrix; 00341 00342 /** 00343 * matrix of a priori estimate error covariance (\f$P^{-}\f$) 00344 */ 00345 matrix<float> errorCovarianceApriori; 00346 00347 /** 00348 * matrix of a posteriore estimate error covariance (P) 00349 */ 00350 matrix<float> errorCovarianceAposteriori; 00351 00352 //@} 00353 00354 /** @name Temporary 00355 */ 00356 //@{ 00357 /** Temporary matrices and vectors 00358 */ 00359 matrix<float> tempMatrix; 00360 matrix<float> tempMatrix2; 00361 vector<float> tempVector; 00362 vector<float> tempVector2; 00363 00364 /** 00365 * matrixInversion used to invert matrices 00366 */ 00367 matrixInversion<float> myMatrixInvertor; 00368 //@} 00369 00370 00371 /** 00372 * Perform time update (with control input). Rows of controlVector 00373 * must match columns of controlMatrix. 00374 */ 00375 bool timeUpdate(const vector<float>& controlVector); 00376 00377 /** 00378 * Perform time update (no control input) 00379 */ 00380 bool timeUpdate(); 00381 00382 /** 00383 * Perform measurement update. Rows of measurementVector must 00384 * match rows of measurementMatrix. 00385 */ 00386 bool measurementUpdate(const vector<float>& measurementVector); 00387 }; 00388 } 00389 00390 #endif