python提取脑电psd特征
时间: 2023-05-31 07:06:41 浏览: 215
脑电特征提取
使用Python提取脑电PSD特征可以按照以下步骤进行:
1. 载入脑电数据,通常是以mat格式存储。
```python
import scipy.io as sio
data = sio.loadmat('EEGdata.mat')
```
2. 选择感兴趣的电极信号。
```python
eegdata = data['EEG'] # 获取所有电极信号
F4 = eegdata[4,:] # 选择第五个电极F4的信号
```
3. 对选定电极的信号进行预处理,如去除噪声和基线漂移。
```python
from scipy import signal
fs = 1000 # 采样率
nyq = 0.5 * fs
low = 1 / nyq
high = 40 / nyq
b, a = signal.butter(4, [low, high], 'bandpass')
filtered_data = signal.filtfilt(b, a, F4)
```
4. 对预处理后的信号进行功率谱密度(PSD)估计。
```python
from scipy import signal
f, Pxx = signal.welch(filtered_data, fs, nperseg=1024)
```
5. 对PSD进行特征提取,如均值、方差、峰值等。
```python
import numpy as np
mean_PSD = np.mean(Pxx)
std_PSD = np.std(Pxx)
max_PSD = np.max(Pxx)
```
6. 将步骤4和步骤5组合成一个函数,以便对多个电极的信号进行特征提取。
```python
def extract_PSD_features(eegdata, fs):
features = []
for i in range(eegdata.shape[0]):
# 预处理
nyq = 0.5 * fs
low = 1 / nyq
high = 40 / nyq
b, a = signal.butter(4, [low, high], 'bandpass')
filtered_data = signal.filtfilt(b, a, eegdata[i,:])
# PSD估计
f, Pxx = signal.welch(filtered_data, fs, nperseg=1024)
# 特征提取
mean_PSD = np.mean(Pxx)
std_PSD = np.std(Pxx)
max_PSD = np.max(Pxx)
features.append([mean_PSD, std_PSD, max_PSD])
return np.array(features)
```
阅读全文