【入门级】学习卡尔曼滤波

由于工作需要,学习了卡尔曼滤波,期间看到一篇不错的文章,原文链接,决定翻译一遍加深理解,如果有翻译不准的地方可以对照原文哟。

How a Kalman filter works, in pictures

卡尔曼滤波是怎么工作的

I have to tell you about the Kalman filter, because what it does is pretty damn amazing.

我要跟您讲讲卡尔曼滤波,因为它他妈牛逼了。

Surprisingly few software engineers and scientists seem to know about it, and that makes me sad because it is such a general and powerful tool for combining information in the presence of uncertainty. At times its ability to extract accurate information seems almost magical— and if it sounds like I’m talking this up too much, then take a look at this previously posted video where I demonstrate a Kalman filter figuring out the orientation of a free-floating body by looking at its velocity. Totally neat!

我靠,怎么软件工程师和数据科学家都不怎么了解它,这也太可惜了,因为这是在不确定情况下整合信息方面一个非常普适又强大的工具。有时它这种提取准确信息的能力就像魔法一样——如果你觉得我是对它过誉了,那你就看看我之前上传的这个视频,讲的是卡尔曼滤波通过观察速度解算自由漂浮体的方向。太6了!

What is it?

卡尔曼滤波是啥?

You can use a Kalman filter in any place where you have uncertain information about some dynamic system, and you can make an educated guess about what the system is going to do next. Even if messy reality comes along and interferes with the clean motion you guessed about, the Kalman filter will often do a very good job of figuring out what actually happened. And it can take advantage of correlations between crazy phenomena that you maybe wouldn’t have thought to exploit!

你可以在任何含有不确定信息的动态系统中使用卡尔曼滤波,然后你可以对系统下一步的动向做出合理的推测。即使有复杂的现实情况伴随和你能想到的对理想化运动的各种干扰,卡尔曼滤波总是能解算出实际发生的情况。它可以找到那些你可能根本考虑不到奇怪现象之间的相关性!

Kalman filters are ideal for systems which are continuously changing. They have the advantage that they are light on memory (they don’t need to keep any history other than the previous state), and they are very fast, making them well suited for real time problems and embedded systems.

卡尔曼滤波是针对那些连续变化系统的完美解决方案。它有个优点就是节省内存(它不需要去存储除了上一个状态的其他历史数据),它运行起来非常快,非常适合解决实时性问题和应用于嵌入式系统。

The math for implementing the Kalman filter appears pretty scary and opaque in most places you find on Google. That’s a bad state of affairs, because the Kalman filter is actually super simple and easy to understand if you look at it in the right way. Thus it makes a great article topic, and I will attempt to illuminate it with lots of clear, pretty pictures and colors. The prerequisites are simple; all you need is a basic understanding of probability and matrices.

你可能在谷歌上任何地方找的关于卡尔曼滤波的数学推导都是晦涩难懂的。这太糟糕了,因为如果你用一种正确的方式,卡尔曼滤波实际上是非常简单和容易去理解的。所以这是一个很棒的文章主题,我将用非常简单、漂亮的图片和色彩去解释它。前提条件是很简单的;你仅需要对概率论和矩阵有基本的了解。

I’ll start with a loose example of the kind of thing a Kalman filter can solve, but if you want to get right to the shiny pictures and math, feel free to jump ahead.

我将会从一个卡尔曼滤波可以解决的一个简单的小例子开始,但是如果你想直接看图解和数学的部分,请直接跳转。

原文这里有个类似超链接的索引,往下跳了一部分,我不知道咋搞,大家顺序往下看吧,都一样的。

What can we do with a Kalman filter?

卡尔曼滤波可以干啥?

Let’s make a toy example: You’ve built a little robot that can wander around in the woods, and the robot needs to know exactly where it is so that it can navigate.

我们拿小玩具举个例子:你造了一个小机器人在森林里瞎溜达,这个机器人需要准确地知道它在哪为了以便导航。

在这里插入图片描述
We’ll say our robot has a state x k ⃗ \vec{x_k} xk , which is just a position and a velocity:

我们的机器人是 x k ⃗ \vec{x_k} xk 状态,仅表示位置和速度:
x k ⃗ = ( p ⃗ , v ⃗ ) \vec{x_k} = (\vec{p}, \vec{v}) xk =(p ,v )
Note that the state is just a list of numbers about the underlying configuration of your system; it could be anything. In our example it’s position and velocity, but it could be data about the amount of fluid in a tank, the temperature of a car engine, the position of a user’s finger on a touchpad, or any number of things you need to keep track of.

要注意状态只是描述你系统基本参数的一系列数;他可以表示任何东西。在我们的例子中,它是位置和速度,也可以表示容器中的液体量,汽车发动机的温度,用户手指在触摸屏上的位置,或者你需要跟踪的任何事物。

Our robot also has a GPS sensor, which is accurate to about 10 meters, which is good, but it needs to know its location more precisely than 10 meters. There are lots of gullies and cliffs in these woods, and if the robot is wrong by more than a few feet, it could fall off a cliff. So GPS by itself is not good enough.

我们的机器人也有一个全球定位系统,可以精确到10米,这已经很不错了,但是它需要知道更加准确的位置。在这篇森林里有许多沟壑和悬崖,如果机器人有几英尺的偏差,就可能坠入悬崖。所以他自身的全球定位系统是远远不够的。

