/** * AC frequency-response sweep runner. * * IMPORTANT — bespoke sine-sweep, NOT SPICE AC analysis: * This module drives the circuit with a real sine waveform at each frequency * and extracts magnitude/phase via a single-bin DFT. It does NOT compute a * small-signal transfer matrix, does NOT linearise the circuit, and does NOT * run an eigenvalue decomposition. Results reflect the actual nonlinear * behaviour of the circuit at the user's configured drive amplitude — * teaching-grade accuracy appropriate for RC filters and resistive dividers, * not SPICE-grade accuracy for BJT amplifiers at small signal. * * WHY cold-start per point (same rationale as run-sweep.ts): * Capacitor and inductor state would carry from one frequency to the next, * introducing spurious transients at each new drive frequency. coldLoad() * zeroes all element state so every point starts from a known initial * condition. * * WHY adaptive step h = min(ANALYSIS_STEP_S, 1/(f × SAMPLES_PER_CYCLE)): * The fixed analysis step (5e-5 s = 20 kHz) would produce fewer than 1 * sample/cycle above 20 kHz and ~20 samples/cycle at 1 kHz. 40 * samples/cycle keeps the single-bin DFT error well under 1 dB across the * full sweep range; below ~500 Hz the fixed step already provides >40 * samples/cycle, so no additional refinement is needed. * * WHY SETTLE_CYCLES = 5 before measuring: * An RC circuit's transient decays as e^(-t/τ). After 5 cycles at the * drive frequency, t = 5/f. For the worst-case RC low-pass (fc = f, so * τ = 1/(2π·f)), e^(-5/(2π·τ · τ^{-1})) = e^(-5) ≈ 0.007 — the * transient is <1% of the drive amplitude. * * IMPORTANT — this derivation only holds for a FIRST-ORDER circuit. A sharp * resonant (high-Q) circuit builds up over an envelope time constant * τ = 2Q/ω₀, so 5 drive cycles at resonance leave the build-up largely * undecayed and the measured peak reads several dB low. We cannot afford to * settle 2Q cycles for every point (Q can be 50+), so instead we DETECT the * non-settled case via the convergence check below and flag those points so * the UI can mark them low-confidence rather than presenting a confident * wrong number. See AcSweepResult.notSettled. * * WHY MEASURE_CYCLES = 6 as two consecutive 3-cycle windows (whole-cycle DFT): * The single-bin DFT accumulates I and Q sums over the measure window. If * the window does not span an integer number of cycles the tails of the * sine contribute a DC-like artefact that biases the amplitude estimate. * Using whole cycles zeroes this error by orthogonality. We split the * measure phase into two consecutive 3-cycle windows (W1, W2): the reported * amplitude/phase come from the combined 6-cycle whole-window DFT (leakage * free, well-averaged), while the per-window amplitudes A(W1) vs A(W2) drive * the settle convergence check — in steady state they are equal; while a * high-Q resonance is still building they differ. Six cycles (vs the old * five) splits evenly into two leakage-free halves at a ~20% per-point cost. * * WHY the settle convergence check (notSettled): * For each output net we compare the amplitude measured over W1 against W2. * If they differ by more than SETTLE_TOL_DB the amplitude is still changing * across the measure window, which means the fixed settle was too short for * this (resonant) point — the magnitude is under-reported. We only evaluate * the check when the output is above SETTLE_FLOOR_FRAC of the reference, so * deep-stopband points (amplitude in the numerical noise floor) do not * produce spurious flags. * * WHY measure relative to the source output net (not the configured amplitude): * A non-zero rSource causes a voltage drop that reduces the actual injected * amplitude below the configured value. Measuring the reference at the * source's "pos" net (after rSource) means all magnitude ratios are relative * to what the circuit actually sees, not the open-circuit drive level. This * keeps the gain flat for a unity-gain buffer at any rSource. * * WHY two run variants: * runAcSweep (synchronous) is the testable core loop. runAcSweepAsync * yields a macrotask between points so the analysis worker's event loop can * process a queued "cancel" message. Without the yield the worker handler * never returns mid-run and cooperative cancellation is a no-op. */ import type { SimCircuit } from "../sim/engine/sim-engine.js"; import { type AcSweepSpec, type AcSweepResult } from "./jobs.js"; import { type AnalysisEngineOptions, type AnalysisRestartHook } from "./engine.js"; export interface AcSweepHooks extends AnalysisEngineOptions, AnalysisRestartHook { onPoint?: (index: number, total: number) => void; shouldCancel?: () => boolean; } /** * Run an AC frequency-response sweep synchronously and return the result. * * Intended for tests and non-worker callers. Inside the analysis worker use * runAcSweepAsync instead — a synchronous run never returns to the worker's * event loop, so cooperative cancellation could not work. * * @param spec - Validated AC sweep specification (call validateJobSpec first). * @param circuit - The circuit to sweep. Mutated via structuredClone per point. * @param hooks - Optional: onPoint(i, total) for progress, shouldCancel() checked after each point. */ export declare function runAcSweep(spec: AcSweepSpec, circuit: SimCircuit, hooks?: AcSweepHooks): AcSweepResult; /** * Async AC sweep runner: identical semantics to runAcSweep, but yields a * macrotask between points so the hosting worker's event loop can process * queued messages — in particular "cancel", which sets the flag behind * shouldCancel(). Without this yield, a cancel posted mid-run would only be * handled after the final point, making cooperative cancellation a no-op. */ export declare function runAcSweepAsync(spec: AcSweepSpec, circuit: SimCircuit, hooks?: AcSweepHooks): Promise;