简体   繁体   English

卡尔曼滤波器行为

[英]Kalman filter behaviour

I have used the kalman filter implented here: https://gist.github.com/alexbw/1867612 我使用过这里实现的kalman过滤器: https ://gist.github.com/alexbw/1867612

I have a very basic understanding of it. 我对它有一个非常基本的了解。 this is the test code I have: 这是我的测试代码:

import matplotlib.pyplot as plt
import numpy as np
from Kalman import Kalman

n = 50    
d = 5

xf = np.zeros(n - d)
yf = np.zeros(n - d)

xp = np.zeros(d)
yp = np.zeros(d)

x = np.zeros(n)
y = np.zeros(n)

for i in range(n):

    if i==0:
        x[i] = 05
        y[i] = 20
        KLF = Kalman(6, 2)

    elif i< (n - d):
        xf[i], yf[i] = KLF.predict()  
        x[i] = x[i-1] + 1
        y[i] = y[i-1] + np.random.random() * 10
        NewPoint = np.r_[x[i], y[i]]
        KLF.update(NewPoint)
    else:
        x[i] = x[i-1] + 1
        y[i] = y[i-1] + np.random.random() * 10
        xp[n - i -1], yp[n - i -1] = KLF.predict()  
        NewPoint = np.r_[x[i] , yp[n - i -1]]
        KLF.update(NewPoint)

plt.figure(1)
plt.plot(x, y, 'ro') #original
plt.plot(xp, yp, 'go-') #predicted kalman
plt.plot(xf, yf, 'b') #kalman filter
plt.legend( ('Original', 'Prediction', 'Filtered') ) 
plt.show()

在此输入图像描述

My question is, why does the kalman filtering starts at 0 if the data starts at x=5, y=20 ? 我的问题是,如果数据从x = 5,y = 20开始,为什么卡尔曼滤波从0开始? Is that some sort of standard behaviour? 这是某种标准行为吗?

Thanks 谢谢

The current state of the Kalman instance is stored in the x attribute: Kalman实例的当前状态存储在x属性中:

In [48]: KLF = Kalman(6, 2)

In [49]: KLF.x
Out[49]: 
matrix([[ 0.],
        [ 0.],
        [ 0.],
        [ 0.],
        [ 0.],
        [ 0.]])

The six components represent the position, velocity and acceleration. 六个分量代表位置,速度和加速度。 So by default the Kalman instance starts at (0,0) with zero velocity and acceleration. 因此,默认情况下,Kalman实例从(0,0)开始,速度和加速度为零。

After instantiating KLF , when i=1 , the first modification to xf and yf is made by calling KLF.predict : 在实例化KLF ,当i=1 ,通过调用KLF.predictxfyf进行第一次修改:

xf[i], yf[i] = KLF.predict()

There are two problems with this. 这有两个问题。 First, xf[0], yf[0] is never updated, so it remains at (0, 0) . 首先, xf[0], yf[0]永远不会更新,因此它保持在(0, 0) Hence the blue line starting at (0, 0) . 因此蓝线从(0, 0)

The second problem is that the current state of KLF.x is at (0, 0) by default, due to the way the Kalman class is defined. 第二个问题是由于Kalman类的定义方式, KLF.x的当前状态KLF.x(0, 0) If you want the KLF instance to begin with a position at (5, 20) then you'll need to modify KLF.x yourself. 如果你希望KLF实例以(5, 20) KLF.x (5, 20)的位置开头,那么你需要自己修改KLF.x

Also bear in mind that the Kalman filter is meant to be updated with an observation first and then make a prediction second . 还要记住,卡尔曼滤波器应先用观测值更新,然后再进行预测 This is mentioned in the class docstring. 这在docstring类中提到。

Now I don't quite understand the intent of your code so I'm not going to try to sort out how the update s should come before the predict s, but as far as setting the initial state is concerned, you could use this: 现在我不太明白你的代码的意图,所以我不打算在predict之前弄清楚update应该如何,但就设置初始状态而言,你可以使用:

if i==0:
    x[i] = 5
    y[i] = 20
    KLF = Kalman(6, 2)
    KLF.x[:2] = np.matrix((x[0], y[0])).T
    xf[i], yf[i] = KLF.predict()  

which yields 产量

在此输入图像描述

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

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