简体   繁体   English

卡尔曼滤波器(一维):几种方法?

[英]Kalman filter (one-dimensional): several approaches?

I try to understand how the Kalman filter works and because the multi-dimensional variants were too confusing for the beginning I started off with a one-dimensional example.我试图了解卡尔曼滤波器的工作原理,并且因为多维变体对于一开始来说太混乱了,所以我从一维示例开始。

I found 3 different sources explaining the scenario of a thermometer but all of these scenarios implement slightly different equations and I do not get the point.我找到了 3 个不同的来源来解释温度计的场景,但所有这些场景都实现了略有不同的方程,我不明白这一点。

I implemented solution 2 but my kalman filter was not really working (it highly adapted itself to the measurements and not really considered the noise on it).我实施了解决方案 2,但我的卡尔曼滤波器并没有真正起作用(它高度适应测量,并没有真正考虑其上的噪声)。

So, before I waste more time trying solution 1 or 3 (which I have just read until now): Can someone supply a clean explanation and/or code example for a one dimensional Kalman filter?因此,在我浪费更多时间尝试解决方案 1 或 3(直到现在我才读过)之前:有人可以为一维卡尔曼滤波器提供清晰的解释和/或代码示例吗?


Solution 1解决方案1

// x_est: current estimate;           p: current estimate error;
// a:     constant of the system;    kg: kalman gain
// z:     current observation; 

// Predict
x_est   =   a * x_est
p       =   a * p * a

// Update
kg      =   p  / (p  + r)
x_est   =   x_est + kg * (z - x_est)
p       =   (1 - kg) * p

The author (here) only explains that we are changing only current values because there is no need for a thermometer to consider the last value.作者(此处)仅说明我们仅更改当前值,因为温度计无需考虑最后一个值。

So he simplified:所以他简化了:

p[k] = (1 - kg) * p[k-1] to p = (1 - kg) * p p[k] = (1 - kg) * p[k-1]p = (1 - kg) * p

x_est[k] = x_est[k-1] + kg * (z - x_est[k-1]) to x_est = x_est + kg * (z - x_est) x_est[k] = x_est[k-1] + kg * (z - x_est[k-1])x_est = x_est + kg * (z - x_est)

...and so on... ...等等...

I do not understand why this is even possible.我不明白为什么这是可能的。 I thought one of the main parts of the Kalman filter is to consider wether the current observation z is useful or not (via the Kalman gain).我认为卡尔曼滤波器的主要部分之一是考虑当前观察值z是否有用(通过卡尔曼增益)。 So that for a high Kalman gain kg * (z - x_est[k-1]) a "big chunk" of the delta z - x_est[k-1] is added to the new estimate.因此,对于高卡尔曼增益kg * (z - x_est[k-1]) ,将增量z - x_est[k-1]的“大块”添加到新估计中。 Isn't this whole thing getting pointless, if one always calculates the current values?如果总是计算当前值,这整件事不是变得毫无意义吗?


Solution 2解决方案2

# q: process variance / process noise
# r: error in measurement

x_est = x_est
p     = p + q;

k     = p / (p + r);
x_est = x_est + k * (z – x_est);
p     = (1 – k) * p;

This is pretty much the same, but the author did not even give an explanation why x[k-1] and p[k-1] can be altered to x and p .这几乎是一样的,但作者甚至没有解释为什么x[k-1]p[k-1]可以更改为xp


Solution 3解决方案3

# Q: process variance / process noise
# R: error in measurement

# prediction
x_est_kminus1[k] = x_est[k - 1]
p_kminus1[k]        = p[k - 1] + Q

# update
kg[k]     = p_kminus1[k] / (p_kminus1[k] + R)
x_est[k] = x_est_kminus1[k] + kg[k] * (z[k] - x_est_kminus1[k])
p[k]     = (1 - kg[k]) * p_kminus1[k]

In this solution the author had two different lists for x_est ( x_est itself and x_est_kminus1 ) and p ( p itself and p_kminus1 ).在这个解决方案中,作者有两个不同的列表用于x_estx_est本身和x_est_kminus1 )和pp本身和p_kminus1 )。

Are two lists needed because otherwise p[k] would be calculated twice (in the prediction and the update step)?是否需要两个列表,否则 p[k] 将被计算两次(在预测和更新步骤中)?

All of these solutions are special cases of the general equations and we'll have to see what's special about each one.所有这些解都是一般方程的特例,我们必须看看每个方程有什么特别之处。

Proper equations适当的方程

Let's start with the proper general equations for the 1D case:让我们从一维情况的适当一般方程开始:

# prediction
x[k] = a * x[k - 1]
p[k] = a * p[k - 1] * a + q
# update
y = z - h * x[k]
kg = p * h / (h * p * h + r)
x[k] = x[k] + kg * y
p[k] = (1 - kg * h) * p[k]
  • x - state x - 状态
  • p - error (covariance) p - 误差(协方差)
  • a - state transition a - 状态转换
  • q - transition error q - 转换错误
  • z - measurement z - 测量
  • h - state-to-measurement transformation h - 状态到测量的转换
  • y - difference between what we would have expected to measure based on the prediction and what we actually measured y - 我们根据预测期望测量的值与我们实际测量的值之间的差异
  • kg - kalman gain kg - 卡尔曼增益
  • r - measurement error r - 测量误差

All of the parameters of the model ( a , q , r , h ) could in principal also have an index k and change as the system evolves.模型的所有参数( aqrh )原则上也可以有一个索引k并随着系统的发展而变化。 But in simple cases they can all be taken as constant.但在简单的情况下,它们都可以视为常数。

