首页 > 解决方案 > 如何使用卡尔曼滤波器模型检测峰值

问题描述

对于我的评估,我想使用PyKalman过滤器库。我已经创建了一个非常小的时间序列数据,其中包含如下格式的三列。由于我无法在 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
 ....      .....     .

我已阅读Python的 PyKalman 库文档并设法使用以下方法进行简单的线性过滤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我找到了一个关于PyKalman UKF基本使用的好例子——但它没有显示如何定义下降的百分比(峰值)。因此,我会感谢任何帮助,至少可以检测到过滤后峰值的下降幅度有多大(例如,图中蓝线前一次下降的 50% 或 80%)。任何帮助,将不胜感激。

标签: pythontime-seriesfilteringkalman-filterpykalman

解决方案


推荐阅读