Jump to content

Programming Tutorial:Working with the FieldTrip buffer: Difference between revisions

From BCI2000 Wiki
No edit summary
Line 7: Line 7:
<pre>
<pre>
% read the header for the first time to determine number of channels and sampling rate
% read the header for the first time to determine number of channels and sampling rate
hdr = read_header(cfg.headerfile, 'cache', true);
hdr = read_header(filename, 'cache', true);


count     = 0;
count     = 0;
blocksize = hdr.Fs;
prevSample = 0
chanindx = 1:hdr.nChans;
blocksize = hdr.Fs;
chanindx   = 1:hdr.nChans;


while true
while true
   % determine number of samples available in buffer
   % determine number of samples available in buffer
   hdr = read_header(cfg.headerfile, 'cache', true);
   hdr = read_header(filename, 'cache', true);


   % see whether new samples are available
   % see whether new samples are available
Line 32: Line 33:


     % read data segment from buffer
     % read data segment from buffer
     dat = read_data(cfg.datafile, 'header', hdr, 'begsample', begsample, 'endsample', endsample, 'chanindx', chanindx);
     dat = read_data(filename, 'header', hdr, 'begsample', begsample, 'endsample', endsample, 'chanindx', chanindx);


     % create a matching time-axis
     % create a matching time-axis
Line 45: Line 46:
     % force Matlab to update the figure
     % force Matlab to update the figure
     drawnow
     drawnow
end


  end % if new samples available
end % while true
</pre>
</pre>


==Closing the loop, writing a control signal from Matlab to BCI2000==
==Closing the loop, writing a control signal from Matlab to BCI2000==

Revision as of 14:38, 2 December 2008

Getting the data in Matlab

The FieldTrip buffer is a multi-threaded and network transparent buffer that allows data to be streamed to it by BCI2000, while at the same time allowing a seperate MATLAB session on the same or another computer to read data from the buffer for analysis. Besides writing the data, BCI2000 also writes the changed status variables as events.

To read from the FieldTrip buffer, you should have a copy of the FieldTrip toolbox, or at least a copy of the FieldTrip fileio module. Subsequently you can do something like this

% read the header for the first time to determine number of channels and sampling rate
hdr = read_header(filename, 'cache', true);

count      = 0;
prevSample = 0
blocksize  = hdr.Fs;
chanindx   = 1:hdr.nChans;

while true
  % determine number of samples available in buffer
  hdr = read_header(filename, 'cache', true);

  % see whether new samples are available
  newsamples = (hdr.nSamples*hdr.nTrials-prevSample);

  if newsamples>=blocksize

    % determine the samples to process
    begsample  = prevSample+1;
    endsample  = prevSample+blocksize ;

    % remember up to where the data was read
    prevSample  = endsample;
    count       = count + 1;
    fprintf('processing segment %d from sample %d to %d\n', count, begsample, endsample);

    % read data segment from buffer
    dat = read_data(filename, 'header', hdr, 'begsample', begsample, 'endsample', endsample, 'chanindx', chanindx);

    % create a matching time-axis
    time = (begsample:endsample)/hdr.Fs;

    % plot the data just like a standard FieldTrip raw data strucute
    plot(time, dat);

    % ensure tight axes
    xlim([time(1) time(end)]);

    % force Matlab to update the figure
    drawnow

  end % if new samples available
end % while true

Closing the loop, writing a control signal from Matlab to BCI2000

See also

Contributions:FieldTripBuffer, Programming Reference:MatlabFilter, User Reference:MatlabFilter