卡尔曼滤波器(Kalman Filter) 理解

您所在的位置:网站首页 荣耀migc4至臻版有滤波器吗 卡尔曼滤波器(Kalman Filter) 理解

卡尔曼滤波器(Kalman Filter) 理解

2024-07-14 21:05| 来源: 网络整理| 查看: 265

卡尔曼滤波器 1 简介(Brief Introduction)

在学习卡尔曼滤波器之前,首先看看为什么叫“卡尔曼”。跟其他著名的理论(例如傅立叶变换,泰勒级数等等)一样,卡尔曼也是一个人的名字,而跟他们不同的是,他是个现代人!

卡尔曼全名Rudolf Emil Kalman,匈牙利数学家,1930年出生于匈牙利首都布达佩斯。1953,1954年于麻省理工学院分别获得电机工程学士及硕士学位。1957年于哥伦比亚大学获得博士学位。我们现在要学习的卡尔曼滤波器,正是源于他的博士论文和1960年发表的论文《A New Approach to Linear Filtering and Prediction Problems》(线性滤波与预测问题的新方法)。如果对这编论文有兴趣,可以到这里的地址下载阅读:论文

卡尔曼滤波器到底是干嘛的?我们来看下wiki上的解释:

卡尔曼滤波的一个典型实例是从一组有限的,包含噪声的,对物体位置的观察序列(可能有偏差)预测出物体的位置的坐标及速度。在很多工程应用(如雷达、计算机视觉)中都可以找到它的身影。同时,卡尔曼滤波也是控制理论以及控制系统工程中的一个重要课题。例如,对于雷达来说,人们感兴趣的是其能够跟踪目标。但目标的位置、速度、加速度的测量值往往在任何时候都有噪声。卡尔曼滤波利用目标的动态信息,设法去掉噪声的影响,得到一个关于目标位置的好的估计。这个估计可以是对当前目标位置的估计(滤波),也可以是对于将来位置的估计(预测),也可以是对过去位置的估计(插值或平滑)。

斯坦利.施密特(Stanley Schmidt)首次实现了卡尔曼滤波器。卡尔曼在NASA埃姆斯研究中心访问时,发现他的方法对于解决阿波罗计划的轨道预测很有用,后来阿波罗飞船的导航电脑便使用了这种滤波器。 关于这种滤波器的论文由Swerling (1958)、Kalman (1960)与 Kalman and Bucy (1961)发表。

目前,卡尔曼滤波已经有很多不同的实现.卡尔曼最初提出的形式现在一般称为简单卡尔曼滤波器。除此以外,还有施密特扩展滤波器、信息滤波器以及很多Bierman, Thornton 开发的平方根滤波器的变种。也许最常见的卡尔曼滤波器是锁相环,它在收音机、计算机和几乎任何视频或通讯设备中广泛存在。

简单来说,卡尔曼滤波器是一个“optimal recursive data processing algorithm(最优化自回归数据处理算法)”。对于解决很大部分的问题,他是最优,效率最高甚至是最有用的。他的广泛应用已经超过30年,包括机器人导航,控制,传感器数据融合甚至在军事方面的雷达系统以及导弹追踪等等。近年来更被应用于计算机图像处理,例如头脸识别,图像分割,图像边缘检测等等。

说起Kalman滤波器的历史

最早要追溯到17世纪,Roger Cotes开始研究最小均方问题。但由于缺少实际案例的支撑(那个时候哪来那么多雷达啊啥的这些信号啊),Cotes的研究让人看着显得很模糊,因此在估计理论的发展中影响很小。17世纪中叶,最小均方估计(Least squares Estimation)理论逐步完善,Tobias Mayer在1750年将其用于月球运动的估计,Leonard Euler在1749年、Pierre Laplace在1787分别用于木星和土星的运动估计。Roger Boscovich在1755用最小均方估计地球的大小。1777年,77岁的Daniel Bernoulli(大名鼎鼎的伯努利)发明了最大似然估计算法。递归的最小均方估计理论是由Karl Gauss建立在1809年(好吧,他声称在1795年就完成了),当时还有Adrien Legendre在1805年完成了这项工作,Robert Adrain在1808年完成的,至于到底谁是Boss,矮子们就别管了吧!

