.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "api/gallery/communications/information/plot_02_hartley_law.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 or to run this example in your browser via JupyterLite. .. rst-class:: sphx-glr-example-title .. _sphx_glr_api_gallery_communications_information_plot_02_hartley_law.py: Hartley's law: information grows with the logarithm of the levels ================================================================= Ralph Hartley (1928) argued that a message selected from :math:`M` distinguishable symbols carries :math:`\log M` units of information, so a channel of bandwidth :math:`B` carrying :math:`2B` symbols per second has a line rate of :math:`2B\log_2 M` bits per second (:func:`~engineeringkit.communications.hartley_capacity`). More levels raise the rate only logarithmically, and noise limits how many levels can be told apart. This example sends multilevel symbols through a noisy channel to show that limit. .. GENERATED FROM PYTHON SOURCE LINES 16-21 .. code-block:: Python import matplotlib.pyplot as plt import numpy as np from engineeringkit.communications import hartley_capacity, symbol_rate .. GENERATED FROM PYTHON SOURCE LINES 22-24 Line rate of a 3 kHz telephone channel -------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 24-30 .. code-block:: Python B = 3000.0 for M in [2, 4, 8, 16, 64]: rate = float(hartley_capacity(B, M)) print(f"M = {M:2d}: {rate:7.0f} bit/s at {float(symbol_rate(rate, M)):.0f} baud") .. rst-class:: sphx-glr-script-out .. code-block:: none M = 2: 6000 bit/s at 6000 baud M = 4: 12000 bit/s at 6000 baud M = 8: 18000 bit/s at 6000 baud M = 16: 24000 bit/s at 6000 baud M = 64: 36000 bit/s at 6000 baud .. GENERATED FROM PYTHON SOURCE LINES 31-36 Noise sets how many levels survive ---------------------------------- Equally spaced levels in [-1, 1] with Gaussian noise of standard deviation 0.05: the symbol error rate jumps once the level spacing approaches the noise. .. GENERATED FROM PYTHON SOURCE LINES 36-55 .. code-block:: Python rng = np.random.default_rng(1928) levels = [2, 4, 8, 16, 32, 64] errors = [] for M in levels: alphabet = np.linspace(-1, 1, M) sent = rng.integers(0, M, 50_000) received = alphabet[sent] + rng.normal(0, 0.05, sent.size) decided = np.argmin(np.abs(received[:, None] - alphabet[None, :]), axis=1) errors.append(np.mean(decided != sent)) fig, axes = plt.subplots(1, 2, figsize=(9, 3.6)) axes[0].plot(levels, [float(hartley_capacity(B, m)) for m in levels], "o-") axes[0].set(xscale="log", xlabel="levels M", ylabel="line rate [bit/s]", title="2B log2 M") axes[1].semilogy(levels, np.maximum(errors, 1e-6), "o-") axes[1].set(xscale="log", xlabel="levels M", ylabel="symbol error rate", title="Noise σ = 0.05") fig.tight_layout() assert errors[0] < 1e-3 < errors[-1] plt.show() .. image-sg:: /api/gallery/communications/information/images/sphx_glr_plot_02_hartley_law_001.png :alt: 2B log2 M, Noise σ = 0.05 :srcset: /api/gallery/communications/information/images/sphx_glr_plot_02_hartley_law_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.482 seconds) .. _sphx_glr_download_api_gallery_communications_information_plot_02_hartley_law.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: lite-badge .. image:: images/jupyterlite_badge_logo.svg :target: ../../../../lite/lab/index.html?path=api/gallery/communications/information/plot_02_hartley_law.ipynb :alt: Launch JupyterLite :width: 150 px .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_02_hartley_law.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_02_hartley_law.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_02_hartley_law.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_