thd
Compute total harmonic distortion (THD and THD+N) from any transient simulation via FFT, using coherent sampling for exact harmonic measurements without requiring a .four directive.
Instructions
Total harmonic distortion (THD and THD+N) of a periodic transient signal via FFT — works on any .tran result without a .four directive in the deck, and on any simulator. Defaults to COHERENT sampling (record trimmed to whole fundamental cycles, rectangular window) so harmonics land exactly on bins and THD is exact; window='hann' is the approximate fallback. Surfaces every condition the number depends on: the fundamental (given vs auto-detected), window kind, cycles analyzed, FFT length, sample rate, and per-harmonic levels. For LTspice's own .four result instead, see simulation_summary's Fourier section.
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| step | No | Step index for .step sweeps | |
| t_end | No | Window end in SPICE notation. | |
| format | No | 'json' or 'text' | |
| job_id | No | Analyze a run of a completed sweep/MC (or single) job instead of a raw_file path; pair with ``run_index``. | |
| signal | Yes | Signal to analyze (e.g. 'V(out)'). | |
| window | No | 'coherent' trims to whole fundamental cycles + rectangular window (exact, no leakage); 'hann' analyzes the full window with a Hann taper (approximate — use when cycles can't be made integer). | coherent |
| t_start | No | Window start (SPICE notation) — skip the startup transient before measuring. | |
| raw_file | No | Path to .raw transient result. Pass this OR ``job_id`` (a job run), not both. | |
| run_index | No | 0-based run to analyze when ``job_id`` is given (default 0). | |
| fundamental | No | Fundamental frequency in SPICE notation (e.g. '1k'). Omit to auto-detect (largest FFT bin); pass it for an exact coherent result. | |
| n_harmonics | No | Harmonics 2..n folded into THD (1..50, default 7). |
Output Schema
| Name | Required | Description | Default |
|---|---|---|---|
| fs_hz | Yes | ||
| n_fft | Yes | ||
| signal | No | ||
| thd_db | Yes | ||
| window | Yes | ||
| thd_pct | Yes | ||
| coherent | Yes | ||
| n_cycles | Yes | ||
| warnings | Yes | ||
| harmonics | Yes | ||
| thd_n_pct | Yes | ||
| thd_ratio | Yes | ||
| thd_n_ratio | Yes | ||
| fundamental_hz | Yes | ||
| n_harmonics_used | Yes | ||
| fundamental_source | Yes |