admin管理员组文章数量:1122852
KAL,MAN
Kalman滤波算法的C++实现
2人收藏此文章, 我要收藏发表于2年前(2010-10-02 16:11) , 已有 1372次阅读 共 0个评论头文件:
/** Copyright (c) 2008-2011 Zhang Ming (M. Zhang), zmjerry@163** This program is free software; you can redistribute it and/or modify it* under the terms of the GNU General Public License as published by the* Free Software Foundation, either version 2 or any later version.** Redistribution and use in source and binary forms, with or without* modification, are permitted provided that the following conditions are met:** 1. Redistributions of source code must retain the above copyright notice,* this list of conditions and the following disclaimer.** 2. Redistributions in binary form must reproduce the above copyright* notice, this list of conditions and the following disclaimer in the* documentation and/or other materials provided with the distribution.** This program is distributed in the hope that it will be useful, but WITHOUT* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for* more details. A copy of the GNU General Public License is available at:* *//****************************************************************************** kalman.h** Kalman Filter.** The Kalman filter is an efficient recursive filter that estimates the* internal state of a linear dynamic system from a series of noisy* measurements. In most applications, the internal state is much larger* (more degrees of freedom) than the few "observable" parameters which are* measured. However, by combining a series of measurements, the Kalman* filter can estimate the entire internal state.** A wide variety of Kalman filters have now been developed, from Kalman's* original formulation, now called the simple Kalman filter, the Kalman-Bucy* filter, Schmidt's extended filter, the information filter, and a variety* of square-root filters that were developed by Bierman, Thornton and so on.** Zhang Ming, 2010-10, Xi'an Jiaotong University.*****************************************************************************/#ifndef KALMAN_H #define KALMAN_H#include <vector.h> #include <matrix.h> #include <inverse.h>namespace splab {template<typename Type>Vector<Type> kalman( const Matrix<Type>&, const Matrix<Type>&,const Matrix<Type>&, const Matrix<Type>&,const Vector<Type>&, const Vector<Type>&,const Vector<Type>& );#include <kalman-impl.h>} // namespace splab#endif // KALMAN_H
实现文件:
/** Copyright (c) 2008-2011 Zhang Ming (M. Zhang), zmjerry@163** This program is free software; you can redistribute it and/or modify it* under the terms of the GNU General Public License as published by the* Free Software Foundation, either version 2 or any later version.** Redistribution and use in source and binary forms, with or without* modification, are permitted provided that the following conditions are met:** 1. Redistributions of source code must retain the above copyright notice,* this list of conditions and the following disclaimer.** 2. Redistributions in binary form must reproduce the above copyright* notice, this list of conditions and the following disclaimer in the* documentation and/or other materials provided with the distribution.** This program is distributed in the hope that it will be useful, but WITHOUT* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for* more details. A copy of the GNU General Public License is available at:* *//****************************************************************************** kalman-impl.h** Implementation for Kalman Filter.** Zhang Ming, 2010-10, Xi'an Jiaotong University.*****************************************************************************//*** The simple Kalman filter for one step.* A ---> system matrix defining linear dynamic system* C ---> measurement matrix defining relationship between system's state* and measurements* Q ---> covariance matrix of process noise in system state dynamics* R ---> covariance matrix of measurements uncertainty* y ---> measurements at time t* xPrev ---> previous estimated state vector of the linear dynamic system* initDiagV ---> diagonal vector for initializing the covariance matrix of* state estimation uncertainty*/ template <typename Type> Vector<Type> kalman( const Matrix<Type> &A, const Matrix<Type> &C,const Matrix<Type> &Q, const Matrix<Type> &R,const Vector<Type> &xPrev, const Vector<Type> &y,const Vector<Type> &initDiagV ) {int N = xPrev.size();// covariance matrix of state estimation uncertaintystatic Matrix<Type> V = diag(initDiagV);// previoused state vectorVector<Type> xPred = A * xPrev;// inovationVector<Type> alpha = y - C * xPred;Matrix<Type> CTran = trT( C );Matrix<Type> VPred = A*V*trT(A) + Q;// Kalman gain matrixMatrix<Type> KGain = VPred*CTran * inv(C*VPred*CTran+R);V = ( eye(N,Type(1.0)) - KGain*C ) * VPred;// return the estimation of the state vectorreturn xPred + KGain * alpha; }
测试代码:
/****************************************************************************** kalman.h** Kalman filter testing.** Zhang Ming, 2010-10, Xi'an Jiaotong University.*****************************************************************************/#define BOUNDS_CHECK#include <iostream> #include <kalman.h>using namespace std; using namespace splab;typedef double Type; const int N = 2; const int M = 2; const int T = 20;int main() {Matrix<Type> A(N,N), C(M,N), Q(N,N), R(M,M);A = eye( N, Type(1.0) ); C = eye( N, Type(1.0) );Q = eye( N, Type(1.0) ); R = eye( N, Type(2.0) );Vector<Type> x(N,Type(1.0)), y(M), ytInit(M);ytInit[0] = Type(0.5); ytInit[1] = Type(2.0);Matrix<Type> yt(M,T);for( int t=0; t<T; ++t )yt.setColumn( ytInit, t );Vector<Type> intV( N, Type(10.0) );for( int t=0; t<T; ++t ){y = yt.getColumn(t);x = kalman( A, C, Q, R, x, y, intV );cout << "Estimation of xt at the " << t << "th iteratin: " << x << endl;}cout << "The theoretical xt should converge to: " << ytInit << endl;return 0; }
运行结果:
Estimation of xt at the 0th iteratin: size: 2 by 1 0.576923 1.84615Estimation of xt at the 1th iteratin: size: 2 by 1 0.532787 1.93443Estimation of xt at the 2th iteratin: size: 2 by 1 0.51581 1.96838Estimation of xt at the 3th iteratin: size: 2 by 1 0.507835 1.98433Estimation of xt at the 4th iteratin: size: 2 by 1 0.503909 1.99218Estimation of xt at the 5th iteratin: size: 2 by 1 0.501953 1.99609Estimation of xt at the 6th iteratin: size: 2 by 1 0.500977 1.99805Estimation of xt at the 7th iteratin: size: 2 by 1 0.500488 1.99902Estimation of xt at the 8th iteratin: size: 2 by 1 0.500244 1.99951Estimation of xt at the 9th iteratin: size: 2 by 1 0.500122 1.99976Estimation of xt at the 10th iteratin: size: 2 by 1 0.500061 1.99988Estimation of xt at the 11th iteratin: size: 2 by 1 0.500031 1.99994Estimation of xt at the 12th iteratin: size: 2 by 1 0.500015 1.99997Estimation of xt at the 13th iteratin: size: 2 by 1 0.500008 1.99998Estimation of xt at the 14th iteratin: size: 2 by 1 0.500004 1.99999Estimation of xt at the 15th iteratin: size: 2 by 1 0.500002 2Estimation of xt at the 16th iteratin: size: 2 by 1 0.500001 2Estimation of xt at the 17th iteratin: size: 2 by 1 0.5 2Estimation of xt at the 18th iteratin: size: 2 by 1 0.5 2Estimation of xt at the 19th iteratin: size: 2 by 1 0.5 2The theoretical xt should converge to: size: 2 by 1 0.5 2Process returned 0 (0x0) execution time : 0.125 s Press any key to continue.
卡尔曼滤波器 Kalman Filter
1人收藏此文章, 我要收藏发表于8个月前(2012-03-13 10:27) , 已有120次阅读 共0个评论1. 卡尔曼滤波的应用
卡尔曼滤波的一个典型实例是从一组有限的,包含噪声的,对物体位置的观察序列(可能有偏差)预测出物体的位置的 坐标及速度。例如,对于雷达来说,人们感兴趣的是其能够跟踪目标。但目标的位置、速度、加速度的测量值往往在任何时候都有噪声。卡尔曼滤波利用目标的动态信息,设法去掉噪声的影响,得到一个关于目标位置的好的 估计。这个估计可以是对当前目标位置的估计(滤波),也可以是对于将来位置的估计(预测),也可以是对过去位置的估计(插值或平滑)。
2. 实例讲解
假设我们要研究的对象是一个房间的温度。(1) 系统预测值。根据你的经验判断,这个房间的温度是恒定的,也就是下一分钟的温度等于现在这一分钟的温度(我们以一分钟为一个单位),且假设你的经验存在高斯白噪声(White Gaussian Noise)。(2) 测量值。我们在房间里放一个温度计,但是这个温度计也不准确的,测量值会比实际值偏差。我们也把这些偏差看成是高斯白噪声。
现在我们要估算k时刻的实际温度值。因为你相信温度是恒定的,所以你会得到k时刻的温度预测值是跟k-1时刻一样的,假设是23度,同时该值的高斯噪声的偏差是5度(5是这样得到的:如果 k-1时刻估算出的最优温度值的偏差是3,你对自己预测的不确定度是4度,他们平方相加再开方,就是5)。然后,你从温度计那里得到了k时刻的温度值,假设是25度,同时该值的偏差是4度。
由于我们用于估算k时刻的实际温度有两个温度值,分别是23度和25度。究竟实际温度是多少呢?相信自己还是相信温度计呢?究竟相信谁多一点,我 们可以用他们的covariance(协方差)来判断。因为Kg^2=5^2/(5^2+4^2),所以Kg=0.78(Kg:卡尔曼增益,Kalman Gain),我们可以估算出k时刻的实际温度值 是:23+0.78*(25-23)=24.56度。可以看出,因为温度计的covariance比较小(比较相信温度计),所以估算出的最优温度值偏向温度计的值。
现在我们已经得到k时刻的最优温度值了,下一步就是要进入k+1时刻,进行新的最优估算。到现在为止,好像还没看到什么自回归的东西出现。对了,在进入 k+1时刻之前,我们还要算出k时刻那个最优值(24.56度)的偏差。算法如下:((1-Kg)*5^2)^0.5=2.35。这里的5就是上面的k时 刻你预测的那个23度温度值的偏差,得出的2.35就是进入k+1时刻以后k时刻估算出的最优温度值的偏差(对应于上面的3)。
就是这样,卡尔曼滤波器就不断的把covariance递归,从而估算出最优的温度值。他运行的很快,而且它只保留了上一时刻的covariance。
3. 数学描述
首先,我们先要引入一个离散控制过程的系统。该系统可用一个线性随机微分方程(Linear Stochastic Difference equation)来描述:
X(k)=A X(k-1)+B U(k)+W(k)
再加上系统的测量值:
Z(k)=H X(k)+V(k)
上 两式子中,X(k)是k时刻的系统状态,U(k)是k时刻对系统的控制量。A和B是系统参数,对于多模型系统,他们为矩阵。Z(k)是k时刻的测量值,H 是测量系统的参数,对于多测量系统,H为矩阵。W(k)和V(k)分别表示过程和测量的噪声。他们被假设成高斯白噪声(White Gaussian Noise),他们的covariance 分别是Q,R(这里我们假设他们不随系统状态变化而变化)。
首先我们要利用系统的过程模型,来预测下一状态的系统。假设现在的系统状态是k,根据系统的模型,可以基于系统的上一状态而预测出现在状态:
X(k|k-1)=A X(k-1|k-1)+B U(k) ……….. (1)
式(1)中,X(k|k-1)是利用上一状态预测的结果,X(k-1|k-1)是上一状态最优的结果,U(k)为现在状态的控制量,如果没有控制量,它可以为0。
到现在为止,我们的系统结果已经更新了,可是,对应于X(k|k-1)的covariance还没更新。我们用P表示covariance:
P(k|k-1)=A P(k-1|k-1) A’+Q ……… (2)
式 (2)中,P(k|k-1)是X(k|k-1)对应的covariance,P(k-1|k-1)是X(k-1|k-1)对应的 covariance,A’表示A的转置矩阵,Q是系统过程的covariance。式子1,2就是卡尔曼滤波器5个公式当中的前两个,也就是对系统的预 测。
现在我们有了现在状态的预测结果,然后我们再收集现在状态的测量值。结合预测值和测量值,我们可以得到现在状态(k)的最优化估算值X(k|k):
X(k|k)= X(k|k-1)+Kg(k) (Z(k)-H X(k|k-1)) ……… (3)
其中Kg为卡尔曼增益(Kalman Gain):
Kg(k)= P(k|k-1) H’ / (H P(k|k-1) H’ + R) ……… (4)
到现在为止,我们已经得到了k状态下最优的估算值X(k|k)。但是为了要另卡尔曼滤波器不断的运行下去直到系统过程结束,我们还要更新k状态下X(k|k)的covariance:
P(k|k)=(I-Kg(k) H)P(k|k-1) ……… (5)
其中I 为1的矩阵,对于单模型单测量,I=1。当系统进入k+1状态时,P(k|k)就是式子(2)的P(k-1|k-1)。这样,算法就可以自回归的运算下去。
卡尔曼滤波器的原理基本描述了,式子1,2,3,4和5就是他的5 个基本公式。根据这5个公式,可以很容易的实现计算机的程序。
4. 再述房间温度问题
把房间看成一个系统,然后对这个系统建模。当然,我们见的模型不需要非常地精确。我们所知道的这个房间的温度是跟前一时刻的温度相同的,所以A=1。没有控制量,所以U(k)=0。因此得出:
X(k|k-1)=X(k-1|k-1) ……….. (6)
式子(2)可以改成:
P(k|k-1)=P(k-1|k-1) +Q ……… (7)
因为测量的值是温度计的,跟温度直接对应,所以H=1。式子3,4,5可以改成以下:
X(k|k)= X(k|k-1)+Kg(k) (Z(k)-X(k|k-1)) ……… (8)
Kg(k)= P(k|k-1) / (P(k|k-1) + R) ……… (9)
P(k|k)=(1-Kg(k))P(k|k-1) ……… (10)
为了令卡尔曼滤波器开始工作,我们需要告诉卡尔曼两个零时刻的初始值,是X(0|0)和P(0|0)。他们的值不用太在意,随便给一个就可以了,因为随着 卡尔曼的工作,X会逐渐的收敛。但是对于P,一般不要取0,因为这样可能会令卡尔曼完全相信你给定的X(0|0)是系统最优的,从而使算法不能收敛。我选 了X(0|0)=1度,P(0|0)=10。
引用:
carpa.bokee/4725695.html
/%E5%8D%A1%E5%B0%94%E6%9B%BC%E6%BB%A4%E6%B3%A2
版权声明:本文标题:KAL,MAN 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/biancheng/1701899850a500918.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论