.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "gallery/0007.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_gallery_0007.py: .. _gallery:0007: Signed Spikes, Gaussian Measurements ============================================================= .. contents:: :depth: 2 :local: In this example we have #. A signal :math:`\bx` of length :math:`n=2560` with :math:`k=20` signed spikes. Each spike has a magnitude of 1. The sign for each spike is randomly assigned. The locations of spikes in the signal are also randomly chosen. #. A Gaussian sensing matrix :math:`\Phi` of shape :math:`m \times n = 600 \times 2560` making 600 random measurements in a vector :math:`\bb` given by the sensing equation :math:`\bb = \Phi \bx`. The columns of the sensing matrix are unit normalized. The signal is sparse in standard basis. This is a relatively easy sparse recovery problem and focuses on the compressive sensing process modeled as: .. math:: \bb = \bA \bx = \Phi \bx. See also: * :ref:`api:problems` * :ref:`api:lop` .. GENERATED FROM PYTHON SOURCE LINES 37-48 .. code-block:: default # Configure JAX to work with 64-bit floating point precision. from jax.config import config config.update("jax_enable_x64", True) import jax.numpy as jnp import cr.nimble as crn import cr.sparse.plots as crplot .. GENERATED FROM PYTHON SOURCE LINES 49-53 Setup ------------------------------ We shall construct our test signal, measurements and sensing matrix using our test problems module. .. GENERATED FROM PYTHON SOURCE LINES 53-62 .. code-block:: default from cr.sparse import problems k=20 m=600 n=2560 prob = problems.generate('signed-spikes:dirac:gaussian', k=k, m=m, n=n) fig, ax = problems.plot(prob) .. image-sg:: /gallery/images/sphx_glr_0007_001.png :alt: Signed Spikes, Measurements :srcset: /gallery/images/sphx_glr_0007_001.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 63-64 Let us access the relevant parts of our test problem .. GENERATED FROM PYTHON SOURCE LINES 64-73 .. code-block:: default # The Gaussian sensing matrix operator A = prob.A # The measurements b0 = prob.b # The sparse signal x0 = prob.x .. GENERATED FROM PYTHON SOURCE LINES 74-77 Sparse Recovery using Subspace Pursuit ------------------------------------------- We shall use subspace pursuit to reconstruct the signal. .. GENERATED FROM PYTHON SOURCE LINES 77-80 .. code-block:: default import cr.sparse.pursuit.sp as sp # We will try to estimate a k-sparse representation sol = sp.solve(A, b0, k) .. GENERATED FROM PYTHON SOURCE LINES 81-82 This utility function helps us quickly analyze the quality of reconstruction .. GENERATED FROM PYTHON SOURCE LINES 82-84 .. code-block:: default problems.analyze_solution(prob, sol) .. rst-class:: sphx-glr-script-out .. code-block:: none m: 600, n: 2560 b_norm: original: 4.441 reconstruction: 4.441 SNR: 297.61 dB x_norm: original: 4.472 reconstruction: 4.472 SNR: 298.08 dB Sparsity: original: 20, reconstructed: 20, overlap: 20, ratio: 1.000 Iterations: 2 .. GENERATED FROM PYTHON SOURCE LINES 85-86 The estimated sparse signal .. GENERATED FROM PYTHON SOURCE LINES 86-87 .. code-block:: default x = sol.x .. GENERATED FROM PYTHON SOURCE LINES 88-89 Let us reconstruct the measurements from this signal .. GENERATED FROM PYTHON SOURCE LINES 89-91 .. code-block:: default b = A.times(x) .. GENERATED FROM PYTHON SOURCE LINES 92-93 Let us visualize the original and reconstructed signal .. GENERATED FROM PYTHON SOURCE LINES 93-103 .. code-block:: default def plot_signals(x0, x): ax = crplot.h_plots(2) ax[0].stem(x0, markerfmt='.') ax[0].set_title('Original signal') ax[1].stem(x, markerfmt='.') ax[1].set_title('Reconstructed signal') plot_signals(x0, x) .. image-sg:: /gallery/images/sphx_glr_0007_002.png :alt: Original signal, Reconstructed signal :srcset: /gallery/images/sphx_glr_0007_002.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 104-105 Let us visualize the original and reconstructed measurements .. GENERATED FROM PYTHON SOURCE LINES 105-113 .. code-block:: default def plot_measurments(b0, b): ax = crplot.h_plots(2) ax[0].plot(b0) ax[0].set_title('Original measurements') ax[1].plot(b) ax[1].set_title('Reconstructed measurements') plot_measurments(b0, b) .. image-sg:: /gallery/images/sphx_glr_0007_003.png :alt: Original measurements, Reconstructed measurements :srcset: /gallery/images/sphx_glr_0007_003.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 114-117 Sparse Recovery using Compressive Sampling Matching Pursuit --------------------------------------------------------------- We shall now use compressive sampling matching pursuit to reconstruct the signal. .. GENERATED FROM PYTHON SOURCE LINES 117-122 .. code-block:: default import cr.sparse.pursuit.cosamp as cosamp # We will try to estimate a k-sparse representation sol = cosamp.solve(A, b0, k) problems.analyze_solution(prob, sol) .. rst-class:: sphx-glr-script-out .. code-block:: none m: 600, n: 2560 b_norm: original: 4.441 reconstruction: 4.441 SNR: 293.05 dB x_norm: original: 4.472 reconstruction: 4.472 SNR: 292.62 dB Sparsity: original: 20, reconstructed: 20, overlap: 20, ratio: 1.000 Iterations: 2 .. GENERATED FROM PYTHON SOURCE LINES 123-124 The estimated sparse signal .. GENERATED FROM PYTHON SOURCE LINES 124-125 .. code-block:: default x = sol.x .. GENERATED FROM PYTHON SOURCE LINES 126-127 Let us reconstruct the measurements from this signal .. GENERATED FROM PYTHON SOURCE LINES 127-130 .. code-block:: default b = A.times(x) .. GENERATED FROM PYTHON SOURCE LINES 131-132 Let us visualize the original and reconstructed signals .. GENERATED FROM PYTHON SOURCE LINES 132-135 .. code-block:: default plot_signals(x0, x) .. image-sg:: /gallery/images/sphx_glr_0007_004.png :alt: Original signal, Reconstructed signal :srcset: /gallery/images/sphx_glr_0007_004.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 136-137 Let us visualize the original and reconstructed measurements .. GENERATED FROM PYTHON SOURCE LINES 137-140 .. code-block:: default plot_measurments(b0, b) .. image-sg:: /gallery/images/sphx_glr_0007_005.png :alt: Original measurements, Reconstructed measurements :srcset: /gallery/images/sphx_glr_0007_005.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 141-143 Sparse Recovery using SPGL1 --------------------------------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 143-148 .. code-block:: default import cr.sparse.cvx.spgl1 as crspgl1 options = crspgl1.SPGL1Options() sol = crspgl1.solve_bp_jit(A, b0, options=options) problems.analyze_solution(prob, sol) .. rst-class:: sphx-glr-script-out .. code-block:: none m: 600, n: 2560 b_norm: original: 4.441 reconstruction: 4.441 SNR: 93.60 dB x_norm: original: 4.472 reconstruction: 4.472 SNR: 89.45 dB Sparsity: original: 20, reconstructed: 20, overlap: 20, ratio: 1.000 Iterations: 36 n_times: 39, n_trans: 37 .. GENERATED FROM PYTHON SOURCE LINES 149-150 The estimated sparse signal .. GENERATED FROM PYTHON SOURCE LINES 150-151 .. code-block:: default x = sol.x .. GENERATED FROM PYTHON SOURCE LINES 152-153 Let us reconstruct the measurements from this signal .. GENERATED FROM PYTHON SOURCE LINES 153-156 .. code-block:: default b = A.times(x) .. GENERATED FROM PYTHON SOURCE LINES 157-158 Let us visualize the original and reconstructed signals .. GENERATED FROM PYTHON SOURCE LINES 158-161 .. code-block:: default plot_signals(x0, x) .. image-sg:: /gallery/images/sphx_glr_0007_006.png :alt: Original signal, Reconstructed signal :srcset: /gallery/images/sphx_glr_0007_006.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 162-163 Let us visualize the original and reconstructed measurements .. GENERATED FROM PYTHON SOURCE LINES 163-164 .. code-block:: default plot_measurments(b0, b) .. image-sg:: /gallery/images/sphx_glr_0007_007.png :alt: Original measurements, Reconstructed measurements :srcset: /gallery/images/sphx_glr_0007_007.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 9.743 seconds) .. _sphx_glr_download_gallery_0007.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: 0007.py <0007.py>` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: 0007.ipynb <0007.ipynb>` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_