在这里插入图片描述
We might also know something about how the robot moves: It knows the commands sent to the wheel motors, and its knows that if it’s headed in one direction and nothing interferes, at the next instant it will likely be further along that same direction. But of course it doesn’t know everything about its motion: It might be buffeted by the wind, the wheels might slip a little bit, or roll over bumpy terrain; so the amount the wheels have turned might not exactly represent how far the robot has actually traveled, and the prediction won’t be perfect.

我们也大概知道机器人是如何运动的:靠发送机给轮子发送指令,如果它朝一个方向并且没有任何干扰,在下一个瞬间,它可能沿着相同的方向走得更远。但当然这不是这个运动的一切:它可能被风吹,轮子也可能会打滑,或者会在崎岖的地形上翻滚;所以轮子转过的数量不能准确地代表机器人实际走了多远,这样的预测并不完美。

The GPS sensor tells us something about the state, but only indirectly, and with some uncertainty or inaccuracy. Our prediction tells us something about how the robot is moving, but only indirectly, and with some uncertainty or inaccuracy.

GPS传感器只是间接地提供了一些状态信息,伴随一些不确定性和不准确性。我们的预测同样也是间接地告诉我们机器人如何运动,也是不确定和不准确的。

But if we use all the information available to us, can we get a better answer than either estimate would give us by itself? Of course the answer is yes, and that’s what a Kalman filter is for.

但是如果我们利用所有对我们有用的信息,我们能得到一个比任何一个估计本身更好的答案吗?答案是肯定的,这就卡尔曼滤波所做的。

How a Kalman filter sees your problem

卡尔曼滤波如何解决这个问题

Let’s look at the landscape we’re trying to interpret. We’ll continue with a simple state having only position and velocity.

来看一下我们试图解决的这个问题。我们继续用一个仅包含位置和速度的状态接着刚才的讨论。
x ⃗ = [ p v ] \vec{x} = \begin{bmatrix}p\\v\end{bmatrix} x =[pv]
We don’t know what the actual position and velocity are; there are a whole range of possible combinations of position and velocity that might be true, but some of them are more likely than others:

我们不知道真实的位置和速度是什么;这里有一个大的范围包含着所有可能的位置和速度的组合,他们都有可能是正确的,但是它们中的一部分的概率比剩下的更大:

在这里插入图片描述
The Kalman filter assumes that both variables (postion and velocity, in our case) are random and Gaussian distributed. Each variable has a mean value μ \mu μ, which is the center of the random distribution (and its most likely state), and a variance σ 2 \sigma^2 σ2, which is the uncertainty:

假设有两个变量(在我们的案例里是位置和速度)都是随机并且符合高斯分布的。每一个变量都有一个期望中值 μ \mu μ,它是随机分布的中心(它是最有可能的状态),还有一个方差 σ 2 \sigma^2 σ2,表示不确定性。

在这里插入图片描述
In the above picture, position and velocity are uncorrelated, which means that the state of one variable tells you nothing about what the other might be.

上图中,位置和速度是无关的,意思就是其中一个变量的状态不能告诉我们任何关于另一个变量的信息。

The example below shows something more interesting: Position and velocity are correlated. The likelihood of observing a particular position depends on what velocity you have:

下面这个例子展示了更有趣的东西:位置和速度是相关的。在某一个特定位置的可能性取决于它的速度:

在这里插入图片描述
This kind of situation might arise if, for example, we are estimating a new position based on an old one. If our velocity was high, we probably moved farther, so our position will be more distant. If we’re moving slowly, we didn’t get as far.

如果出现这种情况,例如,我们估计一个基于旧的位置的新的位置。如果速度很快,我们可能走得更远,所以我们的位置将会更远。如果我们移动得很慢,我们就到不了很远。

This kind of relationship is really important to keep track of, because it gives us more information: One measurement tells us something about what the others could be. And that’s the goal of the Kalman filter, we want to squeeze as much information from our uncertain measurements as we possibly can!

把握好这种关系非常重要,因为它可以给我们提供更多的信息:一个量告诉我们其他量可能是什么。这就是卡尔曼滤波的目的,我们想尽可能地从不确定的测量量中获取更多信息!

This correlation is captured by something called a covariance matrix. In short, each element of the matrix Σ i j \Sigma_{ij} Σij is the degree of correlation between the ith state variable and the jth state variable. (You might be able to guess that the covariance matrix is symmetric, which means that it doesn’t matter if you swap i and j). Covariance matrices are often labelled “ Σ \mathbf{\Sigma} Σ”, so we call their elements “ Σ i j \Sigma_{ij} Σij”.

这种相关性被一种叫协方差矩阵的东西所体现。简言之,矩阵中的每一个元素 Σ i j \Sigma_{ij} Σij 就是第 i 个和第 j 个状态变量之间的相关程度(你也许能猜到协方差矩阵是对称的,意思就是交换 i 和 j 对结果没有影响)。协方差矩阵通常记作 “ Σ \mathbf{\Sigma} Σ” ,所以我们称矩阵中的元素为 “ Σ i j \Sigma_{ij} Σij”。

在这里插入图片描述

Describing the problem with matrices

用矩阵描述问题

