harm-analysis 1.1.2__tar.gz → 1.2.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.2.0/.DS_Store +0 -0
- {harm_analysis-1.1.2/harm_analysis.egg-info → harm_analysis-1.2.0}/PKG-INFO +1 -1
- {harm_analysis-1.1.2 → harm_analysis-1.2.0}/harm_analysis/_harm_analysis.py +108 -5
- {harm_analysis-1.1.2 → harm_analysis-1.2.0}/harm_analysis/cli.py +11 -4
- {harm_analysis-1.1.2 → harm_analysis-1.2.0/harm_analysis.egg-info}/PKG-INFO +1 -1
- {harm_analysis-1.1.2 → harm_analysis-1.2.0}/harm_analysis.egg-info/SOURCES.txt +2 -0
- harm_analysis-1.2.0/tests/test_data_cli.txt +2048 -0
- {harm_analysis-1.1.2 → harm_analysis-1.2.0}/tests/test_harm_analysis.py +46 -1
- {harm_analysis-1.1.2 → harm_analysis-1.2.0}/.gitignore +0 -0
- {harm_analysis-1.1.2 → harm_analysis-1.2.0}/.readthedocs.yaml +0 -0
- {harm_analysis-1.1.2 → harm_analysis-1.2.0}/README.rst +0 -0
- {harm_analysis-1.1.2 → harm_analysis-1.2.0}/docs/Makefile +0 -0
- {harm_analysis-1.1.2 → harm_analysis-1.2.0}/docs/harm_analysis_out_example.png +0 -0
- {harm_analysis-1.1.2 → harm_analysis-1.2.0}/docs/make.bat +0 -0
- {harm_analysis-1.1.2 → harm_analysis-1.2.0}/docs/requirements.txt +0 -0
- {harm_analysis-1.1.2 → harm_analysis-1.2.0}/docs/source/conf.py +0 -0
- {harm_analysis-1.1.2 → harm_analysis-1.2.0}/docs/source/harm_analysis.rst +0 -0
- {harm_analysis-1.1.2 → harm_analysis-1.2.0}/docs/source/index.rst +0 -0
- {harm_analysis-1.1.2 → harm_analysis-1.2.0}/docs/source/tutorial/examples/run_harm_analysis.py +0 -0
- {harm_analysis-1.1.2 → harm_analysis-1.2.0}/docs/source/tutorial/examples/run_shaped_noise_example.py +0 -0
- {harm_analysis-1.1.2 → harm_analysis-1.2.0}/harm_analysis/__init__.py +0 -0
- {harm_analysis-1.1.2 → harm_analysis-1.2.0}/harm_analysis.egg-info/dependency_links.txt +0 -0
- {harm_analysis-1.1.2 → harm_analysis-1.2.0}/harm_analysis.egg-info/entry_points.txt +0 -0
- {harm_analysis-1.1.2 → harm_analysis-1.2.0}/harm_analysis.egg-info/requires.txt +0 -0
- {harm_analysis-1.1.2 → harm_analysis-1.2.0}/harm_analysis.egg-info/top_level.txt +0 -0
- {harm_analysis-1.1.2 → harm_analysis-1.2.0}/pyproject.toml +0 -0
- {harm_analysis-1.1.2 → harm_analysis-1.2.0}/requirents.txt +0 -0
- {harm_analysis-1.1.2 → harm_analysis-1.2.0}/setup.cfg +0 -0
- {harm_analysis-1.1.2/tests → harm_analysis-1.2.0}/test_data_cli.txt +0 -0
- {harm_analysis-1.1.2 → harm_analysis-1.2.0}/tests/__init__.py +0 -0
- {harm_analysis-1.1.2 → harm_analysis-1.2.0}/tests/test_cli.py +0 -0
- {harm_analysis-1.1.2 → harm_analysis-1.2.0}/tests/test_harm_analysis_cli.py +0 -0
|
Binary file
|
|
@@ -3,7 +3,7 @@ from scipy import signal
|
|
|
3
3
|
import sys
|
|
4
4
|
|
|
5
5
|
|
|
6
|
-
__all__ = ["harm_analysis"]
|
|
6
|
+
__all__ = ["harm_analysis", "dc_measurement"]
|
|
7
7
|
|
|
8
8
|
|
|
9
9
|
def _arg_x_as_expected(value):
|
|
@@ -373,17 +373,21 @@ def _plot(x: np.array,
|
|
|
373
373
|
fund_array = _mask_array(x_db, fund_bins)
|
|
374
374
|
dc_noise_array = _mask_array(x_db, np.concatenate((dc_bins, noise_bins)))
|
|
375
375
|
|
|
376
|
-
|
|
376
|
+
if fund_bins is not None:
|
|
377
|
+
ax.plot(freq_array, fund_array, label="Fundamental")
|
|
378
|
+
|
|
377
379
|
if harm_bins is not None:
|
|
378
380
|
harm_array = _mask_array(x_db, harm_bins)
|
|
379
381
|
ax.plot(freq_array, harm_array, label="Harmonics")
|
|
382
|
+
|
|
380
383
|
ax.plot(freq_array, dc_noise_array, label="DC and Noise", color='black')
|
|
381
384
|
ax.plot(freq_array, int_noise, label="Integrated total noise", color='green')
|
|
382
385
|
|
|
383
386
|
# Marker location
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
+
if fund_bins is not None:
|
|
388
|
+
x_marker = np.average(freq_array[fund_bins], weights=x[fund_bins])
|
|
389
|
+
y_marker = 10 * np.log10(np.sum(x[fund_bins] / enbw_bins))
|
|
390
|
+
ax.text(x_marker, y_marker, f"{np.round(y_marker, 2)} dB")
|
|
387
391
|
|
|
388
392
|
if bw_bins != len(freq_array) - 1:
|
|
389
393
|
ax.axvline(freq_array[bw_bins], color='black', alpha=0.3, label='bw',
|
|
@@ -572,3 +576,102 @@ def harm_analysis(x: np.array,
|
|
|
572
576
|
ax=ax, int_noise=int_noise, enbw_bins=enbw_bins, bw_bins=bw_bins)
|
|
573
577
|
|
|
574
578
|
return results, ax
|
|
579
|
+
|
|
580
|
+
|
|
581
|
+
def dc_measurement(x: np.array,
|
|
582
|
+
FS: float = 1,
|
|
583
|
+
bw: float = None,
|
|
584
|
+
window: np.array = None,
|
|
585
|
+
plot=False,
|
|
586
|
+
ax=None) -> dict:
|
|
587
|
+
'''Calculate SNR, THD, Fundamental power, and Noise power of the input signal x.
|
|
588
|
+
|
|
589
|
+
The total harmonic distortion is determined from the fundamental frequency and the
|
|
590
|
+
first five harmonics using a power spectrum of the same length as the input signal.
|
|
591
|
+
A hann window is applied to the signal, before the power spectrum is obtained.
|
|
592
|
+
|
|
593
|
+
Parameters
|
|
594
|
+
----------
|
|
595
|
+
x : array_like
|
|
596
|
+
Input signal, containing a tone.
|
|
597
|
+
FS : float, optional
|
|
598
|
+
Sampling frequency.
|
|
599
|
+
window : array_like, optional
|
|
600
|
+
Window that will be multiplied with the signal. Default is
|
|
601
|
+
Hann window.
|
|
602
|
+
bw : float, optional
|
|
603
|
+
Bandwidth to use for the calculation of the metrics, in same units as FS.
|
|
604
|
+
Also useful to filter another tone (or noise) with amplitude greater than the
|
|
605
|
+
fundamental and located above a certain frequency (see shaped noise example).
|
|
606
|
+
plot : bool or None, optional
|
|
607
|
+
If True, the power spectrum result is plotted. If specified,
|
|
608
|
+
an `ax` must be provided, and the function returns a dictionary
|
|
609
|
+
with the results and the specified axes (`ax`). If plot is not set,
|
|
610
|
+
only the results are returned.
|
|
611
|
+
ax : plt.Axes or None, optional
|
|
612
|
+
Axes to be used for plotting. Required if plot is set to True.
|
|
613
|
+
|
|
614
|
+
Returns
|
|
615
|
+
-------
|
|
616
|
+
dict
|
|
617
|
+
A dictionary containing the analysis results:
|
|
618
|
+
|
|
619
|
+
* "dc_db": DC power in decibels (dc_db),
|
|
620
|
+
* "noise_db": Noise power in decibels (noise_pow_db),
|
|
621
|
+
|
|
622
|
+
plt.axes
|
|
623
|
+
If plot is set to True, the Axes used for plotting is returned.
|
|
624
|
+
|
|
625
|
+
'''
|
|
626
|
+
|
|
627
|
+
sig_len = len(x)
|
|
628
|
+
# length of the array returned by the np.fft.rfft function
|
|
629
|
+
rfft_len = _rfft_length(sig_len)
|
|
630
|
+
|
|
631
|
+
if window is None:
|
|
632
|
+
window = signal.windows.hann(sig_len, sym=False)
|
|
633
|
+
|
|
634
|
+
# window metrics
|
|
635
|
+
coherent_gain, enbw = _win_metrics(window)
|
|
636
|
+
enbw_bins = enbw * sig_len
|
|
637
|
+
|
|
638
|
+
# Obtain the single-sided power spectrum
|
|
639
|
+
x_fft_pow, f_array = _fft_pow(x=x, win=window, n_fft=sig_len, FS=FS,
|
|
640
|
+
coherent_gain=coherent_gain)
|
|
641
|
+
|
|
642
|
+
# Convert bw to number of bins
|
|
643
|
+
if bw is None:
|
|
644
|
+
bw_bins = rfft_len - 1
|
|
645
|
+
else:
|
|
646
|
+
bw_bins = np.argmin(np.abs(f_array - bw))
|
|
647
|
+
|
|
648
|
+
dc_end = _find_dc_bins(x_fft_pow)
|
|
649
|
+
dc_bins = np.arange(dc_end)
|
|
650
|
+
|
|
651
|
+
# Obtain noise bins, by removing the DC bins from the bin list
|
|
652
|
+
noise_bins = np.setdiff1d(np.arange(len(x_fft_pow)), dc_bins)
|
|
653
|
+
|
|
654
|
+
dc_power = _power_from_bins(x_fft_pow, dc_bins, enbw_bins, bw_bins)
|
|
655
|
+
noise_power = _power_from_bins(x_fft_pow, noise_bins, enbw_bins, bw_bins)
|
|
656
|
+
|
|
657
|
+
# total integrated noise curve
|
|
658
|
+
int_noise = _int_noise_curve(x=x_fft_pow / enbw_bins, noise_bins=noise_bins)
|
|
659
|
+
|
|
660
|
+
# Calculate THD, Signal Power and N metrics in dB
|
|
661
|
+
dc_db = 10*np.log10(dc_power)
|
|
662
|
+
dc = 10**(dc_db/20)
|
|
663
|
+
noise_pow_db = 10*np.log10(noise_power)
|
|
664
|
+
|
|
665
|
+
results = {'dc': dc,
|
|
666
|
+
'dc_db': dc_db,
|
|
667
|
+
'noise_db': noise_pow_db,
|
|
668
|
+
}
|
|
669
|
+
|
|
670
|
+
if plot is False:
|
|
671
|
+
return results
|
|
672
|
+
else:
|
|
673
|
+
ax = _plot(x=x_fft_pow, freq_array=f_array, dc_bins=dc_bins,
|
|
674
|
+
fund_bins=None, harm_bins=None, noise_bins=noise_bins,
|
|
675
|
+
ax=ax, int_noise=int_noise, enbw_bins=enbw_bins, bw_bins=bw_bins)
|
|
676
|
+
|
|
677
|
+
return results, ax
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import click
|
|
2
2
|
import numpy as np
|
|
3
3
|
import matplotlib.pyplot as plt
|
|
4
|
-
from harm_analysis import harm_analysis
|
|
4
|
+
from harm_analysis import harm_analysis, dc_measurement
|
|
5
5
|
from matplotlib.ticker import EngFormatter
|
|
6
6
|
|
|
7
7
|
|
|
@@ -9,9 +9,10 @@ from matplotlib.ticker import EngFormatter
|
|
|
9
9
|
@click.argument("filename", type=click.Path(exists=True, readable=True))
|
|
10
10
|
@click.option("--fs", default=1.0, help="Sampling frequency.")
|
|
11
11
|
@click.option("--plot", is_flag=True, help="Plot the power spectrum of the data")
|
|
12
|
+
@click.option("--dc", is_flag=True, help="Run only DC measurement")
|
|
12
13
|
@click.option("--sep", default=" ", help='Separator between items.')
|
|
13
14
|
@click.option("--sfactor", default="1", help='Scaling factor. The data will be multiplied by this number, before the function is called. Examples: 1/8, 5, etc')
|
|
14
|
-
def cli(filename, fs, plot, sep, sfactor):
|
|
15
|
+
def cli(filename, fs, plot, sep, sfactor, dc):
|
|
15
16
|
'''Runs the harm_analysis function for a file containing time domain data'''
|
|
16
17
|
|
|
17
18
|
# scaling factor
|
|
@@ -19,9 +20,15 @@ def cli(filename, fs, plot, sep, sfactor):
|
|
|
19
20
|
|
|
20
21
|
if plot is True:
|
|
21
22
|
fig, ax = plt.subplots(1, 2, figsize=(15, 5))
|
|
22
|
-
|
|
23
|
+
if dc:
|
|
24
|
+
results, ax[1] = dc_measurement(file_data, FS=fs, plot=True, ax=ax[1])
|
|
25
|
+
else:
|
|
26
|
+
results, ax[1] = harm_analysis(file_data, FS=fs, plot=True, ax=ax[1])
|
|
23
27
|
else:
|
|
24
|
-
|
|
28
|
+
if dc:
|
|
29
|
+
results = dc_measurement(file_data, FS=fs)
|
|
30
|
+
else:
|
|
31
|
+
results = harm_analysis(file_data, FS=fs, plot=False)
|
|
25
32
|
|
|
26
33
|
print("Function results:")
|
|
27
34
|
for key, value in results.items():
|