Skip to main content

spectral_error_db

Function spectral_error_db 

Source
pub fn spectral_error_db(
    wdf: &[f64],
    reference: &[f64],
    sample_rate: f64,
    max_freq_hz: Option<f64>,
) -> f64
Expand description

Compute maximum spectral magnitude error in dB.

Compares spectra up to max_freq_hz, only at bins where both the reference and WDF signals have significant energy. The significance gate has two components:

  1. Relative gate — bin must be within 80 dB of the reference peak.
  2. Absolute floor — both ref and WDF bins must be above −100 dBFS.

The absolute floor prevents false errors caused by comparing WDF’s pristine numerical noise floor (≈ −316 dB) against ngspice’s broadband numerical noise grass (≈ −95 dB). Without the floor, a single weak reference harmonic at −99 dBFS that the WDF places at −109 dBFS (below the simulator floor) would contribute a ≈ 200 dB “error” even though the real harmonics agree within 1–2 dB. The floor at −100 dBFS sits 5 dB below a typical 8-th harmonic of a well-simulated diode clipper (≈ −80 dBFS) and 5 dB above ngspice’s typical noise grass (≈ −95 dBFS), so genuine circuit harmonics are still scored while simulator noise bins are excluded.