将Matlab代码转换为python [英] Converting matlab code to python

查看:341
本文介绍了将Matlab代码转换为python的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,所以我有一个来自EEG扫描的数据文件(一个二进制文件,data.eeg),在matlab中,读取文件并绘制部分数据的代码如下:

Okay so I have a datafile from an EEG scan (a binary file, data.eeg), and in matlab the code to read the file and plot a section of the data goes like this:

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并重塑(根据此处的线程: MATLAB到Python fread )使用内置的重塑功能.我不确定这是如何工作的,甚至与matlab方法有关?如果我的问题令人困惑,对不起,matlab对我来说还是很新的

Here's where things get confusing to me. According to the documentation, matlab's fread is a method of reading binary files via fread(loaded_file, size, data_type). The alternative in python is using numpy's fromfile and reshaping (according to this thread here: MATLAB to Python fread) using the built in reshape function. I'm not sure how this works, or even relates to the matlab method? I'm sorry if my question is confusing, matlab is still very new to me

如果您想在这里查看文件,则为: https ://www.dropbox.com/s/zzm6uvjfm9gpamk/data.eeg

If you wanna look have a look at the file here it is: https://www.dropbox.com/s/zzm6uvjfm9gpamk/data.eeg

Edit2:原始输入的答案为t = 10,ch = 32.实际上,我不确定为什么现在考虑后就什至要求用户输入.

The answers to the raw inputs are t=10, ch=32. In fact I'm not sure why I'm even asking for user input now that I think about it..

推荐答案

正如您自己和@JoeKington在评论中所讨论的,这应该可行(我删除了输入内容进行测试)

As discussed in the comments by yourself and @JoeKington, this should work (I removed the input stuff for testing)

import numpy as np

sample_rate = 400
Nyquist = sample_rate/2.0
fneeg = 'data.eeg'
t = 10 
ch = 32
le = t*sample_rate
EEG = np.fromfile(fneeg, 'int16').reshape(ch, le, order='F')

不进行重塑,您将得到:

Without the reshape, you get:

In [45]: EEG
Out[45]: array([ -39,  -25,  -22, ..., -168, -586,  -46], dtype=int16)

In [46]: EEG.shape
Out[46]: (128000,)

随着重塑:

In [47]: EEG.reshape(ch, le, order='F')
Out[47]: 
array([[ -39,  -37,  -12, ...,    5,   19,   21],
       [ -25,  -20,    7, ...,   20,   36,   36],
       [ -22,  -20,    0, ...,   18,   34,   36],
       ..., 
       [ 104,  164,   44, ...,   60,  -67, -168],
       [ 531,  582,   88, ...,   29, -420, -586],
       [ -60,  -63,  -92, ...,  -17,  -44,  -46]], dtype=int16)

In [48]: EEG.reshape(ch, le, order='F').shape
Out[48]: (32, 4000)

这篇关于将Matlab代码转换为python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