@hyperframes/core 0.8.159 → 0.8.160

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,41 @@
1
+ /** Moving reported times onto measured ones, one at a time, and saying how far they moved. */
2
+ /** Index of the value in ascending `sorted` nearest `t`. */
3
+ function nearestIndex(sorted, t) {
4
+ let low = 0;
5
+ let high = sorted.length - 1;
6
+ while (low < high) {
7
+ const mid = (low + high) >> 1;
8
+ if (sorted[mid] < t)
9
+ low = mid + 1;
10
+ else
11
+ high = mid;
12
+ }
13
+ return low > 0 && t - sorted[low - 1] <= sorted[low] - t ? low - 1 : low;
14
+ }
15
+ /** Snaps each time to the nearest of `measured` (ascending) when one is within `toleranceSeconds`, else keeps it. */
16
+ export function snapper(measured, toleranceSeconds, reference) {
17
+ const shifts = [];
18
+ const unsnapped = [];
19
+ return {
20
+ snap(t) {
21
+ const nearest = measured.length ? measured[nearestIndex(measured, t)] : null;
22
+ if (nearest === null || Math.abs(nearest - t) > toleranceSeconds) {
23
+ unsnapped.push(t);
24
+ return t;
25
+ }
26
+ shifts.push(Math.abs(nearest - t));
27
+ return nearest;
28
+ },
29
+ report() {
30
+ const sorted = [...shifts].sort((a, b) => a - b);
31
+ const median = sorted.length ? sorted[sorted.length >> 1] : null;
32
+ return {
33
+ reference,
34
+ toleranceMs: Math.round(toleranceSeconds * 1000),
35
+ snapped: shifts.length,
36
+ unsnapped: [...unsnapped],
37
+ medianShiftMs: median === null ? null : Math.round(median * 1000),
38
+ };
39
+ },
40
+ };
41
+ }
package/dist/fft.d.ts ADDED
@@ -0,0 +1,2 @@
1
+ /** In-place iterative radix-2 FFT. */
2
+ export declare function fft(re: Float64Array, im: Float64Array): void;
package/dist/fft.js ADDED
@@ -0,0 +1,36 @@
1
+ /** In-place iterative radix-2 FFT. */
2
+ export function fft(re, im) {
3
+ const n = re.length;
4
+ for (let i = 1, j = 0; i < n; i++) {
5
+ let bit = n >> 1;
6
+ for (; j & bit; bit >>= 1)
7
+ j ^= bit;
8
+ j ^= bit;
9
+ if (i < j) {
10
+ [re[i], re[j]] = [re[j], re[i]];
11
+ [im[i], im[j]] = [im[j], im[i]];
12
+ }
13
+ }
14
+ for (let len = 2; len <= n; len <<= 1) {
15
+ const ang = (-2 * Math.PI) / len;
16
+ const wr = Math.cos(ang);
17
+ const wi = Math.sin(ang);
18
+ for (let i = 0; i < n; i += len) {
19
+ let cr = 1;
20
+ let ci = 0;
21
+ for (let k = 0; k < len / 2; k++) {
22
+ const ur = re[i + k];
23
+ const ui = im[i + k];
24
+ const vr = re[i + k + len / 2] * cr - im[i + k + len / 2] * ci;
25
+ const vi = re[i + k + len / 2] * ci + im[i + k + len / 2] * cr;
26
+ re[i + k] = ur + vr;
27
+ im[i + k] = ui + vi;
28
+ re[i + k + len / 2] = ur - vr;
29
+ im[i + k + len / 2] = ui - vi;
30
+ const ncr = cr * wr - ci * wi;
31
+ ci = cr * wi + ci * wr;
32
+ cr = ncr;
33
+ }
34
+ }
35
+ }
36
+ }