OpenBCI Lab Streaming Layer (LSL) – From Python to Matlab

Lab streaming layer is a system for synchronizing streaming data for real-time streaming, recording, and analysis of biodata. Theopenbci_pylsl program uses Python to establish an LSL stream that can be received using scripts in Matlab. Follow the steps below to learn how to setup and begin using Matlab for real-time data analysis.

This post will show you how straightforward is to set up the new lab streaming layer from Python to Matlab by following the detailed instructions explained in the OpenBCI repository file. Moreover, and then explain the various ways you can use LSL to perform experiments or record data.

OpenBCI LSL Setup

Before you follow the setup instructions from the “Read me” file, you must have previously downloaded the following programs and files:

  • Matlab
  • Python (either version 2.7 or 3.5)
  • OpenBCI_MATLAB files from GitHub.

For further information, please go to the OpenBCI_MATLAB repository on GitHub, which explains step by step the commands that you need to follow to obtain an OpenBCI data stream directly into Matlab. Moreover, it explains various ways you can use LSL to perform experiments or record data.

Usage

Getting a stream into Matlab can be done in two steps:

  1. Start a stream with the openbci_pylsl.py program
  2. Receive the stream with an LSL script in Matlab

Start streaming in Python

Once you have plugged in your dongle and powered up your OpenBCI Board, you can freely navigate to the Matlab-Python directory on your command line, and enter the following command:

python openbci_pylsl.py -p "PORT"
cmd2.png
Figure 1. Board initialization parameters after typing in the command line.

Some board parameters and information related to the serial port will appear on the command line. After the board initialization, you are ready to start streaming! Isn’t it that straightforward?

The commands to control the streaming are also pretty intuitive:

  • To begin streaming type /start
  • To stop streaming type /start
  • To disconnect from the serial port type /start

Receive the stream in MATLAB

The data stream from you board should now be pushed into the lab streaming layer. The next step is to receive this data in Matlab by running the “ReceiveData” script.

stream.gif
Figure 2. The EEG Data is saved from the first to the eighth column, whereas the time column is saved in the last column.

Congratulations! If you are using eight channels and see nine columns plenty of numbers in your Command Window you better feel lucky. You received the streaming from Python in Matlab! The EEG Data appears from the first to the eighth column, whereas the last column displays the time in milliseconds. If you want a use the auxiliary data from the OpenBCI board you can also change the code and save it in a Matlab variable.

Monitoring in Matlab

Wait! You might want to monitor your stream in Matlab before you deploy any scripts in order to make sure that the biodata is correctly streaming into Matlab, right? OpenBCI LSL files include the vis_stream function to bring up the Matlab Visualizer Toolbox written by Christian Kothe at UCSD.

eeg3.gif
Figure 3. A window will pop-up asking for information about the stream.

Voilà! Here you have your brain waves! Now you can work with them. Besides, you can change the scale with the up-down arrows on your keyboard.

eeg2
Figure 4.

If you want to stop streaming or disconnect from the serial port just come back to the command line in Python and type /stop function or  /exit, respectively.

Potential uses of LSL

Run experiments

You should experiment with different methods of working with the output ofinlet.pull_sample(). You can add to that while loop to call a variety of different scripts while you collect data. You can also experiment with parallel computing in Matlab to allow the lab streaming layer to run in the background while running other scripts.

Signal Processing

You can save the streaming data in a vector and work with the EEG data directly in Matlab without having to use Processing.

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s