python - 非线性系统的卡尔曼滤波器
问题描述
我想在我的实验中使用卡尔曼滤波器。我已经创建了一个非常小的时间序列数据,其中包含如下格式的三列。由于我无法在 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%)。任何帮助,将不胜感激。
解决方案
推荐阅读
- ruby-on-rails - 为已有文件创建 Activestorage 记录
- c# - 发布程序时包含 SQL Server 数据库文件
- spring - jetty-maven-plugin:启动期间的日志
- python - 如何遍历 df 的每一行/列以查看是否存在 0 或 ' '?
- angular - app.module.ts > APP_INITIALIZER 依赖于另一个
- c - 如何释放链接列表中的信息?
- python - 如何修复 Firebase_Admin 错误 TypeError: __init__() got an unexpected keyword argument 'status'
- amazon-web-services - 将字符串转换为 CommaDelimitedList Cloudformation 模板
- sql - Postgres 中的原始 SQL 与 EntityFramework Core
- python - MemoryError pandas 根据其他列值生成新列