We’re modeling our knowledge about the state as a Gaussian blob, so we need two pieces of information at time k k k: We’ll call our best estimate x ^ k \mathbf{\hat{x}_k} x^k (the mean, elsewhere named μ \mu μ ), and its covariance matrix P k \mathbf{P_k} Pk.
x ^ k = [ position velocity ] P k = [ Σ p p Σ p v Σ v p Σ v v ] (1) \begin{aligned} \tag{1} \mathbf{\hat{x}}_k &= \begin{bmatrix} \text{position}\\ \text{velocity} \end{bmatrix}\\ \mathbf{P}_k &= \begin{bmatrix} \Sigma_{pp} & \Sigma_{pv} \\ \Sigma_{vp} & \Sigma_{vv} \\ \end{bmatrix} \end{aligned} x^kPk=[positionvelocity]=[ΣppΣvpΣpvΣvv](1)
(Of course we are using only position and velocity here, but it’s useful to remember that the state can contain any number of variables, and represent anything you want).

(当然我们这里只用位置和速度,但是记住状态可以包含任意变量的值,可以代表你想表示任何东西)

Next, we need some way to look at the current state (at time k-1) and predict the next state at time k. Remember, we don’t know which state is the “real” one, but our prediction function doesn’t care. It just works on all of them, and gives us a new distribution:

接下来,我们需要一些方式去观测当前状态k - 1 时刻)和预测下一时刻 k 的状态。记住,我们不知道那一个状态是真实的,但是我们的预测函数不管这些。他对所有的状态进行变换,给我们一个新的分布。

在这里插入图片描述
We can represent this prediction step with a matrix, F k \mathbf{F_k} Fk:

我们可以用一个矩阵 F k \mathbf{F_k} Fk 来代表这种预测:

在这里插入图片描述
It takes every point in our original estimate and moves it to a new predicted location, which is where the system would move if that original estimate was the right one.

它把原始分布中的每个点移动到一个新的预测位置,如果原始估算是正确的,那么这就是系统的新状态。

Let’s apply this. How would we use a matrix to predict the position and velocity at the next moment in the future? We’ll use a really basic kinematic formula:

实践起来吧。我们如何用矩阵来预测下一个时刻的位置和速度?我们将用一个非常基本的运动学公式:
p k = p k − 1 + Δ t v k − 1 v k = v k − 1 \begin{aligned} \color{deeppink}{\mathbf{p_k}} &= \color{royalblue}{p_{k-1}} + \Delta t &\color{royalblue}{v_{k-1}} \\ \color{deeppink}{v_k} &= &\color{royalblue}{v_{k-1}} \end{aligned} pkvk=pk1+Δt=vk1vk1 In other words: x ^ k = [ 1 Δ t 0 1 ] x ^ k − 1 (2) \begin{aligned} \tag{2} \color{deeppink}{\mathbf{\hat{x}}_k} &= \begin{bmatrix} 1 & \Delta t \\ 0 & 1 \end{bmatrix} \color{royalblue}{\mathbf{\hat{x}}_{k-1}} \\ \end{aligned} x^k=[10Δt1]x^k1(2) = F k x ^ k − 1 (3) \begin{aligned} \tag{3} &= \mathbf{F}_k \color{royalblue}{\mathbf{\hat{x}}_{k-1}} \end{aligned} =Fkx^k1(3)
We now have a prediction matrix which gives us our next state, but we still don’t know how to update the covariance matrix.

我们现在有一个预测矩阵,它给我们下一个状态,但是我们依然不知道如何更新协方差矩阵。

This is where we need another formula. If we multiply every point in a distribution by a matrix A \color{firebrick}{\mathbf{A}} A, then what happens to its covariance matrix Σ \Sigma Σ ?

这里我们需要另外一个公式。如果我们把该分布内的每一个点都乘一个矩阵 A \color{firebrick}{\mathbf{A}} A,那么协方差矩阵 Σ \Sigma Σ 将会有什么变化?

Well, it’s easy. I’ll just give you the identity:

贼简单,我给你公式:
C o v ( x ) = Σ C o v ( A x ) = A Σ A T (4) \begin{aligned} \tag{4} Cov(x) &= \Sigma\\ Cov(\color{firebrick}{\mathbf{A}}x) &= \color{firebrick}{\mathbf{A}} \Sigma \color{firebrick}{\mathbf{A}}^T \end{aligned} Cov(x)Cov(Ax)=Σ=AΣAT(4)
So combining ( 4 ) (4) (4) with equation ( 3 ) (3) (3):

所以结合等式 ( 4 ) (4) (4) 和等式 ( 3 ) (3) (3) 得:
x ^ k = F k x ^ k − 1 P k = F k P k − 1 F k T (5) \begin{aligned} \tag{5} \color{deeppink}{\mathbf{\hat{x}}_k} &= \mathbf{F}_k \color{royalblue}{\mathbf{\hat{x}}_{k-1}} \\ \color{deeppink}{\mathbf{P}_k} &= \mathbf{F_k} \color{royalblue}{\mathbf{P}_{k-1}} \mathbf{F}_k^T \end{aligned} x^kPk=Fkx^k1=FkPk1FkT(5)

External influence

外部影响

We haven’t captured everything, though. There might be some changes that aren’t related to the state itself— the outside world could be affecting the system.

不过,我们没有考虑到每一件事。可能有一些与状态本身无关的变化——外界可能影响着系统。

