好的,我有一个来自EEG扫描的数据文件(一个二进制文件,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)使用内置的reshape函数作为替代方案。我不确定这如何工作,甚至与matlab方法有何关系?如果我的问题令人困惑,我很抱歉,matlab对我来说还很新。
编辑:如果您想查看文件,请单击此处:https://www.dropbox.com/s/zzm6uvjfm9gpamk/data.eeg 编辑2:原始输入的答案是t=10,ch=32。实际上,我现在想起来了,我不确定为什么要询问用户输入..
eeg = numpy.fromfile(filename, 'int16').reshape(ch, le)
。此外,如果你愿意,你可以跳过显式打开文件的步骤。fromfile
函数也可以接受文件名作为参数,而不仅仅是文件对象。 - Joe Kingtonnp.fromfile
只返回一个1d数组(长度为ch*le
),但您希望它成为一个ch
Xle
矩阵。reshape
只是进行此更改,而不触及数据本身。 - askewchan