在1880年,丹麦的天文学家Thorvald Nicolai Thiele在之前最小均方估计的基础上开发了一个递归算法,与Kalman滤波非常相似。在某些标量的情况下,Thiele的滤波器与Kalman滤波器时等价的,Thiele提出了估计过程噪声和测量噪声中方差的方法(过程噪声和测量噪声是Kalman滤波器中关键的概念)。

上面提到的这么多研究估计理论的先驱,大多是天文学家而非数学家。现在,大部分的理论贡献都源自于实际的工程。“There is nothing so practical as a good theory”,应该就是“实践是检验真理的唯一标准”之类吧。

现在,我们的控制论大Wiener终于出场了,还有那个叫Kolmogorov(柯尔莫戈洛夫)的神人。在19世纪40年代,Wiener设计了Wiener滤波器,然而,Wiener滤波器不是在状态空间进行的(这个学过Wiener滤波的就知道,它是直接从观测空间z(n)=s(n)+w(n)进行的滤波),Wiener是稳态过程,它假设测量是通过过去无限多个值估计得到的。Wiener滤波器比Kalman滤波器具有更高的自然统计特性。这些也限制其只是更接近理想的模型,要直接用于实际工程中需要足够的先验知识(要预知协方差矩阵),美国NASA曾花费多年的时间研究维纳理论,但依然没有在空间导航中看到维纳理论的实际应用。

在1950末期,大部分工作开始对维纳滤波器中协方差的先验知识通过状态空间模型进行描述。通过状态空间表述后的算法就和今天看到的Kalman滤波已经极其相似了。Johns Hopkins大学首先将这个算法用在了导弹跟踪中,那时在RAND公司工作的Peter Swerling将它用在了卫星轨道估计,Swerling实际上已经推导出了(1959年发表的)无噪声系统动力学的Kalman滤波器,在他的应用中,他还考虑了使用非线性系统动力学和和测量方程。可以这样说,Swerling和发明Kalman滤波器是失之交臂,一线之隔。在kalman滤波器闻名于世之后,他还写信到AIAA Journal声讨要获得Kalman滤波器发明的荣誉(然而这时已经给滤波器命名Kalman了)。总结其失之交臂的原因,主要是Swerling没有直接在论文中提出Kalman滤波器的理论,而只是在实践中应用。

Rudolph Kalman在1960年发现了离散时间系统的Kalman滤波器,这就是我们在今天各种教材上都能看到的,1961年Kalman和Bucy又推导了连续时间的Kalman滤波器。Ruslan Stratonovich也在1960年也从最大似然估计的角度推导出了Kalman滤波器方程。

目前,卡尔曼滤波已经有很多不同的实现。卡尔曼最初提出的形式现在一般称为简单卡尔曼滤波器。除此以外,还有施密特扩展滤波器、信息滤波器以及很多Bierman, Thornton开发的平方根滤波器的变种。也许最常见的卡尔曼滤波器是锁相环,它在收音机、计算机和几乎任何视频或通讯设备中广泛存在。

2 卡尔曼滤波器的介绍 (Introduction to the Kalman Filter)

为了可以更加容易的理解卡尔曼滤波器,这里会应用形象的描述方法来讲解,而不是像大多数参考书那样罗列一大堆的数学公式和数学符号。 但是,他的5条公式是其核心内容。结合现代的计算机,其实卡尔曼的程序相当的简单,只要你理解了他的那5条公式。

在介绍他的5条公式之前,先让我们来根据下面的例子一步一步的探索。

