.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/temporal-response-functions/mtrf.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_temporal-response-functions_mtrf.py: .. _exa-mtrf: EEG speech envelope TRF ======================= A TRF analysis on data that comes as arrays. Analyze continuous speech data from the mTRF dataset [1]_. Use the boosting algorithm for estimating temporal response functions (TRFs) to the acoustic envelope. .. contents:: Sections :local: :backlinks: top .. GENERATED FROM PYTHON SOURCE LINES 16-45 .. code-block:: Python # Author: Christian Brodbeck # sphinx_gallery_thumbnail_number = 4 import os from scipy.io import loadmat import mne from eelbrain import * # Load the mTRF speech dataset and convert data to NDVars root = mne.datasets.mtrf.data_path() speech_path = os.path.join(root, 'speech_data.mat') mdata = loadmat(speech_path) # Time axis tstep = 1. / mdata['Fs'][0, 0] n_times = mdata['envelope'].shape[0] time = UTS(0, tstep, n_times) # Load the EEG sensor coordinates (drop fiducials coordinates, which are stored # after sensor 128) sensor = Sensor.from_montage('biosemi128')[:128] # Frequency dimension for the spectrogram band = Scalar('frequency', range(16)) # Create variables envelope = NDVar(mdata['envelope'][:, 0], (time,), name='envelope') eeg = NDVar(mdata['EEG'], (time, sensor), name='EEG', info={'unit': 'µV'}) spectrogram = NDVar(mdata['spectrogram'], (time, band), name='spectrogram') # Exclude a bad channel eeg = eeg[sensor.index(exclude='A13')] .. GENERATED FROM PYTHON SOURCE LINES 46-49 Data ---- Plot the spectrogram of the speech stimulus: .. GENERATED FROM PYTHON SOURCE LINES 49-51 .. code-block:: Python plot.Array(spectrogram, xlim=5, w=6, h=2) .. image-sg:: /auto_examples/temporal-response-functions/images/sphx_glr_mtrf_001.png :alt: mtrf :srcset: /auto_examples/temporal-response-functions/images/sphx_glr_mtrf_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none .. GENERATED FROM PYTHON SOURCE LINES 52-53 Plot the envelope used as stimulus representation for TRFs: .. GENERATED FROM PYTHON SOURCE LINES 53-55 .. code-block:: Python plot.UTS(envelope, xlim=5, w=6, h=2) .. image-sg:: /auto_examples/temporal-response-functions/images/sphx_glr_mtrf_002.png :alt: mtrf :srcset: /auto_examples/temporal-response-functions/images/sphx_glr_mtrf_002.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none .. GENERATED FROM PYTHON SOURCE LINES 56-57 Plot the corresponding EEG data: .. GENERATED FROM PYTHON SOURCE LINES 57-60 .. code-block:: Python p = plot.TopoButterfly(eeg, xlim=5, w=7, h=2) p.set_time(1.200) .. image-sg:: /auto_examples/temporal-response-functions/images/sphx_glr_mtrf_003.png :alt: mtrf :srcset: /auto_examples/temporal-response-functions/images/sphx_glr_mtrf_003.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 61-68 TRF estimation -------------- TRF for the envelope using boosting: - TRF from -100 to 400 ms - Basis of 100 ms Hamming windows - Use 4 partitionings of the data for cross-validation based early stopping .. GENERATED FROM PYTHON SOURCE LINES 68-72 .. code-block:: Python res = boosting(eeg, envelope, -0.100, 0.400, basis=0.100, partitions=4) p = plot.TopoButterfly(res.h_scaled, w=6, h=2) p.set_time(.180) .. image-sg:: /auto_examples/temporal-response-functions/images/sphx_glr_mtrf_004.png :alt: mtrf :srcset: /auto_examples/temporal-response-functions/images/sphx_glr_mtrf_004.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 73-76 Multiple predictors ------------------- Multiple predictors additively explain the signal: .. GENERATED FROM PYTHON SOURCE LINES 76-82 .. code-block:: Python # Derive acoustic onsets from the envelope onset = envelope.diff('time', name='onset').clip(0) onset *= envelope.max() / onset.max() plot.UTS([[envelope, onset]], xlim=5, w=6, h=2) .. image-sg:: /auto_examples/temporal-response-functions/images/sphx_glr_mtrf_005.png :alt: mtrf :srcset: /auto_examples/temporal-response-functions/images/sphx_glr_mtrf_005.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none .. GENERATED FROM PYTHON SOURCE LINES 83-87 .. code-block:: Python res_onset = boosting(eeg, [onset, envelope], -0.100, 0.400, basis=0.100, partitions=4) p = plot.TopoButterfly(res_onset.h_scaled, w=6, h=3) p.set_time(.150) .. image-sg:: /auto_examples/temporal-response-functions/images/sphx_glr_mtrf_006.png :alt: mtrf :srcset: /auto_examples/temporal-response-functions/images/sphx_glr_mtrf_006.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 88-92 Compare models -------------- Compare model quality through the correlation between measured and predicted responses: .. GENERATED FROM PYTHON SOURCE LINES 92-95 .. code-block:: Python plot.Topomap([res.r, res_onset.r], w=4, h=2, columns=2, axtitle=['envelope', 'envelope + onset']) .. image-sg:: /auto_examples/temporal-response-functions/images/sphx_glr_mtrf_007.png :alt: envelope, envelope + onset :srcset: /auto_examples/temporal-response-functions/images/sphx_glr_mtrf_007.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none .. GENERATED FROM PYTHON SOURCE LINES 96-102 References ---------- .. [1] Crosse, M. J., Liberto, D., M, G., Bednar, A., & Lalor, E. C. (2016). The Multivariate Temporal Response Function (mTRF) Toolbox: A MATLAB Toolbox for Relating Neural Signals to Continuous Stimuli. Frontiers in Human Neuroscience, 10. https://doi.org/10.3389/fnhum.2016.00604 .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 11.187 seconds) .. _sphx_glr_download_auto_examples_temporal-response-functions_mtrf.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: mtrf.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: mtrf.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: mtrf.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_