� �g�Uc @` s� d Z d d l m Z m Z m Z m Z d d l m Z d d l m Z d d l Z d d l m Z m Z m Z m Z m Z d d l m Z m Z d e f d � � YZ d S( u4 Copyright 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 ( t absolute_importt divisiont print_functiont unicode_literals( t dot3( t unscented_transformN( t eyet zerost dott isscalart outer( t invt choleskyt UnscentedKalmanFilterc B` sk e Z d Z d d d d d d � Z d d d d � Z d d d d � Z d d d d � Z d d d � Z RS( u� Implements the Scaled Unscented Kalman filter (UKF) as defined by Simon Julier in [1], using the formulation provided by Wan and Merle in [2]. This filter scales the sigma points to avoid strong nonlinearities. You will have to set the following attributes after constructing this object for the filter to perform properly. **Attributes** x : numpy.array(dim_x) state estimate vector P : numpy.array(dim_x, dim_x) covariance estimate matrix R : numpy.array(dim_z, dim_z) measurement noise matrix Q : numpy.array(dim_x, dim_x) process noise matrix You may read the following attributes. **Readable Attributes** xp : numpy.array(dim_x) predicted state (result of predict()) Pp : numpy.array(dim_x, dim_x) predicted covariance matrix (result of predict()) **References** .. [1] Julier, Simon J. "The scaled unscented transformation," American Control Converence, 2002, pp 4555-4559, vol 6. Online copy: https://www.cs.unc.edu/~welch/kalman/media/pdf/ACC02-IEEE1357.PDF .. [2] E. A. Wan and R. Van der Merwe, “The unscented Kalman filter for nonlinear estimation,” in Proc. Symp. Adaptive Syst. Signal Process., Commun. Contr., Lake Louise, AB, Canada, Oct. 2000. Online Copy: https://www.seas.harvard.edu/courses/cs281/papers/unscented.pdf c C` s[ t | � | _ t | � | _ t | � | _ t | � | _ | | _ | | _ | | _ d | d | _ | | _ | | _ | | _ | | _ | | _ | d k r� t | _ n | | _ | j j � \ | _ | _ | d k r� t j | _ n | | _ | d k rt j | _ n | | _ t d | j d | j f � | _ t | j | j f � | _ d S( u� Create a 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 filter. For example, if you are tracking the position and velocity of an object in two dimensions, dim_x would be 4. 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. dt : float Time between steps in seconds. hx : function(x) Measurement function. Converts state vector x into a measurement vector of shape (dim_z). fx : function(x,dt) function that returns the state x transformed by the state transistion function. dt is the time step in seconds. points : class Class which computes the sigma points and weights for a UKF algorithm. You can vary the UKF implementation by changing this class. For example, MerweScaledSigmaPoints implements the alpha, beta, kappa parameterization of Van der Merwe, and JulierSigmaPoints implements Julier's original kappa parameterization. See either of those for the required signature of this class if you want to implement your own. sqrt_fn : callable(ndarray), default = scipy.linalg.cholesky Defines how we compute the square root of a matrix, which has no unique answer. Cholesky is the default choice due to its speed. Typically your alternative choice will be scipy.linalg.sqrtm. Different choices affect how the sigma points are arranged relative to the eigenvectors of the covariance matrix. Usually this will not matter to you; if so the default cholesky() yields maximal performance. As of van der Merwe's dissertation of 2004 [6] this was not a well reseached area so I have no advice to give you. If your method returns a triangular matrix it must be upper triangular. Do not use numpy.linalg.cholesky - for historical reasons it returns a lower triangular matrix. The SciPy version does the right thing. x_mean_fn : callable (sigma_points, weights), optional Function that computes the mean of the provided sigma points and weights. Use this if your state variable contains nonlinear values such as angles which cannot be summed. .. code-block:: Python def state_mean(sigmas, Wm): x = np.zeros(3) sum_sin, sum_cos = 0., 0. for i in range(len(sigmas)): s = sigmas[i] x[0] += s[0] * Wm[i] x[1] += s[1] * Wm[i] sum_sin += sin(s[2])*Wm[i] sum_cos += cos(s[2])*Wm[i] x[2] = atan2(sum_sin, sum_cos) return x z_mean_fn : callable (sigma_points, weights), optional Same as x_mean_fn, except it is called for sigma points which form the measurements after being passed through hx(). residual_x : callable (x, y), optional residual_z : callable (x, y), optional Function that computes the residual (difference) between x and y. You will have to supply this if your state variable cannot support subtraction, such as angles (359-1 degreees is 2, not 358). x and y are state vectors, not scalars. One is for the state variable, the other is for the measurement state. .. code-block:: Python def residual(a, b): y = a[0] - b[0] if y > np.pi: y -= 2*np.pi if y < -np.pi: y = 2*np.pi return y **References** .. [3] S. Julier, J. Uhlmann, and H. Durrant-Whyte. "A new method for the nonlinear transformation of means and covariances in filters and estimators," IEEE Transactions on Automatic Control, 45(3), pp. 477-482 (March 2000). .. [4] E. A. Wan and R. Van der Merwe, “The Unscented Kalman filter for Nonlinear Estimation,” in Proc. Symp. Adaptive Syst. Signal Process., Commun. Contr., Lake Louise, AB, Canada, Oct. 2000. https://www.seas.harvard.edu/courses/cs281/papers/unscented.pdf .. [5] Wan, Merle "The Unscented Kalman Filter," chapter in *Kalman Filtering and Neural Networks*, John Wiley & Sons, Inc., 2001. .. [6] R. Van der Merwe "Sigma-Point Kalman Filters for Probabilitic Inference in Dynamic State-Space Models" (Doctoral dissertation) i i N( R t Qt RR t xt Pt _dim_xt _dim_zt _dtt _num_sigmast hxt fxt points_fnt x_meant z_meant NoneR t msqrtt weightst Wmt Wct npt subtractt residual_xt residual_zt sigmas_ft sigmas_h( t selft dim_xt dim_zt dtR R t pointst sqrt_fnt x_mean_fnt z_mean_fnR"