annotate toolboxes/FullBNT-1.0.7/Kalman/kalman_update.m @ 0:cc4b1211e677 tip

initial commit to HG from Changeset: 646 (e263d8a21543) added further path and more save "camirversion.m"
author Daniel Wolff
date Fri, 19 Aug 2016 13:07:06 +0200
parents
children
rev   line source
Daniel@0 1 function [xnew, Vnew, loglik, VVnew] = kalman_update(A, C, Q, R, y, x, V, varargin)
Daniel@0 2 % KALMAN_UPDATE Do a one step update of the Kalman filter
Daniel@0 3 % [xnew, Vnew, loglik] = kalman_update(A, C, Q, R, y, x, V, ...)
Daniel@0 4 %
Daniel@0 5 % INPUTS:
Daniel@0 6 % A - the system matrix
Daniel@0 7 % C - the observation matrix
Daniel@0 8 % Q - the system covariance
Daniel@0 9 % R - the observation covariance
Daniel@0 10 % y(:) - the observation at time t
Daniel@0 11 % x(:) - E[X | y(:, 1:t-1)] prior mean
Daniel@0 12 % V(:,:) - Cov[X | y(:, 1:t-1)] prior covariance
Daniel@0 13 %
Daniel@0 14 % OPTIONAL INPUTS (string/value pairs [default in brackets])
Daniel@0 15 % 'initial' - 1 means x and V are taken as initial conditions (so A and Q are ignored) [0]
Daniel@0 16 % 'u' - u(:) the control signal at time t [ [] ]
Daniel@0 17 % 'B' - the input regression matrix
Daniel@0 18 %
Daniel@0 19 % OUTPUTS (where X is the hidden state being estimated)
Daniel@0 20 % xnew(:) = E[ X | y(:, 1:t) ]
Daniel@0 21 % Vnew(:,:) = Var[ X(t) | y(:, 1:t) ]
Daniel@0 22 % VVnew(:,:) = Cov[ X(t), X(t-1) | y(:, 1:t) ]
Daniel@0 23 % loglik = log P(y(:,t) | y(:,1:t-1)) log-likelihood of innovatio
Daniel@0 24
Daniel@0 25 % set default params
Daniel@0 26 u = [];
Daniel@0 27 B = [];
Daniel@0 28 initial = 0;
Daniel@0 29
Daniel@0 30 args = varargin;
Daniel@0 31 for i=1:2:length(args)
Daniel@0 32 switch args{i}
Daniel@0 33 case 'u', u = args{i+1};
Daniel@0 34 case 'B', B = args{i+1};
Daniel@0 35 case 'initial', initial = args{i+1};
Daniel@0 36 otherwise, error(['unrecognized argument ' args{i}])
Daniel@0 37 end
Daniel@0 38 end
Daniel@0 39
Daniel@0 40 % xpred(:) = E[X_t+1 | y(:, 1:t)]
Daniel@0 41 % Vpred(:,:) = Cov[X_t+1 | y(:, 1:t)]
Daniel@0 42
Daniel@0 43 if initial
Daniel@0 44 if isempty(u)
Daniel@0 45 xpred = x;
Daniel@0 46 else
Daniel@0 47 xpred = x + B*u;
Daniel@0 48 end
Daniel@0 49 Vpred = V;
Daniel@0 50 else
Daniel@0 51 if isempty(u)
Daniel@0 52 xpred = A*x;
Daniel@0 53 else
Daniel@0 54 xpred = A*x + B*u;
Daniel@0 55 end
Daniel@0 56 Vpred = A*V*A' + Q;
Daniel@0 57 end
Daniel@0 58
Daniel@0 59 e = y - C*xpred; % error (innovation)
Daniel@0 60 n = length(e);
Daniel@0 61 ss = length(A);
Daniel@0 62 S = C*Vpred*C' + R;
Daniel@0 63 Sinv = inv(S);
Daniel@0 64 ss = length(V);
Daniel@0 65 loglik = gaussian_prob(e, zeros(1,length(e)), S, 1);
Daniel@0 66 K = Vpred*C'*Sinv; % Kalman gain matrix
Daniel@0 67 % If there is no observation vector, set K = zeros(ss).
Daniel@0 68 xnew = xpred + K*e;
Daniel@0 69 Vnew = (eye(ss) - K*C)*Vpred;
Daniel@0 70 VVnew = (eye(ss) - K*C)*A*V;
Daniel@0 71