This directory contains validation tests that compare the JavaScript DSP implementation against SciPy as the reference implementation.
generate_scipy_reference.py- Python script that generates reference data using SciPyscipy_reference_data.json- Reference data generated by SciPyscipy_validation_test.js- Node.js test suite that validates the JS implementation against SciPygenerate_signalanalysis_reference.jl- Julia script that generates reference data using SignalAnalysis.jlsignalanalysis_reference_data.json- Reference data generated by SignalAnalysis.jlsignalanalysis_validation_test.js- Node.js test suite that validatesupconvert()/downconvert()against SignalAnalysis.jl
Install Python dependencies:
pip install numpy scipyFirst, generate the reference data using SciPy:
cd tests
python generate_scipy_reference.pyThis creates scipy_reference_data.json containing:
- 6 different test signals (sine waves, chirps, noise, etc.)
- 7 parameter combinations for Welch's method
- 5 parameter combinations for spectrogram
- Expected outputs from SciPy for each combination
Run the Node.js tests to validate the JavaScript implementation:
pnpm test:scipyExpected output:
✓ Loaded SciPy reference data
▶ Welch: sine_100hz - default parameters
✔ Welch: sine_100hz - default parameters
...
✔ 17 tests passing
Tests various signals and parameter combinations:
- Pure sine waves (100 Hz)
- Two-tone signals (100 Hz + 250 Hz)
- Chirp signals (frequency sweeps)
- White Gaussian noise
- Signals with DC offset
Parameters tested:
- Default parameters
- No overlap (noverlap=0)
- High overlap (noverlap=192/256)
- Spectrum vs density scaling
- With/without detrending
- Different segment sizes (nperseg=256, 512)
- Zero-padding (nfft > nperseg)
Tests time-frequency analysis with:
- PSD mode vs magnitude mode
- Different overlap settings
- Spectrum vs density scaling
- Various signal types
Tests verify that JavaScript output matches SciPy within numerical precision:
- Frequencies: Exact match (within machine epsilon)
- PSD values: Relative error < 1e-6, Absolute error < 1e-10
- Spectrograms: < 1% of points can have larger errors
All current tests achieve much better precision than required:
- Typical relative errors: ~1e-12 to 1e-15
- Maximum absolute errors: ~1e-16 to 1e-17
The JavaScript implementation matches SciPy's behavior for:
- Window function: Uses periodic Hann window (like SciPy's default for spectral estimation)
- Normalization:
- Density scaling: Divides by
fs * sum(window^2) - Spectrum scaling: Divides by
(sum(window))^2 - Magnitude mode: Divides by
sqrt(sum(window^2) * fs)
- Density scaling: Divides by
- One-sided spectrum: Doubles power for non-DC/non-Nyquist bins
- Detrending: Removes mean from each segment
If you modify the DSP implementation or want to test with different parameters:
- Edit
generate_scipy_reference.pyto add new test cases - Run
python generate_scipy_reference.pyto regenerate reference data - Update
scipy_validation_test.jsto add corresponding test cases - Run the validation tests
SciPy is the de facto standard for scientific computing in Python and is:
- Widely used and battle-tested
- Well-documented with clear API specifications
- Based on peer-reviewed algorithms (Welch 1967, etc.)
- Maintained by the scientific computing community
By matching SciPy's output, we ensure our JavaScript implementation is correct and can be trusted for scientific applications.
signalanalysis_validation_test.js checks upconvert() and downconvert() against reference
data generated by SignalAnalysis.jl, the Julia
library these functions are ported from.
Run the tests:
pnpm test:juliaRegenerate the reference data (requires Julia; it installs SignalAnalysis.jl into a temporary environment on first run):
julia tests/generate_signalanalysis_reference.jlTwo references are emitted per case. SignalAnalysis stores its frame rate as Float32, so its
carrier phase axis is single precision and drifts by up to ~1e-5 over these signals. The *64
fixtures run the same algorithm with a Float64 time axis and are compared strictly (1e-9); the
raw library output is compared loosely (1e-4). Everything else — the rrcosfir pulse shape,
DSP.jl's resample, and the Hilbert transform — is the library's own output.
The stored Julia fixtures use power-of-two passband lengths. The unit suite also checks arbitrary-length downconversion against an independent direct DFT, verifies DC and even-length Nyquist handling, and tests tone round trips at odd, even, and prime lengths. These checks do not require regenerating the Julia fixtures.