| 113 | } |
| 114 | |
| 115 | function countExtremaRetainedWithinWindow( |
| 116 | extremaIndices: Int32Array, |
| 117 | sampledIndices: Int32Array, |
| 118 | windowHalfWidth: number, |
| 119 | ): number { |
| 120 | let retained = 0; |
| 121 | for (let e = 0; e < extremaIndices.length; e++) { |
| 122 | const idx = extremaIndices[e]!; |
| 123 | |
| 124 | // "Within window" definition: |
| 125 | // - exact retention passes |
| 126 | // - otherwise, allow the sampled point to land within +/- windowHalfWidth indices |
| 127 | // around the extremum (a "bucket-sized window" tolerance). |
| 128 | let ok = false; |
| 129 | for (let s = 0; s < sampledIndices.length; s++) { |
| 130 | const j = sampledIndices[s]!; |
| 131 | const d = j - idx; |
| 132 | if (d === 0 || (d <= windowHalfWidth && d >= -windowHalfWidth)) { |
| 133 | ok = true; |
| 134 | break; |
| 135 | } |
| 136 | } |
| 137 | if (ok) retained++; |
| 138 | } |
| 139 | return retained; |
| 140 | } |
| 141 | |
| 142 | { |
| 143 | const N = 100_000; |