For example, if the state models the motion of a train, the train operator might push on the throttle, causing the train to accelerate. Similarly, in our robot example, the navigation software might issue a command to turn the wheels or stop. If we know this additional information about what’s going on in the world, we could stuff it into a vector called u k ⃗ \color{darkorange}{\vec{\mathbf{u}_k}} uk , do something with it, and add it to our prediction as a correction.

例如:如果该状态模拟火车的运动,火车驾驶员可能踩油门,导致火车加速。类似地,在我们的机器人的例子里,导航软件可能让轮子转动或停止。如果我们知道下一步会发生什么的其他信息,我们可以把它装进一个向量 u k ⃗ \color{darkorange}{\vec{\mathbf{u}_k}} uk ,稍加处理,然后作为一个修正加到我们的预测中。

Let’s say we know the expected acceleration a \color{darkorange}{a} a due to the throttle setting or control commands. From basic kinematics we get:

假设我们知道由于踩油门或者刹车产生的加速度是 a \color{darkorange}{a} a。由基本的运动学公式可得:
p k = p k − 1 + Δ t v k − 1 + 1 2 a Δ t 2 v k = v k − 1 + a Δ t \begin{aligned} \color{deeppink}{p_k} &= \color{royalblue}{p_{k-1}} + {\Delta t} &\color{royalblue}{v_{k-1}} + &\frac{1}{2} \color{darkorange}{a} {\Delta t}^2 \\ \color{deeppink}{v_k} &= &\color{royalblue}{v_{k-1}} + & \color{darkorange}{a} {\Delta t} \end{aligned} pkvk=pk1+Δt=vk1+vk1+21aΔt2aΔt In matrix form: x ^ k = F k x ^ k − 1 + [ Δ t 2 2 Δ t ] a = F k x ^ k − 1 + B k u k ⃗ (6) \begin{aligned} \tag{6} \color{deeppink}{\mathbf{\hat{x}}_k} &= \mathbf{F}_k \color{royalblue}{\mathbf{\hat{x}}_{k-1}} + \begin{bmatrix} \frac{\Delta t^2}{2} \\ \Delta t \end{bmatrix} \color{darkorange}{a} \\ &= \mathbf{F}_k \color{royalblue}{\mathbf{\hat{x}}_{k-1}} + \mathbf{B}_k \color{darkorange}{\vec{\mathbf{u}_k}} \end{aligned} x^k=Fkx^k1+[2Δt2Δt]a=Fkx^k1+Bkuk (6)
B k \mathbf{B}_k Bk is called the control matrix and u k ⃗ \color{darkorange}{\vec{\mathbf{u}_k}} uk the control vector. (For very simple systems with no external influence, you could omit these).

B k \mathbf{B}_k Bk 叫做控制矩阵, u k ⃗ \color{darkorange}{\vec{\mathbf{u}_k}} uk 叫做控制向量(对于那些没有外部影响的非常简单的系统,你可以忽略这些)。

Let’s add one more detail. What happens if our prediction is not a 100% accurate model of what’s actually going on?

让我们再添加一个细节。如果我们的预测不是一个100%准确的现实情况下的模型,会发生什么?

External uncertainty

外部不确定性因素

Everything is fine if the state evolves based on its own properties. Everything is still fine if the state evolves based on external forces, so long as we know what those external forces are.

如果状态都根据它自身的属性发展的话,那是很好的。如果状态根据它的外部控制而发生变化的话,也是很不错的,只要我们知道那些外部控制是啥。

But what about forces that we don’t know about? If we’re tracking a quadcopter, for example, it could be buffeted around by wind. If we’re tracking a wheeled robot, the wheels could slip, or bumps on the ground could slow it down. We can’t keep track of these things, and if any of this happens, our prediction could be off because we didn’t account for those extra forces.

如果是那些来自东方世界的神秘未知力量呢?我们正跟踪一个四轴飞行器,例如,它会被风吹。如果我们跟踪一个带轮子的机器人,轮子可能会打滑,或者地面上的坑坑洼洼会让它减速。我们不可能了解这些未知因素,如果这些中任意一个情况发生,我们的预测不可能准确,因为我们没有考虑这些外力作用。

We can model the uncertainty associated with the “world” (i.e. things we aren’t keeping track of) by adding some new uncertainty after every prediction step:

我们通过在每次预测步骤之后添加新的不确定因素,对这些与外界相关的不确定作用进行建模(就是那些我们无法预料的):

在这里插入图片描述
Every state in our original estimate could have moved to a range of states. Because we like Gaussian blobs so much, we’ll say that each point in x ^ k − 1 \color{royalblue}{\mathbf{\hat{x}}_{k-1}} x^k1 is moved to somewhere inside a Gaussian blob with covariance Q k \color{mediumaquamarine}{\mathbf{Q}_k} Qk. Another way to say this is that we are treating the untracked influences as noise with covariance Q k \color{mediumaquamarine}{\mathbf{Q}_k} Qk.

在原始估计中的每一个状态将会变成另一个新的状态。因为在现实中任何事物都倾向于高斯分布,所以我们说在 x ^ k − 1 \color{royalblue}{\mathbf{\hat{x}}_{k-1}} x^k1 中每一个点移动之后产生了一个协方差为 Q k \color{mediumaquamarine}{\mathbf{Q}_k} Qk 的高斯分布。换句话说,我们把那些不确定性因素当做协方差为 Q k \color{mediumaquamarine}{\mathbf{Q}_k} Qk 的噪声处理。

