harm-analysis 1.4.1__tar.gz → 2.1.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {harm_analysis-1.4.1 → harm_analysis-2.1.0}/PKG-INFO +61 -3
- {harm_analysis-1.4.1 → harm_analysis-2.1.0}/README.md +58 -2
- {harm_analysis-1.4.1 → harm_analysis-2.1.0}/pyproject.toml +16 -14
- {harm_analysis-1.4.1 → harm_analysis-2.1.0}/src/harm_analysis/__init__.py +6 -2
- harm_analysis-2.1.0/src/harm_analysis/_harm_analysis.py +27 -0
- harm_analysis-2.1.0/src/harm_analysis/_harmonic.py +420 -0
- harm_analysis-2.1.0/src/harm_analysis/_plotting.py +218 -0
- harm_analysis-2.1.0/src/harm_analysis/_results.py +116 -0
- harm_analysis-2.1.0/src/harm_analysis/_spectral.py +322 -0
- harm_analysis-2.1.0/src/harm_analysis/_spectrum.py +372 -0
- harm_analysis-2.1.0/src/harm_analysis/cli.py +254 -0
- harm_analysis-2.1.0/src/harm_analysis/mcp_server.py +167 -0
- harm_analysis-2.1.0/src/harm_analysis/py.typed +0 -0
- {harm_analysis-1.4.1 → harm_analysis-2.1.0}/tests/__init__.py +1 -1
- {harm_analysis-1.4.1 → harm_analysis-2.1.0}/tests/conftest.py +1 -1
- harm_analysis-2.1.0/tests/test_cli.py +282 -0
- harm_analysis-2.1.0/tests/test_docs_cli_examples.py +74 -0
- harm_analysis-2.1.0/tests/test_edge_cases.py +286 -0
- harm_analysis-2.1.0/tests/test_fft_length.py +130 -0
- {harm_analysis-1.4.1 → harm_analysis-2.1.0}/tests/test_harm_analysis.py +100 -34
- harm_analysis-2.1.0/tests/test_mcp_server.py +143 -0
- harm_analysis-2.1.0/tests/test_plotting.py +133 -0
- harm_analysis-2.1.0/tests/test_spectrum_analysis.py +279 -0
- harm_analysis-2.1.0/tests/test_validation.py +103 -0
- harm_analysis-1.4.1/src/harm_analysis/_harm_analysis.py +0 -834
- harm_analysis-1.4.1/src/harm_analysis/cli.py +0 -142
- harm_analysis-1.4.1/tests/test_cli.py +0 -117
- harm_analysis-1.4.1/tests/test_data_cli.txt +0 -2048
- harm_analysis-1.4.1/tests/test_spectrum_analysis.py +0 -125
- {harm_analysis-1.4.1 → harm_analysis-2.1.0}/LICENSE +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.1
|
|
2
2
|
Name: harm-analysis
|
|
3
|
-
Version: 1.
|
|
3
|
+
Version: 2.1.0
|
|
4
4
|
Summary: A Python library to estimate parameters from a signal containing a tone.
|
|
5
5
|
Keywords: DSP,Signal-Processing,THD,SNR
|
|
6
6
|
Author-Email: Eric Macedo <ericsmacedo@gmail.com>
|
|
@@ -20,6 +20,8 @@ Requires-Dist: matplotlib>=3.10.3
|
|
|
20
20
|
Requires-Dist: numpy>=2.2.6
|
|
21
21
|
Requires-Dist: rich>=14.1.0
|
|
22
22
|
Requires-Dist: scipy>=1.15.3
|
|
23
|
+
Provides-Extra: mcp
|
|
24
|
+
Requires-Dist: mcp>=2; extra == "mcp"
|
|
23
25
|
Description-Content-Type: text/markdown
|
|
24
26
|
|
|
25
27
|
|
|
@@ -41,6 +43,12 @@ Description-Content-Type: text/markdown
|
|
|
41
43
|
* [Sources](https://github.com/ericsmacedo/harm-analysis)
|
|
42
44
|
* [Issues](https://github.com/ericsmacedo/harm-analysis/issues)
|
|
43
45
|
|
|
46
|
+
**THD, SNR, SINAD and THD+N of a signal in one call, from its FFT spectrum.**
|
|
47
|
+
|
|
48
|
+
```bash
|
|
49
|
+
pip install harm-analysis
|
|
50
|
+
```
|
|
51
|
+
|
|
44
52
|
## Introduction
|
|
45
53
|
|
|
46
54
|
The harmonic analysis package uses FFT to estimate parameters of an input signal.
|
|
@@ -49,12 +57,62 @@ The package provides two main functions:
|
|
|
49
57
|
- `harm_analysis`: for simulations with an injected tone, returning SNR, THDN, Noise, etc.
|
|
50
58
|
- `spec_analysis`: for cases without an injected tone, that auto-detects DC, tones, and noise from the spectrum.
|
|
51
59
|
|
|
60
|
+
<!-- overview-start -->
|
|
61
|
+

|
|
62
|
+
<!-- overview-end -->
|
|
63
|
+
|
|
64
|
+
## Why harm-analysis?
|
|
65
|
+
|
|
66
|
+
- **One call, all the metrics**: fundamental, harmonics, DC, noise, THD, SNR, SINAD and THD+N.
|
|
67
|
+
- **Works on real data**: no coherent sampling needed; windowing and spectral leakage are handled for you.
|
|
68
|
+
- **No known tone?** `spec_analysis` finds DC, tones and the noise floor by itself.
|
|
69
|
+
- **Plots included**: pass `plot=True` to draw the annotated spectrum.
|
|
70
|
+
- **Command line too**: `harm-analysis` and `spec-analysis` analyze a text file directly.
|
|
71
|
+
|
|
72
|
+
## Example
|
|
73
|
+
|
|
74
|
+
A 1 kHz tone with a -60 dB second harmonic and some noise:
|
|
75
|
+
|
|
76
|
+
```python
|
|
77
|
+
import numpy as np
|
|
78
|
+
from harm_analysis import harm_analysis
|
|
79
|
+
|
|
80
|
+
rng = np.random.default_rng(0)
|
|
81
|
+
fs = 48000
|
|
82
|
+
t = np.arange(4096) / fs
|
|
83
|
+
x = np.sin(2 * np.pi * 1000 * t) + 1e-3 * np.sin(2 * np.pi * 2000 * t) + 1e-4 * rng.standard_normal(t.size)
|
|
84
|
+
|
|
85
|
+
r = harm_analysis(x, fs=fs)
|
|
86
|
+
for name, value in r.to_dict().items():
|
|
87
|
+
print(f"{name:>15}: {np.round(value, 2)}")
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
Output:
|
|
91
|
+
|
|
92
|
+
```text
|
|
93
|
+
fund_db: -3.01
|
|
94
|
+
fund_freq: 1000.0
|
|
95
|
+
harm_freq: [1999.97 3022.05 4023.99 4958.22 5971.06]
|
|
96
|
+
harm_db: [ -63. -107.8 -103.57 -105.74 -103.45]
|
|
97
|
+
dc_db: -109.41
|
|
98
|
+
noise_db: -79.32
|
|
99
|
+
thd_db: -59.99
|
|
100
|
+
snr_db: 76.31
|
|
101
|
+
sinad_db: 59.89
|
|
102
|
+
thdn_db: -59.89
|
|
103
|
+
total_noise_db: -79.32
|
|
104
|
+
```
|
|
105
|
+
|
|
106
|
+
Powers are in dB (a unit-amplitude sine is -3 dB); `thd_db`, `snr_db`, `sinad_db`
|
|
107
|
+
and `thdn_db` are relative to the fundamental.
|
|
108
|
+
|
|
52
109
|
See [usage](./docs/usage.md) for examples.
|
|
53
110
|
|
|
111
|
+
AI assistants (Claude, GitHub Copilot, ...) can run the analyses through the included
|
|
112
|
+
MCP server; see [AI assistants](./docs/ai.md).
|
|
54
113
|
|
|
55
|
-
## Installation
|
|
56
114
|
|
|
57
|
-
|
|
115
|
+
## Installation
|
|
58
116
|
|
|
59
117
|
```bash
|
|
60
118
|
pip install harm-analysis
|
|
@@ -17,6 +17,12 @@
|
|
|
17
17
|
* [Sources](https://github.com/ericsmacedo/harm-analysis)
|
|
18
18
|
* [Issues](https://github.com/ericsmacedo/harm-analysis/issues)
|
|
19
19
|
|
|
20
|
+
**THD, SNR, SINAD and THD+N of a signal in one call, from its FFT spectrum.**
|
|
21
|
+
|
|
22
|
+
```bash
|
|
23
|
+
pip install harm-analysis
|
|
24
|
+
```
|
|
25
|
+
|
|
20
26
|
## Introduction
|
|
21
27
|
|
|
22
28
|
The harmonic analysis package uses FFT to estimate parameters of an input signal.
|
|
@@ -25,12 +31,62 @@ The package provides two main functions:
|
|
|
25
31
|
- `harm_analysis`: for simulations with an injected tone, returning SNR, THDN, Noise, etc.
|
|
26
32
|
- `spec_analysis`: for cases without an injected tone, that auto-detects DC, tones, and noise from the spectrum.
|
|
27
33
|
|
|
34
|
+
<!-- overview-start -->
|
|
35
|
+

|
|
36
|
+
<!-- overview-end -->
|
|
37
|
+
|
|
38
|
+
## Why harm-analysis?
|
|
39
|
+
|
|
40
|
+
- **One call, all the metrics**: fundamental, harmonics, DC, noise, THD, SNR, SINAD and THD+N.
|
|
41
|
+
- **Works on real data**: no coherent sampling needed; windowing and spectral leakage are handled for you.
|
|
42
|
+
- **No known tone?** `spec_analysis` finds DC, tones and the noise floor by itself.
|
|
43
|
+
- **Plots included**: pass `plot=True` to draw the annotated spectrum.
|
|
44
|
+
- **Command line too**: `harm-analysis` and `spec-analysis` analyze a text file directly.
|
|
45
|
+
|
|
46
|
+
## Example
|
|
47
|
+
|
|
48
|
+
A 1 kHz tone with a -60 dB second harmonic and some noise:
|
|
49
|
+
|
|
50
|
+
```python
|
|
51
|
+
import numpy as np
|
|
52
|
+
from harm_analysis import harm_analysis
|
|
53
|
+
|
|
54
|
+
rng = np.random.default_rng(0)
|
|
55
|
+
fs = 48000
|
|
56
|
+
t = np.arange(4096) / fs
|
|
57
|
+
x = np.sin(2 * np.pi * 1000 * t) + 1e-3 * np.sin(2 * np.pi * 2000 * t) + 1e-4 * rng.standard_normal(t.size)
|
|
58
|
+
|
|
59
|
+
r = harm_analysis(x, fs=fs)
|
|
60
|
+
for name, value in r.to_dict().items():
|
|
61
|
+
print(f"{name:>15}: {np.round(value, 2)}")
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
Output:
|
|
65
|
+
|
|
66
|
+
```text
|
|
67
|
+
fund_db: -3.01
|
|
68
|
+
fund_freq: 1000.0
|
|
69
|
+
harm_freq: [1999.97 3022.05 4023.99 4958.22 5971.06]
|
|
70
|
+
harm_db: [ -63. -107.8 -103.57 -105.74 -103.45]
|
|
71
|
+
dc_db: -109.41
|
|
72
|
+
noise_db: -79.32
|
|
73
|
+
thd_db: -59.99
|
|
74
|
+
snr_db: 76.31
|
|
75
|
+
sinad_db: 59.89
|
|
76
|
+
thdn_db: -59.89
|
|
77
|
+
total_noise_db: -79.32
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
Powers are in dB (a unit-amplitude sine is -3 dB); `thd_db`, `snr_db`, `sinad_db`
|
|
81
|
+
and `thdn_db` are relative to the fundamental.
|
|
82
|
+
|
|
28
83
|
See [usage](./docs/usage.md) for examples.
|
|
29
84
|
|
|
85
|
+
AI assistants (Claude, GitHub Copilot, ...) can run the analyses through the included
|
|
86
|
+
MCP server; see [AI assistants](./docs/ai.md).
|
|
30
87
|
|
|
31
|
-
## Installation
|
|
32
88
|
|
|
33
|
-
|
|
89
|
+
## Installation
|
|
34
90
|
|
|
35
91
|
```bash
|
|
36
92
|
pip install harm-analysis
|
|
@@ -29,7 +29,7 @@ keywords = [
|
|
|
29
29
|
"THD",
|
|
30
30
|
"SNR",
|
|
31
31
|
]
|
|
32
|
-
version = "1.
|
|
32
|
+
version = "2.1.0"
|
|
33
33
|
|
|
34
34
|
[project.urls]
|
|
35
35
|
Homepage = "https://github.com/ericsmacedo/harm-analysis"
|
|
@@ -39,23 +39,31 @@ Documentation = "https://harm-analysis.readthedocs.io/en/stable/"
|
|
|
39
39
|
[project.scripts]
|
|
40
40
|
harm-analysis = "harm_analysis.cli:harm_analysis_cmd"
|
|
41
41
|
spec-analysis = "harm_analysis.cli:spec_analysis_cmd"
|
|
42
|
+
harm-analysis-mcp = "harm_analysis.mcp_server:main"
|
|
43
|
+
|
|
44
|
+
[project.optional-dependencies]
|
|
45
|
+
mcp = [
|
|
46
|
+
"mcp>=2",
|
|
47
|
+
]
|
|
42
48
|
|
|
43
49
|
[dependency-groups]
|
|
44
50
|
dev = [
|
|
51
|
+
"colorama>=0.4.6",
|
|
45
52
|
"contextlib-chdir>=1.0.2",
|
|
46
53
|
"coveralls>=3.3.1",
|
|
47
54
|
"invoke>=2.2.0",
|
|
48
|
-
"mkdocs-click>=0.8.1",
|
|
49
|
-
"mkdocs-include-markdown-plugin>=6.0.5",
|
|
50
|
-
"mkdocs-literate-nav>=0.6.1",
|
|
51
|
-
"mkdocs-material>=9.5.18",
|
|
52
|
-
"mkdocstrings-python>=1.9.2",
|
|
53
|
-
"mypy>=1.9.0",
|
|
54
55
|
"pre-commit>=4.1.0",
|
|
55
56
|
"pytest-cov>=5.0.0",
|
|
56
57
|
"ruff>=0.11.2",
|
|
58
|
+
"furo>=2024.8.6",
|
|
59
|
+
"myst-parser>=4.0.0",
|
|
60
|
+
"sphinx>=8.1.3",
|
|
61
|
+
"sphinx-click>=6.0.0",
|
|
62
|
+
"sphinx-copybutton>=0.5.2",
|
|
57
63
|
"test2ref>=0.4.2",
|
|
58
64
|
"truststore>=0.10.4",
|
|
65
|
+
"ty>=0.0.99",
|
|
66
|
+
"mcp>=2",
|
|
59
67
|
]
|
|
60
68
|
|
|
61
69
|
[build-system]
|
|
@@ -137,6 +145,7 @@ filterwarnings = [
|
|
|
137
145
|
]
|
|
138
146
|
testpaths = [
|
|
139
147
|
"tests",
|
|
148
|
+
"src",
|
|
140
149
|
]
|
|
141
150
|
|
|
142
151
|
[tool.coverage.run]
|
|
@@ -149,10 +158,3 @@ exclude_lines = [
|
|
|
149
158
|
"pragma: no cover",
|
|
150
159
|
"assert False",
|
|
151
160
|
]
|
|
152
|
-
|
|
153
|
-
[tool.mypy]
|
|
154
|
-
disable_error_code = "misc"
|
|
155
|
-
ignore_missing_imports = true
|
|
156
|
-
overrides = [
|
|
157
|
-
{ module = "tests.*", allow_untyped_defs = true },
|
|
158
|
-
]
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# MIT License
|
|
2
2
|
#
|
|
3
|
-
# Copyright (c) 2025 ericsmacedo
|
|
3
|
+
# Copyright (c) 2025-2026 ericsmacedo
|
|
4
4
|
#
|
|
5
5
|
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
6
|
# of this software and associated documentation files (the "Software"), to deal
|
|
@@ -21,9 +21,13 @@
|
|
|
21
21
|
# SOFTWARE.
|
|
22
22
|
"""Harmonic Analysis."""
|
|
23
23
|
|
|
24
|
-
from .
|
|
24
|
+
from ._harmonic import harm_analysis
|
|
25
|
+
from ._results import HarmResult, SpecResult
|
|
26
|
+
from ._spectral import spec_analysis
|
|
25
27
|
|
|
26
28
|
__all__ = [
|
|
29
|
+
"HarmResult",
|
|
30
|
+
"SpecResult",
|
|
27
31
|
"harm_analysis",
|
|
28
32
|
"spec_analysis",
|
|
29
33
|
]
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
# MIT License
|
|
2
|
+
#
|
|
3
|
+
# Copyright (c) 2025-2026 ericsmacedo
|
|
4
|
+
#
|
|
5
|
+
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
# of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
# in the Software without restriction, including without limitation the rights
|
|
8
|
+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
# copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
# furnished to do so, subject to the following conditions:
|
|
11
|
+
#
|
|
12
|
+
# The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
# copies or substantial portions of the Software.
|
|
14
|
+
#
|
|
15
|
+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
# SOFTWARE.
|
|
22
|
+
"""Harm Analysis core functions."""
|
|
23
|
+
|
|
24
|
+
from ._harmonic import harm_analysis
|
|
25
|
+
from ._spectral import spec_analysis
|
|
26
|
+
|
|
27
|
+
__all__ = ["harm_analysis", "spec_analysis"]
|