Jump to content

Contributions:BCI2000SimulinkConnector: Difference between revisions

From BCI2000 Wiki
Mellinger (talk | contribs)
mNo edit summary
 
(14 intermediate revisions by 2 users not shown)
Line 1: Line 1:
==Synopsis==
The BCI2000 Simulink connector allows data to be sent to Simulink in real-time, allowing data to be processed in your own custom Simulink model. Both BCI2000 signal and states can be received with Simulink!
 
[[File:Simulink_Library.PNG|200px|thumb|right|BCI2000 Simulink Component]]
 
==Video Overview==
<youtube >https://youtu.be/Geo3YhIOjX4</youtube>


==Location==
==Location==
Line 34: Line 39:


==Using the Minimal Example==
==Using the Minimal Example==
BCI2000 comes with a minimal example Simulink model, containing just the BCI2000OnlineLink block, and a terminator that absorbs the incoming signal.
BCI2000 comes with a minimal example Simulink model, containing just the BCI2000OnlineLink block with its two output ports, a muxer block, and a "to file" block that saves the output to a file.


To try the BCI2000 Simulink Connector, navigate to the BCI2000/prog/Simulink directory using the Matlab file explorer. Then, double-click the MinimalExample.slx Simulink model file. The BCI2000 signal has already been selected for viewing, so the test reduces to the following three steps:
To try the BCI2000 Simulink Connector, navigate to the BCI2000/prog/Simulink directory using the Matlab file explorer. Then, double-click the MinimalExample.slx Simulink model file. The test consists of the following steps:
* Double-click BCI2000/batch/SimulinkConnector.bat in your OSes file explorer. BCI2000 will start and display status information in the system log.
* Double-click BCI2000/batch/SimulinkConnector.bat in your OSes file explorer. BCI2000 will start and display status information in the system log.
* In the MinimalExample window, click the arrow in the green circle to run the model. By default, it will run for 10 seconds in real time, reading data from BCI2000.
* In the MinimalExample window, click the arrow in the green circle to run the model. It will run indefinitely, reading data from BCI2000, and recording it into a Matlab data file.
* Once the run has terminated, you may view the data log by clicking the data viewer icon right to the green arrow. You will need to expand the "BCI2000" signal, and select channels for viewing.
* To end the run, click the the black square ("stop symbol") right to the green circle.
* You may now inspect the contents of the recorded data file by typing <tt>data=load('bci2000.mat');</tt> into the Matlab prompt. The <tt>data.ans</tt> field consists of the following rows:
** 1: Time in seconds (one row),
** 2: BCI2000 signal data, upsampled to the original sampling rate if necessary (one row per BCI2000 channel),
** 3: BCI2000 state data in the order specified in the ''SimulinkConnectorStates'' parameter on the BCI2000 side (one row per BCI2000 state).


[[File:Simulink_MinimalExample.PNG]]
[[File:Simulink_MinimalExample.PNG]]
For states to be sent to Simulink, they must be specified as a parameter ''SimulinkConnectorStates''.


==Adding BCI2000 your own model==
==Adding BCI2000 your own model==
To add the BCI2000 block to your own model, open the BCI2000.mdl library in Matlab, and drag-and-drop the BCI2000 block into your model window. Then, connect its output signal as you see fit.
To add the BCI2000 block to your own model, open the BCI2000.mdl library in Matlab, and drag-and-drop the BCI2000 block into your model window. Then, connect its output signals as you see fit.
 
'''NOTE:''' If the target model is located in a different directory than BCI2000/prog/Simulink, you need to add BCI2000/prog/Simulink to the Matlab path in order to use the BCI2000 block.


[[File:Simulink_Library.PNG]]
[[File:Simulink_Library.PNG]]
Line 51: Line 64:
To change the port or the IP address for connecting to BCI2000, double-click the BCI2000 block in Simulink. Under "S-function parameters", you see a single entry, which
To change the port or the IP address for connecting to BCI2000, double-click the BCI2000 block in Simulink. Under "S-function parameters", you see a single entry, which
is by default 'localhost:1905' (take care of the single quotes—without them, Simulink will try to find a variable with name "localhost"). Modify as desired, and close the window.
is by default 'localhost:1905' (take care of the single quotes—without them, Simulink will try to find a variable with name "localhost"). Modify as desired, and close the window.
[[File:Simulink_BCI2000BlockParameters.PNG]]


On the BCI2000 side, the equivalent setting is stored in the "SimulinkConnectorAddress" parameter on the "Connector" tab:
On the BCI2000 side, the equivalent setting is stored in the "SimulinkConnectorAddress" parameter on the "Connector" tab:
[[File:Simulink_BCI2000Parameter.PNG]]
Note that connections between different machines are supported but data transport through TCP will be much less efficient than local transport, which takes place through shared memory.
Addresses will always be identical on both ends. When switching to a non-local connection, “localhost” will have to be replaced with the IP address of the machine running BCI2000 (e.g., ''192.168.1.15:1905'').
==Sending States==
State/Event information may be transferred by specifying State/Event names in the ''SimulinkConnectorStates'' parameter (see image above). States listed there will be transferred to the BCI2000 block's "State data" output port.
Depending on where the ''SimulinkConnector'' is located in the filter chain, its input signal may have more or fewer elements (samples) than the State Vector (which has as many samples as the source signal). In that case, the two output ports of the BCI2000 block will have different sampling rates. As in the "minimal example" above, you may use a muxer block to combine the two signals into a single one prior to further processing.


