ukf(无迹卡尔曼滤波)算法的matlab程序.



ukf(无迹卡尔曼滤波)算法的matlab程序.

function [x,P]=ukf(fstate,x,P,hmeas,z,Q,R)
% UKF   Unscented Kalman Filter for nonlinear dynamic systems
% [x, P] = ukf(f,x,P,h,z,Q,R) returns state estimate, x and state covariance, P
% for nonlinear dynamic system (for simplicity, noises are assumed as additive):
%           x_k+1 = f(x_k) + w_k
%           z_k   = h(x_k) + v_k
% where w ~ N(0,Q) meaning w is gaussian noise with covariance Q
%       v ~ N(0,R) meaning v is gaussian noise with covariance R
% Inputs:   f: function handle for f(x)
%           x: "a priori" state estimate
%           P: "a priori" estimated state covariance
%           h: fanction handle for h(x)
%           z: current measurement
%           Q: process noise covariance
%           R: measurement noise covariance
% Output:   x: "a posteriori" state estimate
%           P: "a posteriori" state covariance
%
% Example:
%{
n=3;      %number of state
q=0.1;    %std of process
r=0.1;    %std of measurement
Q=q^2*eye(n); % covariance of process
R=r^2;        % covariance of measurement
f=@(x)[x(2);x(3);0.05*x(1)*(x(2)+x(3))]; % nonlinear state equations
h=@(x)x(1);                               % measurement equation
s=[0;0;1];                                % initial state
x=s+q*randn(3,1); %initial state          % initial state with noise
P = eye(n);                               % initial state covraiance
N=20;                                     % total dynamic steps
xV = zeros(n,N);          %estmate        % allocate memory
sV = zeros(n,N);          %actual
zV = zeros(1,N);
for k=1:N
z = h(s) + r*randn;                     % measurments
sV(:,k)= s;                             % save actual state
zV(k) = z;                             % save measurment
[x, P] = ukf(f,x,P,h,z,Q,R);            % ekf
xV(:,k) = x;                            % save estimate
s = f(s) + q*randn(3,1);                % update process
end
for k=1:3                                 % plot results
subplot(3,1,k)
plot(1:N, sV(k,:), '-', 1:N, xV(k,:), '--')
end
%}
%
% By Yi Cao at Cranfield University, 04/01/2008
%
L=numel(x);                                 %numer of states
m=numel(z);                                 %numer of measurements
alpha=1e-3;                                 %default, tunable
ki=0;                                       %default, tunable
beta=2;                                     %default, tunable
lambda=alpha^2*(L+ki)-L;                    %scaling factor
c=L+lambda;                                 %scaling factor
Wm=[lambda/c 0.5/c+zeros(1,2*L)];           %weights for means
Wc=Wm;
Wc(1)=Wc(1)+(1-alpha^2+beta);               %weights for covariance
c=sqrt(c);
X=sigmas(x,P,c);                            %sigma points around x
[x1,X1,P1,X2]=ut(fstate,X,Wm,Wc,L,Q);          %unscented transformation of process
% X1=sigmas(x1,P1,c);                         %sigma points around x1
% X2=X1-x1(:,ones(1,size(X1,2)));             %deviation of X1
[z1,Z1,P2,Z2]=ut(hmeas,X1,Wm,Wc,m,R);       %unscented transformation of measurments
P12=X2*diag(Wc)*Z2';                        %transformed cross-covariance
K=P12*inv(P2);
x=x1+K*(z-z1);                              %state update
P=P1-K*P12';                                %covariance update

function [y,Y,P,Y1]=ut(f,X,Wm,Wc,n,R)
%Unscented Transformation
%Input:
%        f: nonlinear map
%        X: sigma points
%       Wm: weights for mean
%       Wc: weights for covraiance
%        n: numer of outputs of f
%        R: additive covariance
%Output:
%        y: transformed mean
%        Y: transformed smapling points
%        P: transformed covariance
%       Y1: transformed deviations

L=size(X,2);
y=zeros(n,1);
Y=zeros(n,L);
for k=1:L                  
    Y(:,k)=f(X(:,k));      
    y=y+Wm(k)*Y(:,k);      
end
Y1=Y-y(:,ones(1,L));
P=Y1*diag(Wc)*Y1'+R;         

function X=sigmas(x,P,c)
%Sigma points around reference point
%Inputs:
%       x: reference point
%       P: covariance
%       c: coefficient
%Output:
%       X: Sigma points

A = c*chol(P)';
Y = x(:,ones(1,numel(x)));
X = [x Y+A Y-A];
  • 13
    点赞
  • 80
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
MATLAB基于无迹卡尔曼滤波(Unscented Kalman Filter, UKF算法程序可以通过以下步骤实现: 1. 首先,定义系统的状态方程和测量方程。状态方程描述了系统的动态行为,测量方程描述了系统输出的测量模型。 2. 初始化系统状态和协方差矩阵。系统状态是需要估计的量,协方差矩阵是描述状态估计的不确定性的矩阵。 3. 进入循环,对每个时间步进行以下操作: a. 预测阶段: - 使用状态方程和前一个时间步的估计值来预测当前时间步的系统状态和协方差矩阵。 - 通过定义方差和权重矩阵,计算预测状态的一组sigma点。 b. 修正阶段: - 对每个预测状态的sigma点进行观测,通过测量方程将其映射到测量空间,得到对应的预测测量的sigma点。 - 利用预测测量的sigma点,计算预测测量均值和协方差矩阵。 - 根据测量值与预测测量的偏移,调整预测状态和协方差矩阵,得到修正后的估计状态和协方差矩阵。 4. 循环结束后,得到整个时间段的状态估计值。 无迹卡尔曼滤波相较于传统的卡尔曼滤波算法,采用了一种非线性变换方式,通过一组状态的sigma点来近似非线性函数。这样可以更好地处理非线性系统,并且减少了线性化误差。 在MATLAB中,可以通过相关的函数和工具箱来实现无迹卡尔曼滤波算法。常用函数包括"unscentedKalmanFilter"用于创建无迹卡尔曼滤波器对象,"predict"和"correct"方法用于执行预测和修正阶段的操作。用户可以根据具体的系统和测量方程进行参数设置和状态估计。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值