CHB-MIT波士顿儿童医院癫痫EEG脑电数据处理-STFT(三)

1、数据处理(加入滤波、STFT)

加入STFT后,可以将设置好的不同频带的波形抽取出来做单独的分析,这里以Alpha,Beta,Delta,Theta,Gamma五种波形为例!

from mne import Epochs, pick_types, events_from_annotations
from mne.io import concatenate_raws
from mne.io import read_raw_edf
from mne.datasets import eegbci
import mne
import numpy as np
import pandas as pd
import glob 
import numpy as np
import os
from scipy import signal, fft
import matplotlib.pyplot as plt

path_time = "ttt.csv" # 患者发病发病起止时间表
file_dir = "chb01"
path_save = "data"
# 选择患者共有的通道
ch = ['FP1-F7', 'F7-T7', 'T7-P7', 'P7-O1', 'FP1-F3', 'F3-C3', 'C3-P3', 'P3-O1', 'FP2-F4', 'F4-C4', 'C4-P4', 'P4-O2', 
  'FP2-F8', 'F8-T8', 'T8-P8-0', 'P8-O2', 'FZ-CZ', 'CZ-PZ', 'P7-T7', 'T7-FT9', 'FT9-FT10', 'FT10-T8']
sfreq = 256
bandFreqs = [
    {'name': 'Delta', 'fmin': 1, 'fmax': 3},
    {'name': 'Theta', 'fmin': 4, 'fmax': 7},
    {'name': 'Alpha', 'fmin': 8, 'fmax': 13},
    {'name': 'Beta', 'fmin': 14, 'fmax': 31},
    {'name': 'Gamma', 'fmin': 31, 'fmax': 40}
]
# 定义STFT函数
def STFT(epochsData, sfreq, band=bandFreqs):
    f, t, Zxx = signal.stft(epochsData, fs=sfreq)
    bandResult = []
    for iter_freq in band:
        index = np.where((iter_freq['fmin'] < f) & (f < iter_freq['fmax']))
        portion = np.zeros(Zxx.shape, dtype=np.complex_)
        portion[:, :, index, :] = Zxx[:, :, index, :]
        _, xrec = signal.istft(portion, fs=sfreq)
        # 保存滤波后的结果
        bandResult.append(xrec)
    return bandResult
time = pd.read_csv(path_time,index_col="chb")
files = sorted(os.listdir(file_dir))
for file in files:
  if os.path.splitext(file)[1] == '.edf':
    f = os.path.splitext(file)[0]
    f_str = str(os.path.splitext(os.path.splitext(file)[0])[0])
      if i == 0:
        raws = mne.io.read_raw_edf(file_dir+"/" + file,preload=True,verbose=False)
        raws.pick_channels(ch)
        raws.filter(0.1,50.,method='iir')
        raw_d,raw_t = raws[:,:]
        i+=1
      else:
        i+=1
        if f_str in time.index:
           time.loc[f_str]['start'] = time.loc[f_str]['start'] * 256 + len(raw_t)
           time.loc[f_str]['end'] = time.loc[f_str]['end']*256 + len(raw_t)
           raw = mne.io.read_raw_edf(file_dir+"/" + file, preload=True,verbose=False)
           raw.pick_channels(ch)
           raw.filter(0.1,50.,method='iir')
           raws = concatenate_raws([raws,raw])
           raws_d, raw_t = raws[:,:]
d, t = raws[:,:]
data = d*1e6
stft = STFT(d, sfreq)
pointNum = d.shape[0]
stftFreq = np.abs(fft.fft(stft[:pointNum]))
data = np.transpose(stftFreq, axes=(1,3,2,0))
np.save(path_save+"/"+file_dir+".npy",data)

2、过程展示

在这里插入图片描述

  • 0
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

arbitrary19

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值