卡尔曼滤波器的行为特点

4

我使用了这里实现的卡尔曼滤波器:https://gist.github.com/alexbw/1867612

我对它有一个非常基本的理解。这是我所拥有的测试代码:

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()

我的问题是,如果数据从x=5,y=20开始,为什么卡尔曼滤波要从0开始?这是某种标准行为吗?
谢谢。
1个回答

8
当前的卡尔曼实例状态存储在x属性中:
In [48]: KLF = Kalman(6, 2)

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

这六个组件代表了位置、速度和加速度。因此,默认情况下,Kalman实例从(0,0)开始,速度和加速度均为零。

在实例化KLF后,当i=1时,首先通过调用KLF.predictxfyf进行第一次修改:

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

这里有两个问题。首先,xf[0],yf[0]从未更新,因此它的值仍然是(0,0),因此蓝色线从(0,0)开始。
其次,由于Kalman类的定义方式,KLF.x当前状态默认为(0,0)。如果您想让KLF实例从位置(5,20)开始,则需要自己修改KLF.x
还要注意,Kalman滤波器首先应该使用“观察结果”进行更新,然后才能进行“预测”。这在类docstring中有提到。
现在我不太了解您代码的意图,所以我不会试图弄清楚何时需要使用updatepredict,但就设置初始状态而言,您可以使用以下内容:
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()  

产生

enter image description here


太好了。在这种情况下如何计算R2得分?你能帮我吗?谢谢。 - Redhwan

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接