.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/foundations/plot_01_constellations.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code. .. rst-class:: sphx-glr-example-title .. _sphx_glr_auto_examples_foundations_plot_01_constellations.py: Constellations and Gray labels ============================== The constellations of ``comnum`` carry the Gray labels of OptiCommPy: point ``i`` of ``c.points`` carries the bits of ``i``, most significant bit first, and two nearest neighbours differ by a single bit. A symbol error to a neighbour therefore costs one bit error, which is what makes the approximation :math:`P_b \approx P_s / \log_2 M` of the bit error rate accurate at high SNR. .. GENERATED FROM PYTHON SOURCE LINES 11-40 .. code-block:: Python import matplotlib.pyplot as plt import numpy as np import comnum from comnum.plot import BACKGROUND, BLUE, MUTED fig, axes = plt.subplots(1, 3, figsize=(9.5, 3.6)) for ax, (kind, m) in zip(axes, [("qam", 16), ("psk", 8), ("pam", 4)], strict=True): c = comnum.Constellation(kind, m).normalized() p = c.points k = c.bits_per_symbol ax.scatter(p.real, p.imag, s=46, color=BLUE, zorder=3, edgecolor=BACKGROUND, linewidth=1.5) for i, z in enumerate(p): ax.annotate( format(i, f"0{k}b"), (z.real, z.imag), textcoords="offset points", xytext=(0, 7), ha="center", fontsize=8, color=MUTED, ) lim = 1.35 * np.abs(p).max() ax.set(xlim=(-lim, lim), ylim=(-lim, lim), aspect="equal", title=f"{m}-{kind.upper()}") ax.set_xlabel("in phase") axes[0].set_ylabel("quadrature") fig.tight_layout() .. image-sg:: /auto_examples/foundations/images/sphx_glr_plot_01_constellations_001.png :alt: 16-QAM, 8-PSK, 4-PAM :srcset: /auto_examples/foundations/images/sphx_glr_plot_01_constellations_001.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 41-42 Neighbours differ by one bit: check it on 64-QAM. .. GENERATED FROM PYTHON SOURCE LINES 42-49 .. code-block:: Python c = comnum.Constellation("qam", 64).normalized() p = c.points d = np.abs(p[:, None] - p[None, :]) dmin = d[d > 0].min() pairs = np.argwhere(np.isclose(d, dmin)) print("nearest-neighbour pairs:", len(pairs) // 2) print("bits that differ:", {bin(i ^ j).count("1") for i, j in pairs}) .. rst-class:: sphx-glr-script-out .. code-block:: none nearest-neighbour pairs: 112 bits that differ: {1} .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.163 seconds) .. _sphx_glr_download_auto_examples_foundations_plot_01_constellations.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_01_constellations.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_01_constellations.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_01_constellations.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_