0

我想尝试使用 pykalman 对来自传感器变量的数据应用卡尔曼滤波器。现在,我对观察数据有疑问。在示例中,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)
4

1 回答 1

1

让我们来看看:

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

方法

转移矩阵

因此,您的状态向量由 2 个元素组成,例如:

状态向量

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

方法

观察矩阵

观察矩阵的维数应该是 [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()因为此功能不会对第一次测量进行预测,但我认为应该这样做。

于 2019-06-27T08:04:02.667 回答