How the solutions differ from the proper equations解与正确方程有何不同

Only solution 1 implements an a and that's fine.只有解决方案 1 实现了a ,这很好。 a tells you how the state changes from one step to the other, if you assume the temperature to be stationary then a == 1 , like in solution 2 and 3. a告诉您状态如何从一个步骤更改为另一个步骤,如果您假设温度是固定的,那么a == 1 ,就像在解决方案 2 和 3 中一样。

Solution 1 does not have a q .解决方案 1 没有q q is where we can give an estimate of the process error. q是我们可以估计过程误差的地方。 Again, if the process is about the system being stationary ( a == 1 ) then we could set q = 0 .同样,如果该过程是关于系统静止的( a == 1 ),那么我们可以设置q = 0

None of your solutions have an h , which is the observation transformation (how to get from measurement to state).您的解决方案都没有h ,这是观察转换(如何从测量到状态)。 If you are estimating the temperature, based on measurements of the temperature then h = 1 .如果您要根据温度的测量值估计温度,则h = 1

An example of when h may be different from 1 is if you were measuring something else than you are interested in estimating, eg using a measurement of humidity to estimate the temperature. h可能与 1 不同的一个例子是,如果您测量的不是您感兴趣的估计值,例如使用湿度测量值来估计温度。 Then h would be the linear transformation T(humidity) = h * humidity .那么h将是线性变换T(humidity) = h * humidity I emphasize linear because the above are the linear Kalman filter equations and they only apply to linear (in the mathematical sense) systems.我强调线性,因为以上是线性卡尔曼滤波器方程,它们仅适用于线性(在数学意义上)系统。

Current and previous step issue当前和上一步问题

The question of k vs. k - 1 and having x_est and x_est_kminus1 is purely a matter of implementation. kk - 1以及具有x_estx_est_kminus1的问题纯粹是实现问题。 In this regard all your solutions are the same.在这方面,您的所有解决方案都是相同的。

Your thinking about k and k - 1 in solution 1 is off.您对解决方案 1 中的kk - 1想法是错误的。 Only the prediction stage needs to think about the current and the previous step (since it's a prediction of the current state based on the previous one), not the update step.只有预测阶段需要考虑当前和上一步(因为它是基于上一步对当前状态的预测),而不是更新步骤。 The update step acts on the prediction.更新步骤作用于预测。

From a readability point of view solution 3 is closest to the mathematical equations.从可读性的角度来看,解决方案 3 最接近数学方程。 In principal the prediction step does not give us x_est[k] yet but more like predicted_x_est[k] .原则上,预测步骤还没有给我们x_est[k] ,但更像是x_est[k] predicted_x_est[k] Then the update step runs on this predicted_x_est[k] and gives us our actual x_est[k] .然后在这个更新步骤运行predicted_x_est[k]和赋予我们的实际x_est[k]

However as I said, all implementations are equivalent because when they are programmed, you can see that after the prediction step, the past is not needed any more.然而正如我所说,所有的实现都是等价的,因为当它们被编程时,你可以看到在预测步骤之后,过去不再需要了。 So you can safely use one variable for p and x without needing to keep a list.因此,您可以安全地为px使用一个变量,而无需保留列表。

About kalman gain关于卡尔曼增益

You wrote:你写了:

So that for a high Kalman gain kg * (z - x_est[k-1]) a "big chunk" of the delta z - x_est[k-1] is added to the new estimate.因此,对于高卡尔曼增益 kg * (z - x_est[k-1]),将增量 z - x_est[k-1] 的“大块”添加到新估计中。 Isn't this whole thing getting pointless, if one always calculates the current values?如果总是计算当前值,这整件事不是变得毫无意义吗?

In these cases the kalman gain can only be between 0 and 1. When is it biggest?在这些情况下,卡尔曼增益只能在 0 和 1 之间。什么时候最大? When r (measurement error) is 0, which means that we infinitely trust our measurements.r (测量误差)为 0 时,这意味着我们无限信任我们的测量结果。 The equation then simplifies to然后等式简化为

x_est = x_est + z - x_est

which means that we discard our predicted value (the x_est on the right hand side) and set our updated estimate equal to our measurement.这意味着我们丢弃我们的预测值(右侧的x_est )并将我们更新的估计值设置为等于我们的测量值。 This is a valid thing to do when we infinitely trust what we measure.当我们无限信任我们测量的东西时,这是一件有效的事情。

Adapting to measurements适应测量

I implemented solution 2 but my kalman filter was not really working (it highly adapted itself to the measurements and not really considered the noise on it).我实施了解决方案 2,但我的卡尔曼滤波器并没有真正起作用(它高度适应测量,并没有真正考虑其上的噪声)。

Tuning a Kalman Filter is tricky, and requires deep knowledge of the system and proper estimates of q and r .调整卡尔曼滤波器很棘手,需要对系统有深入的了解以及对qr正确估计。 Remember that q is the error on the process (state evolution) and r is the error on our measurements.请记住, q是过程(状态演化)的误差, r是我们测量的误差。 If your Kalman filter is adapting itself too much to the measurements it means that:如果您的卡尔曼滤波器过度适应测量,则意味着:

  • q is too large q太大
  • r is too small r太小

or a combination of the two.或两者的结合。 You will have to play with the values to find ones that work.您将不得不玩弄这些值以找到有效的值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM