Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
96129 views
�
�g�Uc@`s�dZddlmZmZmZmZddlZddlj	Z	ddlm
Z
mZmZddl
mZmZmZmZdefd��YZdS(u4Copyright 2015 Roger R Labbe Jr.

FilterPy library.
http://github.com/rlabbe/filterpy

Documentation at:
https://filterpy.readthedocs.org

Supporting book at:
https://github.com/rlabbe/Kalman-and-Bayesian-Filters-in-Python

This is licensed under an MIT license. See the readme.MD file
for more information.
i(tabsolute_importtdivisiontprint_functiontunicode_literalsN(tdottzerosteye(tsettert	setter_1dt
setter_scalartdot3tExtendedKalmanFiltercB`sIeZdd�Zdddd�Zdddejd�Zdd�Zdd�Z	e
d��Zejd��Ze
d��Z
e
jd	��Z
e
d
��Zejd��Ze
d��Zejd
��Ze
d��Zejd��Ze
d��Zejd��Ze
d��Ze
d��Ze
d��ZRS(icC`s�||_||_t|df�|_t|�|_d|_d|_t|�|_t|�|_	t|df�|_
tj|�|_dS(u� Extended Kalman filter. You are responsible for setting the
        various state variables to reasonable values; the defaults below will
        not give you a functional filter.

        **Parameters**

        dim_x : int
            Number of state variables for the Kalman filter. For example, if
            you are tracking the position and velocity of an object in two
            dimensions, dim_x would be 4.

            This is used to set the default size of P, Q, and u

        dim_z : int
            Number of of measurement inputs. For example, if the sensor
            provides you with position in (x,y), dim_z would be 2.
        iiN(
tdim_xtdim_zRt_xRt_Pt_Bt_Ft_Rt_Qt_ytnpt_I(tselfRR
tdim_u((s./filterpy/kalman/EKF.pyt__init__s				cC`s�t|t�s|f}nt|t�s6|f}ntj|�rl|jdkrltj|gt�}n|j}|j}|j	}	|j
}
|j}|j}|||�}
t
||�t
||�}t||	|j�|
}	t|
|	|
j�|}t|	|
jtj|��}|t
|||||��|_|jt
||
�}t||	|j�t|||j�|_	dS(u, Performs the predict/update innovation of the extended Kalman
        filter.

        **Parameters**

        z : np.array
            measurement for this step.
            If `None`, only predict step is perfomed.

        HJacobian : function
           function which computes the Jacobian of the H matrix (measurement
           function). Takes state variable (self.x) as input, along with the
           optional arguments in args, and returns H.

        Hx : function
            function which takes as input the state variable (self.x) along
            with the optional arguments in hx_args, and returns the measurement
            that would correspond to that state.

        args : tuple, optional, default (,)
            arguments to be passed into HJacobian after the required state
            variable.

        hx_args : tuple, optional, default (,)
            arguments to be passed into HJacobian after the required state
            variable.

        u : np.array or scalar
            optional control vector input to the filter.
        iN(t
isinstancettupleRtisscalarR
tasarraytfloatRRRRRRRR
tTtlinalgtinvR(Rtzt	HJacobiantHxtargsthx_argstutFtBtPtQtRtxtHtStKtI_KH((s./filterpy/kalman/EKF.pytpredict_update>s( 						#cC`szt|t�s|f}nt|t�s6|f}n|j}|dkrW|j}n%tj|�r|t|j�|}ntj|�r�|jdkr�tj	|gt
�}n|j}	||	|�}
t|
||
j
�|}t||
j
tj|��}||	|�}
|||
�}|	t||�|_|jt||
�}t|||j
�t|||j
�|_dS(u� Performs the update innovation of the extended Kalman filter.

        **Parameters**

        z : np.array
            measurement for this step.
            If `None`, only predict step is perfomed.

        HJacobian : function
           function which computes the Jacobian of the H matrix (measurement
           function). Takes state variable (self.x) as input, returns H.

        Hx : function
            function which takes as input the state variable (self.x) along
            with the optional arguments in hx_args, and returns the measurement
            that would correspond to that state.

        R : np.array, scalar, or None
            Optionally provide R to override the measurement noise for this
            one call, otherwise  self.R will be used.

        args : tuple, optional, default (,)
            arguments to be passed into HJacobian after the required state
            variable. for robot localization you might need to pass in
            information about the map and time of day, so you might have
            `args=(map_data, time)`, where the signature of HCacobian will
            be `def HJacobian(x, map, t)`

        hx_args : tuple, optional, default (,)
            arguments to be passed into Hx function after the required state
            variable.

        residual : function (z, z2), optional
            Optional function that computes the residual (difference) between
            the two measurement vectors. If you do not provide this, then the
            built in minus operator will be used. You will normally want to use
            the built in unless your residual computation is nonlinear (for
            example, if they are angles)
        iN(RRRtNoneRRRRR
RRRR
RR R!RR(RR"R#R$R,R%R&tresidualR*R-R.R/R0thxtyR1((s./filterpy/kalman/EKF.pytupdate~s(*		cC`s,t|j|j�t|j|�|_dS(u� predicts the next state of X. If you need to
        compute the next state yourself, override this function. You would
        need to do this, for example, if the usual Taylor expansion to
        generate F is not providing accurate results for you. N(RRRR(RR'((s./filterpy/kalman/EKF.pyt	predict_x�scC`s9|j|�t|j|j|jj�|j|_dS(u� Predict next position.

        **Parameters**

        u : np.array
            Optional control vector. If non-zero, it is multiplied by B
            to create the control input into the system.
        N(R8R
RRRR(RR'((s./filterpy/kalman/EKF.pytpredict�s

cC`s|jS(u Process uncertainty(R(R((s./filterpy/kalman/EKF.pyR+�scC`st||j�|_dS(N(R	RR(Rtvalue((s./filterpy/kalman/EKF.pyR+�scC`s|jS(u covariance matrix(R(R((s./filterpy/kalman/EKF.pyR*�scC`st||j�|_dS(N(R	RR(RR:((s./filterpy/kalman/EKF.pyR*�scC`s|jS(u measurement uncertainty(R(R((s./filterpy/kalman/EKF.pyR,�scC`st||j�|_dS(N(R	R
R(RR:((s./filterpy/kalman/EKF.pyR,�scC`s|jS(N(R(R((s./filterpy/kalman/EKF.pyR(�scC`st||j|j�|_dS(N(RRR(RR:((s./filterpy/kalman/EKF.pyR(scC`s|jS(N(R(R((s./filterpy/kalman/EKF.pyR)scC`st||j|j�|_dS(u control transition matrixN(RRRR(RR:((s./filterpy/kalman/EKF.pyR)
scC`s|jS(N(R(R((s./filterpy/kalman/EKF.pyR-scC`st||j�|_dS(N(RRR(RR:((s./filterpy/kalman/EKF.pyR-scC`s|jS(u
 Kalman gain (t_K(R((s./filterpy/kalman/EKF.pyR0scC`s|jS(u# measurement residual (innovation) (R(R((s./filterpy/kalman/EKF.pyR6 scC`s|jS(u) system uncertainty in measurement space (t_S(R((s./filterpy/kalman/EKF.pyR/%s((N(((t__name__t
__module__RR2R3RtsubtractR7R8R9tpropertyR+RR*R,R(R)R-R0R6R/(((s./filterpy/kalman/EKF.pyRs*"@	G	(t__doc__t
__future__RRRRtnumpyRtscipy.linalgR RRRtfilterpy.commonRRR	R
tobjectR(((s./filterpy/kalman/EKF.pyt<module>s""