4
one sample or one block of data at a time, and then populate data blocks into a
larger data matrix of dimensions channels-by-samples. This data matrix may then
be processed under MATLAB using feature extraction and translation algorithms.
However, since MATLAB is a single-threaded application, collecting the
continuously streaming data and processing it in near-real time may be
challenging. Imagine acquiring samples of the EEG data stream and then
performing a CPU-intensive computation on those samples, e.g. overlapping fast
Fourier transforms (FFTs). During the time that MATLAB requires to compute
each FFT, new data arriving in the EEG stream from the acquisition system may
be ignored. To allow for full control of the timing of a processing pipeline in
MATLAB, incoming data must be buffered to avoid gaps whenever repeated
computation is taking place. While the low-level TCP/IP network stack of the
operating system will buffer the data for some time, the duration that the data
remains in the network stack buffer cannot be guaranteed.
Therefore, instead of having MATLAB itself read one sample at a time
from the data stream, another standalone application or thread should read the
incoming samples and copy them into a fixed-length or data-adaptive circular
buffer. MATLAB can then read new data from this buffer at any time appropriate,
e.g., after completion of each computation. Successively separating the buffering
of the data stream from computation on segments of that data stream depends on a
having fast interface between MATLAB and the buffering software, so that little
time is lost in copying data from the buffer into MATLAB memory. In the next
sections we present two MATLAB-centered solutions that use this approach:
FieldTrip and DataSuite. FieldTrip aims only to provide a usable interface to a
single online data stream, while DataSuite in addition allows synchronization of
dissimilar data streams, including streams output by online computational clients
running on the same or different machines on the network, plus integrated,
flexible, and if desired distributed stimulus control.
FieldTrip. The FieldTrip toolbox (R. Oostenveld,
www.ru.nl/neuroimaging/fieldtrip) for EEG/MEG analysis under MATLAB
provides an open-source implementation of a realtime data buffering scheme. The
FieldTrip buffer is implemented as a network transparent TCP server, which
allows the acquisition client to stream EEG data to it sample by sample or in small
blocks, while at the same time any data that is present in the buffer can be
retrieved and processed by another application. The buffer is implemented as a
multi-threaded application in C/C++, allowing multiple clients to connect
simultaneously to read/write data and event codes.
The FieldTrip buffer may be used more generally to communicate
between separate applications. One application program is responsible for data
acquisition, writing the data (and optionally also event codes) to the buffer.
Another application can connect to the server to read some of the data and event
codes (typically, the most recent), and may optionally also write new event codes
(e.g., as the output of a classification algorithm) into the same buffer. Source code
for the buffering can be integrated into any EEG/MEG acquisition or analysis
system, first writing the header information and describing the number of channels
and sampling frequency, then delivering the stream of data and/or event codes.