.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/asr/plot_03_adaptive_asr.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_asr_plot_03_adaptive_asr.py: Adaptive ASR with chunk updates =============================== This example demonstrates the AASR-style adaptive update workflow. The calibration state is initialized on one chunk, updated on a second chunk, and then applied to the full signal with MATLAB-style ``reconstruct``. .. GENERATED FROM PYTHON SOURCE LINES 9-66 .. image-sg:: /auto_examples/asr/images/sphx_glr_plot_03_adaptive_asr_001.png :alt: Input, After adaptive update + reconstruct :srcset: /auto_examples/asr/images/sphx_glr_plot_03_adaptive_asr_001.png :class: sphx-glr-single-img .. code-block:: Python from __future__ import annotations import matplotlib.pyplot as plt import numpy as np from mne_denoise.asr import AdaptiveASR rng = np.random.default_rng(7) sfreq = 250.0 duration = 18.0 n_times = int(sfreq * duration) n_channels = 8 t = np.arange(n_times) / sfreq brain = np.zeros((n_channels, n_times), dtype=np.float64) for ch_idx in range(n_channels): phase = rng.uniform(0.0, 2.0 * np.pi) brain[ch_idx] = ( 0.5 * np.sin(2.0 * np.pi * 10.0 * t + phase) + 0.15 * np.sin(2.0 * np.pi * 6.0 * t + 0.5 * phase) + 0.05 * rng.standard_normal(n_times) ) data = brain.copy() spatial = rng.standard_normal((n_channels, 2)) spatial /= np.linalg.norm(spatial, axis=0, keepdims=True) for onset, stop in ((4.0, 4.9), (9.5, 10.2), (13.5, 14.4)): start = int(onset * sfreq) stop_samp = int(stop * sfreq) source = rng.standard_normal((2, stop_samp - start)) * 8.0 data[:, start:stop_samp] += spatial @ source chunk = int(6.0 * sfreq) asr = AdaptiveASR( sfreq=sfreq, cutoff=20.0, variant="psw", verbose=False, ) asr.fit(data[:, :chunk]) asr.partial_fit(data[:, chunk : 2 * chunk]) cleaned = asr.transform(data) fig, axes = plt.subplots(2, 1, figsize=(10, 5), sharex=True, layout="constrained") axes[0].plot(t, data[0], color="tab:red", lw=1.0, label="Noisy") axes[0].plot(t, brain[0], color="k", lw=1.0, alpha=0.7, label="Underlying") axes[0].set_title("Input") axes[0].legend(loc="upper right") axes[1].plot(t, cleaned[0], color="tab:blue", lw=1.0, label="AdaptiveASR") axes[1].plot(t, brain[0], color="k", lw=1.0, alpha=0.7, label="Underlying") axes[1].set_title("After adaptive update + reconstruct") axes[1].set_xlabel("Time (s)") axes[1].legend(loc="upper right") plt.show() .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 1.113 seconds) .. _sphx_glr_download_auto_examples_asr_plot_03_adaptive_asr.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_03_adaptive_asr.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_03_adaptive_asr.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_03_adaptive_asr.zip `