假设我们要研究的对象是一个房间的温度。根据你的经验判断,这个房间的温度是恒定的,也就是下一分钟的温度等于现在这一分钟的温度(假设我们用一分钟来做时间单位)。假设你对你的经验不是100%的相信,可能会有上下偏差几度。我们把这些偏差看成是高斯白噪声(White Gaussian Noise),也就是这些偏差跟前后时间是没有关系的而且符合高斯分配(Gaussian Distribution)。另外,我们在房间里放一个温度计,但是这个温度计也不准确的,测量值会比实际值偏差。我们也把这些偏差看成是高斯白噪声。

好了,现在对于某一分钟我们有两个有关于该房间的温度值:你根据经验的预测值(系统的预测值)和温度计的值(测量值)。下面我们要用这两个值结合他们各自的噪声来估算出房间的实际温度值。

假如我们要估算k时刻的是实际温度值。首先你要根据k-1时刻的温度值,来预测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,我们可以估算出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。上面的Kg,就是卡尔曼增益(Kalman Gain)。他可以随不同的时刻而改变他自己的值,是不是很神奇!

到这里,应该对Kalman滤波有个总体的概念了,有几个观点很重要,是建立Kalman滤波器的基础:

一个是n-1时刻对n时刻的估计值,一个是n时刻的测量值,估计值和测量值都存在误差,且误差都假设满足独立的高斯分布。Kalman滤波器就是充分结合了估计值和测量值得到n时刻更接近真值的估计结果Kalman滤波器引入状态空间的目的是避免了“像Wiener滤波器一样需要对过去所有[0,n-1]时刻协方差先验知识都已知”,而直接可以通过上一时刻即n-1时刻的状态信息和均方误差信息就可递推得到n时刻的估计。尽管递推使得实际应用中方便了,但n-1时刻对n时刻的估计实际上使用到了所有前[0,n-1]时刻的信息,只不过信息一直通过最小均方误差进行传递到n-1时刻。基于此,Kalman滤波也需要先验知识,即-1时刻的初始值。

在上小节中只看到Kalman的结论,那么Kalman滤波器是如何将估计值和测量值结合起来,如何将信息传递下去的呢?这其中,“独立高斯分布”的假设条件功劳不可谓不大!测量值z(n)~N(uz,σz^2),估计值x(n)~N(ux,σx^2)。

Kalman滤波器巧妙的用“独立高斯分布的乘积”将这两个测量值和估计值进行融合!

下面就要言归正传,讨论真正工程系统上的卡尔曼。

3 卡尔曼滤波器算法 (The Kalman Filter Algorithm)

在这一部分,我们就来描述源于Dr. Kalman 的卡尔曼滤波器。

下面的描述,会涉及一些基本的概念知识,包括概率(Probability),随即变量(Random Variable),高斯或正态分配(Gaussian Distribution)还有State-space Model等等。 但对于卡尔曼滤波器的详细证明,这里不能一一描述。

首先,我们先要引入一个离散控制过程的系统。 该系统可用一个线性随机微分方程(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(这里我们假设他们不随系统状态变化而变化)。

对于满足上面的条件(线性随机微分系统,过程和测量都是高斯白噪声),卡尔曼滤波器是最优的信息处理器。

下面我们来用他们结合他们的covariances 来估算系统的最优化输出(类似上一节那个温度的例子)。

首先我们要利用系统的过程模型,来预测下一状态的系统。假设现在的系统状态是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 Simple Example)

这里我们结合第二第三节,举一个非常简单的例子来说明卡尔曼滤波器的工作过程。所举的例子是进一步描述第二节的例子,而且还会配以程序模拟结果。

根据第二节的描述,把房间看成一个系统,然后对这个系统建模。当然,我们见的模型不需要非常地精确。我们所知道的这个房间的温度是跟前一时刻的温度相同的,所以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)

现在我们模拟一组测量值作为输入。假设房间的真实温度为25度,我模拟了200个测量值,这些测量值的平均值为25度,但是加入了标准偏差为几度的高斯白噪声(在图中为蓝线)。

