Python from scratch - PAL
111 subscribers
84 photos
5 videos
6 files
51 links
Download Telegram
.

مرحله 7: شناسایی باندهای فرکانسی مرسوم


معمولاً از این تعاریف استفاده می‌شود (مقادیر قابل تغییر هستند):

Delta: 1–4 Hz

Theta: 4–8 Hz

Alpha: 8–12 Hz

Beta: 13–30 Hz

Gamma: 30–80 Hz (or 30–100 Hz)

.
.
Delta: 1–4 Hz

import mne
import numpy as np
import matplotlib.pyplot as plt
from mne.datasets import sample
from mne.filter import filter_data

# Load sample EEG data
data_path = sample.data_path()
raw_file = data_path / 'MEG' / 'sample' / 'sample_audvis_raw.fif'
raw = mne.io.read_raw_fif(raw_file, preload=True)
raw.pick_types(meg=False, eeg=True)

fs = raw.info['sfreq']
channel_name = raw.ch_names[0]

# Get first channel, first 1000 samples
eeg_data = raw.get_data(picks='eeg')[:1, :1000]
time = np.arange(eeg_data.shape[1]) / fs

# Filter Delta band (1–4 Hz)
delta_data = filter_data(eeg_data, sfreq=fs, l_freq=1, h_freq=4, method='fir', verbose=False)

# Plot time domain
plt.figure(figsize=(10,4))
plt.plot(time, delta_data[0] * 1e6, color='navy')
plt.title(f"Delta Band (1–4 Hz) — Time Domain ({channel_name})", fontsize=13)
plt.xlabel("Time (seconds)")
plt.ylabel("Amplitude (µV)")
plt.grid(True)
plt.tight_layout()
plt.show()

.
.
Theta (4–8 Hz)


# Filter Theta band (4–8 Hz)
theta_data = filter_data(eeg_data, sfreq=fs, l_freq=4, h_freq=8, method='fir', verbose=False)

plt.figure(figsize=(10,4))
plt.plot(time, theta_data[0] * 1e6, color='darkorange')
plt.title(f"Theta Band (4–8 Hz) — Time Domain ({channel_name})", fontsize=13)
plt.xlabel("Time (seconds)")
plt.ylabel("Amplitude (µV)")
plt.grid(True)
plt.tight_layout()
plt.show()

.
.
Alpha (8–13 Hz)


# Filter Alpha band (8–13 Hz)
alpha_data = filter_data(eeg_data, sfreq=fs, l_freq=8, h_freq=13, method='fir', verbose=False)

plt.figure(figsize=(10,4))
plt.plot(time, alpha_data[0] * 1e6, color='green')
plt.title(f"Alpha Band (8–13 Hz) — Time Domain ({channel_name})", fontsize=13)
plt.xlabel("Time (seconds)")
plt.ylabel("Amplitude (µV)")
plt.grid(True)
plt.tight_layout()
plt.show()

.
.
Beta (13–30 Hz)

# Filter Beta band (13–30 Hz)
beta_data = filter_data(eeg_data, sfreq=fs, l_freq=13, h_freq=30, method='fir', verbose=False)

plt.figure(figsize=(10,4))
plt.plot(time, beta_data[0] * 1e6, color='red')
plt.title(f"Beta Band (13–30 Hz) — Time Domain ({channel_name})", fontsize=13)
plt.xlabel("Time (seconds)")
plt.ylabel("Amplitude (µV)")
plt.grid(True)
plt.tight_layout()
plt.show()

.
.
Gamma (30–80 Hz)


# Filter Gamma band (30–80 Hz)
gamma_data = filter_data(eeg_data, sfreq=fs, l_freq=30, h_freq=80, method='fir', verbose=False)

plt.figure(figsize=(10,4))
plt.plot(time, gamma_data[0] * 1e6, color='purple')
plt.title(f"Gamma Band (30–80 Hz) — Time Domain ({channel_name})", fontsize=13)
plt.xlabel("Time (seconds)")
plt.ylabel("Amplitude (µV)")
plt.grid(True)
plt.tight_layout()
plt.show()

.
.

در ادامه یک نسخه‌ی یکجا می‌سازیم که تمام 5 باند EEG را در یک شکل با 5 تا subplot نمایش دهد.
فقط برای کانال اول و 2000 سمپل اول، نمودار Time domain
👇

.
import mne
import numpy as np
import matplotlib.pyplot as plt
from mne.datasets import sample
from mne.filter import filter_data

# -------------------------------
# 1. Load sample EEG data
# -------------------------------
data_path = sample.data_path()
raw_file = data_path / 'MEG' / 'sample' / 'sample_audvis_raw.fif'
raw = mne.io.read_raw_fif(raw_file, preload=True)
raw.pick_types(meg=False, eeg=True) # Keep EEG only

fs = raw.info['sfreq']
channel_name = raw.ch_names[0]

# First channel, first 2000 samples
eeg_data = raw.get_data(picks='eeg')[:1, :2000]
time = np.arange(eeg_data.shape[1]) / fs

# -------------------------------
# 2. Define EEG frequency bands
# -------------------------------
bands = {'Delta (1–4 Hz)': (1, 4),
'Theta (4–8 Hz)': (4, 8),
'Alpha (8–13 Hz)': (8, 13),
'Beta (13–30 Hz)': (13, 30),
'Gamma (30–80 Hz)': (30, 80)}

colors = {'Delta (1–4 Hz)': 'navy',
'Theta (4–8 Hz)': 'darkorange',
'Alpha (8–13 Hz)': 'green',
'Beta (13–30 Hz)': 'red',
'Gamma (30–80 Hz)': 'purple'}

# -------------------------------
# 3. Create subplots
# -------------------------------
fig, axes = plt.subplots(len(bands), 1, figsize=(12, 10), sharex=True)

for ax, (band_name, band_range) in zip(axes, bands.items()):
# Filter band
filtered_data = filter_data(eeg_data, sfreq=fs,
l_freq=band_range[0],
h_freq=band_range[1],
method='fir', verbose=False)
# Plot
ax.plot(time, filtered_data[0] * 1e6, color=colors[band_name])
ax.set_ylabel("µV", fontsize=10)
ax.set_title(f"{band_name} — {channel_name}", fontsize=11)
ax.grid(True)

axes[-1].set_xlabel("Time (seconds)", fontsize=12)
plt.tight_layout()
plt.show()
.

لطفا پیشنهادها به آدرس زیر ارسال شود 👇


@PALDataanalysis

.