.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/example_star.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_auto_examples_example_star.py: Sparse time artifact removal on simulated data ============================================== This example is similar to test_nt_star.m in Noisetools. Results are equivalent (within numerical precision) to the Matlab code. .. GENERATED FROM PYTHON SOURCE LINES 9-17 .. code-block:: Python import matplotlib.pyplot as plt import numpy as np from meegkit import star from meegkit.utils import demean, normcol rng = np.random.default_rng(9) .. GENERATED FROM PYTHON SOURCE LINES 18-22 Create simulated data ----------------------------------------------------------------------------- Simulated data consist of N channels, 1 sinusoidal target, N-3 noise sources, with temporally local artifacts on each channel. .. GENERATED FROM PYTHON SOURCE LINES 22-47 .. code-block:: Python # Create simulated data nchans = 10 n_samples = 1000 f = 2 target = np.sin(np.arange(n_samples) / n_samples * 2 * np.pi * f) target = target[:, np.newaxis] noise = rng.standard_normal((n_samples, nchans - 3)) # Create artifact signal SNR = np.sqrt(1) x0 = normcol(np.dot(noise, rng.standard_normal((noise.shape[1], nchans)))) + \ SNR * target * rng.standard_normal((1, nchans)) x0 = demean(x0) artifact = np.zeros(x0.shape) for k in np.arange(nchans): artifact[k * 100 + np.arange(20), k] = 1 x = x0 + 10 * artifact # This is to compare with matlab numerically # from scipy.io import loadmat # mat = loadmat('/Users/nicolas/Toolboxes/NoiseTools/TEST/X.mat') # x = mat['x'] # x0 = mat['x0'] .. GENERATED FROM PYTHON SOURCE LINES 48-50 Apply STAR ----------------------------------------------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 50-52 .. code-block:: Python y, w, _ = star.star(x, 2) .. rst-class:: sphx-glr-script-out .. code-block:: none proportion artifact free: 0.70 proportion artifact free: 0.70 proportion artifact free: 0.70 depth: 1 fixed channels: 10 fixed samples: 299 ratio: 1.01 power ratio: 0.40 .. GENERATED FROM PYTHON SOURCE LINES 53-55 Plot results ----------------------------------------------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 55-64 .. code-block:: Python f, (ax1, ax2, ax3) = plt.subplots(3, 1) ax1.plot(x, lw=.5) ax1.set_title(f"Signal + Artifacts (SNR = {SNR})") ax2.plot(y, lw=.5) ax2.set_title("Denoised") ax3.plot(demean(y) - x0, lw=.5) ax3.set_title("Residual") f.set_tight_layout(True) plt.show() .. image-sg:: /auto_examples/images/sphx_glr_example_star_001.png :alt: Signal + Artifacts (SNR = 1.0), Denoised, Residual :srcset: /auto_examples/images/sphx_glr_example_star_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.342 seconds) .. _sphx_glr_download_auto_examples_example_star.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: example_star.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: example_star.py ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_