-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcalibrator.js
362 lines (301 loc) · 10.8 KB
/
calibrator.js
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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
// MIT License
// Copyright (c) 2019
// Eugene M. Taranta II <[email protected]>
// Seng Lee Koh <[email protected]>
// Brian M. Williamson <[email protected]>
// Kevin P. Pfeil <[email protected]>
// Corey R. Pittman <[email protected]>
// Joseph J. LaViola Jr. <[email protected]>
// University of Central Florida
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
/**
* Example calibration system.
*/
function Calibrator(
leastPrecision,
worstLag_s,
lowPassFilter)
{
/**
* Steps in calibration procedure.
*/
const calibrationStates =
{
WAIT_TO_START: 'wait to start',
START: 'start',
ESTIMATE_SAMPLE_RATE: 'sample rate (fps)',
ESTIMATE_NOISE_PREPARE: 'noise (prepare)',
ESTIMATE_NOISE: 'noise',
ESTIMATE_AMPLITUDE_PREPARE: 'amplitude (prepare)',
ESTIMATE_AMPLITUDE: 'amplitude',
ESTIMATE_PARAMETERS: 'parameters',
ESTIMATE_TUNED: 'tuned',
COMPLETE: 'complete',
};
this.states = calibrationStates;
/**
* This object is used to estimate the maximum
* amplitude (speed) the user will move AND at
* the same time, we estimate the sampling rate.
*/
function EstimateSampleRate()
{
var frameRateEstimator = new FrameRateEstimator();
/**
* Feed each sampling into this update function.
*/
this.update = function()
{
frameRateEstimator.update();
}
/**
* Get most recent sampling rate estimate.
*/
this.sample_hz = function()
{
return Math.round(frameRateEstimator.fps());
}
};
/**
* This object is used to estimate the maximum
* amplitude (speed) the user will move AND at
* the same time, we estimate the sampling rate.
*/
function EstimateAmplitude(noiseStddev)
{
var distanceEstimatorX = new MaximumDistanceEstimator();
var distanceEstimatorY = new MaximumDistanceEstimator();
var noiseStddev = noiseStddev;
/**
* Feed each sampling into this update function.
*/
this.update = function(
posX,
posY)
{
distanceEstimatorX.update(posX, noiseStddev);
distanceEstimatorY.update(posY, noiseStddev);
}
/**
* Get most recent maximum amplitude estimate.
*/
this.amplitude = function()
{
return Math.max(
distanceEstimatorX.velocity(),
distanceEstimatorY.velocity());
}
};
/**
* Estimate noise in signal. The user ought be asked
* to hold still during this time. Slow, idle motions
* are okay, but rapid movements and jerks may inflate
* the noise estimate.
*/
function EstimateNoise(
sample_hz,
threshold = 0.01)
{
var noiseEstimatorsX = []
var noiseEstimatorsY = []
var stats = new RunningStatistics();
var threshold = threshold;
// The Nyquist frequency is half the sampling rate.
// We can monitor those frequencies that fall
// between 10Hz and the Nyquist, which still allows
// for some slow, low frequency, idling motion.
sample_hz = Math.round(sample_hz)
sample_hz = sample_hz + (sample_hz % 1);
frequency_cnt = sample_hz / 2.0 - 10.0;
for (var ii = 0.0;
ii < frequency_cnt;
ii += 1)
{
noiseEstimatorsX.push(new NoiseEstimator(ii, sample_hz));
noiseEstimatorsY.push(new NoiseEstimator(ii, sample_hz));
}
/**
* Update estimate with new samples. Note, we assume
* noise is homogeneous across X and Y.
*
* Returns true once the 95% CI width is
* within a given threshold of the mean.
*/
this.update = function(
posX,
posY)
{
var ii = 0;
for(ii = 0;
ii < noiseEstimatorsX.length;
ii++)
{
noiseEstimatorsX[ii].update(posX);
noiseEstimatorsY[ii].update(posY);
varX = noiseEstimatorsX[ii].variance();
varY = noiseEstimatorsY[ii].variance();
if(varX == 0)
{
continue;
}
stats.update(varX);
stats.update(varY);
}
ratio = (2.0 * stats.ci95) / stats.mean;
return (ratio < threshold);
}
/**
* Return white noise variance estimate,
* which is the mean of our PSD estimates.
*/
this.variance = function()
{
return stats.mean;
}
/**
* For debug, display purposes.
*/
this.countDown = function()
{
ratio = (2.0 * stats.ci95) / stats.mean;
return ratio - threshold;
}
}
/**
*
*/
function RunCalibrationProcedure()
{
this.currentState = calibrationStates.WAIT_TO_START;
var sampleRateEstimator = null;
var noiseEstimator = null;
var amplitudeEstimator = null;
var startTime_ms = null;
var lastUpdateTime_ms = 0.0;
var noiseStddev = null;
this.targetHitAttempts = 0.0;
this.amplitude = 0.0;
this.sample_hz = 0.0;
var that = this;
this.estNoiseVariance = function()
{
return noiseEstimator.variance();
}
//
this.update = function(x, y)
{
delta_ms = Date.now() - startTime_ms;
// Give first instruction, then kick off.
if(this.currentState == calibrationStates.START)
{
sampleRateEstimator = new EstimateSampleRate();
this.currentState = calibrationStates.ESTIMATE_SAMPLE_RATE;
startTime_ms = Date.now();
}
// First estimate the sample rate
else if (this.currentState == calibrationStates.ESTIMATE_SAMPLE_RATE)
{
sampleRateEstimator.update();
if(delta_ms > 2000.0)
{
this.currentState = calibrationStates.ESTIMATE_NOISE_PREPARE;
sample_hz = sampleRateEstimator.sample_hz();
console.log('Sample rate: ' + sample_hz);
noiseEstimator = new EstimateNoise(sample_hz);
startTime_ms = Date.now();
that.sample_hz = sample_hz;
}
}
// Wait until system is ready.
else if(this.currentState == calibrationStates.ESTIMATE_NOISE_PREPARE)
{
startTime_ms = Date.now();
}
// Estimate noise in signal, after which we can tune
// the filter.
else if (this.currentState == calibrationStates.ESTIMATE_NOISE)
{
// Give time for user to settle
if(delta_ms < 1000.0)
{
return this.currentState;
}
complete = noiseEstimator.update(x, y);
if(Date.now() - lastUpdateTime_ms > 250)
{
lastUpdateTime_ms = Date.now();
}
if(complete == true)
{
noiseStddev = Math.sqrt(noiseEstimator.variance());
this.currentState = calibrationStates.ESTIMATE_AMPLITUDE_PREPARE;
amplitudeEstimator = new EstimateAmplitude(noiseStddev);
this.targetHitAttempts = 0.0;
startTime_ms = Date.now();
}
}
// Wait for caller to advance state
else if (this.currentState == calibrationStates.ESTIMATE_AMPLITUDE_PREPARE)
{
}
// Determine maximum movement size over about three seconds,
// then go to noise estimation state.
else if (this.currentState == calibrationStates.ESTIMATE_AMPLITUDE)
{
amplitudeEstimator.update(
x,
y);
}
// Finally, tune the filter!
else if (this.currentState == calibrationStates.ESTIMATE_PARAMETERS)
{
lowPassFilter.tune(
leastPrecision / 3.0,
worstLag_s,
noiseStddev * noiseStddev,
amplitudeEstimator.amplitude(),
sampleRateEstimator.sample_hz());
this.currentState = calibrationStates.TUNED;
that.amplitude = amplitudeEstimator.amplitude();
}
return this.currentState;
}
}
this.procedure = new RunCalibrationProcedure()
}
// The smallest target in our Fitt's law test.
var minimumTargetSize = 14.0;
// Results show approximately that if spatial jitter
// is less than a quarter of the target size, the impact
// on misses is negligible.
var leastPrecision = Math.floor(minimumTargetSize * 0.25)
// Similarly, lag doesn't become much of a problem
// until it reaches above 80ms.
var maxLag_s = 0.080
// Note, precision is given priority over lag. In general,
// if we can meet the precision requirement and we are
// below max lag, then we can try to tighten precision.
//var calibrator = Calibrator(
// leastPrecision,
// maxLag_s,
// new EmaFilter());
/**
var calibrator = Calibrator(
1.0,
maxLag_s,
new EuroFilter(1, 1));
/**/