📄 dd2.m
字号:
function [xhat_data,Smat]=dd2(kalmfilex,kalmfiley,xbar,P0,q,r,u,y,timeidx,optpar)
% DD2
% This function implements the DD2-filter; a state estimator for nonlinear
% systems that is based on second-order polynomial approximations of the
% nonlinear mappings. The approximations are derived by using a
% multivariable extension of Stirling's interpolation formula.
% The model of the nonlinear system must be specified in the form:
% x(k+1) = f[x(k),u(k),v(k)]
% y(k) = g[x(k),w(k)]
% where 'x' is the state vector, 'u' is a possible input, and 'v' and 'w'
% are (white) noise sources.
%
% Call
% [xhat,Smat]=dd2(xfile,yfile,x0,P0,q,r,u,y,timeidx,optpar)
%
% Input
% xfile - File containing the state equations.
% yfile - File containing the output equations.
% x0 - Initial state vector.
% P0 - Initial covariance matrix (symmetric, nonnegative definite).
% q,r - Covariance matrices for v and w, respectively.
% u - Input signal. Dimension is [samples x inputs].
% Use [] if there are no inputs.
% y - Output signal. Dimension is [observations x outputs].
% timeidx - Vector containing sample numbers for the availability of
% the observations in 'y'. The vector has same length as 'y'.
% optpar - Data structure containing optional parameters:
% .vmean: Mean of process noise vector.
% .wmean: Mean of measurement noise vector.
% .init : Initial parameters for 'xfile' and 'yfile'
% (use an arbitrary format).
%
% Output
% xhat - State estimates. Dimension is [samples+1 x states].
% Smat - Matrix where each row contains elements of (the upper triangular
% part of) the Cholesky factor of the covariance matrix. The
% dimension is [samples+1 x 0.5*states*(states+1)]. The individual
% covariance matrices can later be extracted with SMAT2COV.
%
% The user must write the two m-functions 'xfile' and 'yfile' containing the
% state update and the output equation. The function containing the state
% update should take three arguments:
% function x=my_xfile(x,u,v)
%
% while the function containing the output equation should take two
% arguments:
% function y=my_yfile(x,w)
%
% In both cases, an initialization of constant parameters can be
% made using the parameter 'optpar.init'. This parameter is passed through
% x if the functions are called with only one parameter.
%
% Literature:
% M. Norgaard, N.K. Poulsen, O. Ravn: Easy and Accurate State Estimation
% for Nonlinear Systems," 14th IFAC World Conference
% in Beijing, China, July 5-9, 1999, pp. 343-348.
%
% Written by: Magnus Norgaard, IMM/IAU, Technical University of Denmark
% LastEditDate: Apr. 15, 2000
% >>>>>>>>>>>>>>>>>>>>>>>>>>> INITIALIZATIONS <<<<<<<<<<<<<<<<<<<<<<<<<<
h2 = 3; % Squared divided-difference step size
h = sqrt(h2); % Divided difference step-size
scal1 = 0.5/h; % A convenient scaling factor
scal2 = sqrt((h2-1)/(4*h2*h2)); % Another scaling factor
if isempty(u), % No inputs
nu = 0; samples = timeidx(end); uk1 = [];
else
[samples,nu] = size(u); % # of samples and inputs
end
nx = size(P0,1); % # of states
if isempty(xbar), % Set to x0=0 if not specified
xbar = zeros(nx,1);
elseif length(xbar)~=nx,
error('Dimension mismatch between x0 and P0');
end
ny = size(y,2); % # of outputs
nv = size(q,1); % # of process noise sources
nw = size(r,1); % # of measurement noise sources
[v,d] = eig(P0); % Square root of initial state covariance
Sxbar = triag(real(v*sqrt(d)));
[v,d] = eig(q); % Square root of process noise covariance
Sv = real(v*sqrt(d));
[v,d] = eig(r);
Sw = real(v*sqrt(d)); % Square root of measurement noise cov.
Sxx = zeros(nx,nx);
Sxv = zeros(nx,nv);
Syx = zeros(ny,nx);
Syw = zeros(ny,nw);
Sxx2 = zeros(nx,nx);
Sxv2 = zeros(nx,nv);
Syx2 = zeros(ny,nx);
Syw2 = zeros(ny,nw);
xhat_data = zeros(samples+1,nx); % Matrix for storing state estimates
Smat = zeros(samples+1,0.5*nx*(nx+1)); % Matrix for storing cov. matrices
[I,J] = find(triu(reshape(1:nx*nx,nx,nx))'); % Index to elem. in Sx
sidx = sub2ind([nx nx],J,I);
yidx = 1; % Index into y-vector
% ----- Initialize state+output equations and linearization -----
if nargin<10, % No optional parameters passed
optpar = [];
end
if isfield(optpar,'init') % Parameters for m-functions
initpar = optpar.init;
else
initpar = [];
end
if isfield(optpar,'vmean'),% Mean of process noise
vmean = optpar.vmean;
else
vmean = zeros(nv,1);
end
if isfield(optpar,'wmean'),% Mean of measurement noise
wmean = optpar.wmean;
else
wmean = zeros(nw,1);
end
feval(kalmfilex,initpar); % State equation
feval(kalmfiley,initpar); % Output equation
counter = 0; % Counts the progress of the filtering
waithandle=waitbar(0,'Filtering in progress'); % Initialize waitbar
% >>>>>>>>>>>>>>>>>>>>>>>>>>>> FILTERING <<<<<<<<<<<<<<<<<<<<<<<<<<<
for k=0:samples,
% --- Measurement update (a posteriori update) ---
y0 = feval(kalmfiley,xbar,wmean);
if (k<=timeidx(end) & timeidx(yidx,1)==k),
ybar = ((h2-nx-nw)/h2)*y0;
for kx=1:nx,
syp = feval(kalmfiley,xbar+h*Sxbar(:,kx),wmean);
sym = feval(kalmfiley,xbar-h*Sxbar(:,kx),wmean);
Syx(:,kx) = scal1*(syp-sym);
Syx2(:,kx) = scal2*(syp+sym-2*y0);
ybar = ybar + (syp+sym)/(2*h2);
end
for kw=1:nw,
swp = feval(kalmfiley,xbar,wmean+h*Sw(:,kw));
swm = feval(kalmfiley,xbar,wmean-h*Sw(:,kw));
Syw(:,kw) = scal1*(swp-swm);
Syw2(:,kw) = scal2*(swp+swm-2*y0);
ybar = ybar + (swp+swm)/(2*h2);
end
% Cholesky factor of a'posteriori output estimation error covariance
Sy = triag([Syx Syw Syx2 Syw2]);
K = (Sxbar*Syx')/(Sy*Sy');
xhat = xbar + K*(y(yidx,:)'-ybar); % State estimate
% Cholesky factor of a'posteriori estimation error covariance
Sx = triag([Sxbar-K*Syx K*Syw K*Syx2 K*Syw2]);
yidx = yidx + 1;
% No observations available at this sampling instant
else
xhat = xbar; % Copy a priori state estimate
Sx = Sxbar; % Copy a priori covariance factor
end
% --- Time update (a'priori update) of state and covariance ---
if k<samples,
if nu>0 uk1 = u(k+1,:)'; end
fxbar = feval(kalmfilex,xhat,uk1,vmean);
xbar = ((h2-nx-nv)/h2)*fxbar;
for kx=1:nx,
sxp = feval(kalmfilex,xhat+h*Sx(:,kx),uk1,vmean);
sxm = feval(kalmfilex,xhat-h*Sx(:,kx),uk1,vmean);
Sxx(:,kx) = scal1*(sxp-sxm);
Sxx2(:,kx)= scal2*(sxp+sxm-2*fxbar);
xbar = xbar + (sxp+sxm)/(2*h2);
end
for kv=1:nv,
svp = feval(kalmfilex,xhat,uk1,vmean+h*Sv(:,kv));
svm = feval(kalmfilex,xhat,uk1,vmean-h*Sv(:,kv));
Sxv(:,kv) = scal1*(svp-svm);
Sxv2(:,kv)= scal2*(svp+svm-2*fxbar);
xbar = xbar + (svp+svm)/(2*h2);
end
% Cholesky factor of a'priori estimation error covariance
Sxbar = triag([Sxx Sxv Sxx2 Sxv2]);
end
% --- Store results ---
xhat_data(k+1,:) = xhat';
Smat(k+1,:) = Sx(sidx)';
% --- How much longer? ---
if (counter+0.01<= k/samples),
counter = k/samples;
waitbar(k/samples,waithandle);
end
end
close(waithandle);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -