将pylsl数据流转换成edf格式文件(仅供参考,待完善)

此处的需求是将lsl传进来的数据流进行保存,不是实时保存,而是对一定时间段的数据进行保存。思路是,通过一个循环将lsl的数据流保存一段时间(保存在一个list里),然后转换成一个numpy的数组类型(即array类型,具体可以百度查下list如何转numpy的array),然后通过pyedflib的函数将array类型的数据和文件header等保存在edf文件中。
以下代码仅供参考,由于时间关系,暂未验证,但是思路确保是对的。

from pyedflib import highlevel
import numpy as np
from pylsl import StreamInlet, resolve_stream
import time
# first resolve an EEG stream on the lab network
print("looking for an EEG stream...")
streams = resolve_stream('type', 'EEG')

# create a new inlet to read from the stream
inlet = StreamInlet(streams[0])
t= time.time()
data_list = []
while True:
    # get a new sample (you can also omit the timestamp part if you're not
    # interested in it)
    sample, timestamp = inlet.pull_sample()
    print(timestamp, sample)
    data_list.append(sample)#由于时间关系,此处没有进行验证。此处要注意可能需要对矩阵进行转置之类的操作,然后在后面转换成numpy中的数组类型,才能保存在edf文件中,切记...
    if(time.time()-t>10):
        break

上面保存的data_list就是关键的脑电数据了,也可能要进行转置(这里就不详细写了)…具体可以参考另一篇,自行理解一下edf保存的array到底应该是什么形式(比如行是代表什么,列是代表什么)

# write an edf file
signals = np.array()#要将list类型的数据转换成array类型,才能兼容edf格式。
#np.random.rand(5, 256*300)*200 # 5 minutes of random signal

channel_names = ['ch1', 'ch2', 'ch3', 'ch4', 'ch5','ch6','ch7','ch8']
signal_headers = highlevel.make_signal_headers(channel_names, sample_rate=500)
header = highlevel.make_header(patientname='patient_x', gender='Female')
highlevel.write_edf('edf_file.edf', signals, signal_headers, header)

以上代码仅供参考…后续继续完善。

欢迎加入BCIduino脑机接口开源社区(公众号“BCIduino脑机接口社区”)…更多代码还需要大家一起写…
(加入社群…请扫码备注“BCI”即可…)
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/nvsirgn/article/details/108300723