spectrum_fft
Spectrum of an equally sampled signal (inline samples, a csv, or a synthetic test signal): windowed FFT amplitude spectrum, peak frequencies and amplitudes refined between bins by a least-squares sine fit, power spectral density (one periodogram or Welch's average), total harmonic distortion, SNR, SINAD, effective bits, SFDR and the noise floor. Writes the spectrum, PSD, peaks and harmonics as csv.
- Field
- Measurement and data
- Version
- 0.1.0
- Runs as
- Compiled (Rust)
- Typical run
- 0.2 s
Compiled from source: run sh packs/install.sh once before admitting it.
Known answers it must pass
r0001hand-written samples 2.5 sin(pi n / 2), 16 points at 1 kHz: tone at fs/4 = 250 Hz, amplitude 2.5, RMS 2.5/sqrt 2, mean square A^2/2 = 3.125 (definition of a sampled sine; rel 1e-9)passed
fundamental_Hz250 ± 2.5e-07250lockedunder 0.01 % of the tolerance usedfundamental_amplitude2.5 ± 2.5e-092.5lockedunder 0.01 % of the tolerance usedfundamental_rms1.76777 ± 1.8e-091.76777lockedunder 0.01 % of the tolerance usedpsd_integral3.125 ± 3.1e-093.125lockedunder 0.01 % of the tolerance usedsignal_power3.125 ± 3.1e-093.125lockedunder 0.01 % of the tolerance usedr0002Parseval on hand-written data: the integers 1..16 have mean 8.5 and variance (n^2-1)/12 = 21.25, and the PSD integrates to it (rel 1e-9)passed
mean8.5 ± 8.5e-128.5lockedunder 0.01 % of the tolerance usedvariance21.25 ± 2.1e-1121.25lockedunder 0.01 % of the tolerance usedpsd_integral21.25 ± 2.1e-0821.25lockedunder 0.01 % of the tolerance usedr0003sine of amplitude 3 on bin 200 with offset 1.5, Hann window: amplitude 3, RMS 3/sqrt 2, power 4.5, no distortion (a Hann-windowed tone on a bin occupies exactly 3 bins; rel 1e-9)passed
fundamental_Hz200 ± 2e-07200lockedunder 0.01 % of the tolerance usedfundamental_amplitude3 ± 3e-093lockedunder 0.01 % of the tolerance usedfundamental_rms2.12132 ± 2.1e-092.12132lockedunder 0.01 % of the tolerance usedmean1.5 ± 1.5e-091.5lockedunder 0.01 % of the tolerance usedsignal_power4.5 ± 4.5e-094.5lockedunder 0.01 % of the tolerance usedpsd_integral4.5 ± 4.5e-094.5lockedunder 0.01 % of the tolerance usedthd≤ 1e-098.97211e-17lockedinside the ranger0004Parseval with Welch (Hann, 1024-sample segments, half overlap): tones of amplitude 2 and 1 on segment bins give variance (2^2+1^2)/2 = 2.5 = integral of the PSD; (16384-1024)/512+1 = 31 segments (rel 1e-9)passed
psd_integral2.5 ± 2.5e-092.5lockedunder 0.01 % of the tolerance usedvariance2.5 ± 2.5e-092.5lockedunder 0.01 % of the tolerance usedsegment_count= 3131lockedexact matchfundamental_Hz100 ± 1e-07100lockedunder 0.01 % of the tolerance usedpeak2_frequency_Hz300 ± 3e-07300lockedunder 0.01 % of the tolerance usedpeak2_amplitude1 ± 1e-091lockedunder 0.01 % of the tolerance usedr0005two tones between bins (123.4 Hz amplitude 1 phase 30 deg; 310.7 Hz amplitude 0.25; 4096 points at 1 kHz, Hann): both found at their frequencies and amplitudes (rel 1e-6: the other tone leaks into each fit far below that)passed
fundamental_Hz123.4 ± 0.00012123.4lockedunder 0.01 % of the tolerance usedfundamental_amplitude1 ± 1e-061lockedunder 0.01 % of the tolerance usedfundamental_phase_deg30 ± 0.00130locked0.012 % of the tolerance usedpeak2_frequency_Hz310.7 ± 0.00031310.7lockedunder 0.01 % of the tolerance usedpeak2_amplitude0.25 ± 2.5e-070.25lockedunder 0.01 % of the tolerance usedr0006square wave (Fourier series 4/pi sum sin(n w t)/n over odd n), 8192 points per period: fundamental 4/pi, 3rd and 5th harmonics 1/3 and 1/5, no 2nd, THD = sqrt(pi^2/8 - 1) = 48.34 % (rel 2e-6: sampling moves harmonic n by (n^2-1)(pi/8192)^2/6)passed
fundamental_Hz1 ± 1e-061lockedunder 0.01 % of the tolerance usedfundamental_amplitude1.27324 ± 2.5e-061.27324locked1.2 % of the tolerance usedharmonic_3_rel0.333333 ± 6.7e-070.333333locked9.8 % of the tolerance usedharmonic_5_rel0.2 ± 4e-070.2locked29 % of the tolerance usedharmonic_2_rel≤ 1e-090lockedinside the rangethd0.483426 ± 9.7e-070.483426locked6.5 % of the tolerance usedthd_percent48.3426 ± 9.7e-0548.3426locked6.5 % of the tolerance usedrms1 ± 1e-121lockedunder 0.01 % of the tolerance usedr0007ideal 12-bit quantiser on a full-scale sine (1021 cycles in 65536 points): SINAD = 6.02 N + 1.76 dB = 74.008 dB, 12 effective bits (abs 0.1 dB, 0.02 bit: the uniform-error model q^2/12 is an approximation)passed
sinad_dB74.0081 ± 0.174.0305locked22 % of the tolerance usedenob_bits12 ± 0.0212.0037locked19 % of the tolerance usedsnr_dB74.0081 ± 0.274.0315locked12 % of the tolerance usedfundamental_amplitude1 ± 0.00011locked0.86 % of the tolerance usedr0008white Gaussian noise of RMS 0.5 at 1 kHz, 262144 points, Welch 1024: one-sided PSD = sigma^2/(fs/2) = 5e-4 per Hz, integral sigma^2 = 0.25 (statistical: 2 %, about 8 standard errors)passed
noise_floor_psd0.0005 ± 1e-050.000498536locked15 % of the tolerance usedpsd_integral0.25 ± 0.0050.249394locked12 % of the tolerance usedrms0.5 ± 0.0050.499539locked9.2 % of the tolerance usedr0009inline samples, no generator: 64 points at 6.4 kHz of 2 sin(2 pi 4n/64 + 30 deg) + 0.5 sin(2 pi 12n/64), rect window: 400 Hz, amplitude 2, phase 30 deg, 3rd harmonic 0.5/2, THD = 0.25, SINAD = SFDR = 20 log10 4, mean square (2^2 + 0.5^2)/2 = 2.125 (Parseval; samples rounded to 1e-12, rel 1e-9)passed
fundamental_Hz400 ± 4e-07400lockedunder 0.01 % of the tolerance usedfundamental_amplitude2 ± 2e-092lockedunder 0.01 % of the tolerance usedfundamental_rms1.41421 ± 1.4e-091.41421lockedunder 0.01 % of the tolerance usedfundamental_phase_deg30 ± 1e-0630lockedunder 0.01 % of the tolerance usedpeak2_frequency_Hz1200 ± 1.2e-061200lockedunder 0.01 % of the tolerance usedpeak2_amplitude0.5 ± 5e-100.5locked0.016 % of the tolerance usedharmonic_3_rel0.25 ± 2.5e-100.25lockedunder 0.01 % of the tolerance usedharmonic_2_rel≤ 1e-093.46945e-18lockedinside the rangethd0.25 ± 2.5e-100.25lockedunder 0.01 % of the tolerance usedthd_percent25 ± 2.5e-0825lockedunder 0.01 % of the tolerance usedsinad_dB12.0412 ± 1e-0612.0412lockedunder 0.01 % of the tolerance usedsfdr_dB12.0412 ± 1e-0612.0412lockedunder 0.01 % of the tolerance usedsignal_power2 ± 2e-092locked0.013 % of the tolerance usedvariance2.125 ± 2.1e-092.125locked0.014 % of the tolerance usedrms1.45774 ± 1.5e-091.45774lockedunder 0.01 % of the tolerance usedpsd_integral2.125 ± 2.1e-092.125locked0.014 % of the tolerance usedmean-1e-09 to 1e-091.51788e-17lockedinside the ranger0010inline samples, no generator: 64 points at 64 Hz of 0.75 + 1.5 cos(2 pi 5n/64) + 0.3 sin(2 pi 10n/64) + 0.15 sin(2 pi 15n/64), Hann window: 5 Hz, amplitude 1.5, phase 90 deg, harmonics 0.2 and 0.1, THD = sqrt(0.2^2 + 0.1^2) = sqrt 0.05, SFDR = 20 log10 5, mean 0.75, variance (1.5^2 + 0.3^2 + 0.15^2)/2 = 1.18125 = PSD integral, Hann noise bandwidth 1.5 bins (samples rounded to 1e-12, rel 1e-9)passed
fundamental_Hz5 ± 5e-095lockedunder 0.01 % of the tolerance usedfundamental_amplitude1.5 ± 1.5e-091.5lockedunder 0.01 % of the tolerance usedfundamental_phase_deg90 ± 1e-0690lockedunder 0.01 % of the tolerance usedpeak2_frequency_Hz10 ± 1e-0810lockedunder 0.01 % of the tolerance usedpeak2_amplitude0.3 ± 3e-100.3lockedunder 0.01 % of the tolerance usedharmonic_2_rel0.2 ± 2e-100.2lockedunder 0.01 % of the tolerance usedharmonic_3_rel0.1 ± 1e-100.1locked0.021 % of the tolerance usedthd0.223607 ± 2.2e-100.223607lockedunder 0.01 % of the tolerance usedsinad_dB13.0103 ± 1e-0613.0103lockedunder 0.01 % of the tolerance usedsfdr_dB13.9794 ± 1e-0613.9794lockedunder 0.01 % of the tolerance usedmean0.75 ± 7.5e-100.75lockedunder 0.01 % of the tolerance usedvariance1.18125 ± 1.2e-091.18125lockedunder 0.01 % of the tolerance usedsignal_power1.125 ± 1.1e-091.125lockedunder 0.01 % of the tolerance usedpsd_integral1.18125 ± 1.2e-091.18125lockedunder 0.01 % of the tolerance usedenbw_bins1.5 ± 1.5e-091.5lockedunder 0.01 % of the tolerance usedresolution_Hz1 ± 1e-121lockedunder 0.01 % of the tolerance usedr0011inline samples, no generator: +1.5, -1.5 alternating, 16 points at 1 kHz, is 1.5 cos(pi n): a line at the Nyquist frequency fs/2 = 500 Hz of in-phase amplitude 1.5 and RMS 1.5, mean 0, mean square 1.5^2 = 2.25 = PSD integral (definition; rel 1e-9)passed
fundamental_Hz500 ± 5e-07500lockedunder 0.01 % of the tolerance usedfundamental_amplitude1.5 ± 1.5e-091.5lockedunder 0.01 % of the tolerance usedvariance2.25 ± 2.3e-092.25lockedunder 0.01 % of the tolerance usedpsd_integral2.25 ± 2.3e-092.25lockedunder 0.01 % of the tolerance usedsignal_power2.25 ± 2.3e-092.25lockedunder 0.01 % of the tolerance usedmean-1e-12 to 1e-120lockedinside the rangethd≤ 1e-090lockedinside the rangefundamental_rms1.5 ± 1.5e-091.5lockedunder 0.01 % of the tolerance usedr0012white Gaussian noise of RMS 0.5 at 1 kHz, 262144 points, one periodogram: the noise floor (median / ln 2, the median of an exponential distribution) is the one-sided PSD sigma^2/(fs/2) = 5e-4 per Hz (statistical: 3 %, about 6 standard errors of the median of 131072 Hann-correlated bins)passed
noise_floor_psd0.0005 ± 1.5e-050.00049532locked31 % of the tolerance usedpsd_integral0.25 ± 0.0050.249042locked19 % of the tolerance usedsegment_count= 11lockedexact match
Each line is one reading the kernel checked. The bright band is the tolerance; the needle is the value the instrument actually returned when it was admitted. A needle near the centre line used little of its tolerance.
Inputs
| Name | Unit | Default | Meaning |
|---|---|---|---|
samples | [] | Equally spaced samples, oldest first (16 to 1048576). Give exactly one of samples, csv_text/csv_file, synth. | |
csv_text | The samples as csv text: one row per sample, one header line allowed, # comments skipped. | ||
csv_file | The same csv as a file, e.g. "@r0003/trace.csv". | ||
column | 1 | -1 | csv column holding the signal, counted from 0 (-1: the last column). |
time_column | 1 | -1 | csv column holding time in seconds, counted from 0; the sample rate is then read from it and the steps must be equal to 0.1 % (-1: none, use sample_rate_Hz). |
sample_rate_Hz | Hz | 1.0 | Samples per second. |
unit | 1 | Unit of the samples (V, m/s^2, 1). Amplitudes are in it; the PSD is in its square per Hz. | |
window | hann | Window over the record (periodic cosine-sum form). rect only when the record holds a whole number of periods. | |
detrend | mean | mean: subtract the record's mean before the transform. | |
segment_length | 1 | 0 | Welch segment length in samples for the PSD (0: one periodogram of the whole record). Peaks and tone figures always use the whole record. |
overlap | 1 | 0.5 | Share of each Welch segment shared with the next. |
fundamental_Hz | Hz | Optional: the fundamental to measure distortion against (left out: the largest peak). | |
harmonics | 1 | 10 | Highest harmonic order counted as distortion; harmonics above Nyquist are folded back. 0: every harmonic below Nyquist. |
n_peaks | 1 | 5 | Largest number of peaks reported. |
min_peak_ratio | 1 | 0.001 | Peaks below this share of the largest amplitude are not reported. |
synth | {} | A synthetic signal instead of data: n samples of dc + tones (amplitude sin(2 pi f t + phase)) + a square wave (+amplitude for the first half period) + Gaussian noise (seeded), then an ideal mid-tread quantiser of quantise_bits over +-full_scale. |
Readings
| Name | Unit | Meaning |
|---|---|---|
source | samples, csv or synth | |
unit | the unit amplitudes are in | |
sample_count | 1 | |
sample_rate_Hz | Hz | |
record_length_s | s | |
resolution_Hz | Hz | bin width of the whole-record spectrum |
enbw_bins | 1 | equivalent noise bandwidth of the window, in bins |
mean | ||
rms | root mean square after detrending | |
variance | population variance, in the unit squared | |
peak_to_peak | ||
fundamental_Hz | Hz | refined between bins |
fundamental_amplitude | peak amplitude of the fitted sine | |
fundamental_rms | ||
fundamental_phase_deg | deg | phase of A sin(2 pi f t + phase), t = 0 at the first sample |
fundamental_bin | 1 | |
fundamental_bin_offset | 1 | |
peak_count | 1 | |
peak2_frequency_Hz | Hz | the second largest peak, if any |
peak2_amplitude | ||
peaks | the fundamental, then the other peaks by size | |
harmonics_counted | 1 | |
harmonics | first 20 harmonics (all in out/harmonics.csv); relative is amplitude over the fundamental's | |
harmonic_2_rel | 1 | |
harmonic_3_rel | 1 | |
harmonic_5_rel | 1 | |
thd | 1 | sqrt(harmonic power / fundamental power) |
thd_percent | % | |
thd_dB | dB relative to the fundamental | |
snr_dB | fundamental over everything that is not DC, fundamental or a counted harmonic, in dB | |
sinad_dB | fundamental over noise plus harmonics, in dB | |
enob_bits | 1 | (SINAD - 10 log10 1.5) / (20 log10 2) |
sfdr_dB | fundamental over the largest other spectral line, in dB | |
signal_power | mean square held by the fundamental, unit squared | |
harmonic_power | ||
noise_power | ||
psd_method | periodogram or welch | |
segment_length | 1 | |
segment_count | 1 | |
psd_resolution_Hz | Hz | |
psd_integral | the PSD summed over frequency: the mean square, unit squared | |
noise_floor_psd | noise level of the PSD, unit squared per Hz: the median over frequency (tones do not move it), divided by ln 2 for one periodogram or by (1 - 1/(9 K))^3 for a Welch average of K segments, which is what the median of Gaussian noise falls short of its mean by | |
warnings | ||
spectrum.csv | ||
psd.csv | ||
peaks.csv | ||
harmonics.csv |
Try the call
Change a value and the command updates. Nothing runs here; paste it into your terminal or give it to your agent.