赞
踩
最近在根据【脑机接口社区】的博客和公众号的推文学习python中mne库的用法,也算是真正开始上手EEG信号处理的代码了吧。本系列的推文所用的脑机接口均为OpenBCI,欢迎各位大佬一同交流!
Raw对象主要是用来存储连续型的数据,其核心的数据主要为n_channels和n_times(时间×频率),其中也包括了Info对象。
# 引入python库 import mne from mne.datasets import sample import matplotlib.pyplot as plt # sample的存放地址 data_path = sample.data_path() # 该fif文件存放地址 fname = data_path + '/MEG/sample/sample_audvis_raw.fif' """ 如果上述给定的地址中存在该文件,则直接加载本地文件, 如果不存在则在网上下载改数据 """ raw = mne.io.read_raw_fif(fname) print(raw.info)
<Info | 19 non-empty fields bads : list | 0 items ch_names : list | MEG 0113, MEG 0112, MEG 0111, MEG 0122, MEG 0123, EEG 053, EEG 052, EEG 051 chs : list | 8 items (EEG: 8) comps : list | 0 items custom_ref_applied : bool | False dev_head_t : Transform | 3 items dig : Digitization | 0 items events : list | 0 items file_id : dict | 4 items highpass : float | 0.0 Hz hpi_meas : list | 0 items hpi_results : list | 0 items lowpass : float | 128.0 Hz meas_date : NoneType | unspecified meas_id : dict | 4 items nchan : int | 8 proc_history : list | 0 items projs : list | 0 items sfreq : float | 256.0 Hz acq_pars : NoneType acq_stim : NoneType ctf_head_t : NoneType description : NoneType dev_ctf_t : NoneType device_info : NoneType experimenter : NoneType gantry_angle : NoneType helium_info : NoneType hpi_subsystem : NoneType kit_system_id : NoneType line_freq : NoneType proj_id : NoneType proj_name : NoneType subject_info : NoneType utc_offset : NoneType xplotter_layout : NoneType >
import time import numpy as np import matplotlib #matplotlib.use ('Agg') from brainflow.board_shim import BoardShim, BrainFlowInputParams, BoardIds import mne BoardShim.enable_dev_board_logger () # use synthetic board for demo params = BrainFlowInputParams () board = BoardShim (BoardIds.SYNTHETIC_BOARD.value, params) board.prepare_session () board.start_stream () time.sleep (10) #获取10s的数据 data = board.get_board_data () board.stop_stream () board.release_session () eeg_channels = BoardShim.get_eeg_channels (BoardIds.SYNTHETIC_BOARD.value) eeg_data = data[eeg_channels, :]
方法一
boardshim.py >>> serial_port=‘COM3’
方法二
def main (): parser = argparse.ArgumentParser () # use docs to check which parameters are required for specific board, e.g. for Cyton - set serial port parser.add_argument ('--ip-port', type = int, help = 'ip port', required = False, default = 0) parser.add_argument ('--ip-protocol', type = int, help = 'ip protocol, check IpProtocolType enum', required = False, default = 0) parser.add_argument ('--ip-address', type = str, help = 'ip address', required = False, default = '') parser.add_argument ('--serial-port', type = str, help = 'serial port', required = False, default = 'COM3') parser.add_argument ('--mac-address', type = str, help = 'mac address', required = False, default = '') parser.add_argument ('--other-info', type = str, help = 'other info', required = False, default = '') parser.add_argument ('--streamer-params', type = str, help = 'other info', required = False, default = '') parser.add_argument ('--board-id', type = int, help = 'board id, check docs to get a list of supported boards',default=0) parser.add_argument ('--log', action = 'store_true') args = parser.parse_args () params = BrainFlowInputParams () params.ip_port = args.ip_port params.serial_port = args.serial_port params.mac_address = args.mac_address params.other_info = args.other_info params.ip_address = args.ip_address params.ip_protocol = args.ip_protocol if (args.log): BoardShim.enable_dev_board_logger () else: BoardShim.disable_board_logger () board = BoardShim (args.board_id, params) board.prepare_session () board.start_stream () time.sleep (1) board.config_board ('/2') # enable analog mode only for Cyton Based Boards! time.sleep (5) data = board.get_board_data ()
如果不清楚端口是否连接成功,可以采用以下代码
def get_port():
import serial.tools.list_ports
port_list = list(serial.tools.list_ports.comports())
if len(port_list) == 0:
print('找不到串口')
else:
for i in range(0,len(port_list)):
print(port_list[i])
# Creating MNE objects from brainflow data arrays
ch_types = ['eeg', 'eeg', 'eeg', 'eeg', 'eeg', 'eeg', 'eeg', 'eeg']
ch_names = ['MEG 0113', 'MEG 0112', 'MEG 0111', 'MEG 0122', 'MEG 0123', 'EEG 053', 'EEG 052', 'EEG 051']#通道一般自行设定
sfreq = BoardShim.get_sampling_rate (BoardIds.SYNTHETIC_BOARD.value)
info = mne.create_info (ch_names = ch_names, sfreq = sfreq, ch_types = ch_types)
raw = mne.io.RawArray (eeg_data, info)
picks = mne.pick_types(
info, meg=False, eeg=True, stim=False,
include=ch_names
)
raw.save("raw.fif", picks=picks, overwrite=True)
raw_fif_data = mne.io.read_raw_fif("raw.fif", preload=True, verbose='ERROR')
print(raw_fif_data.info)
scalings = {'eeg': 1000}#采用1000倍缩小。
#scalings = 'auto' 也可以采用自动缩放比例
raw.plot(n_channels=8, scalings=scalings, title='Data from arrays',
show=True, block=True)
以下展示1000倍和8000倍缩小的数据波形。
picks = mne.pick_types(raw.info, eeg=True, exclude='bads')
t_idx = raw.time_as_index([0., 10.])
data, times = raw[picks, t_idx[0]:t_idx[1]]
plt.plot(times,data.T)
plt.title("Sample channels")
plt.show()
采样频率,也称为采样速度或者采样率,定义了每秒从连续信号中提取并组成离散信号的采样个数,它用赫兹(Hz)来表示。
采样频率的倒数是采样周期或者叫作采样时间,它是采样之间的时间间隔。
通俗的讲采样频率是指计算机每秒钟采集多少个信号样本。
sfreq=raw.info['sfreq']
"""
获取索引为m到n的样本,每个样本从第k次到第h次.
data,times=raw[m:n,k:h]
其中data为索引为m到n的样本,每个样本从第k次到第h次.
times是以第k次采样的时间作为开始时间,第h次采样时的时间为结束时间的时间数组。
"""
data,times=raw[:3,int(sfreq*1):int(sfreq*3)]
plt.plot(times,data.T)
plt.title("Sample channels")
以下几幅图因为用的是合成板,所以无法确定通道的位置,因而借用以下脑机接口社区的图片
raw.plot_psd()
plt.show()
raw.plot_projs_topomap()
plt.show()
raw.plot_psd_topo()
plt.show()
raw.plot_sensors()
plt.show()
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。