2014-08-30 124 views
1

我已经用在这里implented卡尔曼滤波器: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() 

enter image description here

我的问题是,为何kalman滤波从0开始,如果数据开始在x = 5,Y = 20? 这是一种标准的行为?

由于

回答

3

卡尔曼实例的当前状态被保存在属性x

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

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

六个分量表示的位置,速度和加速度。因此,默认情况下,卡尔曼实例的启动速度为(0,0),速度和加速度均为零。

实例KLF,当i=1,首先修改xfyf通过调用KLF.predict后作:

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

有两个问题。首先,xf[0], yf[0]从不更新,所以它仍然在(0, 0)。因此蓝线从(0, 0)开始。

第二个问题是KLF.x的当前状态默认为(0, 0),这是由于定义了卡尔曼类的方式。 如果您希望KLF实例以(5, 20)的位置开头,那么您需要自己修改KLF.x

记还承担了卡尔曼滤波器是指与观察第一被更新,然后作出预测第二。 这是在类docstring中提到的。

现在我不太了解你的代码的意图,所以我不会试图理清update应该在predict之前,但就设置初始状态而言,你可以使用此:

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