-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSensorSimulation.py
295 lines (248 loc) · 11.9 KB
/
SensorSimulation.py
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
# See https://github.com/rlabbe/Kalman-and-Bayesian-Filters-in-Python
# =================================================================================
from numpy import sin,pi
from numpy.random import randn
from numpy import array,sin
from pandas import DataFrame
from noise import pnoise1
# =================================================================================
# Each class provides
# step() - take a step of size dt
# sense() - current value + measurement error
# step_and_sense() - combination of step and sense
# =================================================================================
def smooth_noise( dx=0.0005, std=1. ):
d = 0.
dt = dx
s = std
def next():
nonlocal d, dt, s
d += dt
return s*pnoise1(d,repeat=1000000)
return next
# TODO replace [va]_std with upd = smooth_noise()
# use with upd() instead of randn()*self.[vz]_std....
# =================================================================================
def measurements( df, sel ):
m = df.loc[:,sel].as_matrix()
return m.reshape( m.shape[0], len(sel), 1)
# =================================================================================
class Const1DVelocitySensor(object):
''' Simulate an object moving at constant velocity'''
def __init__(self, x=0, v=1, dt=0.1, noise_std=1.):
self.x = float(x)
self.v = float(v)
self.noise_std = float(noise_std)
self.dt = float(dt)
self.dx = float(v*dt)
def step(self):
self.x += self.dx
return self.x
def sense(self):
return self.x + randn() * self.noise_std
def step_and_sense(self):
self.x += self.dx
return ( self.x + randn() * self.noise_std, self.x )
def step_and_track(self, n):
x,t_x = zip( *[self.step_and_sense() for _ in range(n)])
return DataFrame( { 'x' : x, 'x.true' : t_x } )
# ---------------------------------------------------------------------------------
class Const2DVelocitySensor(object):
''' Simulate an object moving at constant velocity'''
def __init__(self, x=(0,0), v=(1,0), dt=0.1, noise_std=1.):
self.x_sensor = Const1DVelocitySensor( x=x[0], v=v[0], dt=dt, noise_std=noise_std )
self.y_sensor = Const1DVelocitySensor( x=x[1], v=v[1], dt=dt, noise_std=noise_std )
def step(self):
x = self.x_sensor.step()
y = self.y_sensor.step()
return (x,y)
def sense(self):
x = self.x_sensor.sense()
y = self.y_sensor.sense()
return (x,y)
def step_and_sense(self):
x,t_x = self.x_sensor.step_and_sense()
y,t_y = self.y_sensor.step_and_sense()
return (x,y,t_x,t_y)
def step_and_track(self, n):
x,y,t_x,t_y = zip( *[self.step_and_sense() for _ in range(n)])
return DataFrame( { 'x' : x, 'y' : y, 'x.true' : t_x, 'y.true' : t_y } )
# =================================================================================
class PseudoConst1DVelocitySensor(object):
''' Simulate an object moving at almost constant velocity
TODO: velocity is currently a random walk - replace with smooth noise....
'''
def __init__(self, x=0, v=1, dt=0.1, v_std=0., noise_std=1.):
self.x = float(x)
self.v = float(v)
self.noise_std = float(noise_std)
self.dt = float(dt)
self.v_std = float(v_std)
def step(self):
self.v += randn() * self.v_std;
self.x += self.v * self.dt
return ( self.x, self.v )
def sense(self):
return (self.x + randn() * self.noise_std, self.v)
def step_and_sense(self):
self.v += randn() * self.v_std;
self.x += self.v * self.dt
return (self.x + randn() * self.noise_std, self.x, self.v)
def step_and_track(self, n):
x,t_x,xDot = zip( *[self.step_and_sense() for _ in range(n)])
return DataFrame( { 'x' : x, 'xDot' : xDot, 'x.true' : t_x } )
# ---------------------------------------------------------------------------------
class PseudoConst2DVelocitySensor(object):
''' Simulate an object moving at constant velocity - TODO smooth_noise'''
def __init__(self, x=(0,0), v=(1,0), dt=0.1, v_std=0., noise_std=1.):
self.x_sensor = PseudoConst1DVelocitySensor( x=x[0], v=v[0], dt=dt, v_std=v_std, noise_std=noise_std )
self.y_sensor = PseudoConst1DVelocitySensor( x=x[1], v=v[1], dt=dt, v_std=v_std, noise_std=noise_std )
def step(self):
x,xv = self.x_sensor.step()
y,yv = self.y_sensor.step()
return (x,y,xv,yv)
def sense(self):
x,xv = self.x_sensor.sense()
y,yv = self.y_sensor.sense()
return (x,y,xv,yv)
def step_and_sense(self):
x,t_x,xDot = self.x_sensor.step_and_sense()
y,t_y,yDot = self.y_sensor.step_and_sense()
return ( x,y, xDot, yDot, t_x, t_y )
def step_and_track(self, n):
x,y, xDot, yDot, t_x, t_y = zip( *[self.step_and_sense() for _ in range(n)])
return DataFrame( { 'x' : x, 'xDot' : xDot, 'x.true' : t_x,
'y' : y, 'yDot' : yDot, 'y.true' : t_y } )
# =================================================================================
class Const1DAccelerationSensor(object):
''' Simulate an object moving at constant acceleration'''
def __init__(self, x=0, v=0, a=0, dt=0.1, x_noise_std=0., v_noise_std=0. ):
self.x = float(x)
self.v = float(v)
self.a = float(a)
self.v_noise_std = float(v_noise_std)
self.x_noise_std = float(x_noise_std)
self.dt = float(dt)
self.dv = float(a*dt)
def step(self):
self.v += self.dv
self.x += self.v * self.dt
return (self.x, self.v)
def sense(self):
return (self.x + randn() * self.x_noise_std,
self.v + randn() * self.v_noise_std)
def step_and_sense(self):
self.v += self.dv
self.x += self.v * self.dt
return (self.x + randn() * self.x_noise_std,
self.v + randn() * self.v_noise_std, self.x, self.v )
def step_and_track(self, n):
x,xDot,t_x,t_v = zip( *[self.step_and_sense() for _ in range(n)])
return DataFrame( { 'x' : x, 'xDot' : xDot, 'x.true' : t_x, 'xDot.true' : t_v } )
# ---------------------------------------------------------------------------------
class Const2DAccelerationSensor(object):
''' Simulate an object moving at constant acceleration'''
def __init__(self, x=(0,0), v=(1,0), a=(1,0), dt=0.1, x_noise_std=1., v_noise_std=0. ):
#self.x_sensor = Const1DyAccelerationSensor( x=x[0], v=v[0], a=a[0], dt=dt, v_std, noise_std=noise_std )
#self.y_sensor = Const1DyAccelerationSensor( x=x[1], v=v[1], a=a[1], dt=dt, v_std, noise_std=noise_std )
self.x_sensor = Const1DAccelerationSensor( x=x[0], v=v[0], a=a[0], dt=dt, noise_std=noise_std )
self.y_sensor = Const1DAccelerationSensor( x=x[1], v=v[1], a=a[1], dt=dt, noise_std=noise_std )
def step(self):
x,xv = self.x_sensor.step()
y,yv = self.y_sensor.step()
return (x,y, xv,yv)
def sense(self):
x,xv = self.x_sensor.sense()
y,yv = self.y_sensor.sense()
return (x,y, xv,yv)
def step_and_sense(self):
x,xDot,t_x,t_xDot = self.x_sensor.step_and_sense()
y,yDot,t_y,t_yDot = self.y_sensor.step_and_sense()
return (x, y, xDot, yDot, t_x, t_y, t_xDot, t_yDot)
def step_and_track(self, n):
x,xDot,t_x,t_xDot,y,yDot,t_y,t_yDot = zip( *[self.step_and_sense() for _ in range(n)])
return DataFrame( { 'x' : x, 'y' : y,
'xDot' : xDot, 'yDot' : yDot,
'x.true' : t_x, 'y.true' : t_y,
'xDot.true' : t_xDot, 'yDot.true' : t_yDot
} )
# =================================================================================
class PseudoConst1DAccelerationSensor(object):
''' Simulate an object moving at almost constant acceleration '''
def __init__(self, x=0, v=0, a=0, dt=0.1, x_noise_std=1., v_noise_std=1., a_std=0. ):
self.x = float(x)
self.v = float(v)
self.a = float(a)
self.dt = float(dt)
self.a_std = float(a_std)
self.v_noise_std = float(v_noise_std)
self.x_noise_std = float(x_noise_std)
def step(self):
self.a += randn() * self.a_std;
self.v += self.a*self.dt
self.x += self.v*self.dt
return (self.x, self.v, self.a)
def sense(self):
return (self.x + randn() * self.x_noise_std,
self.v + randn() * self.v_noise_std,
self.a)
def step_and_sense(self):
self.a += randn() * self.a_std;
self.v += self.a*self.dt
self.x += self.v*self.dt
return (self.x + randn() * self.x_noise_std,
self.v + randn() * self.v_noise_std,
self.x, self.v, self.a)
def step_and_track(self, n):
x,xDot,t_x,t_v,a = zip( *[self.step_and_sense() for _ in range(n)])
return DataFrame( { 'x' : x, 'xDot' : xDot, 'x.true' : t_x, 'xDot.true' : t_v, 'a' : a } )
# ---------------------------------------------------------------------------------
class PseudoConst2DAccelerationSensor(object):
''' Simulate an object moving at constant acceleration'''
def __init__(self, x=(0,0), v=(1,0), a=(1,0), dt=0.1, x_noise_std=1., v_noise_std=0., a_std=0. ):
self.x_sensor = PseudoConst1DAccelerationSensor( x=x[0], v=v[0], a=a[0], dt=dt, x_noise_std=x_noise_std, v_noise_std=v_noise_std, a_std=a_std )
self.y_sensor = PseudoConst1DAccelerationSensor( x=x[1], v=v[1], a=a[1], dt=dt, x_noise_std=x_noise_std, v_noise_std=v_noise_std, a_std=a_std )
def step(self):
x,xv,xa = self.x_sensor.step()
y,yv,ya = self.y_sensor.step()
return (x,y, xv,yv, xa,ya)
def sense(self):
x,xv,xa = self.x_sensor.sense()
y,yv,ya = self.y_sensor.sense()
return (x,y, xv,yv, xa,ya)
def step_and_sense(self):
x,xv,t_x,t_xv,xa = self.x_sensor.step_and_sense()
y,yv,t_y,t_yv,ya = self.y_sensor.step_and_sense()
return (x,y, xv,yv, t_x,t_y, t_xv, t_yv, xa,ya)
def step_and_track(self, n):
x,y, xv,yv, t_x,t_y, t_xv, t_yv, xa,ya = zip( *[self.step_and_sense() for _ in range(n)] )
return DataFrame( { 'x' : x, 'y' : y,
'xDot' : xv, 'yDot' : yv,
'x.true' : t_x, 'y.true' : t_y,
'xDot.true' : t_xv, 'yDot.true' : t_yv,
'ax' : xa, 'ay' : ya
} )
# =================================================================================
class Sinusoidal1DSensor(object):
''' Simulate an object moving at constant angular velocity'''
def __init__(self, phase=0, x0=0, amplitude=1., dt=0.1, freq=1., noise_std=1.):
self.amplitude = amplitude
self.phase = float(phase)
self.noise_std = float(noise_std)
self.dt = float(dt)
self.dtheta = 2. * pi * freq * self.dt
self.x0 = x0;
self.x = x0 + amplitude*sin(phase)
def step(self):
self.phase += self.dtheta
self.x = self.x0+self.amplitude*sin( self.phase )
return self.x
def sense(self):
return self.x + randn() * self.noise_std
def step_and_sense(self):
self.phase += self.dtheta
self.x = self.x0 + self.amplitude*sin( self.phase )
return ( self.x + randn() * self.noise_std, self.x )
def step_and_track(self, n):
x,t_x = zip( *[self.step_and_sense() for _ in range(n)])
return DataFrame( { 'x' : x, 'x.true' : t_x } )