Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
96129 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
from filterpy.gh import (GHFilter, GHKFilter, least_squares_parameters,
21
optimal_noise_smoothing, GHFilterOrder)
22
from numpy import array
23
from numpy.random import randn
24
import matplotlib.pyplot as plt
25
26
27
def test_least_squares():
28
29
""" there is an alternative form for computing h for the least squares.
30
It works for all but the very first term (n=0). Use it to partially test
31
the output of least_squares_parameters(). This test does not test that
32
g is correct"""
33
34
for n in range (1, 100):
35
g,h = least_squares_parameters(n)
36
37
h2 = 4 - 2*g - (4*(g-2)**2 - 3*g**2)**.5
38
39
assert abs(h2-h) < 1.e-12
40
41
42
def test_1d_array():
43
f1 = GHFilter (0, 0, 1, .8, .2)
44
f2 = GHFilter (array([0]), array([0]), 1, .8, .2)
45
46
# test both give same answers, and that we can
47
# use a scalar for the measurment
48
for i in range(1,10):
49
f1.update(i)
50
f2.update(i)
51
52
assert f1.x == f2.x[0]
53
assert f1.dx == f2.dx[0]
54
55
assert f1.VRF() == f2.VRF()
56
57
# test using an array for the measurement
58
for i in range(1,10):
59
f1.update(i)
60
f2.update(array([i]))
61
62
assert f1.x == f2.x[0]
63
assert f1.dx == f2.dx[0]
64
65
assert f1.VRF() == f2.VRF()
66
67
68
def test_2d_array():
69
""" test using 2 independent variables for the
70
state variable.
71
"""
72
73
f = GHFilter(array([0,1]), array([0,0]), 1, .8, .2)
74
f0 = GHFilter(0, 0, 1, .8, .2)
75
f1 = GHFilter(1, 0, 1, .8, .2)
76
77
# test using scalar in update (not normal, but possible)
78
for i in range (1,10):
79
f.update (i)
80
f0.update(i)
81
f1.update(i)
82
83
assert f.x[0] == f0.x
84
assert f.x[1] == f1.x
85
86
assert f.dx[0] == f0.dx
87
assert f.dx[1] == f1.dx
88
89
# test using array for update (typical scenario)
90
f = GHFilter(array([0,1]), array([0,0]), 1, .8, .2)
91
f0 = GHFilter(0, 0, 1, .8, .2)
92
f1 = GHFilter(1, 0, 1, .8, .2)
93
94
for i in range (1,10):
95
f.update (array([i, i+3]))
96
f0.update(i)
97
f1.update(i+3)
98
99
assert f.x[0] == f0.x
100
assert f.x[1] == f1.x
101
102
assert f.dx[0] == f0.dx
103
assert f.dx[1] == f1.dx
104
105
assert f.VRF() == f0.VRF()
106
assert f.VRF() == f1.VRF()
107
108
109
110
def optimal_test():
111
def fx(x):
112
return .1*x**2 + 3*x -4
113
114
g,h,k = optimal_noise_smoothing(.2)
115
f = GHKFilter(-4,0,0,1,g,h,k)
116
117
ys = []
118
zs = []
119
for i in range(100):
120
z = fx(i) + randn()*10
121
f.update(z)
122
ys.append(f.x)
123
zs.append(z)
124
125
plt.plot(ys)
126
plt.plot(zs)
127
128
129
def foo():
130
def fx(x):
131
return 2*x+1
132
133
f1 = GHFilterOrder(x0=array([0,0]), dt=1, order=1, g=.6, h=.02)
134
f2 = GHFilter(x=0, dx=0, dt=1, g=.6, h=.02)
135
136
for i in range(100):
137
z = fx(i) + randn()
138
f1.update(z)
139
f2.update(z)
140
141
assert abs(f1.x[0]-f2.x) < 1.e-18
142
143
foo()
144
145
if __name__ == "__main__":
146
optimal_test()
147
148
test_least_squares()
149
test_1d_array()
150
test_2d_array()
151
152
print('all passed')
153
154