使用mne Python库获取EEG信号的平均频带功率

发布于 2025-01-09 20:36:22 字数 346 浏览 1 评论 0原文

我正在寻找一种方法来从 edf 文件中获取 EEG 信号中通道的伽马波段平均频率,但我无法弄清楚如何做到这一点。我在线检查了各种来源,发现为了做到这一点,我需要使用韦尔奇方法从信号中获取PSD(功率谱密度),但我无法找到一种方法来做到这一点使用多国图书馆。到目前为止我所能完成的所有工作都附在下面。我将非常感谢任何形式的帮助。

import mne
file = "H S1 EC.edf"
data = mne.io.read_raw_edf(file)
raw_data = data.get_data()
info = data.info
channels = data.ch_names

I'm looking for a way to obtain the gamma band's average frequency of a channel in an EEG signal from an edf file and I am unable to figure out how to do so. I checked various sources online and I found out in order to do that I need to obtain the PSD(Power Spectral Density) from the signal using the Welch's method but I was unable to find a way to do that using the mne library. All I have been able to accomplish so far has been attached below. I'd be thankful for any kind of help.

import mne
file = "H S1 EC.edf"
data = mne.io.read_raw_edf(file)
raw_data = data.get_data()
info = data.info
channels = data.ch_names

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

高跟鞋的旋律 2025-01-16 20:36:22

要获得信号的功率谱,您可以:

import mne

file = "H S1 EC.edf"
data = mne.io.read_raw_edf(file, preload=True)
psds, freqs = mne.time_frequency.psd_welch(data)

这为您提供了所有频率的频谱功率,因此要获得平均伽玛功率,您必须对与您的频率相对应的 psds 数组切片进行平均感兴趣的频段(与psds中的功率值相对应的频率可以在freqs数组中找到)。

然而,您似乎对平均伽马功率不感兴趣,而是对伽马频率感兴趣,这要棘手得多。 EEG 中的伽马功率通常较弱,并且该频率范围内的信号可能主要由肌肉伪影(以及可能的微扫视伪影)主导 - 因此,我怀疑您是否能够识别可靠的伽马峰值(不是伪影)。我建议您看一下 Joerg Hipp 和 Markus Siegel 于 2013 年发表的论文“Dissociatingneural gamma-bandactivity from cranial and eyemuscleactivity in EEG”,他们对预处理和分析数据以获得良好的估计给出了一些很好的建议神经元伽玛能量。

To get the power spectrum of your signal you can:

import mne

file = "H S1 EC.edf"
data = mne.io.read_raw_edf(file, preload=True)
psds, freqs = mne.time_frequency.psd_welch(data)

This gives you the spectral power for all frequencies, so to get average gamma power you would have to average a slice of the psds array that corresponds to your frequency band of interest (the frequencies corresponding to power values in psds can be found in freqs array).

However, you seem to be interested not in the average gamma power, but gamma frequency, and this is much trickier. Gamma power is generally weak in EEG and the signal in this frequency range can be dominated by muscle artifacts (and possible microsaccade artifacts) - beacause of this I doubt you would be able identify a reliable gamma peak (one that is not an artifact). I would suggest you to take a look at the "Dissociating neuronal gamma-band activity from cranial and ocular muscle activity in EEG" 2013 paper by Joerg Hipp and Markus Siegel, they give some good advice on preprocessing and analysing the data to get good estimates of neuronal gamma power.

雨后咖啡店 2025-01-16 20:36:22

要在 MNE 中使用 Welch 方法获取 PSD,可以使用 psd_welch() 函数。下面是一个帮助您入门的示例代码片段:

import mne

file = "H S1 EC.edf"
data = mne.io.read_raw_edf(file)

# Define the frequency range of interest (gamma band)
freq_range = (30, 100)

# Obtain the PSD using Welch's method
psds, freqs = mne.time_frequency.psd_welch(data, fmin=freq_range[0], fmax=freq_range[1], n_fft=1024)

# Find the index of the frequency bin with the highest power
gamma_index = psds.mean(axis=0).argmax()

# Obtain the frequency corresponding to the highest power bin
gamma_freq = freqs[gamma_index]

# Print the average gamma frequency for each channel
for i, channel in enumerate(data.ch_names):
    print("Channel {}: {:.2f} Hz".format(channel, psds[i,gamma_index]))

在此代码中,我们首先将感兴趣的频率范围定义为伽马频带 (30-100 Hz)。然后,我们使用 psd_welch() 函数使用 Welch 方法(窗口大小为 1024 个样本)来获取该频率范围的 PSD。生成的 PSD 存储在 psds 变量中,相应的频率存储在 freqs 变量中。

