[英]Kalman filter behaviour
我使用过这里实现的kalman过滤器: 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开始? 这是某种标准行为吗?
谢谢
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)
。
第二个问题是由于Kalman类的定义方式, KLF.x
的当前状态KLF.x
为(0, 0)
。 如果你希望KLF
实例以(5, 20)
KLF.x
(5, 20)
的位置开头,那么你需要自己修改KLF.x
还要记住,卡尔曼滤波器应先用观测值更新,然后再进行预测 。 这在docstring类中提到。
现在我不太明白你的代码的意图,所以我不打算在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()
产量
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.