Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
96131 views
1
# -*- coding: utf-8 -*-
2
"""Copyright 2015 Roger R Labbe Jr.
3
4
FilterPy library.
5
http://github.com/rlabbe/filterpy
6
7
Documentation at:
8
https://filterpy.readthedocs.org
9
10
Supporting book at:
11
https://github.com/rlabbe/Kalman-and-Bayesian-Filters-in-Python
12
13
This is licensed under an MIT license. See the readme.MD file
14
for more information.
15
"""
16
17
from __future__ import (absolute_import, division, print_function,
18
unicode_literals)
19
20
import numpy.random as random
21
import numpy as np
22
import matplotlib.pyplot as plt
23
from filterpy.kalman import KalmanFilter
24
DO_PLOT = False
25
26
27
28
def test_noisy_1d():
29
f = KalmanFilter (dim_x=2, dim_z=1)
30
31
f.x = np.array([[2.],
32
[0.]]) # initial state (location and velocity)
33
34
f.F = np.array([[1.,1.],
35
[0.,1.]]) # state transition matrix
36
37
f.H = np.array([[1.,0.]]) # Measurement function
38
f.P *= 1000. # covariance matrix
39
f.R = 5 # state uncertainty
40
f.Q = 0.0001 # process uncertainty
41
42
measurements = []
43
results = []
44
45
zs = []
46
for t in range (100):
47
# create measurement = t plus white noise
48
z = t + random.randn()*20
49
zs.append(z)
50
51
# perform kalman filtering
52
f.update(z)
53
f.predict()
54
55
# save data
56
results.append (f.x[0,0])
57
measurements.append(z)
58
59
60
# now do a batch run with the stored z values so we can test that
61
# it is working the same as the recursive implementation.
62
# give slightly different P so result is slightly different
63
f.x = np.array([[2.,0]]).T
64
f.P = np.eye(2)*100.
65
m,c,_,_ = f.batch_filter(zs,update_first=False)
66
67
# plot data
68
if DO_PLOT:
69
p1, = plt.plot(measurements,'r', alpha=0.5)
70
p2, = plt.plot (results,'b')
71
p4, = plt.plot(m[:,0], 'm')
72
p3, = plt.plot ([0,100],[0,100], 'g') # perfect result
73
plt.legend([p1,p2, p3, p4],
74
["noisy measurement", "KF output", "ideal", "batch"], loc=4)
75
76
77
plt.show()
78
79
80
81
def test_noisy_11d():
82
f = KalmanFilter (dim_x=2, dim_z=1)
83
84
f.x = np.array([2., 0]) # initial state (location and velocity)
85
86
f.F = np.array([[1.,1.],
87
[0.,1.]]) # state transition matrix
88
89
f.H = np.array([[1.,0.]]) # Measurement function
90
f.P *= 1000. # covariance matrix
91
f.R = 5 # state uncertainty
92
f.Q = 0.0001 # process uncertainty
93
94
measurements = []
95
results = []
96
97
zs = []
98
for t in range (100):
99
# create measurement = t plus white noise
100
z = t + random.randn()*20
101
zs.append(z)
102
103
# perform kalman filtering
104
f.update(z)
105
f.predict()
106
107
# save data
108
results.append (f.x[0])
109
measurements.append(z)
110
111
112
# now do a batch run with the stored z values so we can test that
113
# it is working the same as the recursive implementation.
114
# give slightly different P so result is slightly different
115
f.x = np.array([[2.,0]]).T
116
f.P = np.eye(2)*100.
117
m,c,_,_ = f.batch_filter(zs,update_first=False)
118
119
# plot data
120
if DO_PLOT:
121
p1, = plt.plot(measurements,'r', alpha=0.5)
122
p2, = plt.plot (results,'b')
123
p4, = plt.plot(m[:,0], 'm')
124
p3, = plt.plot ([0,100],[0,100], 'g') # perfect result
125
plt.legend([p1,p2, p3, p4],
126
["noisy measurement", "KF output", "ideal", "batch"], loc=4)
127
128
129
plt.show()
130
131
132
if __name__ == "__main__":
133
DO_PLOT = True
134
135
136
test_noisy_11d()
137