首页 > 解决方案 > 非线性系统的卡尔曼滤波器

问题描述

我想在我的实验中使用卡尔曼滤波器。我已经创建了一个非常小的时间序列数据,其中包含如下格式的三列。由于我无法在 stackoverflow 上附加文件,因此在此处附加了完整的数据集以实现可重复性:

http://www.mediafire.com/file/el1tkrdun0j2dk4/testdata.csv/file

  time        X      Y
 0.040662  1.041667  1
 0.139757  1.760417  2
 0.144357  1.190104  1
 0.145341  1.047526  1
 0.145401  1.011882  1
 0.148465  1.002970  1
 ....      .....     .

我已经阅读了文档Kalman Filter并设法进行了简单的线性过滤,这是我的代码

import matplotlib.pyplot as plt 
from pykalman import KalmanFilter 
import numpy as np
import pandas as pd



df = pd.read_csv('testdata.csv')
print(df)
pd.set_option('use_inf_as_null', True)

df.dropna(inplace=True)


X = df.drop('Y', axis=1)
y = df['Y']



estimated_value= np.array(X)
real_value = np.array(y)

measurements = np.asarray(estimated_value)



kf = KalmanFilter(n_dim_obs=1, n_dim_state=1, 
                  transition_matrices=[1],
                  observation_matrices=[1],
                  initial_state_mean=measurements[0,1], 
                  initial_state_covariance=1,
                  observation_covariance=5,
                  transition_covariance=1)

state_means, state_covariances = kf.filter(measurements[:,1]) 
state_std = np.sqrt(state_covariances[:,0])
print (state_std)
print (state_means)
print (state_covariances)


fig, ax = plt.subplots()
ax.margins(x=0, y=0.05)

plt.plot(measurements[:,0], measurements[:,1], '-r', label='Real Value Input') 
plt.plot(measurements[:,0], state_means, '-b', label='Kalman-Filter') 
plt.legend(loc='best')
ax.set_xlabel("Time")
ax.set_ylabel("Value")
plt.show()

这给出了以下图作为输出

在此处输入图像描述

但是,由于我的输入是非线性的,Kalman Filter因此我想使用它来检测和跟踪滤波信号的下降(上图中的蓝色)。但由于我是新手Kalman Filter,我似乎很难理解数学公式并开始学习Unscented Kalman Filter。老实说,文档也不容易遵循。因此,我会感谢任何帮助,至少可以检测过滤后峰值的下降有多大(例如,前一个下降的 50%)。任何帮助,将不胜感激。

标签: pythontime-seriesfilteringkalman-filterpykalman

解决方案


推荐阅读