forked from regnull/kalman
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgeo_filter_test.go
214 lines (195 loc) · 5.69 KB
/
geo_filter_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
package kalman
import (
"fmt"
"math"
"testing"
"github.com/regnull/kalman/geo"
"github.com/stretchr/testify/assert"
)
func TestSpeedLatAccuracy(t *testing.T) {
assert := assert.New(t)
dirRad := 45.0 * math.Pi / 180.0
dirRadAccuracy := 5.0 * math.Pi / 180.0
metersPerDegreeLat := geo.FastMetersPerDegreeLat(43.0)
acc := speedLatAccuracy(10.0, 1.0, dirRad, dirRadAccuracy, metersPerDegreeLat)
assert.InDelta(8.447849136425959e-06, acc, 0.01)
}
func TestSpeedLngAccuracy(t *testing.T) {
assert := assert.New(t)
dirRad := 45.0 * math.Pi / 180.0
dirRadAccuracy := 5.0 * math.Pi / 180.0
metersPerDegreeLng := geo.FastMetersPerDegreeLng(43.0)
acc := speedLngAccuracy(10.0, 1.0, dirRad, dirRadAccuracy, metersPerDegreeLng)
assert.InDelta(1.1509487689869777e-05, acc, 0.01)
}
func TestImproveGeoAccuracy(t *testing.T) {
// Test that repeated simultaneous observations improve precision.
assert := assert.New(t)
g, err := NewGeoFilter(&GeoProcessNoise{})
assert.NoError(err)
ob := &GeoObserved{
Lat: 43.0,
Lng: -71.0,
Altitude: 100.0,
Speed: 0.0,
SpeedAccuracy: 0.01,
Direction: 0.0,
DirectionAccuracy: 5.0,
HorizontalAccuracy: 100.0,
VerticalAccuracy: 10.0,
}
// First observation.
assert.NoError(g.Observe(0.0, ob))
e := g.Estimate()
assert.InDelta(43.0, e.Lat, 0.01)
assert.InDelta(-71.0, e.Lng, 0.01)
assert.InDelta(100.0, e.HorizontalAccuracy, 0.01)
// Second observation.
assert.NoError(g.Observe(0.0, ob))
e = g.Estimate()
assert.InDelta(43.0, e.Lat, 0.01)
assert.InDelta(-71.0, e.Lng, 0.01)
// TODO: This should really be 50.0
assert.InDelta(70.71067811865476, e.HorizontalAccuracy, 0.01)
// Third observation.
assert.NoError(g.Observe(0.0, ob))
e = g.Estimate()
assert.InDelta(43.0, e.Lat, 0.01)
assert.InDelta(-71.0, e.Lng, 0.01)
assert.InDelta(57.73502691896258, e.HorizontalAccuracy, 0.01)
}
func TestMoreRecentIsBetter(t *testing.T) {
// Test that more recent observation is preferred over the past ones.
assert := assert.New(t)
g, err := NewGeoFilter(&GeoProcessNoise{
BaseLat: 43.0,
DistancePerSecond: 1.0, // Meters.
SpeedPerSecond: 0.1, // Meters per second.
})
assert.NoError(err)
ob := &GeoObserved{
Lat: 43.0,
Lng: -71.0,
Altitude: 100.0,
Speed: 0.0,
SpeedAccuracy: 0.01,
Direction: 0.0,
DirectionAccuracy: 5.0,
HorizontalAccuracy: 10.0,
VerticalAccuracy: 10.0,
}
assert.NoError(g.Observe(0.0, ob))
e := g.Estimate()
assert.InDelta(43.0, e.Lat, 0.01)
assert.InDelta(-71.0, e.Lng, 0.01)
assert.InDelta(10.0, e.HorizontalAccuracy, 0.0001)
// Second observation is a bit further away, and it happens 10 seconds later.
ob.Lat = 43.01
assert.NoError(g.Observe(100.0, ob))
e = g.Estimate()
assert.InDelta(43.006, e.Lat, 0.0001)
}
func TestGeoConvergeOnLocation(t *testing.T) {
assert := assert.New(t)
g, err := NewGeoFilter(&GeoProcessNoise{
BaseLat: 43.0,
DistancePerSecond: 1.0, // Meters.
SpeedPerSecond: 0.1, // Meters per second.
})
assert.NoError(err)
ob0 := &GeoObserved{
Lat: 43.0,
Lng: -71.0,
Altitude: 100.0,
Speed: 0.0,
SpeedAccuracy: 0.01,
Direction: 0.0,
DirectionAccuracy: 5.0,
HorizontalAccuracy: 10.0,
VerticalAccuracy: 10.0,
}
ob1 := &GeoObserved{
Lat: 43.0,
Lng: -71.001,
Altitude: 100.0,
Speed: 0.0,
SpeedAccuracy: 0.01,
Direction: 0.0,
DirectionAccuracy: 5.0,
HorizontalAccuracy: 10.0,
VerticalAccuracy: 10.0,
}
n, err := geoConverge(g, ob0, ob1, 1.0, 1.0, 100)
assert.NoError(err)
fmt.Printf("converged in %d iterations\n", n)
assert.True(n > 0)
}
func TestGeoConvergeFasterWithMoreNoise(t *testing.T) {
assert := assert.New(t)
g, err := NewGeoFilter(&GeoProcessNoise{
BaseLat: 43.0,
DistancePerSecond: 1.0, // Meters.
SpeedPerSecond: 0.1, // Meters per second.
})
assert.NoError(err)
ob0 := &GeoObserved{
Lat: 43.0,
Lng: -71.0,
Altitude: 100.0,
Speed: 0.0,
SpeedAccuracy: 0.01,
Direction: 0.0,
DirectionAccuracy: 5.0,
HorizontalAccuracy: 10.0,
VerticalAccuracy: 10.0,
}
ob1 := &GeoObserved{
Lat: 43.0,
Lng: -71.001,
Altitude: 100.0,
Speed: 0.0,
SpeedAccuracy: 0.01,
Direction: 0.0,
DirectionAccuracy: 5.0,
HorizontalAccuracy: 10.0,
VerticalAccuracy: 10.0,
}
n1, err := geoConverge(g, ob0, ob1, 1.0, 1.0, 100)
assert.NoError(err)
fmt.Printf("1: converged in %d iterations\n", n1)
assert.True(n1 > 0)
// Increase noise.
g, err = NewGeoFilter(&GeoProcessNoise{
BaseLat: 43.0,
DistancePerSecond: 2.0, // Meters.
SpeedPerSecond: 0.1, // Meters per second.
})
n2, err := geoConverge(g, ob0, ob1, 1.0, 1.0, 100)
assert.NoError(err)
fmt.Printf("2: converged in %d iterations\n", n2)
assert.True(n2 > 0)
assert.True(n2 < n1)
}
func geoConverge(filter *GeoFilter, loc0, loc1 *GeoObserved, td float64, distance float64, maxIter int) (int, error) {
if err := filter.Observe(td, loc0); err != nil {
return 0, err
}
for i := 0; i < maxIter; i++ {
if err := filter.Observe(td, loc1); err != nil {
return 0, err
}
e := filter.Estimate()
if e == nil {
return 0, fmt.Errorf("cannot estimate location")
}
dlat := e.Lat - loc1.Lat
dlng := e.Lng - loc1.Lng
dx := dlat * geo.MetersPerDegreeLat(loc1.Lat)
dy := dlng * geo.MetersPerDegreeLng(loc1.Lng)
d := math.Sqrt(dx*dx + dy*dy)
if d < distance {
return i, nil
}
}
return maxIter, fmt.Errorf("max iteration reached")
}