CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.

| Download
Project: test
Views: 91872
1
# -*- coding: utf-8 -*-
2
"""
3
Created on Sun May 11 20:47:52 2014
4
5
@author: rlabbe
6
"""
7
8
from DogSensor import DogSensor
9
from filterpy.kalman import KalmanFilter
10
import numpy as np
11
import matplotlib.pyplot as plt
12
import stats
13
14
def dog_tracking_filter(R,Q=0,cov=1.):
15
f = KalmanFilter (dim_x=2, dim_z=1)
16
f.x = np.matrix([[0], [0]]) # initial state (location and velocity)
17
f.F = np.matrix([[1,1],[0,1]]) # state transition matrix
18
f.H = np.matrix([[1,0]]) # Measurement function
19
f.R = R # measurement uncertainty
20
f.P *= cov # covariance matrix
21
f.Q = Q
22
return f
23
24
25
def plot_track(noise, count, R, Q=0, plot_P=True, title='Kalman Filter'):
26
dog = DogSensor(velocity=1, noise=noise)
27
f = dog_tracking_filter(R=R, Q=Q, cov=10.)
28
29
ps = []
30
zs = []
31
cov = []
32
for t in range (count):
33
z = dog.sense()
34
f.update (z)
35
#print (t,z)
36
ps.append (f.x[0,0])
37
cov.append(f.P)
38
zs.append(z)
39
f.predict()
40
41
p0, = plt.plot([0,count],[0,count],'g')
42
p1, = plt.plot(range(1,count+1),zs,c='r', linestyle='dashed')
43
p2, = plt.plot(range(1,count+1),ps, c='b')
44
plt.axis('equal')
45
plt.legend([p0,p1,p2], ['actual','measurement', 'filter'], 2)
46
plt.title(title)
47
48
for i,p in enumerate(cov):
49
print(i,p)
50
e = stats.sigma_ellipse (p, i+1, ps[i])
51
stats.plot_sigma_ellipse(e, axis_equal=False)
52
plt.xlim((-1,count))
53
plt.show()
54
55
56
if __name__ == "__main__":
57
plot_track (noise=30, R=5, Q=2, count=20)
58