在这里插入图片描述
This produces a new Gaussian blob, with a different covariance (but the same mean):

这会产生一个新的高斯分布,它具有不同的协方差(均值是一样的):

在这里插入图片描述
We get the expanded covariance by simply adding Q k {\color{mediumaquamarine}{\mathbf{Q}_k}} Qk, giving our complete expression for the prediction step:

通过简单地加上 Q k {\color{mediumaquamarine}{\mathbf{Q}_k}} Qk 我们得到了一个更大的协方差,给出预测步骤完整的表达式:
x ^ k = F k x ^ k − 1 + B k u k ⃗ P k = F k P k − 1 F k T + Q k (7) \begin{aligned} \tag{7} \color{deeppink}{\mathbf{\hat{x}}_k} &= \mathbf{F}_k \color{royalblue}{\mathbf{\hat{x}}_{k-1}} + \mathbf{B}_k \color{darkorange}{\vec{\mathbf{u}_k}} \\ \color{deeppink}{\mathbf{P}_k} &= \mathbf{F_k} \color{royalblue}{\mathbf{P}_{k-1}} \mathbf{F}_k^T + \color{mediumaquamarine}{\mathbf{Q}_k} \end{aligned} x^kPk=Fkx^k1+Bkuk =FkPk1FkT+Qk(7)
In other words, the new best estimate is a prediction made from previous best estimate, plus a correction for known external influences.

换句话说,最优估算之前的最佳估算做出的预测,加上已知外部影响修正

And the new uncertainty is predicted from the old uncertainty, with some additional uncertainty from the environment.

和由旧的不确定性预测得到的新的不确定性,加上环境带来的其他不确定因素

All right, so that’s easy enough. We have a fuzzy estimate of where our system might be, given by x ^ k \color{deeppink}{\mathbf{\hat{x}}_k} x^k and P k \color{deeppink}{\mathbf{P}_k} Pk. What happens when we get some data from our sensors?

那好,这很容易。我们有了一个对系统状态模糊的估计,通过 x ^ k \color{deeppink}{\mathbf{\hat{x}}_k} x^k P k \color{deeppink}{\mathbf{P}_k} Pk 表示。当我们从传感器上再得到一些数据呢?

Refining the estimate with measurements

通过测量值完善估计值

We might have several sensors which give us information about the state of our system. For the time being it doesn’t matter what they measure; perhaps one reads position and the other reads velocity. Each sensor tells us something indirect about the state— in other words, the sensors operate on a state and produce a set of readings.

我们可能有几个传感器提供系统状态的信息。就目前来看,测量啥都无所谓;可能一个读位置另一个读速度。每一个传感器间接地告诉我们状态信息——换句话说,传感器运行在某一种状态产生了一系列读数

在这里插入图片描述
Notice that the units and scale of the reading might not be the same as the units and scale of the state we’re keeping track of. You might be able to guess where this is going: We’ll model the sensors with a matrix, H k \mathbf{H}_k Hk.

注意我们读到的可能和实际的状态有不一样的单位和尺度。可能你会猜到要干嘛:我们用 H k \mathbf{H}_k Hk 模拟传感器数据。

在这里插入图片描述
We can figure out the distribution of sensor readings we’d expect to see in the usual way:

我们还是用熟悉的方式解出传感器读数的分布:
μ ⃗ expected = H k x ^ k Σ expected = H k P k H k T (8) \begin{aligned} \tag{8} \vec{\mu}_{\text{expected}} &= \mathbf{H}_k \color{deeppink}{\mathbf{\hat{x}}_k} \\ \mathbf{\Sigma}_{\text{expected}} &= \mathbf{H}_k \color{deeppink}{\mathbf{P}_k} \mathbf{H}_k^T \end{aligned} μ expectedΣexpected=Hkx^k=HkPkHkT(8)
One thing that Kalman filters are great for is dealing with sensor noise. In other words, our sensors are at least somewhat unreliable, and every state in our original estimate might result in a range of sensor readings.

卡尔曼滤波擅长做的一件事是处理传感器噪声。换句话说,我们的传感器至少有些不可靠,我们原始估计的每一个状态状态可能都会导致传感器读数的波动。

在这里插入图片描述
From each reading we observe, we might guess that our system was in a particular state. But because there is uncertainty, some states are more likely than others to have have produced the reading we saw:

根据我们每一次观测的读数,我们可能猜到传感器处于一个怎样的状态。但由于存在不确定性,一些状态更有可能产生我们看到的读数:

在这里插入图片描述
We’ll call the covariance of this uncertainty (i.e. of the sensor noise) R k \color{mediumaquamarine}{\mathbf{R}_k} Rk. The distribution has a mean equal to the reading we observed, which we’ll call z k ⃗ \color{yellowgreen}{\vec{\mathbf{z}_k}} zk .

我们叫这种不确定性的方差表示为 R k \color{mediumaquamarine}{\mathbf{R}_k} Rk(就是传感器噪声)。这个分布有一个中值等于我们观测到的值用 z k ⃗ \color{yellowgreen}{\vec{\mathbf{z}_k}} zk 表示。

So now we have two Gaussian blobs: One surrounding the mean of our transformed prediction, and one surrounding the actual sensor reading we got.

所以现在我们有两个高斯分布:一个在预测值附近,一个在传感器读数附近。

