forked from johannesloor/WebAudio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcomparison.js
74 lines (65 loc) · 2.58 KB
/
comparison.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
const timeMargin = 600; // Within this margin the time value is considered correct
const positionMargin = 13; // Within this margin the coordinates is considered correct
const ratioMargin = 0.35; // The percentage of data points that can be wrong
/**
* A type that represents one data point from a sequence. Consists of a time value and a xy coordinate pair.
* @typedef {Object} DataPoint
* @property {number} time
* @property {{relX: number, relY: number}} value
*/
/**
* Checks if `lastSequence` is equal to `storedSequence` within the specified margins.
* @param {DataPoint[]} lastSequence
* @param {DataPoint[]} storedSequence
* @returns {boolean}
*/
function compare(lastSequence, storedSequence) {
lastSequence = lastSequence._events;
storedSequence = storedSequence._events;
console.log(lastSequence);
console.log(storedSequence);
let numInMargin = 0;
for (let index = 0; index < lastSequence.length; index++) {
const dataPoint = lastSequence[index];
if (dataPointInSequence(dataPoint, storedSequence)) {
numInMargin += 1;
}
}
const correctRatio = numInMargin / lastSequence.length;
const lastSequenceLength = lastSequence[lastSequence.length - 1].time
const storedSequenceLength = storedSequence[storedSequence.length - 1].time
console.log("Ratio of correct points: " + correctRatio);
console.log("Time diff: " + (lastSequenceLength - storedSequenceLength));
return (
inMargin(correctRatio, 1, ratioMargin) && inMargin(lastSequenceLength, storedSequenceLength, timeMargin)
);
}
/**
* Checks if `dataPoint` ± the specified margin exists in the array `sequence`.
* @param {DataPoint} dataPoint
* @param {DataPoint[]} sequence
* @returns {boolean}
*/
function dataPointInSequence(dataPoint, sequence) {
return sequence.some(compareDataPoint.bind(null, dataPoint));
}
/**
* Checks if `singlePoint` is `sequencePoint` ± the specified margin.
* @param {DataPoint} singlePoint
* @param {DataPoint} sequencePoint
*/
function compareDataPoint(singlePoint, sequencePoint) {
return (inMargin(sequencePoint.time, singlePoint.time, timeMargin) &&
inMargin(sequencePoint.value.relX, singlePoint.value.relX, positionMargin) &&
inMargin(sequencePoint.value.relY, singlePoint.value.relY, positionMargin));
}
/**
* A helper that checks if `comparedValue` is in the interval `centerValue ± margin`
* @param {number} comparedValue
* @param {number} centerValue
* @param {number} margin
* @returns {boolean}
*/
function inMargin(comparedValue, centerValue, margin) {
return comparedValue <= centerValue + margin && comparedValue >= centerValue - margin;
}