Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
| Download
Project: test
Views: 91872# -*- coding: utf-8 -*-1"""2Created on Sun May 11 20:47:52 201434@author: rlabbe5"""67from DogSensor import DogSensor8from filterpy.kalman import KalmanFilter9import numpy as np10import matplotlib.pyplot as plt11import stats1213def dog_tracking_filter(R,Q=0,cov=1.):14f = KalmanFilter (dim_x=2, dim_z=1)15f.x = np.matrix([[0], [0]]) # initial state (location and velocity)16f.F = np.matrix([[1,1],[0,1]]) # state transition matrix17f.H = np.matrix([[1,0]]) # Measurement function18f.R = R # measurement uncertainty19f.P *= cov # covariance matrix20f.Q = Q21return f222324def plot_track(noise, count, R, Q=0, plot_P=True, title='Kalman Filter'):25dog = DogSensor(velocity=1, noise=noise)26f = dog_tracking_filter(R=R, Q=Q, cov=10.)2728ps = []29zs = []30cov = []31for t in range (count):32z = dog.sense()33f.update (z)34#print (t,z)35ps.append (f.x[0,0])36cov.append(f.P)37zs.append(z)38f.predict()3940p0, = plt.plot([0,count],[0,count],'g')41p1, = plt.plot(range(1,count+1),zs,c='r', linestyle='dashed')42p2, = plt.plot(range(1,count+1),ps, c='b')43plt.axis('equal')44plt.legend([p0,p1,p2], ['actual','measurement', 'filter'], 2)45plt.title(title)4647for i,p in enumerate(cov):48print(i,p)49e = stats.sigma_ellipse (p, i+1, ps[i])50stats.plot_sigma_ellipse(e, axis_equal=False)51plt.xlim((-1,count))52plt.show()535455if __name__ == "__main__":56plot_track (noise=30, R=5, Q=2, count=20)5758