-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpreprocessing.py
More file actions
41 lines (35 loc) · 1.09 KB
/
preprocessing.py
File metadata and controls
41 lines (35 loc) · 1.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import mne
from mne.datasets import eegbci
import matplotlib.pyplot as plt
import numpy as np
# Load data (example: subject 1, motor imagery tasks)
subject = 1
tasks = [6, 10, 14] # motor imagery: fists vs feet
raw_files = [
mne.io.read_raw_edf(f, preload=True, stim_channel="auto")
for f in eegbci.load_data(subject, tasks)
]
raw = mne.concatenate_raws(raw_files)
# Visualize raw data
raw.plot(duration=5, n_channels=30)
# Filter data (e.g., band-pass filter between 7 and 30 Hz)
raw.filter(7.0, 30.0, fir_design="firwin", skip_by_annotation="edge")
# Visualize data after filtering
raw.plot(duration=5, n_channels=30)
plt.show()
# Compute PSD using compute_psd with multitaper method
spectrum = raw.compute_psd(
method="multitaper", tmin=10, tmax=20, fmin=7, fmax=30, picks="eeg"
)
data, freqs = spectrum.get_data(return_freqs=True)
# Plot PSD
fig, ax = plt.subplots()
ax.plot(freqs, 10 * np.log10(data.mean(axis=0)), color="k")
ax.set(
title="Multitaper PSD (dB)",
xlabel="Frequency (Hz)",
ylabel="Power/Frequency (dB/Hz)",
)
plt.savefig("psd.png")
raw.compute_psd().plot()
plt.show()