在这里插入图片描述
We must try to reconcile our guess about the readings we’d see based on the predicted state (pink) with a different guess based on our sensor readings (green) that we actually observed.

我们必须调和基于预测粉色)的值和基于我们观测到的传感器读数绿色)的值。

So what’s our new most likely state? For any possible reading ( z 1 , z 2 ) (z_1,z_2) (z1,z2), we have two associated probabilities: (1) The probability that our sensor reading z k ⃗ \color{yellowgreen}{\vec{\mathbf{z}_k}} zk is a (mis-)measurement of ( z 1 , z 2 ) (z_1,z_2) (z1,z2), and (2) the probability that our previous estimate thinks ( z 1 , z 2 ) (z_1,z_2) (z1,z2) is the reading we should see.

所以新状态最有可能是什么?对于任何可能的读数 ( z 1 , z 2 ) (z_1,z_2) (z1,z2) 有两种相关的可能性:(1) 传感器读数 z k ⃗ \color{yellowgreen}{\vec{\mathbf{z}_k}} zk 一部分,(2) 预测值占一部分。

If we have two probabilities and we want to know the chance that both are true, we just multiply them together. So, we take the two Gaussian blobs and multiply them:

如果我们有两种可能性,我们想知道这两种情况都为真的概率,把他们相乘就好了。所以,我们把两个高斯分布相乘:

在这里插入图片描述
What we’re left with is the overlap, the region where both blobs are bright/likely. And it’s a lot more precise than either of our previous estimates. The mean of this distribution is the configuration for which both estimates are most likely, and is therefore the best guess of the true configuration given all the information we have.

我们剩下的就是重叠部分——两个分布都有可能的区域。而且比我们之前估计的任何一个都准确。这个分布的意义就是两种估计最有可能的分布,因此这就是基于我们拿到的所有信息对真实情况的最佳猜测

Hmm. This looks like another Gaussian blob.

这看起来像另一个高斯分布:

在这里插入图片描述
As it turns out, when you multiply two Gaussian blobs with separate means and covariance matrices, you get a new Gaussian blob with its own mean and covariance matrix! Maybe you can see where this is going: There’s got to be a formula to get those new parameters from the old ones!

事实证明,如果你把两个不同的高斯分布相乘,你会得到一个新的高斯分布!咱们直接上公式吧!

Combining Gaussians

融合高斯分布

Let’s find that formula. It’s easiest to look at this first in one dimension. A 1D Gaussian bell curve with variance σ 2 \sigma^2 σ2 and mean μ \mu μ is defined as:

先看一维的比较简单。方差是 σ 2 \sigma^2 σ2,中值是 μ \mu μ 的一维高斯分布可以表示为:
N ( x , μ , σ ) = 1 σ 2 π e − ( x – μ ) 2 2 σ 2 (9) \begin{aligned} \tag{9} \mathcal{N}(x, \mu,\sigma) = \frac{1}{ \sigma \sqrt{ 2\pi } } e^{ -\frac{ (x – \mu)^2 }{ 2\sigma^2 } } \end{aligned} N(x,μ,σ)=σ2π 1e2σ2(xμ)2(9)
We want to know what happens when you multiply two Gaussian curves together. The blue curve below represents the (unnormalized) intersection of the two Gaussian populations:

把两个高斯分布相乘会得到什么呢。下面的蓝色曲线就代表的是两个高斯分布的交集(未归一化):
在这里插入图片描述
N ( x , μ 0 , σ 0 ) ⋅ N ( x , μ 1 , σ 1 ) = ? N ( x , μ ’ , σ ’ ) (10) \begin{aligned} \tag{10} \mathcal{N}(x, \color{fuchsia}{\mu_0}, \color{deeppink}{\sigma_0}) \cdot \mathcal{N}(x, \color{yellowgreen}{\mu_1}, \color{mediumaquamarine}{\sigma_1}) \stackrel{?}{=} \mathcal{N}(x, \color{royalblue}{\mu’}, \color{mediumblue}{\sigma’}) \end{aligned} N(x,μ0,σ0)N(x,μ1,σ1)=?N(x,μ,σ)(10)
You can substitute equation ( 9 ) (9) (9) into equation ( 10 ) (10) (10) and do some algebra (being careful to renormalize, so that the total probability is 1) to obtain:

你可以把 ( 9 ) (9) (9) 代入 ( 10 ) (10) (10) 里面去将得到(注意重新归一化,使和为 1):
μ ’ = μ 0 + σ 0 2 ( μ 1 – μ 0 ) σ 0 2 + σ 1 2 σ ’ 2 = σ 0 2 – σ 0 4 σ 0 2 + σ 1 2 (11) \begin{aligned} \tag{11} \color{royalblue}{\mu’} &= \mu_0 + \frac{\sigma_0^2 (\mu_1 – \mu_0)} {\sigma_0^2 + \sigma_1^2}\\ \color{mediumblue}{\sigma’}^2 &= \sigma_0^2 – \frac{\sigma_0^4} {\sigma_0^2 + \sigma_1^2} \end{aligned} μσ2=μ0+σ02+σ12σ02(μ1μ0)=σ02σ02+σ12σ04(11)

推导过程可以看我这篇博客

We can simplify by factoring out a little piece and calling it k \color{purple}{\mathbf{k}} k:

