Programming Tutorial:Working with the FieldTrip buffer

From BCI2000 Wiki
Jump to navigation Jump to search

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 use the FieldTrip buffer, you start BCI2000 with the FieldTripBuffer as the Signal Processing application. Subsequently you start MATLAB yourself, i.e. your MATLAB session is a normal standalone application. You should have a recent copy of the FieldTrip toolbox installed, or at least a copy of the FieldTrip fileio module. The FieldTrip toolbox and its components are available for download from http://www.ru.nl/neuroimaging/fieldtrip. Please make sure that the correct version of the fileio module is on your MATLAB search path.

Subsequently you can do something like this
filename = 'buffer://localhost:1972';

% 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);

    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    % subsequently the data can be processed, here it is only plotted
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

    % 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

To close the loop, you have to write teh control signal back to BCI2000. Since the FieldTrip buffer can only hold raw data, the control signal cannot be written as data. Instead, the control signal is written as an event. This is easily demonstrated if you run the Feedback Demo Task.

event_up.type  = 'Signal';
event_up.value = 1;
event_down.type  = 'Signal';
event_down.value = -1;
event_null.type  = 'Signal';
event_null.value = 0;

You can write the events to the buffer according to the following example code:

filename = 'buffer://localhost:1972';

write_event(filename, event_up);

% ... the cursor will move up ...

write_event(filename, event_down);

% ... the cursor will move down ...

write_event(filename, event_null);

% ... the cursor will stay at a constant value ...

The control signal in BCI2000 remains at a constant value as long as you don't write another event with another control signal.

See also

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