.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "gallery/0200_cs/ecg_cs_bsbl.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_ecg_cs_bsbl.py: .. _gallery:cs:ecg:bsbl:1: ECG Data Compressive Sensing ===================================== .. contents:: :depth: 2 :local: In this example, we demonstrate the compressive sensing of ECG data and reconstruction using Block Sparse Bayesian Learning (BSBL). .. GENERATED FROM PYTHON SOURCE LINES 14-20 .. code-block:: default # Configure JAX to work with 64-bit floating point precision. from jax.config import config config.update("jax_enable_x64", True) .. GENERATED FROM PYTHON SOURCE LINES 21-22 Let's import necessary libraries .. GENERATED FROM PYTHON SOURCE LINES 22-41 .. code-block:: default import timeit import jax import numpy as np import jax.numpy as jnp # CR-Suite libraries import cr.nimble as crn import cr.nimble.dsp as crdsp import cr.sparse.dict as crdict import cr.sparse.plots as crplot import cr.sparse.block.bsbl as bsbl # Sample data from scipy.misc import electrocardiogram # Plotting import matplotlib.pyplot as plt # Miscellaneous from scipy.signal import detrend, butter, filtfilt .. GENERATED FROM PYTHON SOURCE LINES 42-48 Test signal ------------------------------ SciPy includes a test electrocardiogram signal which is a 5 minute long electrocardiogram (ECG), a medical recording of the electrical activity of the heart, sampled at 360 Hz. .. GENERATED FROM PYTHON SOURCE LINES 48-58 .. code-block:: default ecg = electrocardiogram() # Sampling frequency in Hz fs = 360 # We shall only process a part of the signal in this demo N = 400 x = ecg[:N] t = np.arange(N) * (1/fs) fig, ax = plt.subplots(figsize=(16,4)) ax.plot(t, x); .. image-sg:: /gallery/0200_cs/images/sphx_glr_ecg_cs_bsbl_001.png :alt: ecg cs bsbl :srcset: /gallery/0200_cs/images/sphx_glr_ecg_cs_bsbl_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none /home/docs/checkouts/readthedocs.org/user_builds/cr-sparse/checkouts/latest/examples/0200_cs/ecg_cs_bsbl.py:48: DeprecationWarning: scipy.misc.electrocardiogram has been deprecated in SciPy v1.10.0; and will be completely removed in SciPy v1.12.0. Dataset methods have moved into the scipy.datasets module. Use scipy.datasets.electrocardiogram instead. ecg = electrocardiogram() [] .. GENERATED FROM PYTHON SOURCE LINES 59-61 Preprocessing ''''''''''''''''''''''''''''''''''''''' .. GENERATED FROM PYTHON SOURCE LINES 61-80 .. code-block:: default # Remove the linear trend from the signal x = detrend(x) ## bandpass filter # lower cutoff frequency f1 = 5 # upper cutoff frequency f2 = 40 # passband in normalized frequency Wn = np.array([f1, f2]) * 2 / fs # butterworth filter fn = 3 fb, fa = butter(fn, Wn, 'bandpass') x = filtfilt(fb,fa,x) fig, ax = plt.subplots(figsize=(16,4)) ax.plot(t, x); .. image-sg:: /gallery/0200_cs/images/sphx_glr_ecg_cs_bsbl_002.png :alt: ecg cs bsbl :srcset: /gallery/0200_cs/images/sphx_glr_ecg_cs_bsbl_002.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none [] .. GENERATED FROM PYTHON SOURCE LINES 81-84 Compressive Sensing at 70% ------------------------------ We choose the compression ratio (M/N) to be 0.7 .. GENERATED FROM PYTHON SOURCE LINES 84-88 .. code-block:: default CR = 0.70 M = int(N * CR) print(f'M={M}, N={N}, CR={CR}') .. rst-class:: sphx-glr-script-out .. code-block:: none M=280, N=400, CR=0.7 .. GENERATED FROM PYTHON SOURCE LINES 89-90 Sensing matrix .. GENERATED FROM PYTHON SOURCE LINES 90-92 .. code-block:: default Phi = crdict.gaussian_mtx(crn.KEY0, M, N) .. GENERATED FROM PYTHON SOURCE LINES 93-95 Measurements ''''''''''''''''''''''''''''''''''''''' .. GENERATED FROM PYTHON SOURCE LINES 95-100 .. code-block:: default y = Phi @ x fig, ax = plt.subplots(figsize=(16, 4)) ax.plot(y); .. image-sg:: /gallery/0200_cs/images/sphx_glr_ecg_cs_bsbl_003.png :alt: ecg cs bsbl :srcset: /gallery/0200_cs/images/sphx_glr_ecg_cs_bsbl_003.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none [] .. GENERATED FROM PYTHON SOURCE LINES 101-103 Sparse Recovery with BSBL ''''''''''''''''''''''''''''''''''''''' .. GENERATED FROM PYTHON SOURCE LINES 103-110 .. code-block:: default options = bsbl.bsbl_bo_options(y, max_iters=20) start = timeit.default_timer() sol = bsbl.bsbl_bo_np_jit(Phi, y, 25, options=options) stop = timeit.default_timer() print(f'Reconstruction time: {stop - start:.2f} sec', ) print(sol) .. rst-class:: sphx-glr-script-out .. code-block:: none Reconstruction time: 1.70 sec iterations=20 block size=25 blocks=16, nonzero=16 r_norm=1.49e-01 x_norm=5.23e+00 lambda=5.86e-04 dmu=1.49e-04 .. GENERATED FROM PYTHON SOURCE LINES 111-112 Recovered signal .. GENERATED FROM PYTHON SOURCE LINES 112-115 .. code-block:: default x_hat = sol.x print(f'SNR: {crn.signal_noise_ratio(x, x_hat):.2f} dB, PRD: {crn.percent_rms_diff(x, x_hat):.1f}%') .. rst-class:: sphx-glr-script-out .. code-block:: none SNR: 24.79 dB, PRD: 5.8% .. GENERATED FROM PYTHON SOURCE LINES 116-117 Plot the original and recovered signals .. GENERATED FROM PYTHON SOURCE LINES 117-122 .. code-block:: default ax = crplot.h_plots(2) ax[0].plot(x) ax[1].plot(x_hat) .. image-sg:: /gallery/0200_cs/images/sphx_glr_ecg_cs_bsbl_004.png :alt: ecg cs bsbl :srcset: /gallery/0200_cs/images/sphx_glr_ecg_cs_bsbl_004.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none [] .. GENERATED FROM PYTHON SOURCE LINES 123-126 Compressive Sensing at 50% ------------------------------ Let us now increase the compression .. GENERATED FROM PYTHON SOURCE LINES 126-130 .. code-block:: default CR = 0.50 M = int(N * CR) print(f'M={M}, N={N}, CR={CR}') .. rst-class:: sphx-glr-script-out .. code-block:: none M=200, N=400, CR=0.5 .. GENERATED FROM PYTHON SOURCE LINES 131-132 Sensing matrix .. GENERATED FROM PYTHON SOURCE LINES 132-134 .. code-block:: default Phi = crdict.gaussian_mtx(crn.KEY0, M, N) .. GENERATED FROM PYTHON SOURCE LINES 135-137 Measurements ''''''''''''''''''''''''''''' .. GENERATED FROM PYTHON SOURCE LINES 137-142 .. code-block:: default y = Phi @ x fig, ax = plt.subplots(figsize=(16, 4)) ax.plot(y); .. image-sg:: /gallery/0200_cs/images/sphx_glr_ecg_cs_bsbl_005.png :alt: ecg cs bsbl :srcset: /gallery/0200_cs/images/sphx_glr_ecg_cs_bsbl_005.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none [] .. GENERATED FROM PYTHON SOURCE LINES 143-145 Sparse Recovery with BSBL ''''''''''''''''''''''''''''''''''''''' .. GENERATED FROM PYTHON SOURCE LINES 145-152 .. code-block:: default options = bsbl.bsbl_bo_options(y, max_iters=20) start = timeit.default_timer() sol = bsbl.bsbl_bo_np_jit(Phi, y, 25, options=options) stop = timeit.default_timer() print(f'Reconstruction time: {stop - start:.2f} sec', ) print(sol) .. rst-class:: sphx-glr-script-out .. code-block:: none Reconstruction time: 1.38 sec iterations=20 block size=25 blocks=16, nonzero=16 r_norm=2.43e-01 x_norm=5.13e+00 lambda=2.20e-03 dmu=2.36e-03 .. GENERATED FROM PYTHON SOURCE LINES 153-154 Recovered signal .. GENERATED FROM PYTHON SOURCE LINES 154-157 .. code-block:: default x_hat = sol.x print(f'SNR: {crn.signal_noise_ratio(x, x_hat):.2f} dB, PRD: {crn.percent_rms_diff(x, x_hat):.1f}%') .. rst-class:: sphx-glr-script-out .. code-block:: none SNR: 20.33 dB, PRD: 9.6% .. GENERATED FROM PYTHON SOURCE LINES 158-159 Plot the original and recovered signals .. GENERATED FROM PYTHON SOURCE LINES 159-162 .. code-block:: default ax = crplot.h_plots(2) ax[0].plot(x) ax[1].plot(x_hat) .. image-sg:: /gallery/0200_cs/images/sphx_glr_ecg_cs_bsbl_006.png :alt: ecg cs bsbl :srcset: /gallery/0200_cs/images/sphx_glr_ecg_cs_bsbl_006.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none [] .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 5.637 seconds) .. _sphx_glr_download_gallery_0200_cs_ecg_cs_bsbl.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: ecg_cs_bsbl.py ` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: ecg_cs_bsbl.ipynb ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_