MATLAB Code Python de conversion (NumPy, SciPy, MatplotLib?)

Je suis en train de convertir le code suivant à Python à partir de MATLAB pour un EEG Projet (en partie parce que Python est légèrement moins cher!)

J'espère que quelqu'un peut me pointer dans la bonne direction: j'ai commencé à le modifier, mais s'enlisait: en Particulier en essayant de trouver des fonctions équivalentes.

Essayé scipy.org (NumPy_for_Matlab_Users etc.) mais je ne sais pas si mes arguments sont de le droit format/nombre)

J'ai été à l'origine à l'aide de pyserial

ser.read()

Pour lire les données, puis

ord()

Pour la convertir en entier, mais ce code MATLAB y va d'une autre façon ('char')

Mes questions principales ont été avec

fopen
fread
find
repmat

Et l'ensemble du tracé de la section que j'ai encore moins d'une idée à ce sujet en Python (MatPlotLib?)

MATLAB a aussi tendance à commencer avec '1', tandis que Python utilise 0: j'ai essayé de modifier les présentes également, mais ont manqué un peu, je n'étais pas sûr de.

Python heureux avec l'ensemble de la plage, séparés par deux points

...repmat(0:2:10, .....

ou pas?

Donc, ici, c'est le MATLAB:

% EEG data grabber and plotter

N = 256;    % Required number of sample frames

% Read in a block of data from the OpenEEG board
hCom = serial('COM1','BaudRate',57600,'timeout',5);
fopen(hCom);
numBlocks = (ceil(17*N/256) + 1);
rawdata = zeros(numBlocks*256,1);
for n = 1:numBlocks
    rawdata((0:255) + n*256) = fread(hCom, 256, 'uchar');  % Read data
end
fclose(hCom);

% Convert raw data into a Matlab matrix
% First find the first frame start
startIndex = find(rawdata == 165);
while(rawdata(startIndex(1) + 1) ~= 90)
   startIndex = startIndex(2:end);
end
% Now extract the samples
frameStarts = (0:(N-1))'*17 + startIndex(1);
indices = 4 + repmat(frameStarts, 1, 6) + repmat(0:2:10, length(frameStarts), 1);
eegData = (rawdata(indices)*256 + rawdata(indices + 1)) - 512;
% eegData is now a N by 6 matrix, each column is a channel of sampled data

% Plot time-series data
figure(1)
subplot(2,1,1)
plot((0:255)/256,eegData(:,1:2))
xlabel('Time [s]');
ylabel('EEG data'); 
% Calculate FFT and plot spectra
subplot(2,1,2)
window = 0.5 - 0.5 * cos(2*pi*(0:255)/255); % Von-Hann Window
f = abs(fft(repmat(window',1,2) .* eegData(:,1:2)));
plot((0:127),f(1:128,:))
xlabel('Frequency [Hz]');
ylabel('EEG FFT');

Et voici mon cousin pauvre de la version

import scipy
import serial                       #Serial Module to read serial port
from numpy import ceil,zeros        #Ceil module & zeros for blank matrix
N = 256    #no of sample frames (256 = 1s)
#Reads a block of data from the serial port
ser = serial.Serial('COM18',57600,timeout=5)
scipy.fopen(ser)       #MATLAB CODE: fopen(ser)  is this correct????
numBlocks = (ceil(17*N/256) + 1)
rawdata = scipy.zeros(numBlocks*256,1)
for n = 1:numBlocks
rawdata((0:255) + n*256) = numpyio.fread(ser,256,'i')  # read each byte as     unsigned integer
end
ser.close()
#convert raw data to MATLAB matrix
#find start of frame (1st Byte always 165, 2nd always 90)
startIndex = find(rawdata == 165);
while (rawdata(startIndex(0) + 1) ~=90) #confirms 165,90 sequence
startIndex = startIndex(1:end) #uses rest of frame as data
end
#Extraction of sample values
#MATLAB CODE
frameStarts = (0: (N-1))'*17 + startIndex(1);      #'#how to transpose matrix('): zip()??
indices = 4 + (numpy.tile(frameStarts, 1,6)) + (numpy.tile(0:2:10,length(frameStarts), 1); 
eegData = (rawdata(indices)*256 + rawdata(indices +1)) - 512  #values are unsigned     integers 0-1023 and must subtract 512 for actual value
#eeg data now N*6 Matrix each column is a channel of data
#MATLAB CODE: plot time series data  (MatPlotLib?)
figure(1)
subplot (2,1,1)
plot((0:255)/256,eegData(:,1:2))
xlabel('Time [s]')
ylabel('EEG Voltage')
#fft
subplot(2,1,2)
window = 0.5 - 0.5*cos(2*pi*(0:255)/255);
f = abs(fft(repmat(window',1,2) .* eegData(:,1:2)))    '#repmat=tile()? matrix     transposition (')?
plot((0:127),f(1:128,:))
xlabel('Freq [Hz]')
ylabel('EEG FFT')

Toutes les suggestions reçues avec gratitude!

Dave!

Vous pouvez obtenir de plus amples traduire à Octave, même Scilab. Depuis votre Python connaissances de base, vous n'aurez probablement pas donner trop de laisser tomber et essayer quelque chose d'autre.
Marque - t Octave ont de série support de communication?

OriginalL'auteur Davie_boy | 2010-02-24