我们可以简单地分离出一小部分,表示为 k \color{purple}{\mathbf{k}} k
k = σ 0 2 σ 0 2 + σ 1 2 (12) \begin{aligned} \tag{12} \color{purple}{\mathbf{k}} = \frac{\sigma_0^2}{\sigma_0^2 + \sigma_1^2} \end{aligned} k=σ02+σ12σ02(12) μ ’ = μ 0 + k ( μ 1 – μ 0 ) σ ’ 2 = σ 0 2 – k σ 0 2 (13) \begin{aligned} \tag{13} \color{royalblue}{\mu’} &= \mu_0 + &\color{purple}{\mathbf{k}} (\mu_1 – \mu_0)\\ \color{mediumblue}{\sigma’}^2 &= \sigma_0^2 – &\color{purple}{\mathbf{k}} \sigma_0^2 \end{aligned} μσ2=μ0+=σ02k(μ1μ0)kσ02(13)

Take note of how you can take your previous estimate and add something to make a new estimate. And look at how simple that formula is!

你看你是如何把你之前的估计加上一些东西就得到了新的预测。这公式多简单啊!

But what about a matrix version? Well, let’s just re-write equations ( 12 ) (12) (12) and ( 13 ) (13) (13) in matrix form. If Σ \Sigma Σ is the covariance matrix of a Gaussian blob, and μ ⃗ \vec{\mu} μ its mean along each axis, then:

矩阵形式是什么样的?咱们把 ( 12 ) (12) (12) ( 13 ) (13) (13) 改写成矩阵形式。设 Σ \Sigma Σ 是高斯分布的协方差, μ ⃗ \vec{\mu} μ 是每个维度的均值,那么:
K = Σ 0 ( Σ 0 + Σ 1 ) − 1 (14) \begin{aligned} \tag{14} \color{purple}{\mathbf{K}} = \Sigma_0 (\Sigma_0 + \Sigma_1)^{-1} \end{aligned} K=Σ0(Σ0+Σ1)1(14) μ ⃗ ’ = μ 0 ⃗ + K ( μ 1 ⃗ – μ 0 ⃗ ) Σ ’ = Σ 0 – K Σ 0 (15) \begin{aligned} \tag{15} \color{royalblue}{\vec{\mu}’} &= \vec{\mu_0} + &\color{purple}{\mathbf{K}} (\vec{\mu_1} – \vec{\mu_0})\\ \color{mediumblue}{\Sigma’} &= \Sigma_0 – &\color{purple}{\mathbf{K}} \Sigma_0 \end{aligned} μ Σ=μ0 +=Σ0K(μ1 μ0 )KΣ0(15)
K \color{purple}{\mathbf{K}} K is a matrix called the Kalman gain, and we’ll use it in just a moment.

K \color{purple}{\mathbf{K}} K 就是卡尔曼增益,我们即将用到它。

Easy! We’re almost finished!

妙啊!马上就完事儿了!

Putting it all together

把所有的都整合到一起

We have two distributions: The predicted measurement with ( μ 0 , Σ 0 ) = ( H k x ^ k , H k P k H k T ) (\color{fuchsia}{\mu_0}, \color{deeppink}{\Sigma_0}) = (\color{fuchsia}{\mathbf{H}_k \mathbf{\hat{x}}_k}, \color{deeppink}{\mathbf{H}_k \mathbf{P}_k \mathbf{H}_k^T}) (μ0,Σ0)=(Hkx^k,HkPkHkT), and the observed measurement with ( μ 1 , Σ 1 ) = ( z k ⃗ , R k ) (\color{yellowgreen}{\mu_1}, \color{mediumaquamarine}{\Sigma_1}) = (\color{yellowgreen}{\vec{\mathbf{z}_k}}, \color{mediumaquamarine}{\mathbf{R}_k}) (μ1,Σ1)=(zk ,Rk). We can just plug these into equation ( 15 ) (15) (15) to find their overlap:

我们现在有两个分布:一个是预测值 ( μ 0 , Σ 0 ) = ( H k x ^ k , H k P k H k T ) (\color{fuchsia}{\mu_0}, \color{deeppink}{\Sigma_0}) = (\color{fuchsia}{\mathbf{H}_k \mathbf{\hat{x}}_k}, \color{deeppink}{\mathbf{H}_k \mathbf{P}_k \mathbf{H}_k^T}) (μ0,Σ0)=(Hkx^k,HkPkHkT),另一个是观察值 ( μ 1 , Σ 1 ) = ( z k ⃗ , R k ) (\color{yellowgreen}{\mu_1}, \color{mediumaquamarine}{\Sigma_1}) = (\color{yellowgreen}{\vec{\mathbf{z}_k}}, \color{mediumaquamarine}{\mathbf{R}_k}) (μ1,Σ1)=(zk ,Rk)。我们把他们代入到 ( 15 ) (15) (15) 中算出重叠部分:
H k x ^ k ’ = H k x ^ k + K ( z k ⃗ – H k x ^ k ) H k P k ’ H k T = H k P k H k T – K H k P k H k T (16) \begin{aligned} \tag{16} \mathbf{H}_k \color{royalblue}{\mathbf{\hat{x}}_k’} &= \color{fuchsia}{\mathbf{H}_k \mathbf{\hat{x}}_k} & + & \color{purple}{\mathbf{K}} ( \color{yellowgreen}{\vec{\mathbf{z}_k}} – \color{fuchsia}{\mathbf{H}_k \mathbf{\hat{x}}_k} ) \\ \mathbf{H}_k \color{royalblue}{\mathbf{P}_k’} \mathbf{H}_k^T &= \color{deeppink}{\mathbf{H}_k \mathbf{P}_k \mathbf{H}_k^T} & – & \color{purple}{\mathbf{K}} \color{deeppink}{\mathbf{H}_k \mathbf{P}_k \mathbf{H}_k^T} \end{aligned} Hkx^kHkPkHkT=Hkx^k=HkPkHkT+K(zk Hkx^k)KHkPkHkT(16)
And from ( 14 ) (14) (14), the Kalman gain is:

