.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "gallery/0200_cs/cs1bit_biht.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_0200_cs_cs1bit_biht.py: 1 bit Compressive Sensing ========================== This example demonstrates following features - Making 1-bit quantized compressive measurements of a sparse signal - Recovering the original signal using the BIHT (Binary Iterative Hard Thresholding) algorithm. .. GENERATED FROM PYTHON SOURCE LINES 12-13 Let's import necessary libraries .. GENERATED FROM PYTHON SOURCE LINES 13-29 .. code-block:: default import jax.numpy as jnp from jax.numpy.linalg import norm import matplotlib as mpl import matplotlib.pyplot as plt import cr.nimble as cnb import cr.sparse as crs import cr.sparse.dict as crdict import cr.sparse.data as crdata import cr.sparse.cs.cs1bit as cs1bit from cr.nimble.dsp import ( build_signal_from_indices_and_values ) .. GENERATED FROM PYTHON SOURCE LINES 30-32 Setup ------ .. GENERATED FROM PYTHON SOURCE LINES 32-40 .. code-block:: default # Number of measurements M = 256 # Ambient dimension N = 512 # Sparsity level K = 4 .. GENERATED FROM PYTHON SOURCE LINES 41-43 Sensing Matrix ------------------------------------------------ .. GENERATED FROM PYTHON SOURCE LINES 43-53 .. code-block:: default Phi = crdict.gaussian_mtx(cnb.KEYS[0], M, N, normalize_atoms=False) # frame bound s0 = crdict.upper_frame_bound(Phi) print(s0) fig=plt.figure(figsize=(8,6), dpi= 100, facecolor='w', edgecolor='k') plt.imshow(Phi, extent=[0, 2, 0, 1]) plt.gray() plt.colorbar() plt.title(r'$\Phi$') .. image-sg:: /gallery/0200_cs/images/sphx_glr_cs1bit_biht_001.png :alt: $\Phi$ :srcset: /gallery/0200_cs/images/sphx_glr_cs1bit_biht_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none 2.4003416483791433 Text(0.5, 1.0, '$\\Phi$') .. GENERATED FROM PYTHON SOURCE LINES 54-56 K-sparse signal -------------------------- .. GENERATED FROM PYTHON SOURCE LINES 56-64 .. code-block:: default x, omega = crdata.sparse_normal_representations(cnb.KEYS[1], N, K) # normalize signal x = x / norm(x) # the support indices print(omega) fig=plt.figure(figsize=(8,6), dpi= 100, facecolor='w', edgecolor='k') plt.stem(x, markerfmt='.') .. image-sg:: /gallery/0200_cs/images/sphx_glr_cs1bit_biht_002.png :alt: cs1bit biht :srcset: /gallery/0200_cs/images/sphx_glr_cs1bit_biht_002.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none [ 56 128 138 367] .. GENERATED FROM PYTHON SOURCE LINES 65-68 Measurement process ------------------------------------------------ measurements .. GENERATED FROM PYTHON SOURCE LINES 68-73 .. code-block:: default y = cs1bit.measure_1bit(Phi, x) fig=plt.figure(figsize=(8,6), dpi= 100, facecolor='w', edgecolor='k') plt.stem(y, markerfmt='.') print(y) .. image-sg:: /gallery/0200_cs/images/sphx_glr_cs1bit_biht_003.png :alt: cs1bit biht :srcset: /gallery/0200_cs/images/sphx_glr_cs1bit_biht_003.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none [-1. -1. 1. 1. 1. -1. -1. -1. -1. -1. 1. 1. 1. -1. -1. 1. -1. -1. -1. -1. -1. -1. 1. 1. -1. -1. -1. 1. 1. 1. 1. -1. -1. 1. -1. -1. 1. 1. -1. 1. 1. 1. -1. -1. 1. -1. -1. 1. -1. -1. 1. 1. 1. 1. 1. -1. -1. -1. -1. -1. -1. -1. 1. 1. -1. -1. 1. 1. -1. 1. -1. 1. 1. -1. -1. 1. -1. 1. -1. 1. -1. -1. 1. -1. -1. 1. -1. -1. -1. -1. -1. 1. 1. -1. 1. 1. 1. -1. -1. 1. -1. -1. -1. -1. -1. -1. 1. -1. 1. 1. -1. -1. -1. 1. -1. -1. -1. -1. 1. -1. -1. -1. 1. 1. 1. 1. -1. -1. -1. -1. 1. 1. -1. -1. 1. 1. -1. 1. -1. -1. 1. -1. 1. -1. 1. -1. 1. 1. -1. -1. -1. -1. 1. -1. -1. -1. 1. -1. 1. -1. 1. 1. 1. -1. 1. -1. -1. 1. -1. 1. 1. -1. 1. 1. -1. -1. 1. -1. 1. -1. -1. -1. 1. -1. 1. 1. -1. 1. -1. 1. 1. -1. 1. -1. -1. 1. 1. 1. -1. -1. 1. -1. -1. 1. 1. 1. -1. 1. -1. 1. 1. 1. -1. 1. -1. -1. -1. -1. 1. -1. -1. 1. -1. 1. 1. 1. -1. 1. 1. 1. -1. 1. 1. 1. -1. 1. -1. -1. 1. 1. 1. -1. 1. -1. 1. 1. -1. 1. 1. -1. 1. -1. 1. -1. -1. 1.] .. GENERATED FROM PYTHON SOURCE LINES 74-77 Signal Reconstruction using BIHT ------------------------------------------------ solver step-size .. GENERATED FROM PYTHON SOURCE LINES 77-80 .. code-block:: default tau = 0.98 * s0 # solution sol = cs1bit.biht_jit(Phi, y, K, tau) .. GENERATED FROM PYTHON SOURCE LINES 81-82 reconstructed signal .. GENERATED FROM PYTHON SOURCE LINES 82-84 .. code-block:: default x_rec = build_signal_from_indices_and_values(N, sol.I, sol.x_I) .. GENERATED FROM PYTHON SOURCE LINES 85-87 Verification ------------------------------------------------ .. GENERATED FROM PYTHON SOURCE LINES 87-98 .. code-block:: default fig=plt.figure(figsize=(8,6), dpi= 100, facecolor='w', edgecolor='k') plt.subplot(211) plt.title('original') plt.stem(x, markerfmt='.', linefmt='gray') plt.subplot(212) plt.stem(x_rec, markerfmt='.') plt.title('reconstruction') # recovered support I = jnp.sort(sol.I) print(I) .. image-sg:: /gallery/0200_cs/images/sphx_glr_cs1bit_biht_004.png :alt: original, reconstruction :srcset: /gallery/0200_cs/images/sphx_glr_cs1bit_biht_004.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none [ 56 128 138 367] .. GENERATED FROM PYTHON SOURCE LINES 99-100 check if the support is recovered correctly .. GENERATED FROM PYTHON SOURCE LINES 100-107 .. code-block:: default print(jnp.array_equal(omega, I)) # normalize recovered signal x_rec = x_rec / norm(x_rec) # the norm of error print(norm(x - x_rec)) .. rst-class:: sphx-glr-script-out .. code-block:: none True 0.024352999672904573 .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 1.923 seconds) .. _sphx_glr_download_gallery_0200_cs_cs1bit_biht.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: cs1bit_biht.py ` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: cs1bit_biht.ipynb ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_