1 ## Copyright (C) 2009, 2010 Lukas F. Reichlin
3 ## This file is part of LTI Syncope.
5 ## LTI Syncope 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 3 of the License, or
8 ## (at your option) any later version.
10 ## LTI Syncope 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.
15 ## You should have received a copy of the GNU General Public License
16 ## along with LTI Syncope. If not, see <http://www.gnu.org/licenses/>.
19 ## @deftypefn {Function File} {[@var{est}, @var{g}, @var{x}] =} kalman (@var{sys}, @var{q}, @var{r})
20 ## @deftypefnx {Function File} {[@var{est}, @var{g}, @var{x}] =} kalman (@var{sys}, @var{q}, @var{r}, @var{s})
21 ## @deftypefnx {Function File} {[@var{est}, @var{g}, @var{x}] =} kalman (@var{sys}, @var{q}, @var{r}, @var{[]}, @var{sensors}, @var{known})
22 ## @deftypefnx {Function File} {[@var{est}, @var{g}, @var{x}] =} kalman (@var{sys}, @var{q}, @var{r}, @var{s}, @var{sensors}, @var{known})
23 ## Design Kalman estimator for LTI systems.
28 ## Nominal plant model.
30 ## Covariance of white process noise.
32 ## Covariance of white measurement noise.
34 ## Optional cross term covariance. Default value is 0.
36 ## Indices of measured output signals y from @var{sys}. If omitted, all outputs are measured.
38 ## Indices of known input signals u (deterministic) to @var{sys}. All other inputs to @var{sys}
39 ## are assumed stochastic. If argument @var{known} is omitted, no inputs u are known.
45 ## State-space model of the Kalman estimator.
49 ## Solution of the Riccati equation.
52 ## @strong{Block Diagram}
56 ## +---------------------------->| |-------> y
57 ## | +-------+ + y | est | ^
58 ## u ----+--->| |----->(+)------>| |-------> x
59 ## | sys | ^ + +-------+
63 ## Q = cov (w, w') R = cov (v, v') S = cov (w, v')
67 ## @seealso{care, dare, estim, lqr}
70 ## Author: Lukas Reichlin <lukas.reichlin@gmail.com>
71 ## Created: November 2009
74 function [est, k, x] = kalman (sys, q, r, s = [], sensors = [], deterministic = [])
76 ## TODO: type "current" for discrete-time systems
78 if (nargin < 3 || nargin > 6 || ! isa (sys, "lti"))
82 [a, b, c, d, e] = dssdata (sys, []);
84 if (isempty (sensors))
85 sensors = 1 : rows (c);
88 stochastic = setdiff (1 : columns (b), deterministic);
92 h = d(sensors, stochastic);
98 rbar = r + h*q*h.'+ h*s + s.'*h.';
99 sbar = g * (q*h.' + s);
103 [x, l, k] = care (a.', c.', g*q*g.', rbar, sbar, e.');
105 [x, l, k] = dare (a.', c.', g*q*g.', rbar, sbar, e.');
110 est = estim (sys, k, sensors, deterministic);
115 %!shared m, m_exp, g, g_exp, x, x_exp
116 %! sys = ss (-2, 1, 1, 3);
117 %! [est, g, x] = kalman (sys, 1, 1, 1);
118 %! [a, b, c, d] = ssdata (est);
120 %! m_exp = [-2.25, 0.25; 1, 0; 1, 0];
123 %!assert (m, m_exp, 1e-2);
124 %!assert (g, g_exp, 1e-2);
125 %!assert (x, x_exp, 1e-2);