{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "\n\n# Compute source power spectral density (PSD) in a label\n\nReturns an STC file containing the PSD (in dB) of each of the sources\nwithin a label.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "# Authors: Alexandre Gramfort \n#\n# License: BSD-3-Clause\n# Copyright the MNE-Python contributors." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "import matplotlib.pyplot as plt\n\nimport mne\nfrom mne import io\nfrom mne.datasets import sample\nfrom mne.minimum_norm import compute_source_psd, read_inverse_operator\n\nprint(__doc__)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Set parameters\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "data_path = sample.data_path()\nmeg_path = data_path / \"MEG\" / \"sample\"\nraw_fname = meg_path / \"sample_audvis_raw.fif\"\nfname_inv = meg_path / \"sample_audvis-meg-oct-6-meg-inv.fif\"\nfname_label = meg_path / \"labels\" / \"Aud-lh.label\"\n\n# Setup for reading the raw data\nraw = io.read_raw_fif(raw_fname, verbose=False)\nevents = mne.find_events(raw, stim_channel=\"STI 014\")\ninverse_operator = read_inverse_operator(fname_inv)\nraw.info[\"bads\"] = [\"MEG 2443\", \"EEG 053\"]\n\n# picks MEG gradiometers\npicks = mne.pick_types(\n raw.info, meg=True, eeg=False, eog=True, stim=False, exclude=\"bads\"\n)\n\ntmin, tmax = 0, 120 # use the first 120s of data\nfmin, fmax = 4, 100 # look at frequencies between 4 and 100Hz\nn_fft = 2048 # the FFT size (n_fft). Ideally a power of 2\nlabel = mne.read_label(fname_label)\n\nstc = compute_source_psd(\n raw,\n inverse_operator,\n lambda2=1.0 / 9.0,\n method=\"dSPM\",\n tmin=tmin,\n tmax=tmax,\n fmin=fmin,\n fmax=fmax,\n pick_ori=\"normal\",\n n_fft=n_fft,\n label=label,\n dB=True,\n)\n\nstc.save(\"psd_dSPM\", overwrite=True)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "View PSD of sources in label\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "plt.plot(stc.times, stc.data.T)\nplt.xlabel(\"Frequency (Hz)\")\nplt.ylabel(\"PSD (dB)\")\nplt.title(\"Source Power Spectrum (PSD)\")\nplt.show()" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.12.2" } }, "nbformat": 4, "nbformat_minor": 0 }