@cooljapan/scirs2 0.3.4

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,1155 @@
1
+ /* tslint:disable */
2
+ /* eslint-disable */
3
+
4
+ /**
5
+ * Performance timing utilities for benchmarking WASM operations
6
+ */
7
+ export class PerformanceTimer {
8
+ free(): void;
9
+ [Symbol.dispose](): void;
10
+ /**
11
+ * Get elapsed time in milliseconds
12
+ */
13
+ elapsed(): number;
14
+ /**
15
+ * Log the elapsed time to console
16
+ */
17
+ log_elapsed(): void;
18
+ /**
19
+ * Create a new performance timer with a label
20
+ */
21
+ constructor(label: string);
22
+ }
23
+
24
+ /**
25
+ * A wrapper around ndarray for JavaScript interop
26
+ */
27
+ export class WasmArray {
28
+ free(): void;
29
+ [Symbol.dispose](): void;
30
+ /**
31
+ * Create an array with evenly spaced values (like numpy.arange)
32
+ */
33
+ static arange(start: number, end: number, step: number): WasmArray;
34
+ /**
35
+ * Clone the array
36
+ */
37
+ clone(): WasmArray;
38
+ /**
39
+ * Create an array with a specific shape from flat data
40
+ */
41
+ static from_shape(shape: any, data: any): WasmArray;
42
+ /**
43
+ * Create an array filled with a constant value
44
+ */
45
+ static full(shape: any, value: number): WasmArray;
46
+ /**
47
+ * Get a value at the specified index (flat indexing)
48
+ */
49
+ get(index: number): number;
50
+ /**
51
+ * Check if the array is empty
52
+ */
53
+ is_empty(): boolean;
54
+ /**
55
+ * Get the total number of elements
56
+ */
57
+ len(): number;
58
+ /**
59
+ * Create an evenly spaced array (like numpy.linspace)
60
+ */
61
+ static linspace(start: number, end: number, num: number): WasmArray;
62
+ /**
63
+ * Get the number of dimensions
64
+ */
65
+ ndim(): number;
66
+ /**
67
+ * Create a 1D array from a JavaScript array or typed array
68
+ */
69
+ constructor(data: any);
70
+ /**
71
+ * Create an array of ones with the given shape
72
+ */
73
+ static ones(shape: any): WasmArray;
74
+ /**
75
+ * Reshape the array
76
+ */
77
+ reshape(new_shape: any): WasmArray;
78
+ /**
79
+ * Set a value at the specified index (flat indexing)
80
+ */
81
+ set(index: number, value: number): void;
82
+ /**
83
+ * Get the shape of the array
84
+ */
85
+ shape(): Array<any>;
86
+ /**
87
+ * Convert to a flat JavaScript Float64Array
88
+ */
89
+ to_array(): Float64Array;
90
+ /**
91
+ * Convert to a JavaScript array (nested for multi-dimensional arrays)
92
+ */
93
+ to_nested_array(): any;
94
+ /**
95
+ * Transpose the array (2D only for now)
96
+ */
97
+ transpose(): WasmArray;
98
+ /**
99
+ * Create an array of zeros with the given shape
100
+ */
101
+ static zeros(shape: any): WasmArray;
102
+ }
103
+
104
+ /**
105
+ * Add two arrays element-wise
106
+ */
107
+ export function add(a: WasmArray, b: WasmArray): WasmArray;
108
+
109
+ /**
110
+ * Perform Akima interpolation.
111
+ *
112
+ * Akima interpolation is a piecewise cubic method that is designed to be
113
+ * robust against outliers. Unlike standard cubic splines, Akima interpolation
114
+ * uses a local scheme for computing derivatives, making it less prone to
115
+ * oscillations near outliers or abrupt changes in the data.
116
+ *
117
+ * # Arguments
118
+ *
119
+ * * `x` - Known x-coordinates (must be sorted in strictly ascending order, at least 5 points)
120
+ * * `y` - Known y-coordinates (same length as x)
121
+ * * `x_new` - x-coordinates at which to evaluate the interpolation
122
+ *
123
+ * # Returns
124
+ *
125
+ * A `Vec<f64>` containing the interpolated values at each point in `x_new`.
126
+ *
127
+ * # Errors
128
+ *
129
+ * Returns an error if:
130
+ * - Input arrays are empty or have mismatched lengths
131
+ * - Fewer than 5 data points are provided (Akima requires at least 5)
132
+ * - x values are not sorted in strictly ascending order
133
+ * - Evaluation points are outside the data range
134
+ */
135
+ export function akima(x: Float64Array, y: Float64Array, x_new: Float64Array): Float64Array;
136
+
137
+ /**
138
+ * Find a root of a function using the bisection method.
139
+ *
140
+ * Given an interval [a, b] where the function changes sign, repeatedly halves
141
+ * the interval to converge on the root.
142
+ *
143
+ * # Arguments
144
+ * * `a` - Lower bracket endpoint
145
+ * * `b` - Upper bracket endpoint
146
+ * * `fa` - Function value at a: f(a)
147
+ * * `fb` - Function value at b: f(b)
148
+ * * `tol` - Convergence tolerance on the interval width
149
+ * * `max_iter` - Maximum number of iterations
150
+ *
151
+ * # Returns
152
+ * Estimated root location as f64
153
+ */
154
+ export function bisect_root(a: number, b: number, fa: number, fb: number, tol: number, max_iter: number): number;
155
+
156
+ /**
157
+ * Iterative bisection step: given bracket [a, b] with opposite-sign function values,
158
+ * and the function value at the midpoint, returns the narrowed bracket.
159
+ *
160
+ * # Arguments
161
+ * * `a` - Lower bracket
162
+ * * `b` - Upper bracket
163
+ * * `fa` - f(a)
164
+ * * `fb` - f(b)
165
+ * * `f_mid` - f((a+b)/2)
166
+ *
167
+ * # Returns
168
+ * JsValue with:
169
+ * - `a`, `b`: new bracket bounds
170
+ * - `fa`, `fb`: function values at new bounds
171
+ * - `mid`: current midpoint
172
+ * - `converged`: whether |b - a| < given tol (caller can check)
173
+ */
174
+ export function bisection_step(a: number, b: number, fa: number, fb: number, f_mid: number): any;
175
+
176
+ /**
177
+ * Generate a Blackman window of length `n`.
178
+ *
179
+ * The Blackman window is a three-term cosine series window providing
180
+ * excellent sidelobe suppression at the cost of wider main lobe.
181
+ *
182
+ * # Arguments
183
+ *
184
+ * * `n` - Number of points in the window
185
+ *
186
+ * # Returns
187
+ *
188
+ * A `Vec<f64>` containing the window coefficients.
189
+ */
190
+ export function blackman(n: number): Float64Array;
191
+
192
+ /**
193
+ * Find a root of a function using Brent's method.
194
+ *
195
+ * Given an interval [a, b] where the function changes sign, finds x such that f(x) ~ 0.
196
+ * The caller provides the function values at the endpoints.
197
+ *
198
+ * # Arguments
199
+ * * `a` - Lower bracket endpoint
200
+ * * `b` - Upper bracket endpoint
201
+ * * `fa` - Function value at a: f(a)
202
+ * * `fb` - Function value at b: f(b)
203
+ * * `tol` - Convergence tolerance on the root position
204
+ * * `max_iter` - Maximum number of iterations
205
+ *
206
+ * # Returns
207
+ * Estimated root location as f64
208
+ */
209
+ export function brent_root(a: number, b: number, fa: number, fb: number, tol: number, max_iter: number): number;
210
+
211
+ /**
212
+ * Design a Butterworth digital filter.
213
+ *
214
+ * Returns a JSON object with `b` (numerator) and `a` (denominator) coefficients.
215
+ *
216
+ * # Arguments
217
+ *
218
+ * * `order` - Filter order (positive integer)
219
+ * * `cutoff` - Cutoff frequency, normalized 0..1 where 1 is Nyquist
220
+ * * `btype` - Filter type: "lowpass", "highpass", "bandpass", or "bandstop"
221
+ * * `fs` - Sampling frequency in Hz (cutoff will be normalized by fs/2)
222
+ *
223
+ * # Returns
224
+ *
225
+ * A JsValue containing a JSON object `{ b: number[], a: number[] }`.
226
+ */
227
+ export function butter(order: number, cutoff: number, btype: string, fs: number): any;
228
+
229
+ /**
230
+ * Get system capabilities and features available in this build
231
+ */
232
+ export function capabilities(): any;
233
+
234
+ /**
235
+ * Perform 1D convolution of two arrays.
236
+ *
237
+ * # Arguments
238
+ *
239
+ * * `a` - First input array
240
+ * * `b` - Second input array
241
+ * * `mode` - Output mode: "full", "same", or "valid"
242
+ *
243
+ * # Returns
244
+ *
245
+ * A `Vec<f64>` containing the convolution result.
246
+ */
247
+ export function convolve(a: Float64Array, b: Float64Array, mode: string): Float64Array;
248
+
249
+ /**
250
+ * Compute correlation coefficient between two arrays
251
+ */
252
+ export function corrcoef(x: WasmArray, y: WasmArray): number;
253
+
254
+ /**
255
+ * Perform 1D cross-correlation of two arrays.
256
+ *
257
+ * Cross-correlation is equivalent to convolution with the second array reversed.
258
+ *
259
+ * # Arguments
260
+ *
261
+ * * `a` - First input array
262
+ * * `b` - Second input array
263
+ * * `mode` - Output mode: "full", "same", or "valid"
264
+ *
265
+ * # Returns
266
+ *
267
+ * A `Vec<f64>` containing the correlation result.
268
+ */
269
+ export function correlate(a: Float64Array, b: Float64Array, mode: string): Float64Array;
270
+
271
+ /**
272
+ * Perform natural cubic spline interpolation.
273
+ *
274
+ * Constructs a natural cubic spline (zero second derivative at boundaries)
275
+ * through the given data points and evaluates it at the specified x coordinates.
276
+ * Provides C2 continuity (continuous second derivatives).
277
+ *
278
+ * # Arguments
279
+ *
280
+ * * `x` - Known x-coordinates (must be sorted in ascending order, at least 3 points)
281
+ * * `y` - Known y-coordinates (same length as x)
282
+ * * `x_new` - x-coordinates at which to evaluate the spline
283
+ *
284
+ * # Returns
285
+ *
286
+ * A `Vec<f64>` containing the interpolated values at each point in `x_new`.
287
+ *
288
+ * # Errors
289
+ *
290
+ * Returns an error if:
291
+ * - Input arrays are empty or have mismatched lengths
292
+ * - Fewer than 3 data points are provided
293
+ * - x values are not sorted in ascending order
294
+ * - Evaluation points are outside the data range
295
+ */
296
+ export function cubic_spline(x: Float64Array, y: Float64Array, x_new: Float64Array): Float64Array;
297
+
298
+ /**
299
+ * Compute cumulative product of an array
300
+ */
301
+ export function cumprod(arr: WasmArray): WasmArray;
302
+
303
+ /**
304
+ * Compute cumulative sum of an array
305
+ */
306
+ export function cumsum(arr: WasmArray): WasmArray;
307
+
308
+ /**
309
+ * Compute the cumulative integral of `y` over `x` using the trapezoidal
310
+ * rule.
311
+ *
312
+ * Returns a `Float64Array` of length `n - 1` where `n = y.len()`, with
313
+ * each element being the integral from `x[0]` to `x[i+1]`.
314
+ *
315
+ * # Errors
316
+ * Returns a `JsValue` error on mismatched lengths or fewer than 2 points.
317
+ */
318
+ export function cumulative_trapezoid(y_js: any, x_js: any): Float64Array;
319
+
320
+ /**
321
+ * Compute the determinant of a square matrix
322
+ */
323
+ export function det(arr: WasmArray): number;
324
+
325
+ /**
326
+ * Divide two arrays element-wise
327
+ */
328
+ export function divide(a: WasmArray, b: WasmArray): WasmArray;
329
+
330
+ /**
331
+ * Compute dot product (1D) or matrix multiplication (2D)
332
+ */
333
+ export function dot(a: WasmArray, b: WasmArray): WasmArray;
334
+
335
+ /**
336
+ * Compute the forward Fast Fourier Transform (FFT) of complex input data.
337
+ *
338
+ * # Arguments
339
+ *
340
+ * * `data` - Interleaved real/imaginary pairs: `[re_0, im_0, re_1, im_1, ...]`
341
+ *
342
+ * # Returns
343
+ *
344
+ * Interleaved real/imaginary pairs of the FFT result.
345
+ * The output length equals the input length (same number of interleaved pairs).
346
+ *
347
+ * # Errors
348
+ *
349
+ * Returns a `JsValue` error if:
350
+ * - The input length is not even (not valid interleaved complex data)
351
+ * - The input is empty
352
+ * - The FFT computation fails
353
+ *
354
+ * # Example (JavaScript)
355
+ *
356
+ * ```javascript
357
+ * // Signal: [1+0i, 2+0i, 3+0i, 4+0i] as interleaved
358
+ * const input = new Float64Array([1, 0, 2, 0, 3, 0, 4, 0]);
359
+ * const result = fft(input);
360
+ * // result contains interleaved [re, im, re, im, ...]
361
+ * ```
362
+ */
363
+ export function fft(data: Float64Array): Float64Array;
364
+
365
+ /**
366
+ * Compute the magnitude (absolute value) of complex interleaved data.
367
+ *
368
+ * For each complex value `(re, im)`, computes `sqrt(re^2 + im^2)`.
369
+ *
370
+ * # Arguments
371
+ *
372
+ * * `data` - Interleaved real/imaginary pairs: `[re_0, im_0, re_1, im_1, ...]`
373
+ *
374
+ * # Returns
375
+ *
376
+ * A vector of magnitudes, one per complex value. Length is `data.len() / 2`.
377
+ *
378
+ * # Errors
379
+ *
380
+ * Returns a `JsValue` error if the input length is not even.
381
+ *
382
+ * # Example (JavaScript)
383
+ *
384
+ * ```javascript
385
+ * // Complex values: [3+4i, 0+1i] => magnitudes: [5.0, 1.0]
386
+ * const data = new Float64Array([3, 4, 0, 1]);
387
+ * const mags = fft_magnitude(data);
388
+ * ```
389
+ */
390
+ export function fft_magnitude(data: Float64Array): Float64Array;
391
+
392
+ /**
393
+ * Compute the phase angle of complex interleaved data (in radians).
394
+ *
395
+ * For each complex value `(re, im)`, computes `atan2(im, re)`.
396
+ *
397
+ * # Arguments
398
+ *
399
+ * * `data` - Interleaved real/imaginary pairs: `[re_0, im_0, re_1, im_1, ...]`
400
+ *
401
+ * # Returns
402
+ *
403
+ * A vector of phase angles in radians `[-pi, pi]`, one per complex value.
404
+ * Length is `data.len() / 2`.
405
+ *
406
+ * # Errors
407
+ *
408
+ * Returns a `JsValue` error if the input length is not even.
409
+ *
410
+ * # Example (JavaScript)
411
+ *
412
+ * ```javascript
413
+ * // Complex value: [1+1i] => phase: pi/4 (~0.785)
414
+ * const data = new Float64Array([1, 1]);
415
+ * const phases = fft_phase(data);
416
+ * ```
417
+ */
418
+ export function fft_phase(data: Float64Array): Float64Array;
419
+
420
+ /**
421
+ * Compute the FFT sample frequencies.
422
+ *
423
+ * Returns the frequency bin centers in cycles per unit of the sample spacing,
424
+ * following the same convention as NumPy/SciPy.
425
+ *
426
+ * # Arguments
427
+ *
428
+ * * `n` - Number of samples in the signal (window length).
429
+ * * `d` - Sample spacing (inverse of the sampling rate). Must be positive.
430
+ *
431
+ * # Returns
432
+ *
433
+ * A vector of length `n` containing the sample frequencies.
434
+ * For even `n`: `[0, 1, ..., n/2-1, -n/2, ..., -1] / (d*n)`
435
+ * For odd `n`: `[0, 1, ..., (n-1)/2, -(n-1)/2, ..., -1] / (d*n)`
436
+ *
437
+ * # Errors
438
+ *
439
+ * Returns a `JsValue` error if:
440
+ * - `n` is 0
441
+ * - `d` is not positive
442
+ * - The computation fails
443
+ *
444
+ * # Example (JavaScript)
445
+ *
446
+ * ```javascript
447
+ * const freqs = fftfreq(8, 0.1);
448
+ * // freqs = [0.0, 1.25, 2.5, 3.75, -5.0, -3.75, -2.5, -1.25]
449
+ * ```
450
+ */
451
+ export function fftfreq(n: number, d: number): Float64Array;
452
+
453
+ /**
454
+ * Shift the zero-frequency component to the center of the spectrum.
455
+ *
456
+ * Rearranges the FFT output so that the zero-frequency component is at
457
+ * the center, with negative frequencies on the left and positive on the right.
458
+ *
459
+ * # Arguments
460
+ *
461
+ * * `data` - FFT output as a flat f64 array (real-valued, e.g. from `power_spectrum`
462
+ * or magnitude array).
463
+ *
464
+ * # Returns
465
+ *
466
+ * The shifted array with zero-frequency at the center.
467
+ *
468
+ * # Errors
469
+ *
470
+ * Returns a `JsValue` error if the input is empty.
471
+ *
472
+ * # Example (JavaScript)
473
+ *
474
+ * ```javascript
475
+ * const data = new Float64Array([0, 1, 2, 3, 4, -4, -3, -2, -1]);
476
+ * const shifted = fftshift(data);
477
+ * // shifted = [-4, -3, -2, -1, 0, 1, 2, 3, 4]
478
+ * ```
479
+ */
480
+ export function fftshift(data: Float64Array): Float64Array;
481
+
482
+ /**
483
+ * Design an FIR (Finite Impulse Response) lowpass filter using the window method.
484
+ *
485
+ * Designs a linear-phase FIR lowpass filter with the specified number of taps
486
+ * and cutoff frequency, using a Hamming window.
487
+ *
488
+ * # Arguments
489
+ *
490
+ * * `n` - Number of filter taps (must be >= 3)
491
+ * * `cutoff` - Cutoff frequency in Hz
492
+ * * `fs` - Sampling frequency in Hz
493
+ *
494
+ * # Returns
495
+ *
496
+ * A `Vec<f64>` containing the FIR filter coefficients.
497
+ */
498
+ export function firwin(n: number, cutoff: number, fs: number): Float64Array;
499
+
500
+ /**
501
+ * Step-wise golden section search: given bracket [a, b] and two interior
502
+ * function evaluations, returns the narrowed bracket and next evaluation points.
503
+ *
504
+ * # Arguments
505
+ * * `a` - Current lower bound
506
+ * * `b` - Current upper bound
507
+ * * `f_x1` - Function value at interior point x1
508
+ * * `f_x2` - Function value at interior point x2
509
+ * * `tol` - Convergence tolerance
510
+ *
511
+ * # Returns
512
+ * JsValue with:
513
+ * - `a`, `b`: new bracket bounds
514
+ * - `x1`, `x2`: new interior evaluation points
515
+ * - `converged`: whether |b - a| < tol
516
+ * - `x_min`: current best estimate of the minimizer
517
+ */
518
+ export function golden_section_step(a: number, b: number, f_x1: number, f_x2: number, tol: number): any;
519
+
520
+ /**
521
+ * Generate a Hamming window of length `n`.
522
+ *
523
+ * The Hamming window is a raised cosine window with non-zero endpoints,
524
+ * providing better sidelobe suppression than the Hann window.
525
+ *
526
+ * # Arguments
527
+ *
528
+ * * `n` - Number of points in the window
529
+ *
530
+ * # Returns
531
+ *
532
+ * A `Vec<f64>` containing the window coefficients.
533
+ */
534
+ export function hamming(n: number): Float64Array;
535
+
536
+ /**
537
+ * Generate a Hanning (Hann) window of length `n`.
538
+ *
539
+ * The Hann window is a raised cosine window with zero values at both endpoints,
540
+ * providing good frequency resolution and moderate sidelobe suppression.
541
+ *
542
+ * # Arguments
543
+ *
544
+ * * `n` - Number of points in the window
545
+ *
546
+ * # Returns
547
+ *
548
+ * A `Vec<f64>` containing the window coefficients.
549
+ */
550
+ export function hanning(n: number): Float64Array;
551
+
552
+ /**
553
+ * Check if WASM SIMD is supported in the current environment
554
+ */
555
+ export function has_simd_support(): boolean;
556
+
557
+ /**
558
+ * Compute the inverse Fast Fourier Transform (IFFT) of complex input data.
559
+ *
560
+ * # Arguments
561
+ *
562
+ * * `data` - Interleaved real/imaginary pairs: `[re_0, im_0, re_1, im_1, ...]`
563
+ *
564
+ * # Returns
565
+ *
566
+ * Interleaved real/imaginary pairs of the IFFT result.
567
+ * The output length equals the input length (same number of interleaved pairs).
568
+ *
569
+ * # Errors
570
+ *
571
+ * Returns a `JsValue` error if:
572
+ * - The input length is not even
573
+ * - The input is empty
574
+ * - The IFFT computation fails
575
+ *
576
+ * # Example (JavaScript)
577
+ *
578
+ * ```javascript
579
+ * const spectrum = new Float64Array([10, 0, -2, 2, -2, 0, -2, -2]);
580
+ * const signal = ifft(spectrum);
581
+ * ```
582
+ */
583
+ export function ifft(data: Float64Array): Float64Array;
584
+
585
+ /**
586
+ * Inverse of `fftshift`: shift zero-frequency component back to the beginning.
587
+ *
588
+ * This undoes the effect of `fftshift`.
589
+ *
590
+ * # Arguments
591
+ *
592
+ * * `data` - Shifted spectrum (with zero-frequency at center).
593
+ *
594
+ * # Returns
595
+ *
596
+ * The unshifted array with zero-frequency at the beginning.
597
+ *
598
+ * # Errors
599
+ *
600
+ * Returns a `JsValue` error if the input is empty.
601
+ *
602
+ * # Example (JavaScript)
603
+ *
604
+ * ```javascript
605
+ * const shifted = new Float64Array([-4, -3, -2, -1, 0, 1, 2, 3, 4]);
606
+ * const unshifted = ifftshift(shifted);
607
+ * // unshifted = [0, 1, 2, 3, 4, -4, -3, -2, -1]
608
+ * ```
609
+ */
610
+ export function ifftshift(data: Float64Array): Float64Array;
611
+
612
+ /**
613
+ * Initialize the WASM module with panic hooks and logging
614
+ */
615
+ export function init(): void;
616
+
617
+ /**
618
+ * Perform 1D interpolation with a specified method.
619
+ *
620
+ * Interpolates between the given (x, y) data points and evaluates the
621
+ * interpolation at the new x coordinates.
622
+ *
623
+ * # Arguments
624
+ *
625
+ * * `x` - Known x-coordinates (must be sorted in ascending order)
626
+ * * `y` - Known y-coordinates (same length as x)
627
+ * * `x_new` - x-coordinates at which to evaluate the interpolation
628
+ * * `kind` - Interpolation method: "linear", "nearest", or "quadratic" (uses cubic/Catmull-Rom)
629
+ *
630
+ * # Returns
631
+ *
632
+ * A `Vec<f64>` containing the interpolated values at each point in `x_new`.
633
+ *
634
+ * # Errors
635
+ *
636
+ * Returns an error if:
637
+ * - Input arrays are empty or have mismatched lengths
638
+ * - x values are not sorted in ascending order
639
+ * - An unknown interpolation kind is specified
640
+ * - Insufficient points for the requested method
641
+ */
642
+ export function interp1d(x: Float64Array, y: Float64Array, x_new: Float64Array, kind: string): Float64Array;
643
+
644
+ /**
645
+ * Compute matrix inverse
646
+ */
647
+ export function inv(arr: WasmArray): WasmArray;
648
+
649
+ /**
650
+ * Compute the inverse FFT for real-valued output (IRFFT).
651
+ *
652
+ * This is the inverse of `rfft`. It takes the positive-frequency half-spectrum
653
+ * and reconstructs the original real-valued signal.
654
+ *
655
+ * # Arguments
656
+ *
657
+ * * `data` - Interleaved real/imaginary pairs of the half-spectrum:
658
+ * `[re_0, im_0, re_1, im_1, ...]`. The length must be even.
659
+ * * `n` - Length of the output real signal. If 0, it is computed as
660
+ * `2 * (num_complex - 1)` where `num_complex` is the number of complex values.
661
+ *
662
+ * # Returns
663
+ *
664
+ * Real-valued output signal as `f64` array.
665
+ *
666
+ * # Errors
667
+ *
668
+ * Returns a `JsValue` error if:
669
+ * - The input length is not even
670
+ * - The input is empty
671
+ * - The IRFFT computation fails
672
+ *
673
+ * # Example (JavaScript)
674
+ *
675
+ * ```javascript
676
+ * const spectrum = rfft(new Float64Array([1, 2, 3, 4]));
677
+ * const signal = irfft(spectrum, 4); // Recover original 4-element signal
678
+ * ```
679
+ */
680
+ export function irfft(data: Float64Array, n: number): Float64Array;
681
+
682
+ /**
683
+ * Perform Lagrange polynomial interpolation.
684
+ *
685
+ * Uses barycentric Lagrange interpolation, which is numerically more stable
686
+ * than the naive Lagrange formula. The order of the polynomial equals
687
+ * the number of data points minus one.
688
+ *
689
+ * # Arguments
690
+ *
691
+ * * `x` - Known x-coordinates (must have distinct values)
692
+ * * `y` - Known y-coordinates (same length as x)
693
+ * * `x_new` - x-coordinates at which to evaluate the polynomial
694
+ *
695
+ * # Returns
696
+ *
697
+ * A `Vec<f64>` containing the interpolated values at each point in `x_new`.
698
+ *
699
+ * # Errors
700
+ *
701
+ * Returns an error if:
702
+ * - Input arrays are empty or have mismatched lengths
703
+ * - Fewer than 2 data points are provided
704
+ * - x values contain duplicates
705
+ */
706
+ export function lagrange(x: Float64Array, y: Float64Array, x_new: Float64Array): Float64Array;
707
+
708
+ /**
709
+ * Apply a digital filter to a signal using direct form II transposed structure.
710
+ *
711
+ * Implements causal filtering with the inherent group delay of the filter.
712
+ * Both IIR and FIR filters are supported.
713
+ *
714
+ * # Arguments
715
+ *
716
+ * * `b` - Numerator (feedforward) coefficients
717
+ * * `a` - Denominator (feedback) coefficients. For FIR filters, use `[1.0]`.
718
+ * * `x` - Input signal
719
+ *
720
+ * # Returns
721
+ *
722
+ * A `Vec<f64>` containing the filtered signal (same length as input).
723
+ */
724
+ export function lfilter(b: Float64Array, a: Float64Array, x: Float64Array): Float64Array;
725
+
726
+ /**
727
+ * Perform simple linear regression: y = slope * x + intercept.
728
+ *
729
+ * # Arguments
730
+ * * `x` - Independent variable values
731
+ * * `y` - Dependent variable values
732
+ *
733
+ * # Returns
734
+ * JsValue containing:
735
+ * - `slope`: regression slope
736
+ * - `intercept`: regression intercept
737
+ * - `r_squared`: coefficient of determination (R^2)
738
+ * - `std_err_slope`: standard error of the slope
739
+ * - `std_err_intercept`: standard error of the intercept
740
+ * - `n`: number of data points
741
+ */
742
+ export function linear_regression(x: Float64Array, y: Float64Array): any;
743
+
744
+ /**
745
+ * Log a message to the browser console
746
+ */
747
+ export function log(message: string): void;
748
+
749
+ /**
750
+ * Find the maximum value
751
+ */
752
+ export function max(arr: WasmArray): number;
753
+
754
+ /**
755
+ * Compute the mean of all elements
756
+ */
757
+ export function mean(arr: WasmArray): number;
758
+
759
+ /**
760
+ * Compute the median of an array
761
+ */
762
+ export function median(arr: WasmArray): number;
763
+
764
+ /**
765
+ * Memory usage information for the WASM module
766
+ */
767
+ export function memory_usage(): any;
768
+
769
+ /**
770
+ * Find the minimum value
771
+ */
772
+ export function min(arr: WasmArray): number;
773
+
774
+ /**
775
+ * Minimize a scalar function over a bracketed interval using golden section search.
776
+ *
777
+ * This is a derivative-free 1D optimization method. Since WASM cannot pass
778
+ * function closures, this method works on a tabulated function: the caller provides
779
+ * sample points and their values, and the algorithm interpolates.
780
+ *
781
+ * # Arguments
782
+ * * `a` - Lower bound of the search interval
783
+ * * `b` - Upper bound of the search interval
784
+ * * `tol` - Convergence tolerance on the interval width
785
+ * * `max_iter` - Maximum number of iterations
786
+ *
787
+ * # Returns
788
+ * A JsValue containing:
789
+ * - `x`: estimated minimizer
790
+ * - `fun`: estimated function value at minimizer (NaN if unknown)
791
+ * - `a`: final bracket lower bound
792
+ * - `b`: final bracket upper bound
793
+ * - `nit`: number of iterations
794
+ * - `success`: whether tolerance was achieved
795
+ */
796
+ export function minimize_golden(a: number, b: number, tol: number, max_iter: number): any;
797
+
798
+ /**
799
+ * Minimize a function using the Nelder-Mead simplex algorithm.
800
+ *
801
+ * Since WASM cannot pass function closures from JS, this performs the Nelder-Mead
802
+ * optimization using pre-evaluated simplex vertices and their function values.
803
+ *
804
+ * # Arguments
805
+ * * `f_values` - Function values at the initial simplex vertices (n+1 values for n dimensions)
806
+ * * `x0` - Flat array of initial simplex vertex coordinates, row-major layout:
807
+ * each vertex has `n` coordinates, so total length is `(n+1) * n`
808
+ * * `tol` - Convergence tolerance (standard deviation of simplex values)
809
+ * * `max_iter` - Maximum number of iterations
810
+ *
811
+ * # Returns
812
+ * A JsValue containing an object with fields:
813
+ * - `x`: best point found (as array)
814
+ * - `fun`: function value at best point
815
+ * - `nit`: number of iterations performed
816
+ * - `success`: whether convergence was achieved
817
+ * - `simplex`: final simplex vertices (flat array)
818
+ * - `simplex_values`: function values at final simplex vertices
819
+ *
820
+ * Note: With pre-evaluated data, this performs a single Nelder-Mead step cycle.
821
+ * For iterative optimization, call repeatedly from JS, re-evaluating the function
822
+ * at the new simplex points each iteration.
823
+ */
824
+ export function minimize_nelder_mead(f_values: Float64Array, x0: Float64Array, tol: number, max_iter: number): any;
825
+
826
+ /**
827
+ * Multiply two arrays element-wise
828
+ */
829
+ export function multiply(a: WasmArray, b: WasmArray): WasmArray;
830
+
831
+ /**
832
+ * Compute the Frobenius norm of a matrix
833
+ */
834
+ export function norm_frobenius(arr: WasmArray): number;
835
+
836
+ /**
837
+ * Solve the ODE dy/dt = -y (exponential decay) using the classic RK4
838
+ * method.
839
+ *
840
+ * Since JavaScript cannot pass function pointers to WASM, this solver uses
841
+ * a built-in right-hand side (exponential decay, dy_i/dt = -y_i for every
842
+ * component).
843
+ *
844
+ * * `y0_js` -- initial state vector (1-D array).
845
+ * * `t_span_js` -- two-element array `[t_start, t_end]`.
846
+ * * `n_steps` -- number of integration steps.
847
+ *
848
+ * Returns a serialised JSON object (via `JsValue`) with fields:
849
+ * * `t` -- array of time values.
850
+ * * `y` -- array of arrays, one per time point (each inner array has the
851
+ * same length as `y0`).
852
+ *
853
+ * # Errors
854
+ * Returns a `JsValue` error when inputs are invalid.
855
+ */
856
+ export function ode_solve(y0_js: any, t_span_js: any, n_steps: number): any;
857
+
858
+ /**
859
+ * Perform PCHIP (Piecewise Cubic Hermite Interpolating Polynomial) interpolation.
860
+ *
861
+ * PCHIP produces a monotonicity-preserving interpolant: if the input data is
862
+ * monotonically increasing (or decreasing) in an interval, the interpolation
863
+ * will be too. The first derivatives are continuous, but the second derivatives
864
+ * may have jumps at the knot points.
865
+ *
866
+ * # Arguments
867
+ *
868
+ * * `x` - Known x-coordinates (must be sorted in ascending order, at least 2 points)
869
+ * * `y` - Known y-coordinates (same length as x)
870
+ * * `x_new` - x-coordinates at which to evaluate the interpolation
871
+ *
872
+ * # Returns
873
+ *
874
+ * A `Vec<f64>` containing the interpolated values at each point in `x_new`.
875
+ *
876
+ * # Errors
877
+ *
878
+ * Returns an error if:
879
+ * - Input arrays are empty or have mismatched lengths
880
+ * - Fewer than 2 data points are provided
881
+ * - x values are not sorted in ascending order
882
+ */
883
+ export function pchip(x: Float64Array, y: Float64Array, x_new: Float64Array): Float64Array;
884
+
885
+ /**
886
+ * Compute a percentile of an array
887
+ */
888
+ export function percentile(arr: WasmArray, q: number): number;
889
+
890
+ /**
891
+ * Evaluate a fitted polynomial at given points.
892
+ *
893
+ * # Arguments
894
+ * * `coeffs` - Polynomial coefficients in ascending power order [c_0, c_1, ..., c_d]
895
+ * * `x` - Points at which to evaluate the polynomial
896
+ *
897
+ * # Returns
898
+ * Evaluated values as `Vec<f64>`
899
+ */
900
+ export function polynomial_eval(coeffs: Float64Array, x: Float64Array): Float64Array;
901
+
902
+ /**
903
+ * Fit a polynomial of given degree to data points using least squares.
904
+ *
905
+ * Finds coefficients c_0, c_1, ..., c_d such that:
906
+ * y ~ c_0 + c_1*x + c_2*x^2 + ... + c_d*x^d
907
+ *
908
+ * # Arguments
909
+ * * `x` - Independent variable values
910
+ * * `y` - Dependent variable values
911
+ * * `degree` - Degree of the polynomial to fit
912
+ *
913
+ * # Returns
914
+ * Coefficients as a `Vec<f64>` in ascending power order: `[c_0, c_1, ..., c_d]`
915
+ */
916
+ export function polynomial_fit(x: Float64Array, y: Float64Array, degree: number): Float64Array;
917
+
918
+ /**
919
+ * Compute the power spectrum (power spectral density) of a real-valued signal.
920
+ *
921
+ * Computes `|FFT(signal)|^2` for each frequency bin. The result represents
922
+ * the distribution of signal power across frequencies.
923
+ *
924
+ * # Arguments
925
+ *
926
+ * * `data` - Real-valued input signal as `f64` array.
927
+ *
928
+ * # Returns
929
+ *
930
+ * A vector of length `n/2 + 1` containing the power at each frequency bin
931
+ * (only positive frequencies, since the input is real-valued).
932
+ *
933
+ * # Errors
934
+ *
935
+ * Returns a `JsValue` error if:
936
+ * - The input is empty
937
+ * - The FFT computation fails
938
+ *
939
+ * # Example (JavaScript)
940
+ *
941
+ * ```javascript
942
+ * const signal = new Float64Array([1, 0, -1, 0, 1, 0, -1, 0]);
943
+ * const psd = power_spectrum(signal);
944
+ * // psd[2] should have the dominant peak (frequency = 2 cycles per 8 samples)
945
+ * ```
946
+ */
947
+ export function power_spectrum(data: Float64Array): Float64Array;
948
+
949
+ /**
950
+ * Generate random numbers from an exponential distribution
951
+ */
952
+ export function random_exponential(shape: any, lambda: number): WasmArray;
953
+
954
+ /**
955
+ * Generate random integers in the range [low, high)
956
+ */
957
+ export function random_integers(shape: any, low: number, high: number): WasmArray;
958
+
959
+ /**
960
+ * Generate random numbers from a normal (Gaussian) distribution
961
+ */
962
+ export function random_normal(shape: any, mean: number, std_dev: number): WasmArray;
963
+
964
+ /**
965
+ * Generate random numbers from a uniform distribution [0, 1)
966
+ */
967
+ export function random_uniform(shape: any): WasmArray;
968
+
969
+ /**
970
+ * Compute the matrix rank
971
+ */
972
+ export function rank(arr: WasmArray, tolerance?: number | null): number;
973
+
974
+ /**
975
+ * Compute the FFT of real-valued input data (RFFT).
976
+ *
977
+ * This is optimized for real-valued signals. The output contains only the
978
+ * positive-frequency half of the spectrum (plus the DC and Nyquist components),
979
+ * since the negative frequencies are redundant for real input.
980
+ *
981
+ * # Arguments
982
+ *
983
+ * * `data` - Real-valued input signal as `f64` array.
984
+ *
985
+ * # Returns
986
+ *
987
+ * Interleaved real/imaginary pairs of the half-spectrum.
988
+ * Output length is `2 * (n/2 + 1)` where `n` is the input length, because
989
+ * the result contains `n/2 + 1` complex values stored as interleaved pairs.
990
+ *
991
+ * # Errors
992
+ *
993
+ * Returns a `JsValue` error if:
994
+ * - The input is empty
995
+ * - The RFFT computation fails
996
+ *
997
+ * # Example (JavaScript)
998
+ *
999
+ * ```javascript
1000
+ * const signal = new Float64Array([1.0, 2.0, 3.0, 4.0]);
1001
+ * const spectrum = rfft(signal);
1002
+ * // spectrum has (4/2 + 1) * 2 = 6 elements: 3 complex values as interleaved pairs
1003
+ * ```
1004
+ */
1005
+ export function rfft(data: Float64Array): Float64Array;
1006
+
1007
+ /**
1008
+ * Compute the FFT sample frequencies for real-valued FFT (RFFT).
1009
+ *
1010
+ * Returns the frequency bin centers for the positive-frequency half of the
1011
+ * spectrum, which is the output of `rfft`.
1012
+ *
1013
+ * # Arguments
1014
+ *
1015
+ * * `n` - Number of samples in the original signal (window length).
1016
+ * * `d` - Sample spacing (inverse of the sampling rate). Must be positive.
1017
+ *
1018
+ * # Returns
1019
+ *
1020
+ * A vector of length `n/2 + 1` containing the sample frequencies for
1021
+ * the positive half of the spectrum.
1022
+ *
1023
+ * # Errors
1024
+ *
1025
+ * Returns a `JsValue` error if:
1026
+ * - `n` is 0
1027
+ * - `d` is not positive
1028
+ * - The computation fails
1029
+ *
1030
+ * # Example (JavaScript)
1031
+ *
1032
+ * ```javascript
1033
+ * const freqs = rfftfreq(8, 0.1);
1034
+ * // freqs = [0.0, 1.25, 2.5, 3.75, 5.0]
1035
+ * ```
1036
+ */
1037
+ export function rfftfreq(n: number, d: number): Float64Array;
1038
+
1039
+ /**
1040
+ * Perform a single explicit Runge-Kutta 4th order (RK4) step.
1041
+ *
1042
+ * Because JavaScript cannot pass function pointers, the caller must supply
1043
+ * the derivative values pre-computed at the current state.
1044
+ *
1045
+ * * `f_vals_js` -- derivative values dy/dt evaluated at (t, y), one per
1046
+ * component.
1047
+ * * `t` -- current time.
1048
+ * * `y_js` -- current state vector.
1049
+ * * `h` -- step size.
1050
+ *
1051
+ * The function approximates the next state assuming a *constant* derivative
1052
+ * (i.e. Euler) combined with the classical RK4 weighting using the provided
1053
+ * derivative. For a full multi-stage RK4 the caller would need to evaluate
1054
+ * the derivative at the intermediate stages; this helper is therefore best
1055
+ * suited for demonstration / simple cases.
1056
+ *
1057
+ * Returns the new state vector `y_{n+1}` as a `Float64Array`.
1058
+ *
1059
+ * # Errors
1060
+ * Returns a `JsValue` error when `f_vals` and `y` have different lengths.
1061
+ */
1062
+ export function rk4_step(f_vals_js: any, _t: number, y_js: any, h: number): Float64Array;
1063
+
1064
+ /**
1065
+ * Compute the integral of tabulated data `y` with uniform spacing `dx`
1066
+ * using Romberg integration (Richardson extrapolation of the trapezoidal
1067
+ * rule).
1068
+ *
1069
+ * The number of data points must be of the form `2^k + 1` for some
1070
+ * non-negative integer `k` (e.g. 2, 3, 5, 9, 17, 33, ...). If the length
1071
+ * does not satisfy this requirement an error is returned.
1072
+ *
1073
+ * # Arguments
1074
+ * * `y_js` -- ordinate values (array of `f64`).
1075
+ * * `dx` -- uniform spacing between consecutive x-values.
1076
+ *
1077
+ * # Errors
1078
+ * Returns a `JsValue` error when the input length is not `2^k + 1` or when
1079
+ * fewer than 2 points are given.
1080
+ */
1081
+ export function romberg(y_js: any, dx: number): number;
1082
+
1083
+ /**
1084
+ * Set the random seed for reproducibility
1085
+ */
1086
+ export function set_random_seed(seed: bigint): string;
1087
+
1088
+ /**
1089
+ * Compute the definite integral of `y` over `x` using Simpson's rule.
1090
+ *
1091
+ * For segments where three consecutive points are available the classic
1092
+ * Simpson 1/3 rule is used. A final trapezoidal panel is added when the
1093
+ * number of intervals is odd.
1094
+ *
1095
+ * # Errors
1096
+ * Returns a `JsValue` error on mismatched lengths or fewer than 2 points.
1097
+ */
1098
+ export function simpson(y_js: any, x_js: any): number;
1099
+
1100
+ /**
1101
+ * Solve a system of linear equations Ax = b
1102
+ */
1103
+ export function solve(a: WasmArray, b: WasmArray): WasmArray;
1104
+
1105
+ /**
1106
+ * Compute the standard deviation of an array
1107
+ */
1108
+ export function std(arr: WasmArray): number;
1109
+
1110
+ /**
1111
+ * Compute the standard deviation with specified degrees of freedom
1112
+ */
1113
+ export function std_with_ddof(arr: WasmArray, ddof: number): number;
1114
+
1115
+ /**
1116
+ * Subtract two arrays element-wise
1117
+ */
1118
+ export function subtract(a: WasmArray, b: WasmArray): WasmArray;
1119
+
1120
+ /**
1121
+ * Sum all elements in the array
1122
+ */
1123
+ export function sum(arr: WasmArray): number;
1124
+
1125
+ /**
1126
+ * Compute the trace of a matrix (sum of diagonal elements)
1127
+ */
1128
+ export function trace(arr: WasmArray): number;
1129
+
1130
+ /**
1131
+ * Compute the definite integral of `y` over `x` using the composite
1132
+ * trapezoidal rule.
1133
+ *
1134
+ * Both `y_js` and `x_js` must be arrays of the same length (>= 2).
1135
+ *
1136
+ * # Errors
1137
+ * Returns a `JsValue` error when the inputs have mismatched lengths or
1138
+ * contain fewer than 2 points.
1139
+ */
1140
+ export function trapezoid(y_js: any, x_js: any): number;
1141
+
1142
+ /**
1143
+ * Compute the variance of an array
1144
+ */
1145
+ export function variance(arr: WasmArray): number;
1146
+
1147
+ /**
1148
+ * Compute the variance with specified degrees of freedom
1149
+ */
1150
+ export function variance_with_ddof(arr: WasmArray, ddof: number): number;
1151
+
1152
+ /**
1153
+ * Get the version of SciRS2-WASM
1154
+ */
1155
+ export function version(): string;