Skip to content

Commit 73f7b7b

Browse files
authored
added harmonic distortion (#49)
1 parent 8c30971 commit 73f7b7b

14 files changed

Lines changed: 70 additions & 20 deletions

docs/code_examples/b_general/plot_e_fixed_point_aritmetics.py

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -191,17 +191,23 @@
191191
# Compute power spectral density
192192
f, psd = cbadc.utilities.compute_power_spectral_density(u_hat[K1:])
193193
signal_index = cbadc.utilities.find_sinusoidal(psd, 50)
194+
harm_index = 2 * signal_index[24]
195+
harmonics_index = []
196+
while harm_index < (size // OSR):
197+
harmonics_index.append(signal_index + harm_index)
198+
harm_index += signal_index[24]
199+
harmonics_index = np.array(harmonics_index).flatten()
194200
noise_index = np.ones(psd.size, dtype=bool)
195201
noise_index[signal_index] = False
196202
noise_index[0:2] = False
197203
noise_index[size // OSR :] = False
198204
res = cbadc.utilities.snr_spectrum_computation_extended(
199-
psd, signal_index, noise_index, fs=1 / T
205+
psd, signal_index, noise_index, harmonics_mask=harmonics_index, fs=1 / T
200206
)
201207
SNR = 10 * np.log10(res["snr"])
202208
ENOB = np.round((SNR - 1.76) / 6.02, 1)
203209
description.append(
204-
f"ENOB={ENOB}, fixed-point precision={bits} bits, #coeff={digital_estimators[index_de].number_of_filter_coefficients()})"
210+
f"ENOB={ENOB}, fixed-point precision={bits} bits, #coeff={digital_estimators[index_de].number_of_filter_coefficients()}, thd={round(res['thd']*100, 1)}%"
205211
)
206212
# Plot the FIR filters
207213
plt.semilogx(f, 10 * np.log10(psd), label=description[-1])
@@ -224,12 +230,18 @@
224230
u_hats.append(np.copy(u_hat))
225231
f_ref, psd_ref = cbadc.utilities.compute_power_spectral_density(u_hat[K1:])
226232
signal_index = cbadc.utilities.find_sinusoidal(psd_ref, 50)
233+
harm_index = 2 * signal_index[24]
234+
harmonics_index = []
235+
while harm_index < (size // OSR):
236+
harmonics_index.append(signal_index + harm_index)
237+
harm_index += signal_index[24]
238+
harmonics_index = np.array(harmonics_index).flatten()
227239
noise_index = np.ones(psd_ref.size, dtype=bool)
228240
noise_index[signal_index] = False
229241
noise_index[0:2] = False
230242
noise_index[size // OSR :] = False
231243
res = cbadc.utilities.snr_spectrum_computation_extended(
232-
psd_ref, signal_index, noise_index, fs=1 / T
244+
psd_ref, signal_index, noise_index, harmonics_mask=harmonics_index, fs=1 / T
233245
)
234246
SNR = 10 * np.log10(res["snr"])
235247
ENOB = np.round((SNR - 1.76) / 6.02, 1)
3.34 KB
Loading
2.9 KB
Loading
1.21 KB
Loading

docs/source/tutorials/b_general/plot_e_fixed_point_aritmetics.ipynb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@
116116
},
117117
"outputs": [],
118118
"source": [
119-
"plt.rcParams[\"figure.figsize\"] = [12, 8]\nplt.figure()\nu_hats = []\ndescription = []\nfor index_de, bits in enumerate(fixed_point_precision):\n # Compute estimates for each estimator\n for index in range(size):\n u_hat[index] = next(digital_estimators[index_de])\n u_hats.append(np.copy(u_hat))\n\n # Compute power spectral density\n f, psd = cbadc.utilities.compute_power_spectral_density(u_hat[K1:])\n signal_index = cbadc.utilities.find_sinusoidal(psd, 50)\n noise_index = np.ones(psd.size, dtype=bool)\n noise_index[signal_index] = False\n noise_index[0:2] = False\n noise_index[size // OSR :] = False\n res = cbadc.utilities.snr_spectrum_computation_extended(\n psd, signal_index, noise_index, fs=1 / T\n )\n SNR = 10 * np.log10(res[\"snr\"])\n ENOB = np.round((SNR - 1.76) / 6.02, 1)\n description.append(\n f\"ENOB={ENOB}, fixed-point precision={bits} bits, #coeff={digital_estimators[index_de].number_of_filter_coefficients()})\"\n )\n # Plot the FIR filters\n plt.semilogx(f, 10 * np.log10(psd), label=description[-1])\n\ndigital_estimators_ref = cbadc.digital_estimator.FIRFilter(\n analog_system, digital_control, eta2, K1, K2\n)\n\ndigital_estimators_ref(\n cbadc.utilities.byte_stream_2_control_signal(\n cbadc.utilities.read_byte_stream_from_file(\n \"../a_getting_started/sinusodial_simulation.adcs\", M\n ),\n M,\n )\n)\n\nfor index in range(size):\n u_hat[index] = next(digital_estimators_ref)\nu_hats.append(np.copy(u_hat))\nf_ref, psd_ref = cbadc.utilities.compute_power_spectral_density(u_hat[K1:])\nsignal_index = cbadc.utilities.find_sinusoidal(psd_ref, 50)\nnoise_index = np.ones(psd_ref.size, dtype=bool)\nnoise_index[signal_index] = False\nnoise_index[0:2] = False\nnoise_index[size // OSR :] = False\nres = cbadc.utilities.snr_spectrum_computation_extended(\n psd_ref, signal_index, noise_index, fs=1 / T\n)\nSNR = 10 * np.log10(res[\"snr\"])\nENOB = np.round((SNR - 1.76) / 6.02, 1)\ndescription.append(f\"Ref, ENOB={ENOB}\")\n\nplt.semilogx(f_ref, 10 * np.log10(psd_ref), label=description[-1])\n\nplt.legend()\nplt.xlabel(\"frequency [Hz]\")\nplt.grid(b=True, which=\"major\", color=\"gray\", alpha=0.6, lw=1.5)\nplt.ylabel(\"$ \\mathrm{V}^2 \\, / \\, \\mathrm{Hz}$\")\nplt.xlim((0.0002, 0.5))\n_ = plt.ylim((-150, 40))\n\n# Plot snapshot in time domain\nplt.rcParams[\"figure.figsize\"] = [6.40, 6.40]\nplt.figure()\nplt.title(\"Estimates in time domain\")\nfor index in range(len(fixed_point_precision + 1)):\n t_fir = np.arange(-K1 + 1, size - K2 + 1,)\n plt.plot(t_fir, u_hats[index], label=description[index])\nplt.ylabel(\"$\\hat{u}(t)$\")\nplt.xlim((64000, 64500))\nplt.ylim((-0.6, 0.6))\nplt.xlabel(\"$t / T$\")\n_ = plt.legend()"
119+
"plt.rcParams[\"figure.figsize\"] = [12, 8]\nplt.figure()\nu_hats = []\ndescription = []\nfor index_de, bits in enumerate(fixed_point_precision):\n # Compute estimates for each estimator\n for index in range(size):\n u_hat[index] = next(digital_estimators[index_de])\n u_hats.append(np.copy(u_hat))\n\n # Compute power spectral density\n f, psd = cbadc.utilities.compute_power_spectral_density(u_hat[K1:])\n signal_index = cbadc.utilities.find_sinusoidal(psd, 50)\n harm_index = 2 * signal_index[24]\n harmonics_index = []\n while harm_index < (size // OSR):\n harmonics_index.append(signal_index + harm_index)\n harm_index += signal_index[24]\n harmonics_index = np.array(harmonics_index).flatten()\n noise_index = np.ones(psd.size, dtype=bool)\n noise_index[signal_index] = False\n noise_index[0:2] = False\n noise_index[size // OSR :] = False\n res = cbadc.utilities.snr_spectrum_computation_extended(\n psd, signal_index, noise_index, harmonics_mask=harmonics_index, fs=1 / T\n )\n SNR = 10 * np.log10(res[\"snr\"])\n ENOB = np.round((SNR - 1.76) / 6.02, 1)\n description.append(\n f\"ENOB={ENOB}, fixed-point precision={bits} bits, #coeff={digital_estimators[index_de].number_of_filter_coefficients()}, thd={round(res['thd']*100, 1)}%\"\n )\n # Plot the FIR filters\n plt.semilogx(f, 10 * np.log10(psd), label=description[-1])\n\ndigital_estimators_ref = cbadc.digital_estimator.FIRFilter(\n analog_system, digital_control, eta2, K1, K2\n)\n\ndigital_estimators_ref(\n cbadc.utilities.byte_stream_2_control_signal(\n cbadc.utilities.read_byte_stream_from_file(\n \"../a_getting_started/sinusodial_simulation.adcs\", M\n ),\n M,\n )\n)\n\nfor index in range(size):\n u_hat[index] = next(digital_estimators_ref)\nu_hats.append(np.copy(u_hat))\nf_ref, psd_ref = cbadc.utilities.compute_power_spectral_density(u_hat[K1:])\nsignal_index = cbadc.utilities.find_sinusoidal(psd_ref, 50)\nharm_index = 2 * signal_index[24]\nharmonics_index = []\nwhile harm_index < (size // OSR):\n harmonics_index.append(signal_index + harm_index)\n harm_index += signal_index[24]\nharmonics_index = np.array(harmonics_index).flatten()\nnoise_index = np.ones(psd_ref.size, dtype=bool)\nnoise_index[signal_index] = False\nnoise_index[0:2] = False\nnoise_index[size // OSR :] = False\nres = cbadc.utilities.snr_spectrum_computation_extended(\n psd_ref, signal_index, noise_index, harmonics_mask=harmonics_index, fs=1 / T\n)\nSNR = 10 * np.log10(res[\"snr\"])\nENOB = np.round((SNR - 1.76) / 6.02, 1)\ndescription.append(f\"Ref, ENOB={ENOB}\")\n\nplt.semilogx(f_ref, 10 * np.log10(psd_ref), label=description[-1])\n\nplt.legend()\nplt.xlabel(\"frequency [Hz]\")\nplt.grid(b=True, which=\"major\", color=\"gray\", alpha=0.6, lw=1.5)\nplt.ylabel(\"$ \\mathrm{V}^2 \\, / \\, \\mathrm{Hz}$\")\nplt.xlim((0.0002, 0.5))\n_ = plt.ylim((-150, 40))\n\n# Plot snapshot in time domain\nplt.rcParams[\"figure.figsize\"] = [6.40, 6.40]\nplt.figure()\nplt.title(\"Estimates in time domain\")\nfor index in range(len(fixed_point_precision + 1)):\n t_fir = np.arange(-K1 + 1, size - K2 + 1,)\n plt.plot(t_fir, u_hats[index], label=description[index])\nplt.ylabel(\"$\\hat{u}(t)$\")\nplt.xlim((64000, 64500))\nplt.ylim((-0.6, 0.6))\nplt.xlabel(\"$t / T$\")\n_ = plt.legend()"
120120
]
121121
}
122122
],

docs/source/tutorials/b_general/plot_e_fixed_point_aritmetics.py

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -191,17 +191,23 @@
191191
# Compute power spectral density
192192
f, psd = cbadc.utilities.compute_power_spectral_density(u_hat[K1:])
193193
signal_index = cbadc.utilities.find_sinusoidal(psd, 50)
194+
harm_index = 2 * signal_index[24]
195+
harmonics_index = []
196+
while harm_index < (size // OSR):
197+
harmonics_index.append(signal_index + harm_index)
198+
harm_index += signal_index[24]
199+
harmonics_index = np.array(harmonics_index).flatten()
194200
noise_index = np.ones(psd.size, dtype=bool)
195201
noise_index[signal_index] = False
196202
noise_index[0:2] = False
197203
noise_index[size // OSR :] = False
198204
res = cbadc.utilities.snr_spectrum_computation_extended(
199-
psd, signal_index, noise_index, fs=1 / T
205+
psd, signal_index, noise_index, harmonics_mask=harmonics_index, fs=1 / T
200206
)
201207
SNR = 10 * np.log10(res["snr"])
202208
ENOB = np.round((SNR - 1.76) / 6.02, 1)
203209
description.append(
204-
f"ENOB={ENOB}, fixed-point precision={bits} bits, #coeff={digital_estimators[index_de].number_of_filter_coefficients()})"
210+
f"ENOB={ENOB}, fixed-point precision={bits} bits, #coeff={digital_estimators[index_de].number_of_filter_coefficients()}, thd={round(res['thd']*100, 1)}%"
205211
)
206212
# Plot the FIR filters
207213
plt.semilogx(f, 10 * np.log10(psd), label=description[-1])
@@ -224,12 +230,18 @@
224230
u_hats.append(np.copy(u_hat))
225231
f_ref, psd_ref = cbadc.utilities.compute_power_spectral_density(u_hat[K1:])
226232
signal_index = cbadc.utilities.find_sinusoidal(psd_ref, 50)
233+
harm_index = 2 * signal_index[24]
234+
harmonics_index = []
235+
while harm_index < (size // OSR):
236+
harmonics_index.append(signal_index + harm_index)
237+
harm_index += signal_index[24]
238+
harmonics_index = np.array(harmonics_index).flatten()
227239
noise_index = np.ones(psd_ref.size, dtype=bool)
228240
noise_index[signal_index] = False
229241
noise_index[0:2] = False
230242
noise_index[size // OSR :] = False
231243
res = cbadc.utilities.snr_spectrum_computation_extended(
232-
psd_ref, signal_index, noise_index, fs=1 / T
244+
psd_ref, signal_index, noise_index, harmonics_mask=harmonics_index, fs=1 / T
233245
)
234246
SNR = 10 * np.log10(res["snr"])
235247
ENOB = np.round((SNR - 1.76) / 6.02, 1)
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
cf320c5784ce39d386bdd956a681b9ea
1+
0b0bc79ed1685237c8f2a9bfab558868

docs/source/tutorials/b_general/plot_e_fixed_point_aritmetics.rst

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -396,7 +396,7 @@ Resulting Estimate Precision
396396
----------------------------
397397

398398

399-
.. GENERATED FROM PYTHON SOURCE LINES 180-260
399+
.. GENERATED FROM PYTHON SOURCE LINES 180-272
400400
401401
.. code-block:: default
402402
:lineno-start: 181
@@ -415,17 +415,23 @@ Resulting Estimate Precision
415415
# Compute power spectral density
416416
f, psd = cbadc.utilities.compute_power_spectral_density(u_hat[K1:])
417417
signal_index = cbadc.utilities.find_sinusoidal(psd, 50)
418+
harm_index = 2 * signal_index[24]
419+
harmonics_index = []
420+
while harm_index < (size // OSR):
421+
harmonics_index.append(signal_index + harm_index)
422+
harm_index += signal_index[24]
423+
harmonics_index = np.array(harmonics_index).flatten()
418424
noise_index = np.ones(psd.size, dtype=bool)
419425
noise_index[signal_index] = False
420426
noise_index[0:2] = False
421427
noise_index[size // OSR :] = False
422428
res = cbadc.utilities.snr_spectrum_computation_extended(
423-
psd, signal_index, noise_index, fs=1 / T
429+
psd, signal_index, noise_index, harmonics_mask=harmonics_index, fs=1 / T
424430
)
425431
SNR = 10 * np.log10(res["snr"])
426432
ENOB = np.round((SNR - 1.76) / 6.02, 1)
427433
description.append(
428-
f"ENOB={ENOB}, fixed-point precision={bits} bits, #coeff={digital_estimators[index_de].number_of_filter_coefficients()})"
434+
f"ENOB={ENOB}, fixed-point precision={bits} bits, #coeff={digital_estimators[index_de].number_of_filter_coefficients()}, thd={round(res['thd']*100, 1)}%"
429435
)
430436
# Plot the FIR filters
431437
plt.semilogx(f, 10 * np.log10(psd), label=description[-1])
@@ -448,12 +454,18 @@ Resulting Estimate Precision
448454
u_hats.append(np.copy(u_hat))
449455
f_ref, psd_ref = cbadc.utilities.compute_power_spectral_density(u_hat[K1:])
450456
signal_index = cbadc.utilities.find_sinusoidal(psd_ref, 50)
457+
harm_index = 2 * signal_index[24]
458+
harmonics_index = []
459+
while harm_index < (size // OSR):
460+
harmonics_index.append(signal_index + harm_index)
461+
harm_index += signal_index[24]
462+
harmonics_index = np.array(harmonics_index).flatten()
451463
noise_index = np.ones(psd_ref.size, dtype=bool)
452464
noise_index[signal_index] = False
453465
noise_index[0:2] = False
454466
noise_index[size // OSR :] = False
455467
res = cbadc.utilities.snr_spectrum_computation_extended(
456-
psd_ref, signal_index, noise_index, fs=1 / T
468+
psd_ref, signal_index, noise_index, harmonics_mask=harmonics_index, fs=1 / T
457469
)
458470
SNR = 10 * np.log10(res["snr"])
459471
ENOB = np.round((SNR - 1.76) / 6.02, 1)
@@ -506,7 +518,7 @@ Resulting Estimate Precision
506518

507519
.. rst-class:: sphx-glr-timing
508520

509-
**Total running time of the script:** ( 1 minutes 50.090 seconds)
521+
**Total running time of the script:** ( 1 minutes 39.459 seconds)
510522

511523

512524
.. _sphx_glr_download_tutorials_b_general_plot_e_fixed_point_aritmetics.py:
Binary file not shown.

docs/source/tutorials/b_general/sg_execution_times.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@
55

66
Computation times
77
=================
8-
**01:50.090** total execution time for **tutorials_b_general** files:
8+
**01:39.459** total execution time for **tutorials_b_general** files:
99

1010
+-------------------------------------------------------------------------------------------------------------+-----------+--------+
11-
| :ref:`sphx_glr_tutorials_b_general_plot_e_fixed_point_aritmetics.py` (``plot_e_fixed_point_aritmetics.py``) | 01:50.090 | 0.0 MB |
11+
| :ref:`sphx_glr_tutorials_b_general_plot_e_fixed_point_aritmetics.py` (``plot_e_fixed_point_aritmetics.py``) | 01:39.459 | 0.0 MB |
1212
+-------------------------------------------------------------------------------------------------------------+-----------+--------+
1313
| :ref:`sphx_glr_tutorials_b_general_plot_a_compare_estimator.py` (``plot_a_compare_estimator.py``) | 00:00.000 | 0.0 MB |
1414
+-------------------------------------------------------------------------------------------------------------+-----------+--------+

0 commit comments

Comments
 (0)