( 14 ) (14) (14),可得卡尔曼增益为:
K = H k P k H k T ( H k P k H k T + R k ) − 1 (17) \begin{aligned} \tag{17} \color{purple}{\mathbf{K}} = \color{deeppink}{\mathbf{H}_k \mathbf{P}_k \mathbf{H}_k^T} ( \color{deeppink}{\mathbf{H}_k \mathbf{P}_k \mathbf{H}_k^T} + \color{mediumaquamarine}{\mathbf{R}_k})^{-1} \end{aligned} K=HkPkHkT(HkPkHkT+Rk)1(17)
We can knock an H k \mathbf{H}_k Hk off the front of every term in ( 16 ) (16) (16) and ( 17 ) (17) (17) (note that one is hiding inside K \color{purple}{\mathbf{K}} K ), and an H k T \mathbf{H}_k^T HkT off the end of all terms in the equation for P k ’ \color{royalblue}{\mathbf{P}_k’} Pk.

我们可以把 ( 16 ) (16) (16) ( 17 ) (17) (17) 中的 H k \mathbf{H}_k Hk 约掉(注意有一个隐藏在 K \color{purple}{\mathbf{K}} K 里),再把 P k ’ \color{royalblue}{\mathbf{P}_k’} Pk 这一项中的 H k T \mathbf{H}_k^T HkT 约掉。
x ^ k ’ = x ^ k + K ’ ( z k ⃗ – H k x ^ k ) P k ’ = P k – K ’ H k P k (18) \begin{aligned} \tag{18} \color{royalblue}{\mathbf{\hat{x}}_k’} &= \color{fuchsia}{\mathbf{\hat{x}}_k} & + & \color{purple}{\mathbf{K}’} ( \color{yellowgreen}{\vec{\mathbf{z}_k}} – \color{fuchsia}{\mathbf{H}_k \mathbf{\hat{x}}_k} ) \\ \color{royalblue}{\mathbf{P}_k’} &= \color{deeppink}{\mathbf{P}_k} & – & \color{purple}{\mathbf{K}’} \color{deeppink}{\mathbf{H}_k \mathbf{P}_k} \end{aligned} x^kPk=x^k=Pk+K(zk Hkx^k)KHkPk(18) K ’ = P k H k T ( H k P k H k T + R k ) − 1 (19) \begin{aligned} \tag{19} \color{purple}{\mathbf{K}’} = \color{deeppink}{\mathbf{P}_k \mathbf{H}_k^T} ( \color{deeppink}{\mathbf{H}_k \mathbf{P}_k \mathbf{H}_k^T} + \color{mediumaquamarine}{\mathbf{R}_k})^{-1} \end{aligned} K=PkHkT(HkPkHkT+Rk)1(19)
…giving us the complete equations for the update step.

…得到了更新步骤的完整公式。

And that’s it! x ^ k ’ \color{royalblue}{\mathbf{\hat{x}}_k’} x^k is our new best estimate, and we can go on and feed it (along with P k ’ \color{royalblue}{\mathbf{P}_k’} Pk ) back into another round of predict or update as many times as we like.

就是这样! x ^ k ’ \color{royalblue}{\mathbf{\hat{x}}_k’} x^k 是最优估计,我们可以继续把它(和 P k ’ \color{royalblue}{\mathbf{P}_k’} Pk)继续迭代。

在这里插入图片描述

Wrapping up

收尾

Of all the math above, all you need to implement are equations ( 7 ) (7) (7), ( 18 ) (18) (18), and ( 19 ) (19) (19). (Or if you forget those, you could re-derive everything from equations ( 4 ) (4) (4) and ( 15 ) (15) (15).)

对于以上所有的公式,应用的时候只需要用到 ( 7 ) (7) (7), ( 18 ) (18) (18) ( 19 ) (19) (19)(如果你忘了,你可以根据 ( 4 ) (4) (4) ( 15 ) (15) (15) 重新推导一下)。

This will allow you to model any linear system accurately. For nonlinear systems, we use the extended Kalman filter, which works by simply linearizing the predictions and measurements about their mean. (I may do a second write-up on the EKF in the future).

这能够让你对任何线性系统进行精确地建模。对于非线性模型,我们用扩展卡尔曼滤波,它把预测和测量部分的均值进行线性化(我以后可能会再写EKF)。

If I’ve done my job well, hopefully someone else out there will realize how cool these things are and come up with an unexpected new place to put them into action.

如果我这篇文章写的不错,希望不了解卡尔曼滤波的人知道它有多牛逼,然后把它应用到一些新地方。

done!

如果有需要1960论文原文的读者,可以留言邮箱,我发给你,这里上传需要转md格式,懒得转了。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值