contrib/oul/ouel/kalman_filter.h
Go to the documentation of this file.
00001 // This is oul/ouel/kalman_filter.h
00002 // Copyright (c) 1999 Brendan McCane
00003 // University of Otago, Dunedin, New Zealand
00004 // Reproduction rights limited as described in the COPYRIGHT file.
00005 //----------------------------------------------------------------------
00006 #ifndef OTAGO_kalman_filter__h_INCLUDED
00007 #define OTAGO_kalman_filter__h_INCLUDED
00008 //:
00009 // \file
00010 // \brief A linear Kalman filter class
00011 //
00012 // Implementation of a linear Kalman filter. We want to estimate the
00013 // value of a time-varying signal from a measurement and predict the
00014 // next value of the signal.
00015 //
00016 // The relationship between signal and measurement is:
00017 // \verbatim
00018 // z(k)        =  H(k)    *    x(k)     +    v(k)
00019 // measurement    relation     signal        noise
00020 //                matrix
00021 // \endverbatim
00022 //
00023 // The relationship between signals at different times is:
00024 // \verbatim
00025 // x(k+1)      =  A(k)    *    x(k)   +   B(k)   *   u(k)   +   w(k)
00026 // signal         relation     signal     control    control    noise
00027 //                matrix                  matrix     signal
00028 // \endverbatim
00029 //
00030 // Typical usage:
00031 //
00032 // \status Completed
00033 // \author Brendan McCane
00034 //----------------------------------------------------------------------
00035 
00036 #include <vnl/vnl_matrix.h>
00037 #include <vcl_iosfwd.h>
00038 
00039 class KalmanFilter
00040 {
00041   // The number of dimensions in signal (ns)
00042   unsigned int num_signal_dimensions;
00043 
00044   // The number of dimensions in measurement (nm)
00045   unsigned int num_measurement_dimensions;
00046 
00047   // The number of dimensions in control input (nc)
00048   unsigned int num_control_dimensions;
00049 
00050   // The relation between one signal and the next (ns*ns)
00051   vnl_matrix<double> A;
00052 
00053   // The relation between measurement and signal (nm*ns)
00054   vnl_matrix<double> H;
00055 
00056   // The control input relation  (nc*ns)
00057   vnl_matrix<double> B;
00058 
00059   // The signal vector estimate - actually a column matrix (ns*1)
00060   vnl_matrix<double> x;
00061 
00062   // The signal vector prediction - actually a column matrix (ns*1)
00063   vnl_matrix<double> x_pred;
00064 
00065   // The measurement vector - again a column matrix (nm*1)
00066   vnl_matrix<double> z;
00067 
00068   // The error covariance matrix  (ns*ns)
00069   vnl_matrix<double> P;
00070 
00071   // The Kalman gain matrix (ns*nm)
00072   vnl_matrix<double> K;
00073 
00074  public:
00075   KalmanFilter(unsigned int ns, unsigned int nm, unsigned int nc,
00076                const vnl_matrix<double> &Ai,
00077                const vnl_matrix<double> &Hi,
00078                const vnl_matrix<double> &Bi,
00079                const vnl_matrix<double> &z_initial,
00080                const vnl_matrix<double> &x_initial,
00081                const vnl_matrix<double> &Pi);
00082 
00083   // this version does not have a control input
00084   KalmanFilter(unsigned int ns, unsigned int nm,
00085                const vnl_matrix<double> &Ai,
00086                const vnl_matrix<double> &Hi,
00087                const vnl_matrix<double> &z_initial,
00088                const vnl_matrix<double> &x_initial,
00089                const vnl_matrix<double> &Pi);
00090 
00091   // some utility functions
00092   void set_initial_input(const vnl_matrix<double> &x_initial)
00093     {x_pred = x_initial;}
00094 
00095   // zk is the next input measurement
00096   // Rk is the measurement  error covariance matrix
00097   //    which is calculated from the noise distribution
00098   void measurement_update(const vnl_matrix<double> &zk,
00099                           const vnl_matrix<double> &Rk);
00100 
00101   // Qk is the process error covariance matrix
00102   //    which is calculated from the noise distribution
00103   // The predicted value of x(k+1) is returned
00104   vnl_matrix<double> predict(const vnl_matrix<double> &Qk);
00105 
00106   // Qk is the process error covariance matrix
00107   //    which is calculated from the noise distribution
00108   // uk is the control input
00109   // The predicted value of x(k+1) is returned
00110   vnl_matrix<double> predict(const vnl_matrix<double> &Qk,
00111                              const vnl_matrix<double> &uk);
00112 
00113   // Do both measurement update and predict
00114   vnl_matrix<double> update_predict(const vnl_matrix<double> &zk,
00115                                     const vnl_matrix<double> &Rk,
00116                                     const vnl_matrix<double> &Qk);
00117 
00118   // accessor functions
00119   // return the current signal estimate
00120   inline vnl_matrix<double> estimate() const {return x;}
00121   // return the current signal prediction
00122   inline vnl_matrix<double> prediction() const {return x_pred;}
00123 
00124   friend vcl_ostream &operator<<(vcl_ostream &os, const KalmanFilter &kf);
00125 };
00126 
00127 #endif // OTAGO_kalman_filter__h_INCLUDED