pykalman的使用

时间:2019-06-25 19:08:34

标签: python kalman-filter pykalman

我想尝试使用pykalman对来自传感器变量的数据应用kalman过滤器。现在,我对这些观察数据感到怀疑。在示例中,这3个观测值是在三个瞬间测量的两个变量,或者是在一个瞬间测量的3个变量

from pykalman import KalmanFilter
>>> import numpy as np
>>> kf = KalmanFilter(transition_matrices = [[1, 1], [0, 1]], observation_matrices = [[0.1, 0.5], [-0.3, 0.0]])
>>> measurements = np.asarray([[1,0], [0,0], [0,1]])  # 3 observations
>>> kf = kf.em(measurements, n_iter=5)
>>> (filtered_state_means, filtered_state_covariances) = kf.filter(measurements)
>>> (smoothed_state_means, smoothed_state_covariances) = kf.smooth(measurements)

1 个答案:

答案 0 :(得分:1)

让我们看看:

transition_matrices = [[1, 1], [0, 1]]

表示

Transition matrix

因此,您的状态向量包含2个元素,例如:

state vector

observation_matrices = [[0.1, 0.5], [-0.3, 0.0]]

表示

Observation matrix

观察矩阵的维数应为[n_dim_obs, n_dim_state]。 因此,您的测量向量还包含2个元素。

结论:代码中包含3 observations of two variables measured at 3 different points in time

您可以更改给定的代码,以便它可以一次处理每个测量。您对每次测量都使用kf.filter_update(),而不是对所有测量都使用kf.filter()

from pykalman import KalmanFilter
import numpy as np
kf = KalmanFilter(transition_matrices = [[1, 1], [0, 1]], observation_matrices = [[0.1, 0.5], [-0.3, 0.0]])
measurements = np.asarray([[1,0], [0,0], [0,1]])  # 3 observations
kf = kf.em(measurements, n_iter=5)

filtered_state_means = kf.initial_state_mean
filtered_state_covariances = kf.initial_state_covariance

for m in measurements:

    filtered_state_means, filtered_state_covariances = (
        kf.filter_update(
            filtered_state_means,
            filtered_state_covariances,
            observation = m)
        )

print(filtered_state_means);

输出:

[-1.69112511  0.30509999]

结果与使用kf.filter()时的结果略有不同,因为此函数不会在第一次测量时执行预测,但我认为应该。