Note
Go to the end to download the full example code.
Spectral analysis of the trials#
This example demonstrates how to perform spectral
analysis on epochs extracted from a specific subject
within the moabb.datasets.Cattan2019_PHMD dataset.
# Authors: Pedro Rodrigues <pedro.rodrigues01@gmail.com>
# Modified by: Gregoire Cattan <gcattan@hotmail.fr>
# License: BSD (3-clause)
import warnings
import matplotlib.pyplot as plt
import numpy as np
from moabb.datasets import Cattan2019_PHMD
from moabb.paradigms import RestingStateToP300Adapter
warnings.filterwarnings("ignore")
Initialization#
Specify the channel and subject to compute the power spectrum.
Create an instance of the
moabb.datasets.Cattan2019_PHMDdataset.Create an instance of the
moabb.paradigms.RestingStateToP300Adapterparadigm. By default, the data is filtered between 1-35 Hz, and epochs are extracted from 10 to 50 seconds after event tagging.
# Select channel and subject for the remaining of the example.
channel = "Cz"
subject = 1
dataset = Cattan2019_PHMD()
events = ["on", "off"]
paradigm = RestingStateToP300Adapter(events=events, channels=[channel])
Estimate Power Spectral Density#
Obtain the epochs for the specified subject.
Use Welch’s method to estimate the power spectral density.
This is nemar-py 0.3.0.
Preparing to download nm000341 from https://data.nemar.org/
Retrieving 5 of 119 manifest files (16 concurrent downloads).
primary backend failed; falling back to next layer: 'README.md' is not annexed (no sha256/md5 checksum on the manifest entry); the S3 backend has nothing to fetch.
Overall: 0%| | 0.00/10.6k [00:00<?, ?B/s]
Overall: 6%|▌ | 616/10.6k [00:00<00:01, 5.19kB/s]
Overall: 43%|████▎ | 4.63k/10.6k [00:00<00:00, 34.0kB/s]
Finished downloading nm000341 v1.0.5.
This is nemar-py 0.3.0.
Preparing to download nm000341 from https://data.nemar.org/
Retrieving 5 of 119 manifest files (16 concurrent downloads).
S3: 0%| | 0.00/23.9M [00:00<?, ?B/s]
S3: 4%|▍ | 1.00M/23.9M [00:00<00:09, 2.45MB/s]
S3: 17%|█▋ | 4.00M/23.9M [00:00<00:02, 9.51MB/s]
S3: 50%|█████ | 12.0M/23.9M [00:00<00:00, 28.2MB/s]
S3: 96%|█████████▋| 23.0M/23.9M [00:00<00:00, 50.5MB/s]
S3: 100%|██████████| 23.9M/23.9M [00:00<00:00, 33.2MB/s]
Finished downloading nm000341 v1.0.5.
Display of the data#
Plot the averaged Power Spectral Density (PSD) for each label condition, using the selected channel specified at the beginning of the script.
fig, ax = plt.subplots(facecolor="white", figsize=(8.2, 5.1))
for condition in events:
mean_power = np.mean(S[y == condition], axis=0).flatten()
ax.plot(f, 10 * np.log10(mean_power), label=condition)
ax.set_xlim(paradigm.fmin, paradigm.fmax)
ax.set_ylim(100, 135)
ax.set_ylabel("Spectrum Magnitude (dB)", fontsize=14)
ax.set_xlabel("Frequency (Hz)", fontsize=14)
ax.set_title("PSD for Channel " + channel, fontsize=16)
ax.legend()
fig.show()

Total running time of the script: (0 minutes 4.034 seconds)
Estimated memory usage: 608 MB