我使用了这里植入的卡尔曼滤波器: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开始?这是某种标准的行为吗?
谢谢
发布于 2014-08-30 10:37:53
Kalman实例的当前状态存储在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.predict对xf和yf进行第一次修改
xf[i], yf[i] = KLF.predict()这有两个问题。首先,xf[0], yf[0]从来没有更新过,所以它仍然保留在(0, 0)上。因此,从(0, 0)开始的蓝线。
第二个问题是,由于卡尔曼类的定义方式,默认情况下KLF.x的当前状态处于(0, 0)状态。如果希望KLF实例从(5, 20)的职位开始,那么您需要自己修改KLF.x。
还请记住,Kalman滤波器意味着首先要用观测来更新,然后再进行预测。在类docstring中提到了这一点。
现在,我不太明白您的代码的意图,所以我不打算尝试将updates放在predicts之前,但是就设置初始状态而言,您可以使用以下方法:
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() 产额

https://stackoverflow.com/questions/25581263
复制相似问题