- mongodb - 在 MongoDB mapreduce 中,如何展平值对象?
- javascript - 对象传播与 Object.assign
- html - 输入类型 ="submit"Vs 按钮标签它们可以互换吗?
- sql - 使用 MongoDB 而不是 MS SQL Server 的优缺点
[编辑]
@Claudio 的回答给了我一个关于如何过滤异常值的非常好的提示。不过,我确实想开始对我的数据使用卡尔曼滤波器。因此,我更改了下面的示例数据,使其具有不那么极端的细微变化噪声(我也经常看到)。如果其他人能给我一些关于如何在我的数据上使用 PyKalman 的指导,那就太好了。
[/编辑]
对于机器人项目,我试图用相机跟踪空中的风筝。我正在用 Python 编程,并在下面粘贴了一些嘈杂的位置结果(每个项目还包含一个日期时间对象,但为了清楚起见,我将它们省略了)。
[ # X Y
{'loc': (399, 293)},
{'loc': (403, 299)},
{'loc': (409, 308)},
{'loc': (416, 315)},
{'loc': (418, 318)},
{'loc': (420, 323)},
{'loc': (429, 326)}, # <== Noise in X
{'loc': (423, 328)},
{'loc': (429, 334)},
{'loc': (431, 337)},
{'loc': (433, 342)},
{'loc': (434, 352)}, # <== Noise in Y
{'loc': (434, 349)},
{'loc': (433, 350)},
{'loc': (431, 350)},
{'loc': (430, 349)},
{'loc': (428, 347)},
{'loc': (427, 345)},
{'loc': (425, 341)},
{'loc': (429, 338)}, # <== Noise in X
{'loc': (431, 328)}, # <== Noise in X
{'loc': (410, 313)},
{'loc': (406, 306)},
{'loc': (402, 299)},
{'loc': (397, 291)},
{'loc': (391, 294)}, # <== Noise in Y
{'loc': (376, 270)},
{'loc': (372, 272)},
{'loc': (351, 248)},
{'loc': (336, 244)},
{'loc': (327, 236)},
{'loc': (307, 220)}
]
>>> 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)
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([(399,293),(403,299),(409,308),(416,315),(418,318),(420,323),(429,326),(423,328),(429,334),(431,337),(433,342),(434,352),(434,349),(433,350),(431,350),(430,349),(428,347),(427,345),(425,341),(429,338),(431,328),(410,313),(406,306),(402,299),(397,291),(391,294),(376,270),(372,272),(351,248),(336,244),(327,236),(307,220)])
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)
smoothed_state_means
变为以下内容:
>>> smoothed_state_means
array([[-235.47463353, 36.95271449],
[-354.8712597 , 27.70011485],
[-402.19985301, 21.75847069],
[-423.24073418, 17.54604304],
[-433.96622233, 14.36072376],
[-443.05275258, 11.94368163],
[-446.89521434, 9.97960296],
[-456.19359012, 8.54765215],
[-465.79317394, 7.6133633 ],
[-474.84869079, 7.10419182],
[-487.66174033, 7.1211321 ],
[-504.6528746 , 7.81715451],
[-506.76051587, 8.68135952],
[-510.13247696, 9.7280697 ],
[-512.39637431, 10.9610031 ],
[-511.94189431, 12.32378146],
[-509.32990832, 13.77980587],
[-504.39389762, 15.29418648],
[-495.15439769, 16.762472 ],
[-480.31085928, 18.02633612],
[-456.80082586, 18.80355017],
[-437.35977492, 19.24869224],
[-420.7706184 , 19.52147918],
[-405.59500937, 19.70357845],
[-392.62770281, 19.8936389 ],
[-388.8656724 , 20.44525168],
[-361.95411607, 20.57651509],
[-352.32671579, 20.84174084],
[-327.46028214, 20.77224385],
[-319.75994982, 20.9443245 ],
[-306.69948771, 21.24618955],
[-287.03222693, 21.43135098]])
最佳答案
TL;DR,请参阅底部的代码和图片。
我认为卡尔曼滤波器可以在您的应用程序中很好地工作,但它需要更多地考虑风筝的动力学/物理。
我强烈推荐阅读 this webpage .我与作者没有联系,也不了解作者,但我花了大约一天的时间试图了解卡尔曼滤波器,这个页面真的让我点击了它。
简要地;对于线性且具有已知动态特性的系统(即,如果您知道状态和输入,则可以预测 future 状态),它提供了一种结合您对系统的了解来估计其真实状态的最佳方式。聪明的一点(由您在描述它的页面上看到的所有矩阵代数处理)是它如何最佳地组合您拥有的两条信息:
observation_matrices
在
pykalman
库中):
transition_matrices
库中的
pykalman
):
from pykalman import KalmanFilter
import numpy as np
import matplotlib.pyplot as plt
import time
measurements = np.asarray([(399,293),(403,299),(409,308),(416,315),(418,318),(420,323),(429,326),(423,328),(429,334),(431,337),(433,342),(434,352),(434,349),(433,350),(431,350),(430,349),(428,347),(427,345),(425,341),(429,338),(431,328),(410,313),(406,306),(402,299),(397,291),(391,294),(376,270),(372,272),(351,248),(336,244),(327,236),(307,220)])
initial_state_mean = [measurements[0, 0],
0,
measurements[0, 1],
0]
transition_matrix = [[1, 1, 0, 0],
[0, 1, 0, 0],
[0, 0, 1, 1],
[0, 0, 0, 1]]
observation_matrix = [[1, 0, 0, 0],
[0, 0, 1, 0]]
kf1 = KalmanFilter(transition_matrices = transition_matrix,
observation_matrices = observation_matrix,
initial_state_mean = initial_state_mean)
kf1 = kf1.em(measurements, n_iter=5)
(smoothed_state_means, smoothed_state_covariances) = kf1.smooth(measurements)
plt.figure(1)
times = range(measurements.shape[0])
plt.plot(times, measurements[:, 0], 'bo',
times, measurements[:, 1], 'ro',
times, smoothed_state_means[:, 0], 'b--',
times, smoothed_state_means[:, 2], 'r--',)
plt.show()
observation_covariance
,相对于我们的
state_covariance
)。
observation covariance
为之前估计值的 10 倍,设置
em_vars
如图所示,以避免重新估计观察协方差(见
here)
kf2 = KalmanFilter(transition_matrices = transition_matrix,
observation_matrices = observation_matrix,
initial_state_mean = initial_state_mean,
observation_covariance = 10*kf1.observation_covariance,
em_vars=['transition_covariance', 'initial_state_covariance'])
kf2 = kf2.em(measurements, n_iter=5)
(smoothed_state_means, smoothed_state_covariances) = kf2.smooth(measurements)
plt.figure(2)
times = range(measurements.shape[0])
plt.plot(times, measurements[:, 0], 'bo',
times, measurements[:, 1], 'ro',
times, smoothed_state_means[:, 0], 'b--',
times, smoothed_state_means[:, 2], 'r--',)
plt.show()
filter_update
来实现。方法。请注意,这使用了
filter
方法而不是
smooth
方法,因为
smooth
方法只能应用于批量测量。更多
here :
time_before = time.time()
n_real_time = 3
kf3 = KalmanFilter(transition_matrices = transition_matrix,
observation_matrices = observation_matrix,
initial_state_mean = initial_state_mean,
observation_covariance = 10*kf1.observation_covariance,
em_vars=['transition_covariance', 'initial_state_covariance'])
kf3 = kf3.em(measurements[:-n_real_time, :], n_iter=5)
(filtered_state_means, filtered_state_covariances) = kf3.filter(measurements[:-n_real_time,:])
print("Time to build and train kf3: %s seconds" % (time.time() - time_before))
x_now = filtered_state_means[-1, :]
P_now = filtered_state_covariances[-1, :]
x_new = np.zeros((n_real_time, filtered_state_means.shape[1]))
i = 0
for measurement in measurements[-n_real_time:, :]:
time_before = time.time()
(x_now, P_now) = kf3.filter_update(filtered_state_mean = x_now,
filtered_state_covariance = P_now,
observation = measurement)
print("Time to update kf3: %s seconds" % (time.time() - time_before))
x_new[i, :] = x_now
i = i + 1
plt.figure(3)
old_times = range(measurements.shape[0] - n_real_time)
new_times = range(measurements.shape[0]-n_real_time, measurements.shape[0])
plt.plot(times, measurements[:, 0], 'bo',
times, measurements[:, 1], 'ro',
old_times, filtered_state_means[:, 0], 'b--',
old_times, filtered_state_means[:, 2], 'r--',
new_times, x_new[:, 0], 'b-',
new_times, x_new[:, 2], 'r-')
plt.show()
filter_update
找到的 3 个点。方法。点是测量值,虚线是过滤器训练期间的状态估计,实线是“在线”期间的状态估计。
Time to build and train kf3: 0.0677888393402 seconds
Time to update kf3: 0.00038480758667 seconds
Time to update kf3: 0.000465154647827 seconds
Time to update kf3: 0.000463008880615 seconds
关于python - 如何在 Python 中使用卡尔曼滤波器获取位置数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43377626/
所以我试图增强我的图像的对比度,我发现一位绅士通过在线 Gamma 校正来做到这一点,代码如下: (im/255).^0.45*255 据我了解,1/gammavalue = 0.45,其中 gamm
我有一个包含简单时间序列数据的向量(从 deSolve 矩阵中提取),用于测试目的可以是: x x r for (n in 2:length(x)) r[n] (r) [1] NA 1 1
我有这段实现 Prewitt 边缘检测的代码。我需要做的是只用一个缓冲区来实现它,也就是说,我不会创建图像的拷贝,而是编辑原始图像。所以如果我想改变值 78 的像素,我不能把新值,例如100,直到所有
我想制作一个 FIR 滤波器。我有一个系数数组 (buffer[size]) 和一个数据数组 (filter[size_filter])。我必须在两个数组之间进行卷积: for(j = 0;j < s
我正在尝试制作 IIR 滤波器。我做了FIR滤波器,但是我觉得IIR比FIR难。 我认为 IIR 与 FIR 类似,但它让我感到困惑。 我觉得过滤器是这样的 FIR : y(n) = b0(x[n])
我想在 Python 中通过窗口创建一个基本的高通 FIR 滤波器。 我的代码在下面并且是故意惯用的 - 我知道你可以(很可能)用 Python 中的一行代码完成它,但我正在学习。我使用了一个带有矩形
我正在尝试用树莓派创建一个相机来检测在走廊中移动的人(这里我假设只有移动的东西是人),并识别那些在该区域花费太多时间的人(使用计时器),我使用背景减法来检测运动并尝试使用基于相关性的跟踪器(例如 MO
我正在研究用于特征提取的超像素。我已经成功地将超像素功能应用于图像。 A = imread('kobi.png'); [L,N] = superpixels(A,5); figure BW = bou
你好 我需要在应用中使用这个 Kolmogorov 过滤器。您将一些测量数据放入其中,并使用过滤器对其进行一些平滑处理。我试着用“nchoosek”来做,但是当我尝试为 50 或更多的 I 做这件事时
我正在尝试在具有静态掩码 5x5 并在 applyFilter() 函数中进行卷积编码的图像上实现 LoG 过滤器。然而,无论我使用什么面具,我都会得到奇怪的结果。保存图像而不通过函数传递它是有效的,
我已经在 Haskell 中实现了一个 FIR 滤波器。我不太了解 FIR 滤波器,我的代码很大程度上基于现有的 C# 实现。因此,我觉得我的实现有太多的 C# 风格,而不是真正的 Haskell 风
我需要制作一个简单的带通音频滤波器。现在我使用了这个简单的 C++ 类:http://www.cardinalpeak.com/blog/a-c-class-to-implement-low-pass
CUDA NPP 库支持使用 nppiFilter_8u_C1R 命令过滤图像,但不断出现错误。我可以毫无问题地启动并运行 boxFilterNPP 示例代码。 eStatusNPP = nppiFi
我是 OpenCV 和 gabor 过滤器的新手,只想获得这样的 gabor 小波: 我在 Java 中使用这个 OpenCV 代码: double sigma_bar = 40; double th
我正在使用 FIR 滤波器对音频进行过采样。这是一个简单的典型窗口 sinc,即一个被截断和窗口化的 sinc 函数。像往常一样,它需要过去和“ future ”的样本才能工作。实际上,这意味着音频输
目前我正在尝试实现 FIR 低通滤波器。 FIR 系数在 MATLAB 中计算。现在我需要用 C++ 实现 FIR 算法。 我将一个类定义为过滤器,将 FIR 的一个函数定义为: double * F
我有一个用 C 语言实现 FIR 滤波器的家庭作业,我想知道您是否认为我理解正确。我认为解决问题的程序是: #include float FIRfloats[5]; void floatFIR(fl
我希望对图像的每条水平线应用频域滤波器,例如低通或带通。这可能使用 opencv 吗? 最佳答案 我认为您需要详细说明您的问题。也许,举一些具体的例子。 如果我将您的问题解释为: 你有一张 10 x
我的问题与 A. Levy 的解释相关: Analyze audio using Fast Fourier Transform 如何在这些复数上生成带通滤波器... [-636.00000000 +0
FIR 滤波器有一个算法,但它是 float : FIR filter implementation in C programming 如果我想要一个符合此规范的定点算法,我该怎么做? the FIR
我是一名优秀的程序员,十分优秀!