然后我们找到每个通道具有最高功率的频率仓的索引,并获得相应的频率。最后,我们打印出每个通道的平均伽玛频率。

请注意,psd_welch() 函数将 PSD 作为 2D 数组返回,其中每行对应一个通道,每列对应一个频率仓。为了获得每个通道的平均伽玛频率,我们首先取频率轴(轴=1)的平均值以获得每个通道的平均功率,然后找到每个通道的最高功率箱的索引。

或者,要使用 Welch 方法获取 EEG 信号的 PSD 并计算伽马带的平均频率,您可以使用 YASA 库。下面是一些示例代码,展示了如何执行此操作:

import mne
import yasa

# Load EEG data
file = "H S1 EC.edf"
data = mne.io.read_raw_edf(file)
raw_data = data.get_data()
sf = data.info['sfreq']  # Sampling frequency

# Compute the power spectral density using Welch's method
psd, freqs = yasa.psd_welch(raw_data, sf=sf, fmax=100)

# Calculate the average frequency of the gamma band (30-100 Hz)
gamma_freqs = freqs[(freqs >= 30) & (freqs <= 100)]
gamma_psd = psd[:, (freqs >= 30) & (freqs <= 100)]
avg_gamma_freq = (gamma_freqs * gamma_psd).sum() / gamma_psd.sum()

print("Average gamma frequency:", avg_gamma_freq)

此代码使用 MNE 加载 EEG 数据,使用 YASA 的 psd_welch() 函数计算 PSD,然后通过取以下值的加权平均值来计算伽玛波段的平均频率:伽马范围内的频率段。请注意,psd_welch() 函数负责对 EEG 信号进行加窗和重叠,这是获得准确的 PSD 估计的重要步骤。

此外,您可以参考我的github 代码 如何计算脑电图信号的平均频带功率

To obtain the PSD using Welch's method in MNE, you can use the psd_welch() function. Here's an example code snippet to get you started:

import mne

file = "H S1 EC.edf"
data = mne.io.read_raw_edf(file)

# Define the frequency range of interest (gamma band)
freq_range = (30, 100)

# Obtain the PSD using Welch's method
psds, freqs = mne.time_frequency.psd_welch(data, fmin=freq_range[0], fmax=freq_range[1], n_fft=1024)

# Find the index of the frequency bin with the highest power
gamma_index = psds.mean(axis=0).argmax()

# Obtain the frequency corresponding to the highest power bin
gamma_freq = freqs[gamma_index]

# Print the average gamma frequency for each channel
for i, channel in enumerate(data.ch_names):
    print("Channel {}: {:.2f} Hz".format(channel, psds[i,gamma_index]))

In this code, we first define the frequency range of interest as the gamma band (30-100 Hz). We then use the psd_welch() function to obtain the PSD for this frequency range using the Welch's method with a window size of 1024 samples. The resulting PSDs are stored in the psds variable, and the corresponding frequencies are stored in the freqs variable.

We then find the index of the frequency bin with the highest power for each channel, and obtain the corresponding frequency. Finally, we print out the average gamma frequency for each channel.

Note that the psd_welch() function returns the PSD as a 2D array, where each row corresponds to a channel and each column corresponds to a frequency bin. To obtain the average gamma frequency for each channel, we first take the mean across the frequency axis (axis=1) to obtain the average power for each channel, and then find the index of the highest power bin for each channel.

Alternatively,To obtain the PSD of an EEG signal using the Welch's method and calculate the average frequency of the gamma band, you can use the YASA library. Here's some sample code that shows how to do this:

import mne
import yasa

# Load EEG data
file = "H S1 EC.edf"
data = mne.io.read_raw_edf(file)
raw_data = data.get_data()
sf = data.info['sfreq']  # Sampling frequency

# Compute the power spectral density using Welch's method
psd, freqs = yasa.psd_welch(raw_data, sf=sf, fmax=100)

# Calculate the average frequency of the gamma band (30-100 Hz)
gamma_freqs = freqs[(freqs >= 30) & (freqs <= 100)]
gamma_psd = psd[:, (freqs >= 30) & (freqs <= 100)]
avg_gamma_freq = (gamma_freqs * gamma_psd).sum() / gamma_psd.sum()

print("Average gamma frequency:", avg_gamma_freq)

This code loads the EEG data using MNE, computes the PSD using YASA's psd_welch() function, and then calculates the average frequency of the gamma band by taking the weighted average of the frequency bins in the gamma range. Note that the psd_welch() function takes care of windowing and overlapping the EEG signal, which are important steps in obtaining an accurate PSD estimate.

Additionally you can refer to my github code on how to Compute the average bandpower of an EEG signal

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文