python中的kalman二维滤波器

2024-04-28 12:34:01 发布

您现在位置:Python中文网/ 问答频道 /正文

我的输入是二维(x,y)时间序列的一个点在屏幕上移动的跟踪器软件。它有一些噪音我想用卡尔曼滤波去除。有人能给我指一个用于卡尔曼二维滤波器的python代码吗? 在scipy食谱中,我只找到了一个1d的例子: http://www.scipy.org/Cookbook/KalmanFiltering 我在OpenCV中看到了Kalman滤波器的实现,但是找不到代码示例。 谢谢!


Tags: 代码orghttp软件屏幕www时间序列
1条回答
网友
1楼 · 发布于 2024-04-28 12:34:01

这里是我基于equations given on wikipedia的卡尔曼滤波器的实现。请注意,我对卡尔曼滤波器的理解还很初级,所以有很多方法可以改进这个代码。(例如,它受到讨论的数值不稳定性问题here。据我所知,当运动噪声Q非常小时,这只会影响数值稳定性。在现实生活中,噪声通常不小,所以幸运的是(至少在我的实现中),在实际中,数值不稳定性不会出现。)

在下面的例子中,kalman_xy假设状态向量是一个4元组:2个数字表示位置,2个数字表示速度。 为这个状态向量专门定义了FH矩阵:如果x是4元组状态,那么

new_x = F * x
position = H * x

然后它调用kalman,这是广义Kalman滤波器。一般来说,如果你想定义一个不同的状态向量——可能是一个表示位置、速度和加速度的6元组——它还是很有用的。你只需要通过提供适当的FH来定义运动方程。

import numpy as np
import matplotlib.pyplot as plt

def kalman_xy(x, P, measurement, R,
              motion = np.matrix('0. 0. 0. 0.').T,
              Q = np.matrix(np.eye(4))):
    """
    Parameters:    
    x: initial state 4-tuple of location and velocity: (x0, x1, x0_dot, x1_dot)
    P: initial uncertainty convariance matrix
    measurement: observed position
    R: measurement noise 
    motion: external motion added to state vector x
    Q: motion noise (same shape as P)
    """
    return kalman(x, P, measurement, R, motion, Q,
                  F = np.matrix('''
                      1. 0. 1. 0.;
                      0. 1. 0. 1.;
                      0. 0. 1. 0.;
                      0. 0. 0. 1.
                      '''),
                  H = np.matrix('''
                      1. 0. 0. 0.;
                      0. 1. 0. 0.'''))

def kalman(x, P, measurement, R, motion, Q, F, H):
    '''
    Parameters:
    x: initial state
    P: initial uncertainty convariance matrix
    measurement: observed position (same shape as H*x)
    R: measurement noise (same shape as H)
    motion: external motion added to state vector x
    Q: motion noise (same shape as P)
    F: next state function: x_prime = F*x
    H: measurement function: position = H*x

    Return: the updated and predicted new values for (x, P)

    See also http://en.wikipedia.org/wiki/Kalman_filter

    This version of kalman can be applied to many different situations by
    appropriately defining F and H 
    '''
    # UPDATE x, P based on measurement m    
    # distance between measured and current position-belief
    y = np.matrix(measurement).T - H * x
    S = H * P * H.T + R  # residual convariance
    K = P * H.T * S.I    # Kalman gain
    x = x + K*y
    I = np.matrix(np.eye(F.shape[0])) # identity matrix
    P = (I - K*H)*P

    # PREDICT x, P based on motion
    x = F*x + motion
    P = F*P*F.T + Q

    return x, P

def demo_kalman_xy():
    x = np.matrix('0. 0. 0. 0.').T 
    P = np.matrix(np.eye(4))*1000 # initial uncertainty

    N = 20
    true_x = np.linspace(0.0, 10.0, N)
    true_y = true_x**2
    observed_x = true_x + 0.05*np.random.random(N)*true_x
    observed_y = true_y + 0.05*np.random.random(N)*true_y
    plt.plot(observed_x, observed_y, 'ro')
    result = []
    R = 0.01**2
    for meas in zip(observed_x, observed_y):
        x, P = kalman_xy(x, P, meas, R)
        result.append((x[:2]).tolist())
    kalman_x, kalman_y = zip(*result)
    plt.plot(kalman_x, kalman_y, 'g-')
    plt.show()

demo_kalman_xy()

enter image description here

红点表示噪声位置测量,绿线表示卡尔曼预测位置。

相关问题 更多 >