input:
n :状态维度
alpha :均值附近sigma点的分布,通常选0.1~1之间。调节中心点与周围点的权重。
beta :由先验知识,高斯分布选2。
kappa :k = 3-n
output:
Wm :sigma点均值的权重
Wc :sigma点协方差的权重
可以看到,sigma点介于第一和第二标准差。 α \alpha α越大,中心点越突出,所占权重配比越大。
输入参数:
dim_x :2维滤波,dim=4
dim_ z:2维滤波,dim=2
dt : 时间差
hx:function(x)
fx:function(x, dt)
points:点类,就是MerweScaledSigmaPoints生成的点
特征参数:
x:numpy.array(dim_x), 状态估计向量
P: numpy.array(dim_x, dim_x),协方差估计矩阵
Q: numpy.array(dim_x, dim_x),过程噪声矩阵
R: numpy.array(dim_z, dim_z),测量噪声矩阵
K: 卡尔曼增益矩阵
y:残差矩阵
from filterpy.kalman import KalmanFilter
from filterpy.common import Q_discrete_white_noise
from numpy.random import randn
import numpy as np
from numpy.random import randn
from filterpy.kalman import UnscentedKalmanFilter
import matplotlib.pyplot as plt
from filterpy.kalman import UnscentedKalmanFilter as UKF
from filterpy.kalman import unscented_transform, MerweScaledSigmaPoints
def f_cv(x, dt):
""" 匀速直线运动函数"""
F = np.array([[1, dt, 0, 0],
[0, 1, 0, 0],
[0, 0, 1, dt],
[0, 0, 0, 1]])
return F @ x
def h_cv(x):
return x[[0, 2]]
std_x, std_y = .3, .3
dt = 1.0
zs = [np.array([i + randn() * std_x,
i + randn() * std_y]) for i in range(100)]
np.random.seed(1234)
sigmas = MerweScaledSigmaPoints(4, alpha=.1, beta=2., kappa=1.)
ukf = UKF(dim_x=4, dim_z=2, fx=f_cv,
hx=h_cv, dt=dt, points=sigmas)
ukf.x = np.array([0., 0., 0., 0.])
ukf.R = np.diag([0.09, 0.09])
ukf.Q[0:2, 0:2] = Q_discrete_white_noise(2, dt=1, var=0.02)
ukf.Q[2:4, 2:4] = Q_discrete_white_noise(2, dt=1, var=0.02)
uxs = []
for z in zs:
ukf.predict()
ukf.update(z)
uxs.append(ukf.x.copy())
uxs = np.array(uxs)
plt.plot(uxs[:, 0], uxs[:, 2])
print(ukf)
这是原书地址:https://github.com/rlabbe/Kalman-and-Bayesian-Filters-in-Python
youtobe上一个老大叔讲卡尔曼滤波讲的忒别好,地址
https://www.youtube.com/watch?v=owQq4zpmXdo