【发布时间】:2013-11-12 19:30:57
【问题描述】:
好的,我有一个来自脑电图扫描的数据文件(二进制文件 data.eeg),在 matlab 中读取文件并绘制数据部分的代码如下所示:
sr=400; % Sample Rate
Nyq_freq=sr/2; % Nyquist Frequency
fneeg=input('Filename (with path and extension) :', 's');
t=input('How many seconds in total of EEG ? : ');
ch=input('How many channels of EEG ? : ');
le=t*sr; % Length of the Recording
fid=fopen(fneeg, 'r', 'l'); % Open the file to read
EEG=fread(fid,[ch,le],'int16'); % Read Data -> EEG Matrix
fclose ('all');
plot(EEG(:,3))
这是我“翻译”的尝试
from numpy import *
from matplotlib.pylab import *
sample_rate = 400
Nyquist = sample_rate/2.
fneeg = raw_input("Filename (full path & extension): ")
t = int(raw_input("How many secs in total of EEG?: "))
ch = int(raw_input("How many channels of EEG?: "))
le = t*sample_rate
fid = open(fneeg, 'r')
EEG = fromfile(fneeg, int16)
这就是让我感到困惑的地方。根据文档,matlab的fread是一种通过fread(loaded_file, size, data_type)读取二进制文件的方法。 python 中的替代方法是使用 numpy 的 fromfile 并使用内置的 reshape 函数进行整形(根据这里的线程:MATLAB to Python fread)。我不确定这是如何工作的,甚至与 matlab 方法有关吗?如果我的问题令人困惑,我很抱歉,matlab 对我来说仍然很新
编辑:如果你想看看这里的文件:https://www.dropbox.com/s/zzm6uvjfm9gpamk/data.eeg
Edit2:原始输入的答案是 t=10,ch=32。事实上,我不知道为什么我现在考虑到它甚至要求用户输入..
【问题讨论】:
-
你能发布一个输入文件的样本吗?
-
听起来您已经回答了自己的问题。据我所知,您只需要
eeg = numpy.fromfile(filename, 'int16').reshape(ch, le)。此外,如果您愿意,可以跳过显式打开文件。fromfile将接受文件名和文件对象。 -
我在编辑中添加了指向数据的链接。 @JoeKington,这也是我的推理,但为什么我们需要重塑?我不明白其中的道理,你明白我的意思吗?
-
@Norman,
np.fromfile只返回一个一维数组(长度为ch*le,但您希望它是一个chXle矩阵。reshape只是进行了此更改,无需接触数据本身。 -
@NormanB,你碰巧在使用 Neuroscan 吗?我正在尝试将 Neuroscan 文件(.cnt 或 .eeg)导入 python 并遇到很多问题。