Command line interface¶
Two commands run the analyses on a text file of samples, without writing any Python:
Command |
Runs |
Use it for |
|---|---|---|
|
a signal with an injected tone: SNR, THD, SINAD, THD+N |
|
|
a signal without a known tone: DC, tones and noise |
Both are installed with the package (pip install harm-analysis).
Input file¶
A text file of time-domain samples, by default separated by any whitespace (one
sample per line works). Use --sep for other separators, e.g. --sep "," for
comma-separated values. The examples on this page use
signal.txt:
4096 samples at 1 kHz of a 100.3 Hz tone with a 2nd harmonic at −40 dB, a 3rd at
−50 dB, a DC offset of 0.1 and a little noise.
Basic use¶
Pass the file and the sampling frequency. Frequencies in the results are in the
units of --fs:
harm-analysis signal.txt --fs 1000
Function results:
Parameters
| Parameter | value |
|----------------|---------------------------------------------|
| fund_db | -3.010296203662002 |
| fund_freq | 100.30000108819499 |
| harm_freq | [200.59998447 300.90020213 401.82403781] |
| harm_db | [ -43.01117732 -53.48492695 -109.13948073] |
| dc_db | -20.000290917237553 |
| noise_db | -78.83719484148429 |
| thd_db | -39.62794841486818 |
| snr_db | 75.82689863782228 |
| sinad_db | 39.626906488109654 |
| thdn_db | -39.626906488109654 |
| total_noise_db | -78.8371948414843 |
spec-analysis lists the tones it detects:
spec-analysis signal.txt --fs 1000
Parameters
| Parameter | value |
|-----------|---------------------|
| dc | 0.09999665074761628 |
| dc_db | -20.000290917237553 |
| noise_db | -78.83314579041507 |
Tones
| Tone | Power [dB] | Frequency [Hz] |
|------|---------------------|--------------------|
| T0 | -3.010296203662002 | 100.30000108819499 |
| T1 | -43.011177321238165 | 200.5999844722146 |
| T2 | -53.48492695452096 | 300.90020212952754 |
Machine-readable output: --json¶
--json prints the results as one JSON object, for scripts and AI tools. Values
that are not finite (NaN, or -inf dB for a power of zero) are written as
null:
harm-analysis signal.txt --fs 1000 --json
{
"fund_db": -3.010296203662002,
"fund_freq": 100.30000108819499,
"harm_freq": [
200.5999844722146,
300.90020212952754,
401.8240378088242
],
"harm_db": [
-43.011177321238165,
-53.48492695452096,
-109.13948072763867
],
"dc_db": -20.000290917237553,
"noise_db": -78.83719484148429,
"thd_db": -39.62794841486818,
"snr_db": 75.82689863782228,
"sinad_db": 39.626906488109654,
"thdn_db": -39.626906488109654,
"total_noise_db": -78.8371948414843
}
The tone you injected: --fund-freq¶
If the data may contain a component stronger than your test tone, give its
frequency (see
A stronger tone than the one you injected).
Here interferer.txt
has a 100.3 Hz tone next to an interferer twice as large at 150.7 Hz:
harm-analysis interferer.txt --fs 1000 --fund-freq 100.3 --json
{
"fund_db": -3.01029620365304,
"fund_freq": 100.30000108816988,
"harm_freq": [
200.7408670227082,
300.88750898481493,
401.824017543752
],
"harm_db": [
-107.26795665619738,
-104.04470985146786,
-109.13942725310375
],
"dc_db": -106.91973599592912,
"noise_db": 3.010316924381552,
"thd_db": -98.51686228501677,
"snr_db": -6.020613128034592,
"sinad_db": -6.020613128187361,
"thdn_db": 6.020613128187361,
"total_noise_db": 3.010316924381553
}
Only part of the spectrum: --bw¶
--bw limits the analysis of both commands to the band from DC to --bw, in
the units of --fs; the noise is integrated only up to --bw. Use it for the
signal band of an oversampled converter whose noise is shaped above it, or for
a band such as audio (see
Shaped noise outside your band).
With spec-analysis, tones above --bw are not listed. With --bw 150, the
harmonics of signal.txt at 200.6 Hz and 300.9 Hz are left out:
spec-analysis signal.txt --fs 1000 --bw 150
Parameters
| Parameter | value |
|-----------|---------------------|
| dc | 0.09999665074761628 |
| dc_db | -20.000290917237553 |
| noise_db | -82.21766568634183 |
Tones
| Tone | Power [dB] | Frequency [Hz] |
|------|--------------------|--------------------|
| T0 | -3.010296203662002 | 100.30000108819499 |
With harm-analysis, harmonics above --bw are not analyzed (here the 3rd
harmonic at 300.9 Hz, so THD includes only the 2nd), and total_noise_db still
reports the noise over the whole spectrum, where the 3rd harmonic counts as noise:
harm-analysis signal.txt --fs 1000 --bw 250 --json
{
"fund_db": -3.010296203662002,
"fund_freq": 100.30000108819499,
"harm_freq": [
200.5999844722146
],
"harm_db": [
-43.011177321238165
],
"dc_db": -20.000290917237553,
"noise_db": -81.02499519640281,
"thd_db": -40.000881117576164,
"snr_db": 78.0146989927408,
"sinad_db": 40.0001950479069,
"thdn_db": -40.0001950479069,
"total_noise_db": -53.47226999398494
}
ADC codes and scaling: --sfactor¶
--sfactor multiplies the data before the analysis, for example to turn ADC
codes into volts or full-scale units. It accepts numbers (including 1e5),
+ - * /, parentheses and log2, e.g. 1/32768, 3.3/4096, 1/2048/2. Here
adc_codes.csv
holds comma-separated 16-bit codes of a 10.3 kHz tone at 0.9 of full scale,
sampled at 1 MHz:
harm-analysis adc_codes.csv --fs 1e6 --sep "," --sfactor "1/32768" --json
{
"fund_db": -3.925723432864347,
"fund_freq": 10300.000185869647,
"harm_freq": [
19833.553773979955,
29953.37998359807,
40447.56906842345,
50737.869900671314,
62296.07549686566
],
"harm_db": [
-107.37920014318485,
-109.47061657027915,
-107.65631938198902,
-112.0721431203195,
-108.55041417603266
],
"dc_db": -134.09934905753195,
"noise_db": -83.66525306219793,
"thd_db": -97.81983305292351,
"snr_db": 79.73952962933359,
"sinad_db": 79.67247987851135,
"thdn_db": -79.67247987851135,
"total_noise_db": -83.66525306219793
}
fund_db ≈ −3.93 dB is what a sine of amplitude 0.9 gives in full-scale units
(\(20 \log_{10}(0.9/\sqrt{2})\)).
Plot: --plot¶
--plot opens a window with the data and its power spectrum:
harm-analysis signal.txt --fs 1000 --plot
Reference¶
harm-analysis¶
Runs the harm_analysis function for a file containing time domain data.
Usage
harm-analysis [OPTIONS] FILENAME
Options
- --fs <fs>¶
Sampling frequency.
- --sep <sep>¶
Separator between items.
- --plot¶
Plot the power spectrum of the data
- --sfactor <sfactor>¶
Scaling factor to apply to the data. Supports + - * /, parentheses, 1e5 and log2. Examples: 1/8, 1e-3, log2(256)/2
- --fund-freq <fund_freq>¶
Expected frequency of the fundamental, in the units of –fs. Default: the highest spectral component. Use it if the data may contain a stronger tone.
- --bw <bw>¶
Bandwidth of the analysis, in the units of –fs. Default: fs/2. Only the spectrum from DC to –bw is analyzed.
- --json¶
Print the results as JSON instead of tables. Values that are not finite (NaN, or -inf dB for a power of zero) are written as null.
Arguments
- FILENAME¶
Required argument
spec-analysis¶
Runs the spec_analysis function for a file containing time domain data.
Usage
spec-analysis [OPTIONS] FILENAME
Options
- --fs <fs>¶
Sampling frequency.
- --plot¶
Plot the power spectrum of the data
- --sep <sep>¶
Separator between items.
- --sfactor <sfactor>¶
Scaling factor to apply to the data. Supports + - * /, parentheses, 1e5 and log2. Examples: 1/8, 1e-3, log2(256)/2
- --bw <bw>¶
Bandwidth of the analysis, in the units of –fs. Default: fs/2. Only the spectrum from DC to –bw is analyzed.
- --json¶
Print the results as JSON instead of tables. Values that are not finite (NaN, or -inf dB for a power of zero) are written as null.
Arguments
- FILENAME¶
Required argument