-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGapLERP.js
73 lines (67 loc) · 2.26 KB
/
GapLERP.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
class GapLERP {
/**
* Runs the GapLERP algorithm on the given array.
* @param {Array} arr - The array with undefined values.
* @returns {Array} - The array with gaps seamlessly filled.
*/
static run(arr) {
const gaps = this.undefined_gap(arr);
return this.fix_gaps(arr, gaps);
}
/**
* Fills gaps in the array using linear interpolation.
* @param {Array} arr - The array with gaps.
* @param {Array} gaps - Array indices where gaps exist.
* @returns {Array} - The array with gaps filled.
*/
static fix_gaps(arr, gaps) {
for (let i = 0, L = gaps.length; i !== L; i += 2) {
const start = gaps[i];
const startVal = arr[start];
const totalUndefined = gaps[i + 1] - start;
const alg = arr[gaps[i + 1]] - startVal;
const spread = alg / totalUndefined;
for (let n = 0; n !== totalUndefined; ++n) {
arr[start + n] = startVal + (spread * n);
}
}
return arr;
}
/**
* Finds indices where undefined values create gaps in the array.
* @param {Array} arr - The array with undefined values.
* @returns {Array} - Array indices where gaps exist.
*/
static undefined_gap(arr) {
for (let i = 1, L = arr.length, gaps = []; i !== L; ++i) {
if (arr[i] === undefined && arr[i - 1] !== undefined) gaps.push(i - 1);
else if (arr[i] !== undefined && arr[i - 1] === undefined) gaps.push(i);
}
return gaps;
}
/**
* Replaces specific values in the array with a provided value.
* @param {Array} arr - The array to modify.
* @param {number} targetValue - The value to replace.
* @param {number} replacementValue - The new value.
* @returns {Array} - The modified array.
*/
static replace_values(arr, targetValue, replacementValue) {
for (let i = 0, L = arr.length; i !== L; ++i) {
if (arr[i] === targetValue) {
arr[i] = replacementValue;
}
}
return arr;
}
/**
* Calculates the average value of the array.
* @param {Array} arr - The array of numeric values.
* @returns {number} - The average value.
*/
static calculate_average(arr) {
const sum = arr.reduce((acc, val) => acc + val, 0);
return sum / arr.length;
}
}
module.exports = GapLERP;