为了令卡尔曼滤波器开始工作,我们需要告诉卡尔曼两个零时刻的初始值,是X(0|0)和P(0|0)。他们的值不用太在意,随便给一个就可以了,因为随着卡尔曼的工作,X会逐渐的收敛。但是对于P,一般不要取0,因为这样可能会令卡尔曼完全相信你给定的X(0|0)是系统最优的,从而使算法不能收敛。我选了X(0|0)=1度,P(0|0)=10。

该系统的真实温度为25度,图中用黑线表示。图中红线是卡尔曼滤波器输出的最优化结果(该结果在算法中设置了Q=1e-6,R=1e-1)。

一段Matlab代码是从网上找到的,程序简单直接,但作为学习分析用很棒:

% KALMANF - updates a system state vector estimate based upon an % observation, using a discrete Kalman filter. % % Version 1.0, June 30, 2004 % % This tutorial function was written by Michael C. Kleder % % INTRODUCTION % % Many people have heard of Kalman filtering, but regard the topic % as mysterious. While it's true that deriving the Kalman filter and % proving mathematically that it is "optimal" under a variety of % circumstances can be rather intense, applying the filter to % a basic linear system is actually very easy. This Matlab file is % intended to demonstrate that. % % An excellent paper on Kalman filtering at the introductory level, % without detailing the mathematical underpinnings, is: % "An Introduction to the Kalman Filter" % Greg Welch and Gary Bishop, University of North Carolina % http://www.cs.unc.edu/~welch/kalman/kalmanIntro.html % % PURPOSE: % % The purpose of each iteration of a Kalman filter is to update % the estimate of the state vector of a system (and the covariance % of that vector) based upon the information in a new observation. % The version of the Kalman filter in this function assumes that % observations occur at fixed discrete time intervals. Also, this % function assumes a linear system, meaning that the time evolution % of the state vector can be calculated by means of a state transition % matrix. % % USAGE: % % s = kalmanf(s) % % "s" is a "system" struct containing various fields used as input % and output. The state estimate "x" and its covariance "P" are % updated by the function. The other fields describe the mechanics % of the system and are left unchanged. A calling routine may change % these other fields as needed if state dynamics are time-dependent; % otherwise, they should be left alone after initial values are set. % The exceptions are the observation vectro "z" and the input control % (or forcing function) "u." If there is an input function, then % "u" should be set to some nonzero value by the calling routine. % % SYSTEM DYNAMICS: % % The system evolves according to the following difference equations, % where quantities are further defined below: % % x = Ax + Bu + w meaning the state vector x evolves during one time % step by premultiplying by the "state transition % matrix" A. There is optionally (if nonzero) an input % vector u which affects the state linearly, and this % linear effect on the state is represented by % premultiplying by the "input matrix" B. There is also % gaussian process noise w. % z = Hx + v meaning the observation vector z is a linear function % of the state vector, and this linear relationship is % represented by premultiplication by "observation % matrix" H. There is also gaussian measurement % noise v. % 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 % % VECTOR VARIABLES: % % s.x = state vector estimate. In the input struct, this is the % "a priori" state estimate (prior to the addition of the % information from the new observation). In the output struct, % this is the "a posteriori" state estimate (after the new % measurement information is included). % s.z = observation vector % s.u = input control vector, optional (defaults to zero). % % MATRIX VARIABLES: % % s.A = state transition matrix (defaults to identity). % s.P = covariance of the state vector estimate. In the input struct, % this is "a priori," and in the output it is "a posteriori." % (required unless autoinitializing as described below). % s.B = input matrix, optional (defaults to zero). % s.Q = process noise covariance (defaults to zero). % s.R = measurement noise covariance (required). % s.H = observation matrix (defaults to identity). % % NORMAL OPERATION: % % (1) define all state definition fields: A,B,H,Q,R % (2) define intial state estimate: x,P % (3) obtain observation and control vectors: z,u % (4) call the filter to obtain updated state estimate: x,P % (5) return to step (3) and repeat % % INITIALIZATION: % % If an initial state estimate is unavailable, it can be obtained % from the first observation as follows, provided that there are the % same number of observable variables as state variables. This "auto- % intitialization" is done automatically if s.x is absent or NaN. % % x = inv(H)*z % P = inv(H)*R*inv(H') % % This is mathematically equivalent to setting the initial state estimate % covariance to infinity. function s = kalmanf(s) % set defaults for absent fields: if ~isfield(s,'x'); s.x=nan*z; end if ~isfield(s,'P'); s.P=nan; end if ~isfield(s,'z'); error('Observation vector missing'); end if ~isfield(s,'u'); s.u=0; end if ~isfield(s,'A'); s.A=eye(length(x)); end if ~isfield(s,'B'); s.B=0; end if ~isfield(s,'Q'); s.Q=zeros(length(x)); end if ~isfield(s,'R'); error('Observation covariance missing'); end if ~isfield(s,'H'); s.H=eye(length(x)); end if isnan(s.x) % initialize state estimate from first observation if diff(size(s.H)) error('Observation matrix must be square and invertible for state autointialization.'); end s.x = inv(s.H)*s.z; s.P = inv(s.H)*s.R*inv(s.H'); else % This is the code which implements the discrete Kalman filter: % Prediction for state vector and covariance: s.x = s.A*s.x + s.B*s.u; s.P = s.A * s.P * s.A' + s.Q; % Compute Kalman gain factor: K = s.P*s.H'*inv(s.H*s.P*s.H'+s.R); % Correction based on observation: s.x = s.x + K*(s.z-s.H*s.x); s.P = s.P - K*s.H*s.P; % Note that the desired result, which is an improved estimate % of the sytem state vector x and its covariance P, was obtained % in only five lines of code, once the system was defined. (That's % how simple the discrete Kalman filter is to use.) Later, % we'll discuss how to deal with nonlinear systems. end return

下面是一段测试代码:

% Define the system as a constant of 12 volts: clear all s.x = 12; s.A = 1; % Define a process noise (stdev) of 2 volts as the car operates: s.Q = 2^2; % variance, hence stdev^2 % Define the voltimeter to measure the voltage itself: s.H = 1; % Define a measurement error (stdev) of 2 volts: s.R = 2^2; % variance, hence stdev^2 % Do not define any system input (control) functions: s.B = 0; s.u = 0; % Do not specify an initial state: s.x = nan; s.P = nan; % Generate random voltages and watch the filter operate. tru=[]; % truth voltage for t=1:20 tru(end+1) = randn*2+12; s(end).z = tru(end) + randn*2; % create a measurement s(end+1)=kalmanf(s(end)); % perform a Kalman filter iteration end figure hold on grid on % plot measurement data: hz=plot([s(1:end-1).z],'r.'); % plot a-posteriori state estimates: hk=plot([s(2:end).x],'b-'); ht=plot(tru,'g-'); legend([hz hk ht],'observations','Kalman output','true voltage',0) title('Automobile Voltimeter Example') hold off Kalman滤波C程序

我就在上面公式的基础上实现了基本的Kalman滤波器,包括1维和2维状态的情况。先在头文件中声明1维和2维Kalman滤波器结构:

/* * FileName : kalman_filter.h * Author : xiahouzuoxin @163.com * Version : v1.0 * Date : 2014/9/24 20:37:01 * Brief : * * Copyright (C) MICL,USTB */ #ifndef _KALMAN_FILTER_H #define _KALMAN_FILTER_H /* * NOTES: n Dimension means the state is n dimension, * measurement always 1 dimension */ /* 1 Dimension */ typedef struct { float x; /* state */ float A; /* x(n)=A*x(n-1)+u(n),u(n)~N(0,q) */ float H; /* z(n)=H*x(n)+w(n),w(n)~N(0,r) */ float q; /* process(predict) noise convariance */ float r; /* measure noise convariance */ float p; /* estimated error convariance */ float gain; } kalman1_state; /* 2 Dimension */ typedef struct { float x[2]; /* state: [0]-angle [1]-diffrence of angle, 2x1 */ float A[2][2]; /* X(n)=A*X(n-1)+U(n),U(n)~N(0,q), 2x2 */ float H[2]; /* Z(n)=H*X(n)+W(n),W(n)~N(0,r), 1x2 */ float q[2]; /* process(predict) noise convariance,2x1 [q0,0; 0,q1] */ float r; /* measure noise convariance */ float p[2][2]; /* estimated error convariance,2x2 [p0 p1; p2 p3] */ float gain[2]; /* 2x1 */ } kalman2_state; extern void kalman1_init(kalman1_state *state, float init_x, float init_p); extern float kalman1_filter(kalman1_state *state, float z_measure); extern void kalman2_init(kalman2_state *state, float *init_x, float (*init_p)[2]); extern float kalman2_filter(kalman2_state *state, float z_measure); #endif /*_KALMAN_FILTER_H*/

源码给了有详细的注释,kalman1_state是状态空间为1维/测量空间1维的Kalman滤波器,kalman2_state是状态空间为2维/测量空间1维的Kalman滤波器。

两个结构体都需要通过初始化函数初始化相关参数、状态值和均方差值。

/* * FileName : kalman_filter.c * Author : xiahouzuoxin @163.com * Version : v1.0 * Date : 2014/9/24 20:36:51 * Brief : * * Copyright (C) MICL,USTB */ #include "kalman_filter.h" /* * @brief * Init fields of structure @kalman1_state. * I make some defaults in this init function: * A = 1; * H = 1; * and @q,@r are valued after prior tests. * * NOTES: Please change A,H,q,r according to your application. * * @inputs * state - Klaman filter structure * init_x - initial x state value * init_p - initial estimated error convariance * @outputs * @retval */ void kalman1_init(kalman1_state *state, float init_x, float init_p) { state->x = init_x; state->p = init_p; state->A = 1; state->H = 1; state->q = 2e2;//10e-6; /* predict noise convariance */ state->r = 5e2;//10e-5; /* measure error convariance */ } /* * @brief * 1 Dimension Kalman filter * @inputs * state - Klaman filter structure * z_measure - Measure value * @outputs * @retval * Estimated result */ float kalman1_filter(kalman1_state *state, float z_measure) { /* Predict */ state->x = state->A * state->x; state->p = state->A * state->A * state->p + state->q; /* p(n|n-1)=A^2*p(n-1|n-1)+q */ /* Measurement */ state->gain = state->p * state->H / (state->p * state->H * state->H + state->r); state->x = state->x + state->gain * (z_measure - state->H * state->x); state->p = (1 - state->gain * state->H) * state->p; return state->x; } /* * @brief * Init fields of structure @kalman1_state. * I make some defaults in this init function: * A = {{1, 0.1}, {0, 1}}; * H = {1,0}; * and @q,@r are valued after prior tests. * * NOTES: Please change A,H,q,r according to your application. * * @inputs * @outputs * @retval */ void kalman2_init(kalman2_state *state, float *init_x, float (*init_p)[2]) { state->x[0] = init_x[0]; state->x[1] = init_x[1]; state->p[0][0] = init_p[0][0]; state->p[0][1] = init_p[0][1]; state->p[1][0] = init_p[1][0]; state->p[1][1] = init_p[1][1]; //state->A = {{1, 0.1}, {0, 1}}; state->A[0][0] = 1; state->A[0][1] = 0.1; state->A[1][0] = 0; state->A[1][1] = 1; //state->H = {1,0}; state->H[0] = 1; state->H[1] = 0; //state->q = {{10e-6,0}, {0,10e-6}}; /* measure noise convariance */ state->q[0] = 10e-7; state->q[1] = 10e-7; state->r = 10e-7; /* estimated error convariance */ } /* * @brief * 2 Dimension kalman filter * @inputs * state - Klaman filter structure * z_measure - Measure value * @outputs * state->x[0] - Updated state value, Such as angle,velocity * state->x[1] - Updated state value, Such as diffrence angle, acceleration * state->p - Updated estimated error convatiance matrix * @retval * Return value is equals to state->x[0], so maybe angle or velocity. */ float kalman2_filter(kalman2_state *state, float z_measure) { float temp0 = 0.0f; float temp1 = 0.0f; float temp = 0.0f; /* Step1: Predict */ state->x[0] = state->A[0][0] * state->x[0] + state->A[0][1] * state->x[1]; state->x[1] = state->A[1][0] * state->x[0] + state->A[1][1] * state->x[1]; /* p(n|n-1)=A^2*p(n-1|n-1)+q */ state->p[0][0] = state->A[0][0] * state->p[0][0] + state->A[0][1] * state->p[1][0] + state->q[0]; state->p[0][1] = state->A[0][0] * state->p[0][1] + state->A[1][1] * state->p[1][1]; state->p[1][0] = state->A[1][0] * state->p[0][0] + state->A[0][1] * state->p[1][0]; state->p[1][1] = state->A[1][0] * state->p[0][1] + state->A[1][1] * state->p[1][1] + state->q[1]; /* Step2: Measurement */ /* gain = p * H^T * [r + H * p * H^T]^(-1), H^T means transpose. */ temp0 = state->p[0][0] * state->H[0] + state->p[0][1] * state->H[1]; temp1 = state->p[1][0] * state->H[0] + state->p[1][1] * state->H[1]; temp = state->r + state->H[0] * temp0 + state->H[1] * temp1; state->gain[0] = temp0 / temp; state->gain[1] = temp1 / temp; /* x(n|n) = x(n|n-1) + gain(n) * [z_measure - H(n)*x(n|n-1)]*/ temp = state->H[0] * state->x[0] + state->H[1] * state->x[1]; state->x[0] = state->x[0] + state->gain[0] * (z_measure - temp); state->x[1] = state->x[1] + state->gain[1] * (z_measure - temp); /* Update @p: p(n|n) = [I - gain * H] * p(n|n-1) */ state->p[0][0] = (1 - state->gain[0] * state->H[0]) * state->p[0][0]; state->p[0][1] = (1 - state->gain[0] * state->H[1]) * state->p[0][1]; state->p[1][0] = (1 - state->gain[1] * state->H[0]) * state->p[1][0]; state->p[1][1] = (1 - state->gain[1] * state->H[1]) * state->p[1][1]; return state->x[0]; }

其实,Kalman滤波器由于其递推特性,实现起来很简单。但调参有很多可研究的地方,主要需要设定的参数如下:

init_x:待测量的初始值,如有中值一般设成中值(如陀螺仪) init_p:后验状态估计值误差的方差的初始值 q:预测(过程)噪声方差 r:测量(观测)噪声方差。以陀螺仪为例,测试方法是:保持陀螺仪不动,统计一段时间内的陀螺仪输出数据。数据会近似正态分布,按3σ原则,取正态分布的(3σ)^2作为r的初始化值。 其中q和r参数尤为重要,一般得通过实验测试得到。

找两组声阵列测向的角度数据,对上面的C程序进行测试。一维Kalman(一维也是标量的情况,就我所知,现在网上看到的代码大都是使用标量的情况)和二维Kalman(一个状态是角度值,另一个状态是向量角度差,也就是角速度)的结果都在图中显示。这里再稍微提醒一下:状态量不要取那些能突变的量,如加速度.

上面所有C程序的源代码及测试程序都公布在原作者的Github上。



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3