==See also==
==See also==
Line 58: Line 84:
[[Programming Reference:MatlabFilter]]
[[Programming Reference:MatlabFilter]]


[[Category:Contributions]][[Category:External Interfaces]]
[[Category:Contributions]][[Category:External Interfaces]][[Category:Video]]

Latest revision as of 15:58, 17 June 2025

The BCI2000 Simulink connector allows data to be sent to Simulink in real-time, allowing data to be processed in your own custom Simulink model. Both BCI2000 signal and states can be received with Simulink!

BCI2000 Simulink Component

Video Overview

Location

http://www.bci2000.org/svn/trunk/src/contrib/ExternalLinks/Simulink

Versioning

Author

mellinger@neurotechcenter.org

Source Code Revisions

  • Initial development: 7669
  • Tested under: 7669

Functional Description

The BCI2000 Simulink connector is a means to send signal data from BCI2000 into a Simulink model. It consists of a BCI2000 filter that listens for connections on a TCP port, and a Simulink S-function that connects to that port and reads data from it. As an optimization in case of a machine-local TCP connection, signal data is transmitted through a shared memory block instead of the TCP connection. Still, the TCP connection is necessary to synchronize arrival of new data on the BCI2000 side with consumption on the Simulink side.

Building the Simulink Connector

The BCI2000 Simulink connector consists of two projects: A BCI2000 SignalProcessing module, called SimulinkConnectorSignalProcessing, and a Simullink S-function mex file, called BCI2000OnlineLink.

Building the S-function mex file relies on a Matlab installtion being present, and thus requires enabling through a CMake switch.

In your BCI2000 build folder, execute Configure.sh.bat. Then, navigate to BUILD_CONTRIB and make sure it is checked. Likewise, navigate to BUILD_SIMULINK_MEX_FILES, and make sure it is checked. Then, click "Generate" in the CMake GUI to re-generate the BCI2000 project files (Makefile or Solution file).

In case you have multiple versions of Matlab installed, CMake will pick a random one. To control which Matlab version is used, set the Matlab_ROOT variable to the root directory of the desired version (i.e., the directory that has "Matlab R…" in its name).

Similarly, if you have the Matlab Compiler Runtime (MCR) installed, CMake may pick it but will fail to identify the Simulink include directory, which is not present in the MCR installation. If this happens, set the Matlab_ROOT variable to the root directory of a full Matlab installation, and re-generate project files.

Once built, Simulink related files are located in BCI2000/prog/Simulink, and the BCI2000/batch directory contains a batch file called SimulinkConnector.bat.

Using the Minimal Example

BCI2000 comes with a minimal example Simulink model, containing just the BCI2000OnlineLink block with its two output ports, a muxer block, and a "to file" block that saves the output to a file.

To try the BCI2000 Simulink Connector, navigate to the BCI2000/prog/Simulink directory using the Matlab file explorer. Then, double-click the MinimalExample.slx Simulink model file. The test consists of the following steps:

  • Double-click BCI2000/batch/SimulinkConnector.bat in your OSes file explorer. BCI2000 will start and display status information in the system log.
  • In the MinimalExample window, click the arrow in the green circle to run the model. It will run indefinitely, reading data from BCI2000, and recording it into a Matlab data file.
  • To end the run, click the the black square ("stop symbol") right to the green circle.
  • You may now inspect the contents of the recorded data file by typing data=load('bci2000.mat'); into the Matlab prompt. The data.ans field consists of the following rows:
    • 1: Time in seconds (one row),
    • 2: BCI2000 signal data, upsampled to the original sampling rate if necessary (one row per BCI2000 channel),
    • 3: BCI2000 state data in the order specified in the SimulinkConnectorStates parameter on the BCI2000 side (one row per BCI2000 state).

For states to be sent to Simulink, they must be specified as a parameter SimulinkConnectorStates.

Adding BCI2000 your own model

To add the BCI2000 block to your own model, open the BCI2000.mdl library in Matlab, and drag-and-drop the BCI2000 block into your model window. Then, connect its output signals as you see fit.

NOTE: If the target model is located in a different directory than BCI2000/prog/Simulink, you need to add BCI2000/prog/Simulink to the Matlab path in order to use the BCI2000 block.

Changing the IP:Port setting

To change the port or the IP address for connecting to BCI2000, double-click the BCI2000 block in Simulink. Under "S-function parameters", you see a single entry, which is by default 'localhost:1905' (take care of the single quotes—without them, Simulink will try to find a variable with name "localhost"). Modify as desired, and close the window.

On the BCI2000 side, the equivalent setting is stored in the "SimulinkConnectorAddress" parameter on the "Connector" tab:

Note that connections between different machines are supported but data transport through TCP will be much less efficient than local transport, which takes place through shared memory.

Addresses will always be identical on both ends. When switching to a non-local connection, “localhost” will have to be replaced with the IP address of the machine running BCI2000 (e.g., 192.168.1.15:1905).

Sending States

State/Event information may be transferred by specifying State/Event names in the SimulinkConnectorStates parameter (see image above). States listed there will be transferred to the BCI2000 block's "State data" output port.

Depending on where the SimulinkConnector is located in the filter chain, its input signal may have more or fewer elements (samples) than the State Vector (which has as many samples as the source signal). In that case, the two output ports of the BCI2000 block will have different sampling rates. As in the "minimal example" above, you may use a muxer block to combine the two signals into a single one prior to further processing.

See also

Contributions:BCI2000PresentationLink, Programming Reference:SignalSharingDemo Signal Processing, Programming Reference:MatlabFilter