.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "api/gallery/special_functions/wavelets/plot_03_daubechies_compression.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_special_functions_wavelets_plot_03_daubechies_compression.py: Daubechies wavelets: vanishing moments and compression ====================================================== Daubechies' 1988 compactly supported orthonormal wavelets have p vanishing moments with a filter of only 2p taps, so polynomial pieces of degree below p produce near-zero detail coefficients. Keeping only the largest coefficients then compresses a smooth signal far better than Haar (p = 1) can. .. GENERATED FROM PYTHON SOURCE LINES 13-18 .. code-block:: Python import matplotlib.pyplot as plt import numpy as np from mathematicskit.special_functions import WaveletDecompositionResult, daubechies_filter, discrete_wavelet_transform, inverse_discrete_wavelet_transform .. GENERATED FROM PYTHON SOURCE LINES 19-21 The db2 filter in closed form ----------------------------- .. GENERATED FROM PYTHON SOURCE LINES 21-26 .. code-block:: Python r3 = np.sqrt(3.0) print("db2 by spectral factorization:", np.round(daubechies_filter(2), 10)) print("(1+-sqrt3, 3+-sqrt3)/(4 sqrt2):", np.round(np.array([1 + r3, 3 + r3, 3 - r3, 1 - r3]) / (4 * np.sqrt(2)), 10)) .. rst-class:: sphx-glr-script-out .. code-block:: none db2 by spectral factorization: [ 0.48296291 0.8365163 0.22414387 -0.12940952] (1+-sqrt3, 3+-sqrt3)/(4 sqrt2): [ 0.48296291 0.8365163 0.22414387 -0.12940952] .. GENERATED FROM PYTHON SOURCE LINES 27-31 The scaling function by the cascade algorithm --------------------------------------------- Iterating phi(x) = sqrt2 sum h[k] phi(2x - k) from a single spike (inverse DWT of one approximation coefficient) converges to phi. .. GENERATED FROM PYTHON SOURCE LINES 31-46 .. code-block:: Python fig, ax = plt.subplots() for p in (1, 2, 4): level = 8 approx = np.zeros(2 * p * 2) approx[0] = 1.0 details = [np.zeros(approx.size * 2**j) for j in range(level)][::-1] phi = inverse_discrete_wavelet_transform(WaveletDecompositionResult(approx, details, f"db{p}")) support = 2 * p - 1 xs = np.arange(phi.size) / 2**level mask = xs <= support ax.plot(xs[mask], phi[mask] * 2 ** (level / 2), label=f"db{p}") ax.set_title("Daubechies scaling functions") ax.legend() .. image-sg:: /api/gallery/special_functions/wavelets/images/sphx_glr_plot_03_daubechies_compression_001.png :alt: Daubechies scaling functions :srcset: /api/gallery/special_functions/wavelets/images/sphx_glr_plot_03_daubechies_compression_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none .. GENERATED FROM PYTHON SOURCE LINES 47-49 Keep the largest 5% of coefficients ----------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 49-67 .. code-block:: Python n = 1024 t = np.linspace(0.0, 1.0, n, endpoint=False) x = np.sin(4 * np.pi * t) * np.exp(-2 * t) + 0.5 * t**2 keep = int(0.05 * n) fig, ax = plt.subplots() ax.plot(t, x, lw=3, alpha=0.3, color="k", label="signal") for wavelet in ("haar", "db2", "db4"): result = discrete_wavelet_transform(x, wavelet, level=6) threshold = np.sort(np.abs(np.concatenate([result.approximation, *result.details])))[-keep] result.details = [np.where(np.abs(d) >= threshold, d, 0.0) for d in result.details] y = inverse_discrete_wavelet_transform(result) error = np.linalg.norm(y - x) / np.linalg.norm(x) print(f"{wavelet:>4}: relative error keeping {keep} of {n} coefficients = {error:.2e}") ax.plot(t, y, label=f"{wavelet} ({error:.1e})") ax.set_title("5% of the wavelet coefficients") ax.legend() .. image-sg:: /api/gallery/special_functions/wavelets/images/sphx_glr_plot_03_daubechies_compression_002.png :alt: 5% of the wavelet coefficients :srcset: /api/gallery/special_functions/wavelets/images/sphx_glr_plot_03_daubechies_compression_002.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none haar: relative error keeping 51 of 1024 coefficients = 4.91e-02 db2: relative error keeping 51 of 1024 coefficients = 7.17e-03 db4: relative error keeping 51 of 1024 coefficients = 1.11e-03 .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.156 seconds) .. _sphx_glr_download_api_gallery_special_functions_wavelets_plot_03_daubechies_compression.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/special_functions/wavelets/plot_03_daubechies_compression.ipynb :alt: Launch JupyterLite :width: 150 px .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_03_daubechies_compression.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_03_daubechies_compression.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_03_daubechies_compression.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_