<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://www.bci2000.org/mediawiki/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=Roostenveld</id>
	<title>BCI2000 Wiki - User contributions [en]</title>
	<link rel="self" type="application/atom+xml" href="https://www.bci2000.org/mediawiki/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=Roostenveld"/>
	<link rel="alternate" type="text/html" href="https://www.bci2000.org/mediawiki/index.php/Special:Contributions/Roostenveld"/>
	<updated>2026-06-29T22:27:53Z</updated>
	<subtitle>User contributions</subtitle>
	<generator>MediaWiki 1.43.6</generator>
	<entry>
		<id>https://www.bci2000.org/mediawiki/index.php?title=User:Roostenveld&amp;diff=8289</id>
		<title>User:Roostenveld</title>
		<link rel="alternate" type="text/html" href="https://www.bci2000.org/mediawiki/index.php?title=User:Roostenveld&amp;diff=8289"/>
		<updated>2019-06-25T15:07:14Z</updated>

		<summary type="html">&lt;p&gt;Roostenveld: updated URL, capitalize MATLAB&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Robert Oostenveld is the main developer of the [http://www.fieldtriptoolbox.org FieldTrip] software, an open source MATLAB toolbox for (mainly offline) EEG and MEG analysis. He has contributed the [[Contributions:FieldTripBuffer|FieldTripBuffer]] to the BCI2000 project and contributes to the documentation of using BCI2000 together with MATLAB for offline and online scenarios.&lt;/div&gt;</summary>
		<author><name>Roostenveld</name></author>
	</entry>
	<entry>
		<id>https://www.bci2000.org/mediawiki/index.php?title=Contributions:FieldTripBuffer&amp;diff=4752</id>
		<title>Contributions:FieldTripBuffer</title>
		<link rel="alternate" type="text/html" href="https://www.bci2000.org/mediawiki/index.php?title=Contributions:FieldTripBuffer&amp;diff=4752"/>
		<updated>2008-12-02T15:57:03Z</updated>

		<summary type="html">&lt;p&gt;Roostenveld: /* Synopsis */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Synopsis==&lt;br /&gt;
The &#039;&#039;FieldTripBuffer&#039;&#039; filter is an interface between BCI2000 and the FieldTrip Toolbox. The filter hosts a [http://neuroimaging.ruhosting.nl/fieldtrip/doku.php?id=fieldtrip:development:realtime FieldTrip realtime buffer], and writes its input into that buffer, formatted as &#039;&#039;buffer data&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
A separate MATLAB session on either the same or another computer  can then use the reading and writing functions from the [http://neuroimaging.ruhosting.nl/fieldtrip FieldTrip toolbox] to communicate with the buffer asynchronously, reading data from BCI2000, and writing back results into BCI2000.&lt;br /&gt;
&lt;br /&gt;
BCI2000 [[BCI2000 Glossary#State Variable|state variables]] may be translated into FieldTrip buffer events; in turn, buffer events are read back from the buffer into BCI2000 state variables. A dedicated FieldTrip buffer event type may be used to determine the filter&#039;s output into the BCI2000 processing pipe.&lt;br /&gt;
&lt;br /&gt;
==Location==&lt;br /&gt;
http://{{SERVERNAME}}/svn/trunk/src/contrib/SignalProcessing/FieldTripBuffer&lt;br /&gt;
&lt;br /&gt;
==Versioning==&lt;br /&gt;
===Authors===&lt;br /&gt;
Jürgen Mellinger, Robert Oostenveld&lt;br /&gt;
===Source Code Revisions===&lt;br /&gt;
*Initial development: 2044&lt;br /&gt;
*Tested under: 2094&lt;br /&gt;
*Known to compile under: 2095&lt;br /&gt;
&lt;br /&gt;
==Parameters==&lt;br /&gt;
===FTBufferAddress===&lt;br /&gt;
The FieldTrip buffer&#039;s listening address, given as an IP:port combination. Software agents connect to this address using a TCP-based protocol.&lt;br /&gt;
&lt;br /&gt;
===FTInitialOutput===&lt;br /&gt;
A matrix-valued parameter that determines dimensions as well as initial values of the filter&#039;s output signal.&lt;br /&gt;
In the matrix, rows map to signal channels, and columns map to elements.&lt;br /&gt;
&lt;br /&gt;
===FTOutputEventType===&lt;br /&gt;
A string representing the FieldTrip buffer event type that corresponds to the filter&#039;s output signal.&lt;br /&gt;
For each block of data, the &#039;&#039;FieldTripBuffer&#039;&#039; filter checks whether an event of that type has been inserted into the buffer; whenever this is the case, the filter&#039;s output signal will be updated from the values contained in the event. If the event&#039;s data dimensions do not fit the output signal, parts of the output signal will not be updated, or event data will be ignored silently.&lt;br /&gt;
&lt;br /&gt;
===FTStatesToBuffer===&lt;br /&gt;
A list of states that are to be converted into events, and written into the buffer. The buffer event type matches the BCI2000 state name.&lt;br /&gt;
An asterisk (*) represents any state name; a minus (-) sign before a state name means to exclude the respective state from the transmission.&lt;br /&gt;
States may either be written to, or read from, the buffer, but not both; states listed in the &#039;&#039;FTStatesFromBuffer&#039;&#039; parameter are always excluded from writing.&lt;br /&gt;
&lt;br /&gt;
===FTStatesFromBuffer===&lt;br /&gt;
A list of states that are to be set from events. When no event of matching type has been posted to the buffer, the state will not change; when multiple events have been posted since the FieldTrip buffer processed the last block of data, then the most recent event determines the state&#039;s value.&lt;br /&gt;
&lt;br /&gt;
==States==&lt;br /&gt;
Any state name may be specified in the &#039;&#039;FTStatesToBuffer&#039;&#039; and &#039;&#039;FTStatesFromBuffer&#039;&#039; parameters.&lt;br /&gt;
&lt;br /&gt;
==See also==&lt;br /&gt;
[[User Reference:MatlabFilter]], [[Programming Reference:MatlabFilter]]&lt;br /&gt;
&lt;br /&gt;
An example is given in the [[Programming Tutorial:Working with the FieldTrip buffer]]&lt;br /&gt;
&lt;br /&gt;
[[Category:Filters]]&lt;br /&gt;
[[Category:Signal Processing]]&lt;br /&gt;
[[Category:Matlab]]&lt;br /&gt;
[[Category:Development]]&lt;br /&gt;
[[Category:External Interfaces]]&lt;/div&gt;</summary>
		<author><name>Roostenveld</name></author>
	</entry>
	<entry>
		<id>https://www.bci2000.org/mediawiki/index.php?title=Contributions:FieldTripBuffer&amp;diff=4751</id>
		<title>Contributions:FieldTripBuffer</title>
		<link rel="alternate" type="text/html" href="https://www.bci2000.org/mediawiki/index.php?title=Contributions:FieldTripBuffer&amp;diff=4751"/>
		<updated>2008-12-02T15:56:10Z</updated>

		<summary type="html">&lt;p&gt;Roostenveld: /* Synopsis */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Synopsis==&lt;br /&gt;
The &#039;&#039;FieldTripBuffer&#039;&#039; filter is an interface between BCI2000 and the FieldTrip Toolbox. The filter hosts a [http://neuroimaging.ruhosting.nl/fieldtrip/doku.php?id=fieldtrip:development:realtime FieldTrip realtime buffer], and writes its input into that buffer, formatted as &#039;&#039;buffer data&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
A separate MATLAB session on either the same or another computer  can then use the reading and writing functions from the FieldTrip toolbox to communicate with the buffer asynchronously, reading data from BCI2000, and writing back results into BCI2000.&lt;br /&gt;
&lt;br /&gt;
BCI2000 [[BCI2000 Glossary#State Variable|state variables]] may be translated into FieldTrip buffer events; in turn, buffer events are read back from the buffer into BCI2000 state variables. A dedicated FieldTrip buffer event type may be used to determine the filter&#039;s output into the BCI2000 processing pipe.&lt;br /&gt;
&lt;br /&gt;
==Location==&lt;br /&gt;
http://{{SERVERNAME}}/svn/trunk/src/contrib/SignalProcessing/FieldTripBuffer&lt;br /&gt;
&lt;br /&gt;
==Versioning==&lt;br /&gt;
===Authors===&lt;br /&gt;
Jürgen Mellinger, Robert Oostenveld&lt;br /&gt;
===Source Code Revisions===&lt;br /&gt;
*Initial development: 2044&lt;br /&gt;
*Tested under: 2094&lt;br /&gt;
*Known to compile under: 2095&lt;br /&gt;
&lt;br /&gt;
==Parameters==&lt;br /&gt;
===FTBufferAddress===&lt;br /&gt;
The FieldTrip buffer&#039;s listening address, given as an IP:port combination. Software agents connect to this address using a TCP-based protocol.&lt;br /&gt;
&lt;br /&gt;
===FTInitialOutput===&lt;br /&gt;
A matrix-valued parameter that determines dimensions as well as initial values of the filter&#039;s output signal.&lt;br /&gt;
In the matrix, rows map to signal channels, and columns map to elements.&lt;br /&gt;
&lt;br /&gt;
===FTOutputEventType===&lt;br /&gt;
A string representing the FieldTrip buffer event type that corresponds to the filter&#039;s output signal.&lt;br /&gt;
For each block of data, the &#039;&#039;FieldTripBuffer&#039;&#039; filter checks whether an event of that type has been inserted into the buffer; whenever this is the case, the filter&#039;s output signal will be updated from the values contained in the event. If the event&#039;s data dimensions do not fit the output signal, parts of the output signal will not be updated, or event data will be ignored silently.&lt;br /&gt;
&lt;br /&gt;
===FTStatesToBuffer===&lt;br /&gt;
A list of states that are to be converted into events, and written into the buffer. The buffer event type matches the BCI2000 state name.&lt;br /&gt;
An asterisk (*) represents any state name; a minus (-) sign before a state name means to exclude the respective state from the transmission.&lt;br /&gt;
States may either be written to, or read from, the buffer, but not both; states listed in the &#039;&#039;FTStatesFromBuffer&#039;&#039; parameter are always excluded from writing.&lt;br /&gt;
&lt;br /&gt;
===FTStatesFromBuffer===&lt;br /&gt;
A list of states that are to be set from events. When no event of matching type has been posted to the buffer, the state will not change; when multiple events have been posted since the FieldTrip buffer processed the last block of data, then the most recent event determines the state&#039;s value.&lt;br /&gt;
&lt;br /&gt;
==States==&lt;br /&gt;
Any state name may be specified in the &#039;&#039;FTStatesToBuffer&#039;&#039; and &#039;&#039;FTStatesFromBuffer&#039;&#039; parameters.&lt;br /&gt;
&lt;br /&gt;
==See also==&lt;br /&gt;
[[User Reference:MatlabFilter]], [[Programming Reference:MatlabFilter]]&lt;br /&gt;
&lt;br /&gt;
An example is given in the [[Programming Tutorial:Working with the FieldTrip buffer]]&lt;br /&gt;
&lt;br /&gt;
[[Category:Filters]]&lt;br /&gt;
[[Category:Signal Processing]]&lt;br /&gt;
[[Category:Matlab]]&lt;br /&gt;
[[Category:Development]]&lt;br /&gt;
[[Category:External Interfaces]]&lt;/div&gt;</summary>
		<author><name>Roostenveld</name></author>
	</entry>
	<entry>
		<id>https://www.bci2000.org/mediawiki/index.php?title=Contributions:FieldTripBuffer&amp;diff=4750</id>
		<title>Contributions:FieldTripBuffer</title>
		<link rel="alternate" type="text/html" href="https://www.bci2000.org/mediawiki/index.php?title=Contributions:FieldTripBuffer&amp;diff=4750"/>
		<updated>2008-12-02T15:53:47Z</updated>

		<summary type="html">&lt;p&gt;Roostenveld: /* See also */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Synopsis==&lt;br /&gt;
The &#039;&#039;FieldTripBuffer&#039;&#039; filter is an interface between BCI2000 and the FieldTrip Toolbox. The filter hosts a [http://neuroimaging.ruhosting.nl/fieldtrip/doku.php?id=fieldtrip:development:realtime FieldTrip realtime buffer], and writes its input into that buffer, formatted as &#039;&#039;buffer data&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
FieldTrip software agents may then use the FieldTrip realtime protocol to communicate with the buffer asynchronously, reading data from BCI2000, and writing back results into BCI2000.&lt;br /&gt;
&lt;br /&gt;
BCI2000 [[BCI2000 Glossary#State Variable|state variables]] may be translated into FieldTrip buffer events; in turn, buffer events are read back from the buffer into BCI2000 state variables. A dedicated FieldTrip buffer event type may be used to determine the filter&#039;s output into the BCI2000 processing pipe.&lt;br /&gt;
&lt;br /&gt;
==Location==&lt;br /&gt;
http://{{SERVERNAME}}/svn/trunk/src/contrib/SignalProcessing/FieldTripBuffer&lt;br /&gt;
&lt;br /&gt;
==Versioning==&lt;br /&gt;
===Authors===&lt;br /&gt;
Jürgen Mellinger, Robert Oostenveld&lt;br /&gt;
===Source Code Revisions===&lt;br /&gt;
*Initial development: 2044&lt;br /&gt;
*Tested under: 2094&lt;br /&gt;
*Known to compile under: 2095&lt;br /&gt;
&lt;br /&gt;
==Parameters==&lt;br /&gt;
===FTBufferAddress===&lt;br /&gt;
The FieldTrip buffer&#039;s listening address, given as an IP:port combination. Software agents connect to this address using a TCP-based protocol.&lt;br /&gt;
&lt;br /&gt;
===FTInitialOutput===&lt;br /&gt;
A matrix-valued parameter that determines dimensions as well as initial values of the filter&#039;s output signal.&lt;br /&gt;
In the matrix, rows map to signal channels, and columns map to elements.&lt;br /&gt;
&lt;br /&gt;
===FTOutputEventType===&lt;br /&gt;
A string representing the FieldTrip buffer event type that corresponds to the filter&#039;s output signal.&lt;br /&gt;
For each block of data, the &#039;&#039;FieldTripBuffer&#039;&#039; filter checks whether an event of that type has been inserted into the buffer; whenever this is the case, the filter&#039;s output signal will be updated from the values contained in the event. If the event&#039;s data dimensions do not fit the output signal, parts of the output signal will not be updated, or event data will be ignored silently.&lt;br /&gt;
&lt;br /&gt;
===FTStatesToBuffer===&lt;br /&gt;
A list of states that are to be converted into events, and written into the buffer. The buffer event type matches the BCI2000 state name.&lt;br /&gt;
An asterisk (*) represents any state name; a minus (-) sign before a state name means to exclude the respective state from the transmission.&lt;br /&gt;
States may either be written to, or read from, the buffer, but not both; states listed in the &#039;&#039;FTStatesFromBuffer&#039;&#039; parameter are always excluded from writing.&lt;br /&gt;
&lt;br /&gt;
===FTStatesFromBuffer===&lt;br /&gt;
A list of states that are to be set from events. When no event of matching type has been posted to the buffer, the state will not change; when multiple events have been posted since the FieldTrip buffer processed the last block of data, then the most recent event determines the state&#039;s value.&lt;br /&gt;
&lt;br /&gt;
==States==&lt;br /&gt;
Any state name may be specified in the &#039;&#039;FTStatesToBuffer&#039;&#039; and &#039;&#039;FTStatesFromBuffer&#039;&#039; parameters.&lt;br /&gt;
&lt;br /&gt;
==See also==&lt;br /&gt;
[[User Reference:MatlabFilter]], [[Programming Reference:MatlabFilter]]&lt;br /&gt;
&lt;br /&gt;
An example is given in the [[Programming Tutorial:Working with the FieldTrip buffer]]&lt;br /&gt;
&lt;br /&gt;
[[Category:Filters]]&lt;br /&gt;
[[Category:Signal Processing]]&lt;br /&gt;
[[Category:Matlab]]&lt;br /&gt;
[[Category:Development]]&lt;br /&gt;
[[Category:External Interfaces]]&lt;/div&gt;</summary>
		<author><name>Roostenveld</name></author>
	</entry>
	<entry>
		<id>https://www.bci2000.org/mediawiki/index.php?title=Programming_Tutorial:Working_with_the_FieldTrip_buffer&amp;diff=4749</id>
		<title>Programming Tutorial:Working with the FieldTrip buffer</title>
		<link rel="alternate" type="text/html" href="https://www.bci2000.org/mediawiki/index.php?title=Programming_Tutorial:Working_with_the_FieldTrip_buffer&amp;diff=4749"/>
		<updated>2008-12-02T15:52:07Z</updated>

		<summary type="html">&lt;p&gt;Roostenveld: /* Getting the data in Matlab */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;One of the user-contributed Signal Processing filters is the &amp;lt;em&amp;gt;FieldTripBuffer&amp;lt;/em&amp;gt;. The  FieldTripBuffer allows you to use BCI2000 in conjunction with MATLAB, just as with the &amp;lt;em&amp;gt;MatlabFilter&amp;lt;/em&amp;gt;. The difference between the two interfaces to MATLAB is that the MatlabFilter causes some MATLAB code to be executed in the BCI2000 pipeline, i.e. BCI2000 stays in control over the timing and the MATLAB session only sees a small fragment of the data. That also forces you to write your MATLAB code to use recursive/incremental updating schemes. The FieldTripBuffer interface to MATLAB allows you to control the timing from within MATLAB and read arbitrary sections of data from the ongoing data stream as if it were a continuously growing file, giving more flexibility in the MATLAB code that you can employ. &lt;br /&gt;
&lt;br /&gt;
The advantage of the FieldTripBuffer interface is that you have all control in MATLAB that you are used to. You can write your MATLAB code for offline-analysis (i.e. reading data from a file) and apply exactly the same code to online analysis (i.e. reading from BCI2000). Of course for the online analysis to make some sense, your analysis script has to be meaningful and has to work with relatively small data fragments (e.g. one second or less), otherwise the MATLAB code would not really run in real-time. Another interesting feature is that in MATLAB you can use the profiler (type &amp;quot;help profile&amp;quot;) to determine which parts of your code take a long time to execute and speed those parts up. &lt;br /&gt;
&lt;br /&gt;
The remainder of this page gives an example of how to get the data into MATLAB, plot the data using standard MATLAB code, and how to close the BCI loop by writing an event back to BCI2000. The example below does not do any useful processing, it is up to you to decide how you want to process the data in MATLAB. A number of realtime applications are included in the realtime module of the FieldTrip toolbox. Additional documentation for that can be found on  the FieldTrip website, under &amp;lt;em&amp;gt;development-&amp;gt;realtime&amp;lt;/em&amp;gt;. &lt;br /&gt;
&lt;br /&gt;
==Getting the data in Matlab==&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
Subsequently you can do something like the following code belos. You should be able to copy and paste the code into the MATLAB command window and get a real-time updating MATLAB figure with the data from BCI2000.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
filename = &#039;buffer://localhost:1972&#039;;&lt;br /&gt;
&lt;br /&gt;
% read the header for the first time to determine number of channels and sampling rate&lt;br /&gt;
hdr = read_header(filename, &#039;cache&#039;, true);&lt;br /&gt;
&lt;br /&gt;
count      = 0;&lt;br /&gt;
prevSample = 0&lt;br /&gt;
blocksize  = hdr.Fs;&lt;br /&gt;
chanindx   = 1:hdr.nChans;&lt;br /&gt;
&lt;br /&gt;
while true&lt;br /&gt;
  % determine number of samples available in buffer&lt;br /&gt;
  hdr = read_header(filename, &#039;cache&#039;, true);&lt;br /&gt;
&lt;br /&gt;
  % see whether new samples are available&lt;br /&gt;
  newsamples = (hdr.nSamples*hdr.nTrials-prevSample);&lt;br /&gt;
&lt;br /&gt;
  if newsamples&amp;gt;=blocksize&lt;br /&gt;
&lt;br /&gt;
    % determine the samples to process&lt;br /&gt;
    begsample  = prevSample+1;&lt;br /&gt;
    endsample  = prevSample+blocksize ;&lt;br /&gt;
&lt;br /&gt;
    % remember up to where the data was read&lt;br /&gt;
    prevSample  = endsample;&lt;br /&gt;
    count       = count + 1;&lt;br /&gt;
    fprintf(&#039;processing segment %d from sample %d to %d\n&#039;, count, begsample, endsample);&lt;br /&gt;
&lt;br /&gt;
    % read data segment from buffer&lt;br /&gt;
    dat = read_data(filename, &#039;header&#039;, hdr, &#039;begsample&#039;, begsample, &#039;endsample&#039;, endsample, &#039;chanindx&#039;, chanindx);&lt;br /&gt;
&lt;br /&gt;
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%&lt;br /&gt;
    % subsequently the data can be processed, here it is only plotted&lt;br /&gt;
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%&lt;br /&gt;
&lt;br /&gt;
    % create a matching time-axis&lt;br /&gt;
    time = (begsample:endsample)/hdr.Fs;&lt;br /&gt;
&lt;br /&gt;
    % plot the data just like a standard FieldTrip raw data strucute&lt;br /&gt;
    plot(time, dat);&lt;br /&gt;
&lt;br /&gt;
    % ensure tight axes&lt;br /&gt;
    xlim([time(1) time(end)]);&lt;br /&gt;
&lt;br /&gt;
    % force Matlab to update the figure&lt;br /&gt;
    drawnow&lt;br /&gt;
&lt;br /&gt;
  end % if new samples available&lt;br /&gt;
end % while true&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Closing the loop, writing a control signal from Matlab to BCI2000==&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
event_up.type  = &#039;Signal&#039;;&lt;br /&gt;
event_up.value = 1;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
event_down.type  = &#039;Signal&#039;;&lt;br /&gt;
event_down.value = -1;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
event_null.type  = &#039;Signal&#039;;&lt;br /&gt;
event_null.value = 0;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can write the events to the buffer according to the following example code:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
filename = &#039;buffer://localhost:1972&#039;;&lt;br /&gt;
&lt;br /&gt;
write_event(filename, event_up);    % ... the cursor will move up ...&lt;br /&gt;
&lt;br /&gt;
write_event(filename, event_down);  % ... the cursor will move down ...&lt;br /&gt;
&lt;br /&gt;
write_event(filename, event_null);  % ... the cursor will stay at a constant value ...&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The control signal in BCI2000 remains at a constant value as long as you don&#039;t write another event with another control signal.&lt;br /&gt;
&lt;br /&gt;
==See also==&lt;br /&gt;
&lt;br /&gt;
[[Contributions:FieldTripBuffer]],&lt;br /&gt;
[[Programming Reference:MatlabFilter]], &lt;br /&gt;
[[User Reference:MatlabFilter]] &lt;br /&gt;
&lt;br /&gt;
[[Category:Tutorial]]&lt;br /&gt;
[[Category:Signal Processing]]&lt;br /&gt;
[[Category:Development]]&lt;br /&gt;
[[Category:Matlab]]&lt;br /&gt;
[[Category:External Interfaces]]&lt;/div&gt;</summary>
		<author><name>Roostenveld</name></author>
	</entry>
	<entry>
		<id>https://www.bci2000.org/mediawiki/index.php?title=Programming_Tutorial:Working_with_the_FieldTrip_buffer&amp;diff=4748</id>
		<title>Programming Tutorial:Working with the FieldTrip buffer</title>
		<link rel="alternate" type="text/html" href="https://www.bci2000.org/mediawiki/index.php?title=Programming_Tutorial:Working_with_the_FieldTrip_buffer&amp;diff=4748"/>
		<updated>2008-12-02T15:50:41Z</updated>

		<summary type="html">&lt;p&gt;Roostenveld: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;One of the user-contributed Signal Processing filters is the &amp;lt;em&amp;gt;FieldTripBuffer&amp;lt;/em&amp;gt;. The  FieldTripBuffer allows you to use BCI2000 in conjunction with MATLAB, just as with the &amp;lt;em&amp;gt;MatlabFilter&amp;lt;/em&amp;gt;. The difference between the two interfaces to MATLAB is that the MatlabFilter causes some MATLAB code to be executed in the BCI2000 pipeline, i.e. BCI2000 stays in control over the timing and the MATLAB session only sees a small fragment of the data. That also forces you to write your MATLAB code to use recursive/incremental updating schemes. The FieldTripBuffer interface to MATLAB allows you to control the timing from within MATLAB and read arbitrary sections of data from the ongoing data stream as if it were a continuously growing file, giving more flexibility in the MATLAB code that you can employ. &lt;br /&gt;
&lt;br /&gt;
The advantage of the FieldTripBuffer interface is that you have all control in MATLAB that you are used to. You can write your MATLAB code for offline-analysis (i.e. reading data from a file) and apply exactly the same code to online analysis (i.e. reading from BCI2000). Of course for the online analysis to make some sense, your analysis script has to be meaningful and has to work with relatively small data fragments (e.g. one second or less), otherwise the MATLAB code would not really run in real-time. Another interesting feature is that in MATLAB you can use the profiler (type &amp;quot;help profile&amp;quot;) to determine which parts of your code take a long time to execute and speed those parts up. &lt;br /&gt;
&lt;br /&gt;
The remainder of this page gives an example of how to get the data into MATLAB, plot the data using standard MATLAB code, and how to close the BCI loop by writing an event back to BCI2000. The example below does not do any useful processing, it is up to you to decide how you want to process the data in MATLAB. A number of realtime applications are included in the realtime module of the FieldTrip toolbox. Additional documentation for that can be found on  the FieldTrip website, under &amp;lt;em&amp;gt;development-&amp;gt;realtime&amp;lt;/em&amp;gt;. &lt;br /&gt;
&lt;br /&gt;
==Getting the data in Matlab==&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
 Subsequently you can do something like this&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
filename = &#039;buffer://localhost:1972&#039;;&lt;br /&gt;
&lt;br /&gt;
% read the header for the first time to determine number of channels and sampling rate&lt;br /&gt;
hdr = read_header(filename, &#039;cache&#039;, true);&lt;br /&gt;
&lt;br /&gt;
count      = 0;&lt;br /&gt;
prevSample = 0&lt;br /&gt;
blocksize  = hdr.Fs;&lt;br /&gt;
chanindx   = 1:hdr.nChans;&lt;br /&gt;
&lt;br /&gt;
while true&lt;br /&gt;
  % determine number of samples available in buffer&lt;br /&gt;
  hdr = read_header(filename, &#039;cache&#039;, true);&lt;br /&gt;
&lt;br /&gt;
  % see whether new samples are available&lt;br /&gt;
  newsamples = (hdr.nSamples*hdr.nTrials-prevSample);&lt;br /&gt;
&lt;br /&gt;
  if newsamples&amp;gt;=blocksize&lt;br /&gt;
&lt;br /&gt;
    % determine the samples to process&lt;br /&gt;
    begsample  = prevSample+1;&lt;br /&gt;
    endsample  = prevSample+blocksize ;&lt;br /&gt;
&lt;br /&gt;
    % remember up to where the data was read&lt;br /&gt;
    prevSample  = endsample;&lt;br /&gt;
    count       = count + 1;&lt;br /&gt;
    fprintf(&#039;processing segment %d from sample %d to %d\n&#039;, count, begsample, endsample);&lt;br /&gt;
&lt;br /&gt;
    % read data segment from buffer&lt;br /&gt;
    dat = read_data(filename, &#039;header&#039;, hdr, &#039;begsample&#039;, begsample, &#039;endsample&#039;, endsample, &#039;chanindx&#039;, chanindx);&lt;br /&gt;
&lt;br /&gt;
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%&lt;br /&gt;
    % subsequently the data can be processed, here it is only plotted&lt;br /&gt;
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%&lt;br /&gt;
&lt;br /&gt;
    % create a matching time-axis&lt;br /&gt;
    time = (begsample:endsample)/hdr.Fs;&lt;br /&gt;
&lt;br /&gt;
    % plot the data just like a standard FieldTrip raw data strucute&lt;br /&gt;
    plot(time, dat);&lt;br /&gt;
&lt;br /&gt;
    % ensure tight axes&lt;br /&gt;
    xlim([time(1) time(end)]);&lt;br /&gt;
&lt;br /&gt;
    % force Matlab to update the figure&lt;br /&gt;
    drawnow&lt;br /&gt;
&lt;br /&gt;
  end % if new samples available&lt;br /&gt;
end % while true&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Closing the loop, writing a control signal from Matlab to BCI2000==&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
event_up.type  = &#039;Signal&#039;;&lt;br /&gt;
event_up.value = 1;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
event_down.type  = &#039;Signal&#039;;&lt;br /&gt;
event_down.value = -1;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
event_null.type  = &#039;Signal&#039;;&lt;br /&gt;
event_null.value = 0;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can write the events to the buffer according to the following example code:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
filename = &#039;buffer://localhost:1972&#039;;&lt;br /&gt;
&lt;br /&gt;
write_event(filename, event_up);    % ... the cursor will move up ...&lt;br /&gt;
&lt;br /&gt;
write_event(filename, event_down);  % ... the cursor will move down ...&lt;br /&gt;
&lt;br /&gt;
write_event(filename, event_null);  % ... the cursor will stay at a constant value ...&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The control signal in BCI2000 remains at a constant value as long as you don&#039;t write another event with another control signal.&lt;br /&gt;
&lt;br /&gt;
==See also==&lt;br /&gt;
&lt;br /&gt;
[[Contributions:FieldTripBuffer]],&lt;br /&gt;
[[Programming Reference:MatlabFilter]], &lt;br /&gt;
[[User Reference:MatlabFilter]] &lt;br /&gt;
&lt;br /&gt;
[[Category:Tutorial]]&lt;br /&gt;
[[Category:Signal Processing]]&lt;br /&gt;
[[Category:Development]]&lt;br /&gt;
[[Category:Matlab]]&lt;br /&gt;
[[Category:External Interfaces]]&lt;/div&gt;</summary>
		<author><name>Roostenveld</name></author>
	</entry>
	<entry>
		<id>https://www.bci2000.org/mediawiki/index.php?title=Programming_Tutorial:Working_with_the_FieldTrip_buffer&amp;diff=4747</id>
		<title>Programming Tutorial:Working with the FieldTrip buffer</title>
		<link rel="alternate" type="text/html" href="https://www.bci2000.org/mediawiki/index.php?title=Programming_Tutorial:Working_with_the_FieldTrip_buffer&amp;diff=4747"/>
		<updated>2008-12-02T15:34:29Z</updated>

		<summary type="html">&lt;p&gt;Roostenveld: /* Closing the loop, writing a control signal from Matlab to BCI2000 */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Getting the data in Matlab==&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
 Subsequently you can do something like this&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
filename = &#039;buffer://localhost:1972&#039;;&lt;br /&gt;
&lt;br /&gt;
% read the header for the first time to determine number of channels and sampling rate&lt;br /&gt;
hdr = read_header(filename, &#039;cache&#039;, true);&lt;br /&gt;
&lt;br /&gt;
count      = 0;&lt;br /&gt;
prevSample = 0&lt;br /&gt;
blocksize  = hdr.Fs;&lt;br /&gt;
chanindx   = 1:hdr.nChans;&lt;br /&gt;
&lt;br /&gt;
while true&lt;br /&gt;
  % determine number of samples available in buffer&lt;br /&gt;
  hdr = read_header(filename, &#039;cache&#039;, true);&lt;br /&gt;
&lt;br /&gt;
  % see whether new samples are available&lt;br /&gt;
  newsamples = (hdr.nSamples*hdr.nTrials-prevSample);&lt;br /&gt;
&lt;br /&gt;
  if newsamples&amp;gt;=blocksize&lt;br /&gt;
&lt;br /&gt;
    % determine the samples to process&lt;br /&gt;
    begsample  = prevSample+1;&lt;br /&gt;
    endsample  = prevSample+blocksize ;&lt;br /&gt;
&lt;br /&gt;
    % remember up to where the data was read&lt;br /&gt;
    prevSample  = endsample;&lt;br /&gt;
    count       = count + 1;&lt;br /&gt;
    fprintf(&#039;processing segment %d from sample %d to %d\n&#039;, count, begsample, endsample);&lt;br /&gt;
&lt;br /&gt;
    % read data segment from buffer&lt;br /&gt;
    dat = read_data(filename, &#039;header&#039;, hdr, &#039;begsample&#039;, begsample, &#039;endsample&#039;, endsample, &#039;chanindx&#039;, chanindx);&lt;br /&gt;
&lt;br /&gt;
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%&lt;br /&gt;
    % subsequently the data can be processed, here it is only plotted&lt;br /&gt;
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%&lt;br /&gt;
&lt;br /&gt;
    % create a matching time-axis&lt;br /&gt;
    time = (begsample:endsample)/hdr.Fs;&lt;br /&gt;
&lt;br /&gt;
    % plot the data just like a standard FieldTrip raw data strucute&lt;br /&gt;
    plot(time, dat);&lt;br /&gt;
&lt;br /&gt;
    % ensure tight axes&lt;br /&gt;
    xlim([time(1) time(end)]);&lt;br /&gt;
&lt;br /&gt;
    % force Matlab to update the figure&lt;br /&gt;
    drawnow&lt;br /&gt;
&lt;br /&gt;
  end % if new samples available&lt;br /&gt;
end % while true&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Closing the loop, writing a control signal from Matlab to BCI2000==&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
event_up.type  = &#039;Signal&#039;;&lt;br /&gt;
event_up.value = 1;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
event_down.type  = &#039;Signal&#039;;&lt;br /&gt;
event_down.value = -1;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
event_null.type  = &#039;Signal&#039;;&lt;br /&gt;
event_null.value = 0;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can write the events to the buffer according to the following example code:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
filename = &#039;buffer://localhost:1972&#039;;&lt;br /&gt;
&lt;br /&gt;
write_event(filename, event_up);    % ... the cursor will move up ...&lt;br /&gt;
&lt;br /&gt;
write_event(filename, event_down);  % ... the cursor will move down ...&lt;br /&gt;
&lt;br /&gt;
write_event(filename, event_null);  % ... the cursor will stay at a constant value ...&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The control signal in BCI2000 remains at a constant value as long as you don&#039;t write another event with another control signal.&lt;br /&gt;
&lt;br /&gt;
==See also==&lt;br /&gt;
&lt;br /&gt;
[[Contributions:FieldTripBuffer]],&lt;br /&gt;
[[Programming Reference:MatlabFilter]], &lt;br /&gt;
[[User Reference:MatlabFilter]] &lt;br /&gt;
&lt;br /&gt;
[[Category:Tutorial]]&lt;br /&gt;
[[Category:Signal Processing]]&lt;br /&gt;
[[Category:Development]]&lt;br /&gt;
[[Category:Matlab]]&lt;br /&gt;
[[Category:External Interfaces]]&lt;/div&gt;</summary>
		<author><name>Roostenveld</name></author>
	</entry>
	<entry>
		<id>https://www.bci2000.org/mediawiki/index.php?title=Programming_Tutorial:Working_with_the_FieldTrip_buffer&amp;diff=4746</id>
		<title>Programming Tutorial:Working with the FieldTrip buffer</title>
		<link rel="alternate" type="text/html" href="https://www.bci2000.org/mediawiki/index.php?title=Programming_Tutorial:Working_with_the_FieldTrip_buffer&amp;diff=4746"/>
		<updated>2008-12-02T15:26:34Z</updated>

		<summary type="html">&lt;p&gt;Roostenveld: /* Getting the data in Matlab */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Getting the data in Matlab==&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
 Subsequently you can do something like this&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
filename = &#039;buffer://localhost:1972&#039;;&lt;br /&gt;
&lt;br /&gt;
% read the header for the first time to determine number of channels and sampling rate&lt;br /&gt;
hdr = read_header(filename, &#039;cache&#039;, true);&lt;br /&gt;
&lt;br /&gt;
count      = 0;&lt;br /&gt;
prevSample = 0&lt;br /&gt;
blocksize  = hdr.Fs;&lt;br /&gt;
chanindx   = 1:hdr.nChans;&lt;br /&gt;
&lt;br /&gt;
while true&lt;br /&gt;
  % determine number of samples available in buffer&lt;br /&gt;
  hdr = read_header(filename, &#039;cache&#039;, true);&lt;br /&gt;
&lt;br /&gt;
  % see whether new samples are available&lt;br /&gt;
  newsamples = (hdr.nSamples*hdr.nTrials-prevSample);&lt;br /&gt;
&lt;br /&gt;
  if newsamples&amp;gt;=blocksize&lt;br /&gt;
&lt;br /&gt;
    % determine the samples to process&lt;br /&gt;
    begsample  = prevSample+1;&lt;br /&gt;
    endsample  = prevSample+blocksize ;&lt;br /&gt;
&lt;br /&gt;
    % remember up to where the data was read&lt;br /&gt;
    prevSample  = endsample;&lt;br /&gt;
    count       = count + 1;&lt;br /&gt;
    fprintf(&#039;processing segment %d from sample %d to %d\n&#039;, count, begsample, endsample);&lt;br /&gt;
&lt;br /&gt;
    % read data segment from buffer&lt;br /&gt;
    dat = read_data(filename, &#039;header&#039;, hdr, &#039;begsample&#039;, begsample, &#039;endsample&#039;, endsample, &#039;chanindx&#039;, chanindx);&lt;br /&gt;
&lt;br /&gt;
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%&lt;br /&gt;
    % subsequently the data can be processed, here it is only plotted&lt;br /&gt;
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%&lt;br /&gt;
&lt;br /&gt;
    % create a matching time-axis&lt;br /&gt;
    time = (begsample:endsample)/hdr.Fs;&lt;br /&gt;
&lt;br /&gt;
    % plot the data just like a standard FieldTrip raw data strucute&lt;br /&gt;
    plot(time, dat);&lt;br /&gt;
&lt;br /&gt;
    % ensure tight axes&lt;br /&gt;
    xlim([time(1) time(end)]);&lt;br /&gt;
&lt;br /&gt;
    % force Matlab to update the figure&lt;br /&gt;
    drawnow&lt;br /&gt;
&lt;br /&gt;
  end % if new samples available&lt;br /&gt;
end % while true&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Closing the loop, writing a control signal from Matlab to BCI2000==&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
event_up.type  = &#039;Signal&#039;;&lt;br /&gt;
event_up.value = 1;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
event_down.type  = &#039;Signal&#039;;&lt;br /&gt;
event_down.value = -1;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
event_null.type  = &#039;Signal&#039;;&lt;br /&gt;
event_null.value = 0;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can write the events to the buffer according to the following example code:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
filename = &#039;buffer://localhost:1972&#039;;&lt;br /&gt;
&lt;br /&gt;
write_event(filename, event_up);&lt;br /&gt;
&lt;br /&gt;
% ... the cursor will move up ...&lt;br /&gt;
&lt;br /&gt;
write_event(filename, event_down);&lt;br /&gt;
&lt;br /&gt;
% ... the cursor will move down ...&lt;br /&gt;
&lt;br /&gt;
write_event(filename, event_null);&lt;br /&gt;
&lt;br /&gt;
% ... the cursor will stay at a constant value ...&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The control signal in BCI2000 remains at a constant value as long as you don&#039;t write another event with another control signal.&lt;br /&gt;
&lt;br /&gt;
==See also==&lt;br /&gt;
&lt;br /&gt;
[[Contributions:FieldTripBuffer]],&lt;br /&gt;
[[Programming Reference:MatlabFilter]], &lt;br /&gt;
[[User Reference:MatlabFilter]] &lt;br /&gt;
&lt;br /&gt;
[[Category:Tutorial]]&lt;br /&gt;
[[Category:Signal Processing]]&lt;br /&gt;
[[Category:Development]]&lt;br /&gt;
[[Category:Matlab]]&lt;br /&gt;
[[Category:External Interfaces]]&lt;/div&gt;</summary>
		<author><name>Roostenveld</name></author>
	</entry>
	<entry>
		<id>https://www.bci2000.org/mediawiki/index.php?title=Programming_Tutorial:Working_with_the_FieldTrip_buffer&amp;diff=4745</id>
		<title>Programming Tutorial:Working with the FieldTrip buffer</title>
		<link rel="alternate" type="text/html" href="https://www.bci2000.org/mediawiki/index.php?title=Programming_Tutorial:Working_with_the_FieldTrip_buffer&amp;diff=4745"/>
		<updated>2008-12-02T15:07:24Z</updated>

		<summary type="html">&lt;p&gt;Roostenveld: /* Getting the data in Matlab */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Getting the data in Matlab==&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
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&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
filename = &#039;buffer://localhost:1972&#039;;&lt;br /&gt;
&lt;br /&gt;
% read the header for the first time to determine number of channels and sampling rate&lt;br /&gt;
hdr = read_header(filename, &#039;cache&#039;, true);&lt;br /&gt;
&lt;br /&gt;
count      = 0;&lt;br /&gt;
prevSample = 0&lt;br /&gt;
blocksize  = hdr.Fs;&lt;br /&gt;
chanindx   = 1:hdr.nChans;&lt;br /&gt;
&lt;br /&gt;
while true&lt;br /&gt;
  % determine number of samples available in buffer&lt;br /&gt;
  hdr = read_header(filename, &#039;cache&#039;, true);&lt;br /&gt;
&lt;br /&gt;
  % see whether new samples are available&lt;br /&gt;
  newsamples = (hdr.nSamples*hdr.nTrials-prevSample);&lt;br /&gt;
&lt;br /&gt;
  if newsamples&amp;gt;=blocksize&lt;br /&gt;
&lt;br /&gt;
    % determine the samples to process&lt;br /&gt;
    begsample  = prevSample+1;&lt;br /&gt;
    endsample  = prevSample+blocksize ;&lt;br /&gt;
&lt;br /&gt;
    % remember up to where the data was read&lt;br /&gt;
    prevSample  = endsample;&lt;br /&gt;
    count       = count + 1;&lt;br /&gt;
    fprintf(&#039;processing segment %d from sample %d to %d\n&#039;, count, begsample, endsample);&lt;br /&gt;
&lt;br /&gt;
    % read data segment from buffer&lt;br /&gt;
    dat = read_data(filename, &#039;header&#039;, hdr, &#039;begsample&#039;, begsample, &#039;endsample&#039;, endsample, &#039;chanindx&#039;, chanindx);&lt;br /&gt;
&lt;br /&gt;
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%&lt;br /&gt;
    % subsequently the data can be processed, here it is only plotted&lt;br /&gt;
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%&lt;br /&gt;
&lt;br /&gt;
    % create a matching time-axis&lt;br /&gt;
    time = (begsample:endsample)/hdr.Fs;&lt;br /&gt;
&lt;br /&gt;
    % plot the data just like a standard FieldTrip raw data strucute&lt;br /&gt;
    plot(time, dat);&lt;br /&gt;
&lt;br /&gt;
    % ensure tight axes&lt;br /&gt;
    xlim([time(1) time(end)]);&lt;br /&gt;
&lt;br /&gt;
    % force Matlab to update the figure&lt;br /&gt;
    drawnow&lt;br /&gt;
&lt;br /&gt;
  end % if new samples available&lt;br /&gt;
end % while true&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Closing the loop, writing a control signal from Matlab to BCI2000==&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
event_up.type  = &#039;Signal&#039;;&lt;br /&gt;
event_up.value = 1;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
event_down.type  = &#039;Signal&#039;;&lt;br /&gt;
event_down.value = -1;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
event_null.type  = &#039;Signal&#039;;&lt;br /&gt;
event_null.value = 0;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can write the events to the buffer according to the following example code:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
filename = &#039;buffer://localhost:1972&#039;;&lt;br /&gt;
&lt;br /&gt;
write_event(filename, event_up);&lt;br /&gt;
&lt;br /&gt;
% ... the cursor will move up ...&lt;br /&gt;
&lt;br /&gt;
write_event(filename, event_down);&lt;br /&gt;
&lt;br /&gt;
% ... the cursor will move down ...&lt;br /&gt;
&lt;br /&gt;
write_event(filename, event_null);&lt;br /&gt;
&lt;br /&gt;
% ... the cursor will stay at a constant value ...&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The control signal in BCI2000 remains at a constant value as long as you don&#039;t write another event with another control signal.&lt;br /&gt;
&lt;br /&gt;
==See also==&lt;br /&gt;
&lt;br /&gt;
[[Contributions:FieldTripBuffer]],&lt;br /&gt;
[[Programming Reference:MatlabFilter]], &lt;br /&gt;
[[User Reference:MatlabFilter]] &lt;br /&gt;
&lt;br /&gt;
[[Category:Tutorial]]&lt;br /&gt;
[[Category:Signal Processing]]&lt;br /&gt;
[[Category:Development]]&lt;br /&gt;
[[Category:Matlab]]&lt;br /&gt;
[[Category:External Interfaces]]&lt;/div&gt;</summary>
		<author><name>Roostenveld</name></author>
	</entry>
	<entry>
		<id>https://www.bci2000.org/mediawiki/index.php?title=Programming_Tutorial:Working_with_the_FieldTrip_buffer&amp;diff=4744</id>
		<title>Programming Tutorial:Working with the FieldTrip buffer</title>
		<link rel="alternate" type="text/html" href="https://www.bci2000.org/mediawiki/index.php?title=Programming_Tutorial:Working_with_the_FieldTrip_buffer&amp;diff=4744"/>
		<updated>2008-12-02T14:56:01Z</updated>

		<summary type="html">&lt;p&gt;Roostenveld: /* Getting the data in Matlab */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Getting the data in Matlab==&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
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&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
filename = &#039;buffer://localhost:1972&#039;;&lt;br /&gt;
&lt;br /&gt;
% read the header for the first time to determine number of channels and sampling rate&lt;br /&gt;
hdr = read_header(filename, &#039;cache&#039;, true);&lt;br /&gt;
&lt;br /&gt;
count      = 0;&lt;br /&gt;
prevSample = 0&lt;br /&gt;
blocksize  = hdr.Fs;&lt;br /&gt;
chanindx   = 1:hdr.nChans;&lt;br /&gt;
&lt;br /&gt;
while true&lt;br /&gt;
  % determine number of samples available in buffer&lt;br /&gt;
  hdr = read_header(filename, &#039;cache&#039;, true);&lt;br /&gt;
&lt;br /&gt;
  % see whether new samples are available&lt;br /&gt;
  newsamples = (hdr.nSamples*hdr.nTrials-prevSample);&lt;br /&gt;
&lt;br /&gt;
  if newsamples&amp;gt;=blocksize&lt;br /&gt;
&lt;br /&gt;
    % determine the samples to process&lt;br /&gt;
    begsample  = prevSample+1;&lt;br /&gt;
    endsample  = prevSample+blocksize ;&lt;br /&gt;
&lt;br /&gt;
    % remember up to where the data was read&lt;br /&gt;
    prevSample  = endsample;&lt;br /&gt;
    count       = count + 1;&lt;br /&gt;
    fprintf(&#039;processing segment %d from sample %d to %d\n&#039;, count, begsample, endsample);&lt;br /&gt;
&lt;br /&gt;
    % read data segment from buffer&lt;br /&gt;
    dat = read_data(filename, &#039;header&#039;, hdr, &#039;begsample&#039;, begsample, &#039;endsample&#039;, endsample, &#039;chanindx&#039;, chanindx);&lt;br /&gt;
&lt;br /&gt;
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%&lt;br /&gt;
    % subsequently the data can be processed, here it is only plotted&lt;br /&gt;
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%&lt;br /&gt;
&lt;br /&gt;
    % create a matching time-axis&lt;br /&gt;
    time = (begsample:endsample)/hdr.Fs;&lt;br /&gt;
&lt;br /&gt;
    % plot the data just like a standard FieldTrip raw data strucute&lt;br /&gt;
    plot(time, dat);&lt;br /&gt;
&lt;br /&gt;
    % ensure tight axes&lt;br /&gt;
    xlim([time(1) time(end)]);&lt;br /&gt;
&lt;br /&gt;
    % force Matlab to update the figure&lt;br /&gt;
    drawnow&lt;br /&gt;
&lt;br /&gt;
  end % if new samples available&lt;br /&gt;
end % while true&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Closing the loop, writing a control signal from Matlab to BCI2000==&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
event_up.type  = &#039;Signal&#039;;&lt;br /&gt;
event_up.value = 1;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
event_down.type  = &#039;Signal&#039;;&lt;br /&gt;
event_down.value = -1;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
event_null.type  = &#039;Signal&#039;;&lt;br /&gt;
event_null.value = 0;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can write the events to the buffer according to the following example code:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
filename = &#039;buffer://localhost:1972&#039;;&lt;br /&gt;
&lt;br /&gt;
write_event(filename, event_up);&lt;br /&gt;
&lt;br /&gt;
% ... the cursor will move up ...&lt;br /&gt;
&lt;br /&gt;
write_event(filename, event_down);&lt;br /&gt;
&lt;br /&gt;
% ... the cursor will move down ...&lt;br /&gt;
&lt;br /&gt;
write_event(filename, event_null);&lt;br /&gt;
&lt;br /&gt;
% ... the cursor will stay at a constant value ...&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The control signal in BCI2000 remains at a constant value as long as you don&#039;t write another event with another control signal.&lt;br /&gt;
&lt;br /&gt;
==See also==&lt;br /&gt;
&lt;br /&gt;
[[Contributions:FieldTripBuffer]],&lt;br /&gt;
[[Programming Reference:MatlabFilter]], &lt;br /&gt;
[[User Reference:MatlabFilter]] &lt;br /&gt;
&lt;br /&gt;
[[Category:Tutorial]]&lt;br /&gt;
[[Category:Signal Processing]]&lt;br /&gt;
[[Category:Development]]&lt;br /&gt;
[[Category:Matlab]]&lt;br /&gt;
[[Category:External Interfaces]]&lt;/div&gt;</summary>
		<author><name>Roostenveld</name></author>
	</entry>
	<entry>
		<id>https://www.bci2000.org/mediawiki/index.php?title=Programming_Tutorial:Working_with_the_FieldTrip_buffer&amp;diff=4743</id>
		<title>Programming Tutorial:Working with the FieldTrip buffer</title>
		<link rel="alternate" type="text/html" href="https://www.bci2000.org/mediawiki/index.php?title=Programming_Tutorial:Working_with_the_FieldTrip_buffer&amp;diff=4743"/>
		<updated>2008-12-02T14:54:42Z</updated>

		<summary type="html">&lt;p&gt;Roostenveld: /* Getting the data in Matlab */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Getting the data in Matlab==&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
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&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
% read the header for the first time to determine number of channels and sampling rate&lt;br /&gt;
hdr = read_header(filename, &#039;cache&#039;, true);&lt;br /&gt;
&lt;br /&gt;
count      = 0;&lt;br /&gt;
prevSample = 0&lt;br /&gt;
blocksize  = hdr.Fs;&lt;br /&gt;
chanindx   = 1:hdr.nChans;&lt;br /&gt;
&lt;br /&gt;
while true&lt;br /&gt;
  % determine number of samples available in buffer&lt;br /&gt;
  hdr = read_header(filename, &#039;cache&#039;, true);&lt;br /&gt;
&lt;br /&gt;
  % see whether new samples are available&lt;br /&gt;
  newsamples = (hdr.nSamples*hdr.nTrials-prevSample);&lt;br /&gt;
&lt;br /&gt;
  if newsamples&amp;gt;=blocksize&lt;br /&gt;
&lt;br /&gt;
    % determine the samples to process&lt;br /&gt;
    begsample  = prevSample+1;&lt;br /&gt;
    endsample  = prevSample+blocksize ;&lt;br /&gt;
&lt;br /&gt;
    % remember up to where the data was read&lt;br /&gt;
    prevSample  = endsample;&lt;br /&gt;
    count       = count + 1;&lt;br /&gt;
    fprintf(&#039;processing segment %d from sample %d to %d\n&#039;, count, begsample, endsample);&lt;br /&gt;
&lt;br /&gt;
    % read data segment from buffer&lt;br /&gt;
    dat = read_data(filename, &#039;header&#039;, hdr, &#039;begsample&#039;, begsample, &#039;endsample&#039;, endsample, &#039;chanindx&#039;, chanindx);&lt;br /&gt;
&lt;br /&gt;
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%&lt;br /&gt;
    % subsequently the data can be processed, here it is only plotted&lt;br /&gt;
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%&lt;br /&gt;
&lt;br /&gt;
    % create a matching time-axis&lt;br /&gt;
    time = (begsample:endsample)/hdr.Fs;&lt;br /&gt;
&lt;br /&gt;
    % plot the data just like a standard FieldTrip raw data strucute&lt;br /&gt;
    plot(time, dat);&lt;br /&gt;
&lt;br /&gt;
    % ensure tight axes&lt;br /&gt;
    xlim([time(1) time(end)]);&lt;br /&gt;
&lt;br /&gt;
    % force Matlab to update the figure&lt;br /&gt;
    drawnow&lt;br /&gt;
&lt;br /&gt;
  end % if new samples available&lt;br /&gt;
end % while true&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Closing the loop, writing a control signal from Matlab to BCI2000==&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
event_up.type  = &#039;Signal&#039;;&lt;br /&gt;
event_up.value = 1;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
event_down.type  = &#039;Signal&#039;;&lt;br /&gt;
event_down.value = -1;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
event_null.type  = &#039;Signal&#039;;&lt;br /&gt;
event_null.value = 0;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can write the events to the buffer according to the following example code:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
filename = &#039;buffer://localhost:1972&#039;;&lt;br /&gt;
&lt;br /&gt;
write_event(filename, event_up);&lt;br /&gt;
&lt;br /&gt;
% ... the cursor will move up ...&lt;br /&gt;
&lt;br /&gt;
write_event(filename, event_down);&lt;br /&gt;
&lt;br /&gt;
% ... the cursor will move down ...&lt;br /&gt;
&lt;br /&gt;
write_event(filename, event_null);&lt;br /&gt;
&lt;br /&gt;
% ... the cursor will stay at a constant value ...&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The control signal in BCI2000 remains at a constant value as long as you don&#039;t write another event with another control signal.&lt;br /&gt;
&lt;br /&gt;
==See also==&lt;br /&gt;
&lt;br /&gt;
[[Contributions:FieldTripBuffer]],&lt;br /&gt;
[[Programming Reference:MatlabFilter]], &lt;br /&gt;
[[User Reference:MatlabFilter]] &lt;br /&gt;
&lt;br /&gt;
[[Category:Tutorial]]&lt;br /&gt;
[[Category:Signal Processing]]&lt;br /&gt;
[[Category:Development]]&lt;br /&gt;
[[Category:Matlab]]&lt;br /&gt;
[[Category:External Interfaces]]&lt;/div&gt;</summary>
		<author><name>Roostenveld</name></author>
	</entry>
	<entry>
		<id>https://www.bci2000.org/mediawiki/index.php?title=Programming_Tutorial:Working_with_the_FieldTrip_buffer&amp;diff=4742</id>
		<title>Programming Tutorial:Working with the FieldTrip buffer</title>
		<link rel="alternate" type="text/html" href="https://www.bci2000.org/mediawiki/index.php?title=Programming_Tutorial:Working_with_the_FieldTrip_buffer&amp;diff=4742"/>
		<updated>2008-12-02T14:54:21Z</updated>

		<summary type="html">&lt;p&gt;Roostenveld: /* Getting the data in Matlab */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Getting the data in Matlab==&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
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&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
% read the header for the first time to determine number of channels and sampling rate&lt;br /&gt;
hdr = read_header(filename, &#039;cache&#039;, true);&lt;br /&gt;
&lt;br /&gt;
count      = 0;&lt;br /&gt;
prevSample = 0&lt;br /&gt;
blocksize  = hdr.Fs;&lt;br /&gt;
chanindx   = 1:hdr.nChans;&lt;br /&gt;
&lt;br /&gt;
while true&lt;br /&gt;
  % determine number of samples available in buffer&lt;br /&gt;
  hdr = read_header(filename, &#039;cache&#039;, true);&lt;br /&gt;
&lt;br /&gt;
  % see whether new samples are available&lt;br /&gt;
  newsamples = (hdr.nSamples*hdr.nTrials-prevSample);&lt;br /&gt;
&lt;br /&gt;
  if newsamples&amp;gt;=blocksize&lt;br /&gt;
&lt;br /&gt;
    % determine the samples to process&lt;br /&gt;
    begsample  = prevSample+1;&lt;br /&gt;
    endsample  = prevSample+blocksize ;&lt;br /&gt;
&lt;br /&gt;
    % remember up to where the data was read&lt;br /&gt;
    prevSample  = endsample;&lt;br /&gt;
    count       = count + 1;&lt;br /&gt;
    fprintf(&#039;processing segment %d from sample %d to %d\n&#039;, count, begsample, endsample);&lt;br /&gt;
&lt;br /&gt;
    % read data segment from buffer&lt;br /&gt;
    dat = read_data(filename, &#039;header&#039;, hdr, &#039;begsample&#039;, begsample, &#039;endsample&#039;, endsample, &#039;chanindx&#039;, chanindx);&lt;br /&gt;
&lt;br /&gt;
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%&lt;br /&gt;
    % subsequently the data can be processed, here it is only plotted&lt;br /&gt;
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%&lt;br /&gt;
&lt;br /&gt;
    % create a matching time-axis&lt;br /&gt;
    time = (begsample:endsample)/hdr.Fs;&lt;br /&gt;
&lt;br /&gt;
    % plot the data just like a standard FieldTrip raw data strucute&lt;br /&gt;
    plot(time, dat);&lt;br /&gt;
&lt;br /&gt;
    % ensure tight axes&lt;br /&gt;
    xlim([time(1) time(end)]);&lt;br /&gt;
&lt;br /&gt;
    % force Matlab to update the figure&lt;br /&gt;
    drawnow&lt;br /&gt;
&lt;br /&gt;
  end % if new samples available&lt;br /&gt;
end % while true&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Closing the loop, writing a control signal from Matlab to BCI2000==&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
event_up.type  = &#039;Signal&#039;;&lt;br /&gt;
event_up.value = 1;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
event_down.type  = &#039;Signal&#039;;&lt;br /&gt;
event_down.value = -1;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
event_null.type  = &#039;Signal&#039;;&lt;br /&gt;
event_null.value = 0;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can write the events to the buffer according to the following example code:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
filename = &#039;buffer://localhost:1972&#039;;&lt;br /&gt;
&lt;br /&gt;
write_event(filename, event_up);&lt;br /&gt;
&lt;br /&gt;
% ... the cursor will move up ...&lt;br /&gt;
&lt;br /&gt;
write_event(filename, event_down);&lt;br /&gt;
&lt;br /&gt;
% ... the cursor will move down ...&lt;br /&gt;
&lt;br /&gt;
write_event(filename, event_null);&lt;br /&gt;
&lt;br /&gt;
% ... the cursor will stay at a constant value ...&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The control signal in BCI2000 remains at a constant value as long as you don&#039;t write another event with another control signal.&lt;br /&gt;
&lt;br /&gt;
==See also==&lt;br /&gt;
&lt;br /&gt;
[[Contributions:FieldTripBuffer]],&lt;br /&gt;
[[Programming Reference:MatlabFilter]], &lt;br /&gt;
[[User Reference:MatlabFilter]] &lt;br /&gt;
&lt;br /&gt;
[[Category:Tutorial]]&lt;br /&gt;
[[Category:Signal Processing]]&lt;br /&gt;
[[Category:Development]]&lt;br /&gt;
[[Category:Matlab]]&lt;br /&gt;
[[Category:External Interfaces]]&lt;/div&gt;</summary>
		<author><name>Roostenveld</name></author>
	</entry>
	<entry>
		<id>https://www.bci2000.org/mediawiki/index.php?title=Programming_Tutorial:Working_with_the_FieldTrip_buffer&amp;diff=4741</id>
		<title>Programming Tutorial:Working with the FieldTrip buffer</title>
		<link rel="alternate" type="text/html" href="https://www.bci2000.org/mediawiki/index.php?title=Programming_Tutorial:Working_with_the_FieldTrip_buffer&amp;diff=4741"/>
		<updated>2008-12-02T14:44:44Z</updated>

		<summary type="html">&lt;p&gt;Roostenveld: /* Closing the loop, writing a control signal from Matlab to BCI2000 */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Getting the data in Matlab==&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
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&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
% read the header for the first time to determine number of channels and sampling rate&lt;br /&gt;
hdr = read_header(filename, &#039;cache&#039;, true);&lt;br /&gt;
&lt;br /&gt;
count      = 0;&lt;br /&gt;
prevSample = 0&lt;br /&gt;
blocksize  = hdr.Fs;&lt;br /&gt;
chanindx   = 1:hdr.nChans;&lt;br /&gt;
&lt;br /&gt;
while true&lt;br /&gt;
  % determine number of samples available in buffer&lt;br /&gt;
  hdr = read_header(filename, &#039;cache&#039;, true);&lt;br /&gt;
&lt;br /&gt;
  % see whether new samples are available&lt;br /&gt;
  newsamples = (hdr.nSamples*hdr.nTrials-prevSample);&lt;br /&gt;
&lt;br /&gt;
  if newsamples&amp;gt;=blocksize&lt;br /&gt;
&lt;br /&gt;
    % determine the samples to process&lt;br /&gt;
    begsample  = prevSample+1;&lt;br /&gt;
    endsample  = prevSample+blocksize ;&lt;br /&gt;
&lt;br /&gt;
    % remember up to where the data was read&lt;br /&gt;
    prevSample  = endsample;&lt;br /&gt;
    count       = count + 1;&lt;br /&gt;
    fprintf(&#039;processing segment %d from sample %d to %d\n&#039;, count, begsample, endsample);&lt;br /&gt;
&lt;br /&gt;
    % read data segment from buffer&lt;br /&gt;
    dat = read_data(filename, &#039;header&#039;, hdr, &#039;begsample&#039;, begsample, &#039;endsample&#039;, endsample, &#039;chanindx&#039;, chanindx);&lt;br /&gt;
&lt;br /&gt;
    % create a matching time-axis&lt;br /&gt;
    time = (begsample:endsample)/hdr.Fs;&lt;br /&gt;
&lt;br /&gt;
    % plot the data just like a standard FieldTrip raw data strucute&lt;br /&gt;
    plot(time, dat);&lt;br /&gt;
&lt;br /&gt;
    % ensure tight axes&lt;br /&gt;
    xlim([time(1) time(end)]);&lt;br /&gt;
&lt;br /&gt;
    % force Matlab to update the figure&lt;br /&gt;
    drawnow&lt;br /&gt;
&lt;br /&gt;
  end % if new samples available&lt;br /&gt;
end % while true&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Closing the loop, writing a control signal from Matlab to BCI2000==&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
event_up.type  = &#039;Signal&#039;;&lt;br /&gt;
event_up.value = 1;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
event_down.type  = &#039;Signal&#039;;&lt;br /&gt;
event_down.value = -1;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
event_null.type  = &#039;Signal&#039;;&lt;br /&gt;
event_null.value = 0;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can write the events to the buffer according to the following example code:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
filename = &#039;buffer://localhost:1972&#039;;&lt;br /&gt;
&lt;br /&gt;
write_event(filename, event_up);&lt;br /&gt;
&lt;br /&gt;
% ... the cursor will move up ...&lt;br /&gt;
&lt;br /&gt;
write_event(filename, event_down);&lt;br /&gt;
&lt;br /&gt;
% ... the cursor will move down ...&lt;br /&gt;
&lt;br /&gt;
write_event(filename, event_null);&lt;br /&gt;
&lt;br /&gt;
% ... the cursor will stay at a constant value ...&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The control signal in BCI2000 remains at a constant value as long as you don&#039;t write another event with another control signal.&lt;br /&gt;
&lt;br /&gt;
==See also==&lt;br /&gt;
&lt;br /&gt;
[[Contributions:FieldTripBuffer]],&lt;br /&gt;
[[Programming Reference:MatlabFilter]], &lt;br /&gt;
[[User Reference:MatlabFilter]] &lt;br /&gt;
&lt;br /&gt;
[[Category:Tutorial]]&lt;br /&gt;
[[Category:Signal Processing]]&lt;br /&gt;
[[Category:Development]]&lt;br /&gt;
[[Category:Matlab]]&lt;br /&gt;
[[Category:External Interfaces]]&lt;/div&gt;</summary>
		<author><name>Roostenveld</name></author>
	</entry>
	<entry>
		<id>https://www.bci2000.org/mediawiki/index.php?title=Programming_Tutorial:Working_with_the_FieldTrip_buffer&amp;diff=4740</id>
		<title>Programming Tutorial:Working with the FieldTrip buffer</title>
		<link rel="alternate" type="text/html" href="https://www.bci2000.org/mediawiki/index.php?title=Programming_Tutorial:Working_with_the_FieldTrip_buffer&amp;diff=4740"/>
		<updated>2008-12-02T14:44:29Z</updated>

		<summary type="html">&lt;p&gt;Roostenveld: /* Closing the loop, writing a control signal from Matlab to BCI2000 */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Getting the data in Matlab==&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
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&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
% read the header for the first time to determine number of channels and sampling rate&lt;br /&gt;
hdr = read_header(filename, &#039;cache&#039;, true);&lt;br /&gt;
&lt;br /&gt;
count      = 0;&lt;br /&gt;
prevSample = 0&lt;br /&gt;
blocksize  = hdr.Fs;&lt;br /&gt;
chanindx   = 1:hdr.nChans;&lt;br /&gt;
&lt;br /&gt;
while true&lt;br /&gt;
  % determine number of samples available in buffer&lt;br /&gt;
  hdr = read_header(filename, &#039;cache&#039;, true);&lt;br /&gt;
&lt;br /&gt;
  % see whether new samples are available&lt;br /&gt;
  newsamples = (hdr.nSamples*hdr.nTrials-prevSample);&lt;br /&gt;
&lt;br /&gt;
  if newsamples&amp;gt;=blocksize&lt;br /&gt;
&lt;br /&gt;
    % determine the samples to process&lt;br /&gt;
    begsample  = prevSample+1;&lt;br /&gt;
    endsample  = prevSample+blocksize ;&lt;br /&gt;
&lt;br /&gt;
    % remember up to where the data was read&lt;br /&gt;
    prevSample  = endsample;&lt;br /&gt;
    count       = count + 1;&lt;br /&gt;
    fprintf(&#039;processing segment %d from sample %d to %d\n&#039;, count, begsample, endsample);&lt;br /&gt;
&lt;br /&gt;
    % read data segment from buffer&lt;br /&gt;
    dat = read_data(filename, &#039;header&#039;, hdr, &#039;begsample&#039;, begsample, &#039;endsample&#039;, endsample, &#039;chanindx&#039;, chanindx);&lt;br /&gt;
&lt;br /&gt;
    % create a matching time-axis&lt;br /&gt;
    time = (begsample:endsample)/hdr.Fs;&lt;br /&gt;
&lt;br /&gt;
    % plot the data just like a standard FieldTrip raw data strucute&lt;br /&gt;
    plot(time, dat);&lt;br /&gt;
&lt;br /&gt;
    % ensure tight axes&lt;br /&gt;
    xlim([time(1) time(end)]);&lt;br /&gt;
&lt;br /&gt;
    % force Matlab to update the figure&lt;br /&gt;
    drawnow&lt;br /&gt;
&lt;br /&gt;
  end % if new samples available&lt;br /&gt;
end % while true&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Closing the loop, writing a control signal from Matlab to BCI2000==&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
event_up.type  = &#039;Signal&#039;;&lt;br /&gt;
event_up.value = 1;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
event_down.type  = &#039;Signal&#039;;&lt;br /&gt;
event_down.value = -1;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
event_null.type  = &#039;Signal&#039;;&lt;br /&gt;
event_null.value = 0;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can write the events to the buffer according to the following example code:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
filename = &#039;buffer://localhost:1972&#039;;&lt;br /&gt;
write_event(filename, event_up);&lt;br /&gt;
&lt;br /&gt;
% ... the cursor will move up ...&lt;br /&gt;
&lt;br /&gt;
write_event(filename, event_down);&lt;br /&gt;
&lt;br /&gt;
% ... the cursor will move down ...&lt;br /&gt;
&lt;br /&gt;
write_event(filename, event_null);&lt;br /&gt;
&lt;br /&gt;
% ... the cursor will stay at a constant value ...&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The control signal in BCI2000 remains at a constant value as long as you don&#039;t write another event with another control signal.&lt;br /&gt;
&lt;br /&gt;
==See also==&lt;br /&gt;
&lt;br /&gt;
[[Contributions:FieldTripBuffer]],&lt;br /&gt;
[[Programming Reference:MatlabFilter]], &lt;br /&gt;
[[User Reference:MatlabFilter]] &lt;br /&gt;
&lt;br /&gt;
[[Category:Tutorial]]&lt;br /&gt;
[[Category:Signal Processing]]&lt;br /&gt;
[[Category:Development]]&lt;br /&gt;
[[Category:Matlab]]&lt;br /&gt;
[[Category:External Interfaces]]&lt;/div&gt;</summary>
		<author><name>Roostenveld</name></author>
	</entry>
	<entry>
		<id>https://www.bci2000.org/mediawiki/index.php?title=Programming_Tutorial:Working_with_the_FieldTrip_buffer&amp;diff=4739</id>
		<title>Programming Tutorial:Working with the FieldTrip buffer</title>
		<link rel="alternate" type="text/html" href="https://www.bci2000.org/mediawiki/index.php?title=Programming_Tutorial:Working_with_the_FieldTrip_buffer&amp;diff=4739"/>
		<updated>2008-12-02T14:43:52Z</updated>

		<summary type="html">&lt;p&gt;Roostenveld: /* Closing the loop, writing a control signal from Matlab to BCI2000 */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Getting the data in Matlab==&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
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&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
% read the header for the first time to determine number of channels and sampling rate&lt;br /&gt;
hdr = read_header(filename, &#039;cache&#039;, true);&lt;br /&gt;
&lt;br /&gt;
count      = 0;&lt;br /&gt;
prevSample = 0&lt;br /&gt;
blocksize  = hdr.Fs;&lt;br /&gt;
chanindx   = 1:hdr.nChans;&lt;br /&gt;
&lt;br /&gt;
while true&lt;br /&gt;
  % determine number of samples available in buffer&lt;br /&gt;
  hdr = read_header(filename, &#039;cache&#039;, true);&lt;br /&gt;
&lt;br /&gt;
  % see whether new samples are available&lt;br /&gt;
  newsamples = (hdr.nSamples*hdr.nTrials-prevSample);&lt;br /&gt;
&lt;br /&gt;
  if newsamples&amp;gt;=blocksize&lt;br /&gt;
&lt;br /&gt;
    % determine the samples to process&lt;br /&gt;
    begsample  = prevSample+1;&lt;br /&gt;
    endsample  = prevSample+blocksize ;&lt;br /&gt;
&lt;br /&gt;
    % remember up to where the data was read&lt;br /&gt;
    prevSample  = endsample;&lt;br /&gt;
    count       = count + 1;&lt;br /&gt;
    fprintf(&#039;processing segment %d from sample %d to %d\n&#039;, count, begsample, endsample);&lt;br /&gt;
&lt;br /&gt;
    % read data segment from buffer&lt;br /&gt;
    dat = read_data(filename, &#039;header&#039;, hdr, &#039;begsample&#039;, begsample, &#039;endsample&#039;, endsample, &#039;chanindx&#039;, chanindx);&lt;br /&gt;
&lt;br /&gt;
    % create a matching time-axis&lt;br /&gt;
    time = (begsample:endsample)/hdr.Fs;&lt;br /&gt;
&lt;br /&gt;
    % plot the data just like a standard FieldTrip raw data strucute&lt;br /&gt;
    plot(time, dat);&lt;br /&gt;
&lt;br /&gt;
    % ensure tight axes&lt;br /&gt;
    xlim([time(1) time(end)]);&lt;br /&gt;
&lt;br /&gt;
    % force Matlab to update the figure&lt;br /&gt;
    drawnow&lt;br /&gt;
&lt;br /&gt;
  end % if new samples available&lt;br /&gt;
end % while true&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Closing the loop, writing a control signal from Matlab to BCI2000==&lt;br /&gt;
&lt;br /&gt;
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 &#039;&#039;&#039;Feedback Demo Task&#039;&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
event_up.type  = &#039;Signal&#039;;&lt;br /&gt;
event_up.value = 1;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
event_down.type  = &#039;Signal&#039;;&lt;br /&gt;
event_down.value = -1;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
event_null.type  = &#039;Signal&#039;;&lt;br /&gt;
event_null.value = 0;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can write the events to the buffer according to the following example code:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
filename = &#039;buffer://localhost:1972&#039;;&lt;br /&gt;
write_event(filename, event_up);&lt;br /&gt;
% ... the cursor will move up&lt;br /&gt;
write_event(filename, event_down);&lt;br /&gt;
% ... the cursor will move down&lt;br /&gt;
write_event(filename, event_null);&lt;br /&gt;
% ... the cursor will stay at a constant value&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The control signal in BCI2000 remains at a constant value as long as you don&#039;t write another event with another control signal.&lt;br /&gt;
&lt;br /&gt;
==See also==&lt;br /&gt;
&lt;br /&gt;
[[Contributions:FieldTripBuffer]],&lt;br /&gt;
[[Programming Reference:MatlabFilter]], &lt;br /&gt;
[[User Reference:MatlabFilter]] &lt;br /&gt;
&lt;br /&gt;
[[Category:Tutorial]]&lt;br /&gt;
[[Category:Signal Processing]]&lt;br /&gt;
[[Category:Development]]&lt;br /&gt;
[[Category:Matlab]]&lt;br /&gt;
[[Category:External Interfaces]]&lt;/div&gt;</summary>
		<author><name>Roostenveld</name></author>
	</entry>
	<entry>
		<id>https://www.bci2000.org/mediawiki/index.php?title=Programming_Tutorial:Working_with_the_FieldTrip_buffer&amp;diff=4738</id>
		<title>Programming Tutorial:Working with the FieldTrip buffer</title>
		<link rel="alternate" type="text/html" href="https://www.bci2000.org/mediawiki/index.php?title=Programming_Tutorial:Working_with_the_FieldTrip_buffer&amp;diff=4738"/>
		<updated>2008-12-02T14:38:57Z</updated>

		<summary type="html">&lt;p&gt;Roostenveld: /* Getting the data in Matlab */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Getting the data in Matlab==&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
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&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
% read the header for the first time to determine number of channels and sampling rate&lt;br /&gt;
hdr = read_header(filename, &#039;cache&#039;, true);&lt;br /&gt;
&lt;br /&gt;
count      = 0;&lt;br /&gt;
prevSample = 0&lt;br /&gt;
blocksize  = hdr.Fs;&lt;br /&gt;
chanindx   = 1:hdr.nChans;&lt;br /&gt;
&lt;br /&gt;
while true&lt;br /&gt;
  % determine number of samples available in buffer&lt;br /&gt;
  hdr = read_header(filename, &#039;cache&#039;, true);&lt;br /&gt;
&lt;br /&gt;
  % see whether new samples are available&lt;br /&gt;
  newsamples = (hdr.nSamples*hdr.nTrials-prevSample);&lt;br /&gt;
&lt;br /&gt;
  if newsamples&amp;gt;=blocksize&lt;br /&gt;
&lt;br /&gt;
    % determine the samples to process&lt;br /&gt;
    begsample  = prevSample+1;&lt;br /&gt;
    endsample  = prevSample+blocksize ;&lt;br /&gt;
&lt;br /&gt;
    % remember up to where the data was read&lt;br /&gt;
    prevSample  = endsample;&lt;br /&gt;
    count       = count + 1;&lt;br /&gt;
    fprintf(&#039;processing segment %d from sample %d to %d\n&#039;, count, begsample, endsample);&lt;br /&gt;
&lt;br /&gt;
    % read data segment from buffer&lt;br /&gt;
    dat = read_data(filename, &#039;header&#039;, hdr, &#039;begsample&#039;, begsample, &#039;endsample&#039;, endsample, &#039;chanindx&#039;, chanindx);&lt;br /&gt;
&lt;br /&gt;
    % create a matching time-axis&lt;br /&gt;
    time = (begsample:endsample)/hdr.Fs;&lt;br /&gt;
&lt;br /&gt;
    % plot the data just like a standard FieldTrip raw data strucute&lt;br /&gt;
    plot(time, dat);&lt;br /&gt;
&lt;br /&gt;
    % ensure tight axes&lt;br /&gt;
    xlim([time(1) time(end)]);&lt;br /&gt;
&lt;br /&gt;
    % force Matlab to update the figure&lt;br /&gt;
    drawnow&lt;br /&gt;
&lt;br /&gt;
  end % if new samples available&lt;br /&gt;
end % while true&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Closing the loop, writing a control signal from Matlab to BCI2000==&lt;br /&gt;
&lt;br /&gt;
==See also==&lt;br /&gt;
&lt;br /&gt;
[[Contributions:FieldTripBuffer]],&lt;br /&gt;
[[Programming Reference:MatlabFilter]], &lt;br /&gt;
[[User Reference:MatlabFilter]] &lt;br /&gt;
&lt;br /&gt;
[[Category:Tutorial]]&lt;br /&gt;
[[Category:Signal Processing]]&lt;br /&gt;
[[Category:Development]]&lt;br /&gt;
[[Category:Matlab]]&lt;br /&gt;
[[Category:External Interfaces]]&lt;/div&gt;</summary>
		<author><name>Roostenveld</name></author>
	</entry>
	<entry>
		<id>https://www.bci2000.org/mediawiki/index.php?title=Programming_Tutorial:Working_with_the_FieldTrip_buffer&amp;diff=4737</id>
		<title>Programming Tutorial:Working with the FieldTrip buffer</title>
		<link rel="alternate" type="text/html" href="https://www.bci2000.org/mediawiki/index.php?title=Programming_Tutorial:Working_with_the_FieldTrip_buffer&amp;diff=4737"/>
		<updated>2008-12-02T14:36:56Z</updated>

		<summary type="html">&lt;p&gt;Roostenveld: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Getting the data in Matlab==&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
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&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
% read the header for the first time to determine number of channels and sampling rate&lt;br /&gt;
hdr = read_header(cfg.headerfile, &#039;cache&#039;, true);&lt;br /&gt;
&lt;br /&gt;
count     = 0;&lt;br /&gt;
blocksize = hdr.Fs;&lt;br /&gt;
chanindx  = 1:hdr.nChans;&lt;br /&gt;
&lt;br /&gt;
while true&lt;br /&gt;
  % determine number of samples available in buffer&lt;br /&gt;
  hdr = read_header(cfg.headerfile, &#039;cache&#039;, true);&lt;br /&gt;
&lt;br /&gt;
  % see whether new samples are available&lt;br /&gt;
  newsamples = (hdr.nSamples*hdr.nTrials-prevSample);&lt;br /&gt;
&lt;br /&gt;
  if newsamples&amp;gt;=blocksize&lt;br /&gt;
&lt;br /&gt;
    % determine the samples to process&lt;br /&gt;
    begsample  = prevSample+1;&lt;br /&gt;
    endsample  = prevSample+blocksize ;&lt;br /&gt;
&lt;br /&gt;
    % remember up to where the data was read&lt;br /&gt;
    prevSample  = endsample;&lt;br /&gt;
    count       = count + 1;&lt;br /&gt;
    fprintf(&#039;processing segment %d from sample %d to %d\n&#039;, count, begsample, endsample);&lt;br /&gt;
&lt;br /&gt;
    % read data segment from buffer&lt;br /&gt;
    dat = read_data(cfg.datafile, &#039;header&#039;, hdr, &#039;begsample&#039;, begsample, &#039;endsample&#039;, endsample, &#039;chanindx&#039;, chanindx);&lt;br /&gt;
&lt;br /&gt;
    % create a matching time-axis&lt;br /&gt;
    time = (begsample:endsample)/hdr.Fs;&lt;br /&gt;
&lt;br /&gt;
    % plot the data just like a standard FieldTrip raw data strucute&lt;br /&gt;
    plot(time, dat);&lt;br /&gt;
&lt;br /&gt;
    % ensure tight axes&lt;br /&gt;
    xlim([time(1) time(end)]);&lt;br /&gt;
&lt;br /&gt;
    % force Matlab to update the figure&lt;br /&gt;
    drawnow&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Closing the loop, writing a control signal from Matlab to BCI2000==&lt;br /&gt;
&lt;br /&gt;
==See also==&lt;br /&gt;
&lt;br /&gt;
[[Contributions:FieldTripBuffer]],&lt;br /&gt;
[[Programming Reference:MatlabFilter]], &lt;br /&gt;
[[User Reference:MatlabFilter]] &lt;br /&gt;
&lt;br /&gt;
[[Category:Tutorial]]&lt;br /&gt;
[[Category:Signal Processing]]&lt;br /&gt;
[[Category:Development]]&lt;br /&gt;
[[Category:Matlab]]&lt;br /&gt;
[[Category:External Interfaces]]&lt;/div&gt;</summary>
		<author><name>Roostenveld</name></author>
	</entry>
	<entry>
		<id>https://www.bci2000.org/mediawiki/index.php?title=Programming_Tutorial:Working_with_the_FieldTrip_buffer&amp;diff=4736</id>
		<title>Programming Tutorial:Working with the FieldTrip buffer</title>
		<link rel="alternate" type="text/html" href="https://www.bci2000.org/mediawiki/index.php?title=Programming_Tutorial:Working_with_the_FieldTrip_buffer&amp;diff=4736"/>
		<updated>2008-12-02T14:28:23Z</updated>

		<summary type="html">&lt;p&gt;Roostenveld: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==See also==&lt;br /&gt;
&lt;br /&gt;
[[Contributions:FieldTripBuffer]],&lt;br /&gt;
[[Programming Reference:MatlabFilter]], &lt;br /&gt;
[[User Reference:MatlabFilter]] &lt;br /&gt;
&lt;br /&gt;
[[Category:Tutorial]]&lt;br /&gt;
[[Category:Signal Processing]]&lt;br /&gt;
[[Category:Development]]&lt;br /&gt;
[[Category:Matlab]]&lt;br /&gt;
[[Category:External Interfaces]]&lt;/div&gt;</summary>
		<author><name>Roostenveld</name></author>
	</entry>
	<entry>
		<id>https://www.bci2000.org/mediawiki/index.php?title=Programming_Reference:Contents&amp;diff=4735</id>
		<title>Programming Reference:Contents</title>
		<link rel="alternate" type="text/html" href="https://www.bci2000.org/mediawiki/index.php?title=Programming_Reference:Contents&amp;diff=4735"/>
		<updated>2008-12-02T14:27:14Z</updated>

		<summary type="html">&lt;p&gt;Roostenveld: /* Tutorials */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This part of the reference provides background information which you need in order&lt;br /&gt;
to understand, modify, or create code that depends on the BCI2000 framework.&lt;br /&gt;
&lt;br /&gt;
You should read it before writing your own BCI2000 module, or modifying an existing&lt;br /&gt;
one as presented in the tutorial examples.&lt;br /&gt;
&lt;br /&gt;
==Getting Started==&lt;br /&gt;
*[[Programming Howto:Building BCI2000]]&lt;br /&gt;
*[[Programming Howto:SVN Client Setup]]&lt;br /&gt;
*[[Programming Howto:Using TortoiseSVN]]&lt;br /&gt;
*[[Programming FAQ]]&lt;br /&gt;
&lt;br /&gt;
==Conceptual Overview==&lt;br /&gt;
*[[Programming Reference:Filter Chain]]&lt;br /&gt;
*[[Programming Reference:Signals]]&lt;br /&gt;
*[[Programming Reference:Error Handling]]&lt;br /&gt;
*[[Programming Reference:Localization]]&lt;br /&gt;
*[[Programming Reference:Rapid Development]]&lt;br /&gt;
&lt;br /&gt;
==Programming Interface Documentation==&lt;br /&gt;
&lt;br /&gt;
===Generic Filter Interface===&lt;br /&gt;
*[[Programming Reference:GenericFilter Class]]&lt;br /&gt;
*[[Programming Reference:Environment Class]]&lt;br /&gt;
*[[Programming Reference:GenericVisualization Class]]&lt;br /&gt;
&lt;br /&gt;
===Text Messages===&lt;br /&gt;
*[[Programming Reference:Errors and Warnings]]&lt;br /&gt;
*[[Programming Reference:Debug Output]]&lt;br /&gt;
*[[Programming Reference:LogFile Class]]&lt;br /&gt;
&lt;br /&gt;
===Data I/O===&lt;br /&gt;
*[[Programming Reference:GenericFileWriter Class]]&lt;br /&gt;
*[[Programming Reference:GenericADC Class]]&lt;br /&gt;
*[[Programming Reference:BCI2000FileReader Class]]&lt;br /&gt;
&lt;br /&gt;
===Signal Processing Modules===&lt;br /&gt;
*[[Programming Reference:IIRFilterBase Class]]&lt;br /&gt;
&lt;br /&gt;
===Application Modules===&lt;br /&gt;
*[[Programming Reference:ApplicationBase Class]]&lt;br /&gt;
&lt;br /&gt;
====Feedback Applications====&lt;br /&gt;
*[[Programming Reference:FeedbackTask Class]]&lt;br /&gt;
*[[Programming Reference:FeedbackDemo Application]]&lt;br /&gt;
&lt;br /&gt;
====Stimulus Presentation/ERP Spelling Applications====&lt;br /&gt;
*[[Programming Reference:StimulusTask Class]]&lt;br /&gt;
*[[Programming Reference:Stimulus Class]]&lt;br /&gt;
*[[Programming Reference:Target Class]]&lt;br /&gt;
*[[Programming Reference:AssociationMap Class]]&lt;br /&gt;
*[[Programming Reference:Speller Class]]&lt;br /&gt;
&lt;br /&gt;
====Visual Output====&lt;br /&gt;
*[[Programming Reference:GraphDisplay Class]]&lt;br /&gt;
*[[Programming Reference:GraphObject Class]]&lt;br /&gt;
*[[Programming Reference:3D API]]&lt;br /&gt;
&lt;br /&gt;
====Audio Output====&lt;br /&gt;
*[[Programming Reference:MidiPlayer Class]]&lt;br /&gt;
*[[Programming Reference:WavePlayer Class]]&lt;br /&gt;
*[[Programming Reference:TextToSpeech Class]]&lt;br /&gt;
&lt;br /&gt;
===Miscellaneous===&lt;br /&gt;
*[[Programming Reference:TrialStatistics Class]]&lt;br /&gt;
*[[Programming Reference:BlockRandSeq Class]]&lt;br /&gt;
*[[Programming Reference:RandomGenerator Class]]&lt;br /&gt;
*[[Programming Reference:EnvironmentExtension Class]]&lt;br /&gt;
&lt;br /&gt;
==Tutorials==&lt;br /&gt;
*[[Programming Tutorial:Implementing a Data Acquisition Module]]&lt;br /&gt;
*[[Programming Tutorial:Implementing a Signal Processing Filter]]&lt;br /&gt;
*[[Programming Tutorial:Implementing a Matlab-based Filter]]&lt;br /&gt;
*[[Programming Tutorial:Working with the FieldTrip buffer]]&lt;br /&gt;
*[[Programming Tutorial:Implementing another Matlab-based Filter]]&lt;br /&gt;
*[[Programming Tutorial:Implementing an Input Logger]]&lt;br /&gt;
&lt;br /&gt;
==See also==&lt;br /&gt;
[[Technical Reference:Contents]], [[User Reference:Contents]]&lt;br /&gt;
&lt;br /&gt;
[[Category:Contents]]&lt;/div&gt;</summary>
		<author><name>Roostenveld</name></author>
	</entry>
	<entry>
		<id>https://www.bci2000.org/mediawiki/index.php?title=Programming_Reference:Contents&amp;diff=4734</id>
		<title>Programming Reference:Contents</title>
		<link rel="alternate" type="text/html" href="https://www.bci2000.org/mediawiki/index.php?title=Programming_Reference:Contents&amp;diff=4734"/>
		<updated>2008-12-02T14:27:03Z</updated>

		<summary type="html">&lt;p&gt;Roostenveld: /* Tutorials */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This part of the reference provides background information which you need in order&lt;br /&gt;
to understand, modify, or create code that depends on the BCI2000 framework.&lt;br /&gt;
&lt;br /&gt;
You should read it before writing your own BCI2000 module, or modifying an existing&lt;br /&gt;
one as presented in the tutorial examples.&lt;br /&gt;
&lt;br /&gt;
==Getting Started==&lt;br /&gt;
*[[Programming Howto:Building BCI2000]]&lt;br /&gt;
*[[Programming Howto:SVN Client Setup]]&lt;br /&gt;
*[[Programming Howto:Using TortoiseSVN]]&lt;br /&gt;
*[[Programming FAQ]]&lt;br /&gt;
&lt;br /&gt;
==Conceptual Overview==&lt;br /&gt;
*[[Programming Reference:Filter Chain]]&lt;br /&gt;
*[[Programming Reference:Signals]]&lt;br /&gt;
*[[Programming Reference:Error Handling]]&lt;br /&gt;
*[[Programming Reference:Localization]]&lt;br /&gt;
*[[Programming Reference:Rapid Development]]&lt;br /&gt;
&lt;br /&gt;
==Programming Interface Documentation==&lt;br /&gt;
&lt;br /&gt;
===Generic Filter Interface===&lt;br /&gt;
*[[Programming Reference:GenericFilter Class]]&lt;br /&gt;
*[[Programming Reference:Environment Class]]&lt;br /&gt;
*[[Programming Reference:GenericVisualization Class]]&lt;br /&gt;
&lt;br /&gt;
===Text Messages===&lt;br /&gt;
*[[Programming Reference:Errors and Warnings]]&lt;br /&gt;
*[[Programming Reference:Debug Output]]&lt;br /&gt;
*[[Programming Reference:LogFile Class]]&lt;br /&gt;
&lt;br /&gt;
===Data I/O===&lt;br /&gt;
*[[Programming Reference:GenericFileWriter Class]]&lt;br /&gt;
*[[Programming Reference:GenericADC Class]]&lt;br /&gt;
*[[Programming Reference:BCI2000FileReader Class]]&lt;br /&gt;
&lt;br /&gt;
===Signal Processing Modules===&lt;br /&gt;
*[[Programming Reference:IIRFilterBase Class]]&lt;br /&gt;
&lt;br /&gt;
===Application Modules===&lt;br /&gt;
*[[Programming Reference:ApplicationBase Class]]&lt;br /&gt;
&lt;br /&gt;
====Feedback Applications====&lt;br /&gt;
*[[Programming Reference:FeedbackTask Class]]&lt;br /&gt;
*[[Programming Reference:FeedbackDemo Application]]&lt;br /&gt;
&lt;br /&gt;
====Stimulus Presentation/ERP Spelling Applications====&lt;br /&gt;
*[[Programming Reference:StimulusTask Class]]&lt;br /&gt;
*[[Programming Reference:Stimulus Class]]&lt;br /&gt;
*[[Programming Reference:Target Class]]&lt;br /&gt;
*[[Programming Reference:AssociationMap Class]]&lt;br /&gt;
*[[Programming Reference:Speller Class]]&lt;br /&gt;
&lt;br /&gt;
====Visual Output====&lt;br /&gt;
*[[Programming Reference:GraphDisplay Class]]&lt;br /&gt;
*[[Programming Reference:GraphObject Class]]&lt;br /&gt;
*[[Programming Reference:3D API]]&lt;br /&gt;
&lt;br /&gt;
====Audio Output====&lt;br /&gt;
*[[Programming Reference:MidiPlayer Class]]&lt;br /&gt;
*[[Programming Reference:WavePlayer Class]]&lt;br /&gt;
*[[Programming Reference:TextToSpeech Class]]&lt;br /&gt;
&lt;br /&gt;
===Miscellaneous===&lt;br /&gt;
*[[Programming Reference:TrialStatistics Class]]&lt;br /&gt;
*[[Programming Reference:BlockRandSeq Class]]&lt;br /&gt;
*[[Programming Reference:RandomGenerator Class]]&lt;br /&gt;
*[[Programming Reference:EnvironmentExtension Class]]&lt;br /&gt;
&lt;br /&gt;
==Tutorials==&lt;br /&gt;
*[[Programming Tutorial:Implementing a Data Acquisition Module]]&lt;br /&gt;
*[[Programming Tutorial:Implementing a Signal Processing Filter]]&lt;br /&gt;
*[[Programming Tutorial:Implementing a Matlab-based Filter]]&lt;br /&gt;
*[[Programming Tutorial:IWorking with the FieldTrip buffer]]&lt;br /&gt;
*[[Programming Tutorial:Implementing another Matlab-based Filter]]&lt;br /&gt;
*[[Programming Tutorial:Implementing an Input Logger]]&lt;br /&gt;
&lt;br /&gt;
==See also==&lt;br /&gt;
[[Technical Reference:Contents]], [[User Reference:Contents]]&lt;br /&gt;
&lt;br /&gt;
[[Category:Contents]]&lt;/div&gt;</summary>
		<author><name>Roostenveld</name></author>
	</entry>
	<entry>
		<id>https://www.bci2000.org/mediawiki/index.php?title=Contributions:FieldTripBuffer&amp;diff=4722</id>
		<title>Contributions:FieldTripBuffer</title>
		<link rel="alternate" type="text/html" href="https://www.bci2000.org/mediawiki/index.php?title=Contributions:FieldTripBuffer&amp;diff=4722"/>
		<updated>2008-11-26T10:08:01Z</updated>

		<summary type="html">&lt;p&gt;Roostenveld: fix url&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Synopsis==&lt;br /&gt;
The &#039;&#039;FieldTripBuffer&#039;&#039; filter is an interface between BCI2000 and the FieldTrip Toolbox. The filter hosts a [http://neuroimaging.ruhosting.nl/fieldtrip/doku.php?id=fieldtrip:development:realtime FieldTrip realtime buffer], and writes its input into that buffer, formatted as &#039;&#039;buffer data&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
FieldTrip software agents may then use the FieldTrip realtime protocol to communicate with the buffer asynchronously, reading data from BCI2000, and writing back results into BCI2000.&lt;br /&gt;
&lt;br /&gt;
BCI2000 [[BCI2000 Glossary#State Variable|state variables]] may be translated into FieldTrip buffer events; in turn, buffer events are read back from the buffer into BCI2000 state variables. A dedicated FieldTrip buffer event type may be used to determine the filter&#039;s output into the BCI2000 processing pipe.&lt;br /&gt;
&lt;br /&gt;
==Location==&lt;br /&gt;
http://{{SERVERNAME}}/svn/trunk/src/contrib/SignalProcessing/FieldTripBuffer&lt;br /&gt;
&lt;br /&gt;
==Versioning==&lt;br /&gt;
===Authors===&lt;br /&gt;
Jürgen Mellinger, Robert Oostenveld&lt;br /&gt;
===Source Code Revisions===&lt;br /&gt;
*Initial development: 2044&lt;br /&gt;
*Tested under: 2094&lt;br /&gt;
*Known to compile under: 2095&lt;br /&gt;
&lt;br /&gt;
==Parameters==&lt;br /&gt;
===FTBufferAddress===&lt;br /&gt;
The FieldTrip buffer&#039;s listening address, given as an IP:port combination. Software agents connect to this address using a TCP-based protocol.&lt;br /&gt;
&lt;br /&gt;
===FTInitialOutput===&lt;br /&gt;
A matrix-valued parameter that determines dimensions as well as initial values of the filter&#039;s output signal.&lt;br /&gt;
In the matrix, rows map to signal channels, and columns map to elements.&lt;br /&gt;
&lt;br /&gt;
===FTOutputEventType===&lt;br /&gt;
A string representing the FieldTrip buffer event type that corresponds to the filter&#039;s output signal.&lt;br /&gt;
For each block of data, the &#039;&#039;FieldTripBuffer&#039;&#039; filter checks whether an event of that type has been inserted into the buffer; whenever this is the case, the filter&#039;s output signal will be updated from the values contained in the event. If the event&#039;s data dimensions do not fit the output signal, parts of the output signal will not be updated, or event data will be ignored silently.&lt;br /&gt;
&lt;br /&gt;
===FTStatesToBuffer===&lt;br /&gt;
A list of states that are to be converted into events, and written into the buffer. The buffer event type matches the BCI2000 state name.&lt;br /&gt;
An asterisk (*) represents any state name; a minus (-) sign before a state name means to exclude the respective state from the transmission.&lt;br /&gt;
States may either be written to, or read from, the buffer, but not both; states listed in the &#039;&#039;FTStatesFromBuffer&#039;&#039; parameter are always excluded from writing.&lt;br /&gt;
&lt;br /&gt;
===FTStatesFromBuffer===&lt;br /&gt;
A list of states that are to be set from events. When no event of matching type has been posted to the buffer, the state will not change; when multiple events have been posted since the FieldTrip buffer processed the last block of data, then the most recent event determines the state&#039;s value.&lt;br /&gt;
&lt;br /&gt;
==States==&lt;br /&gt;
Any state name may be specified in the &#039;&#039;FTStatesToBuffer&#039;&#039; and &#039;&#039;FTStatesFromBuffer&#039;&#039; parameters.&lt;br /&gt;
&lt;br /&gt;
==See also==&lt;br /&gt;
[[User Reference:MatlabFilter]], [[Programming Reference:MatlabFilter]]&lt;br /&gt;
&lt;br /&gt;
[[Category:Filters]]&lt;br /&gt;
[[Category:Signal Processing]]&lt;br /&gt;
[[Category:Matlab]]&lt;br /&gt;
[[Category:Development]]&lt;br /&gt;
[[Category:External Interfaces]]&lt;/div&gt;</summary>
		<author><name>Roostenveld</name></author>
	</entry>
	<entry>
		<id>https://www.bci2000.org/mediawiki/index.php?title=Contributions:FieldTripBuffer&amp;diff=4721</id>
		<title>Contributions:FieldTripBuffer</title>
		<link rel="alternate" type="text/html" href="https://www.bci2000.org/mediawiki/index.php?title=Contributions:FieldTripBuffer&amp;diff=4721"/>
		<updated>2008-11-26T10:07:33Z</updated>

		<summary type="html">&lt;p&gt;Roostenveld: updated URL to FT wiki&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Synopsis==&lt;br /&gt;
The &#039;&#039;FieldTripBuffer&#039;&#039; filter is an interface between BCI2000 and the FieldTrip Toolbox. The filter hosts a [http://neuroimaging.ru.nl/fieldtrip/doku.php?id=fieldtrip:development:realtime FieldTrip realtime buffer], and writes its input into that buffer, formatted as &#039;&#039;buffer data&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
FieldTrip software agents may then use the FieldTrip realtime protocol to communicate with the buffer asynchronously, reading data from BCI2000, and writing back results into BCI2000.&lt;br /&gt;
&lt;br /&gt;
BCI2000 [[BCI2000 Glossary#State Variable|state variables]] may be translated into FieldTrip buffer events; in turn, buffer events are read back from the buffer into BCI2000 state variables. A dedicated FieldTrip buffer event type may be used to determine the filter&#039;s output into the BCI2000 processing pipe.&lt;br /&gt;
&lt;br /&gt;
==Location==&lt;br /&gt;
http://{{SERVERNAME}}/svn/trunk/src/contrib/SignalProcessing/FieldTripBuffer&lt;br /&gt;
&lt;br /&gt;
==Versioning==&lt;br /&gt;
===Authors===&lt;br /&gt;
Jürgen Mellinger, Robert Oostenveld&lt;br /&gt;
===Source Code Revisions===&lt;br /&gt;
*Initial development: 2044&lt;br /&gt;
*Tested under: 2094&lt;br /&gt;
*Known to compile under: 2095&lt;br /&gt;
&lt;br /&gt;
==Parameters==&lt;br /&gt;
===FTBufferAddress===&lt;br /&gt;
The FieldTrip buffer&#039;s listening address, given as an IP:port combination. Software agents connect to this address using a TCP-based protocol.&lt;br /&gt;
&lt;br /&gt;
===FTInitialOutput===&lt;br /&gt;
A matrix-valued parameter that determines dimensions as well as initial values of the filter&#039;s output signal.&lt;br /&gt;
In the matrix, rows map to signal channels, and columns map to elements.&lt;br /&gt;
&lt;br /&gt;
===FTOutputEventType===&lt;br /&gt;
A string representing the FieldTrip buffer event type that corresponds to the filter&#039;s output signal.&lt;br /&gt;
For each block of data, the &#039;&#039;FieldTripBuffer&#039;&#039; filter checks whether an event of that type has been inserted into the buffer; whenever this is the case, the filter&#039;s output signal will be updated from the values contained in the event. If the event&#039;s data dimensions do not fit the output signal, parts of the output signal will not be updated, or event data will be ignored silently.&lt;br /&gt;
&lt;br /&gt;
===FTStatesToBuffer===&lt;br /&gt;
A list of states that are to be converted into events, and written into the buffer. The buffer event type matches the BCI2000 state name.&lt;br /&gt;
An asterisk (*) represents any state name; a minus (-) sign before a state name means to exclude the respective state from the transmission.&lt;br /&gt;
States may either be written to, or read from, the buffer, but not both; states listed in the &#039;&#039;FTStatesFromBuffer&#039;&#039; parameter are always excluded from writing.&lt;br /&gt;
&lt;br /&gt;
===FTStatesFromBuffer===&lt;br /&gt;
A list of states that are to be set from events. When no event of matching type has been posted to the buffer, the state will not change; when multiple events have been posted since the FieldTrip buffer processed the last block of data, then the most recent event determines the state&#039;s value.&lt;br /&gt;
&lt;br /&gt;
==States==&lt;br /&gt;
Any state name may be specified in the &#039;&#039;FTStatesToBuffer&#039;&#039; and &#039;&#039;FTStatesFromBuffer&#039;&#039; parameters.&lt;br /&gt;
&lt;br /&gt;
==See also==&lt;br /&gt;
[[User Reference:MatlabFilter]], [[Programming Reference:MatlabFilter]]&lt;br /&gt;
&lt;br /&gt;
[[Category:Filters]]&lt;br /&gt;
[[Category:Signal Processing]]&lt;br /&gt;
[[Category:Matlab]]&lt;br /&gt;
[[Category:Development]]&lt;br /&gt;
[[Category:External Interfaces]]&lt;/div&gt;</summary>
		<author><name>Roostenveld</name></author>
	</entry>
	<entry>
		<id>https://www.bci2000.org/mediawiki/index.php?title=Contributions:ctfneurod&amp;diff=4583</id>
		<title>Contributions:ctfneurod</title>
		<link rel="alternate" type="text/html" href="https://www.bci2000.org/mediawiki/index.php?title=Contributions:ctfneurod&amp;diff=4583"/>
		<updated>2008-10-22T14:50:58Z</updated>

		<summary type="html">&lt;p&gt;Roostenveld: added detailed build instructions, based on Donders system&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Synopsis==&lt;br /&gt;
&#039;&#039;ctfneurod&#039;&#039; is a relay program that reads data from the CTF Acq program&#039;s RealTime &lt;br /&gt;
interface, and provides them to client software over a TCP socket.&lt;br /&gt;
On the client side, &#039;&#039;ctfneurod&#039;&#039; implements the Neuroscan Acquire protocol, allowing&lt;br /&gt;
to use the [[Contributions:NeuroscanADC|BCI2000 Neuroscan Source Module]] for MEG&lt;br /&gt;
data acquisition.&lt;br /&gt;
&lt;br /&gt;
==Location==&lt;br /&gt;
http://{{SERVERNAME}}/svn/trunk/src/contrib/SignalSource/Neuroscan/ctfneurod&lt;br /&gt;
&lt;br /&gt;
==Author==&lt;br /&gt;
&#039;&#039;ctfneurod&#039;&#039; was written by &amp;lt;tt&amp;gt;juergen.mellinger@uni-tuebingen.de&amp;lt;/tt&amp;gt;. When using it in your&lt;br /&gt;
research, please give credit by citing the following reference:&lt;br /&gt;
&lt;br /&gt;
J. Mellinger, G. Schalk, C. Braun, H. Preissl, W. Rosenstiel, N. Birbaumer, A. K&amp;amp;uuml;bler:&lt;br /&gt;
An MEG-based brain-computer interface (BCI), NeuroImage 2007 Mar;36(3):581-93.&lt;br /&gt;
&lt;br /&gt;
==Compiling and Linking==&lt;br /&gt;
&#039;&#039;ctfneurod&#039;&#039; requires CTF&#039;s development SDK. It links against the &amp;lt;tt&amp;gt;ctfds&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;ctfutil&amp;lt;/tt&amp;gt; libraries, and requires the associated header files to compile. If the CTF development SDK is not yet installed on your CTF acquisition system, you can find it on a CDROM labeled &amp;quot;C++ Dataset Library&amp;quot;. You should copy &amp;quot;/mnt/cdrom/develop.linux&amp;quot; to your home directory, resulting in the following files and directories&lt;br /&gt;
&lt;br /&gt;
&amp;lt;tt&amp;gt;&lt;br /&gt;
/home/meg/develop.linux/code &amp;lt;br&amp;gt;&lt;br /&gt;
/home/meg/develop.linux/doc  &amp;lt;br&amp;gt;&lt;br /&gt;
/home/meg/develop.linux/Makefile  &amp;lt;br&amp;gt;&lt;br /&gt;
/home/meg/develop.linux/Makefile.local  &amp;lt;br&amp;gt;&lt;br /&gt;
develop.linux/obj.linux&lt;br /&gt;
&amp;lt;/tt&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The ctfneurod directory from BCI2000 should be copied into /home/meg/develop.linux/code/ctfneurod and the Makefile should be edited to list ctfneurod as one of the &amp;quot;PROGS&amp;quot; to be compiled. Subsequently you can type &amp;quot;make&amp;quot; on the linux command line and all programs, including ctfneurod will be compiled.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Usage==&lt;br /&gt;
&#039;&#039;ctfneurod&#039;&#039; runs on the data acquisition machine connected to the MEG system, and must be started before CTF&#039;s Acq program. When started up, &#039;&#039;ctfneurod&#039;&#039; waits for&lt;br /&gt;
the Acq software to begin streaming data, and acts like a Neuroscan Acquire server once &lt;br /&gt;
data is available.&lt;br /&gt;
Its command line syntax is&lt;br /&gt;
&lt;br /&gt;
  ctfneurod &amp;lt;listening address&amp;gt; &amp;lt;options&amp;gt;&lt;br /&gt;
&lt;br /&gt;
There, the server&#039;s listening address is specified in the form &amp;lt;tt&amp;gt;&amp;lt;ip&amp;gt;:&amp;lt;port&amp;gt;&amp;lt;/tt&amp;gt;, and defaults to&lt;br /&gt;
&amp;lt;tt&amp;gt;localhost:4000&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
==Options==&lt;br /&gt;
===&amp;lt;tt&amp;gt;--pattern &amp;lt;regular expression&amp;gt;&amp;lt;/tt&amp;gt;===&lt;br /&gt;
Restricts transmission to a subset of channels available from the Acq RealTime interface.&lt;br /&gt;
The subset is defined by matching an extended regular expression against channel names, e.g.&lt;br /&gt;
&amp;lt;tt&amp;gt;ctfneurod --pattern &amp;quot;^MLC.*&amp;quot;&amp;lt;/tt&amp;gt;&lt;br /&gt;
will only transfer channels whose names begin with &amp;quot;MLC&amp;quot;.&lt;br /&gt;
The default is to transfer all channels that are present on the Acq side.&lt;br /&gt;
&lt;br /&gt;
===&amp;lt;tt&amp;gt;--freqCorrection &amp;lt;factor&amp;gt;&amp;lt;/tt&amp;gt;===&lt;br /&gt;
Normally, the sampling rate on the Neuroscan side is reported to be identical to the&lt;br /&gt;
one on the Acq side. When a correction factor is specified, this is multiplied onto the&lt;br /&gt;
Acq sampling rate prior to reporting the sampling rate.&lt;br /&gt;
&lt;br /&gt;
===&amp;lt;tt&amp;gt;--outputFormat &amp;lt;nbits&amp;gt;&amp;lt;/tt&amp;gt;===&lt;br /&gt;
Defines the bit depth of data transmission, which is either 16 or 32, and defaults to 16.&lt;br /&gt;
When 16 bit data transmission is selected, the lower two bits of data are ignored to avoid &lt;br /&gt;
numeric overflow.&lt;br /&gt;
&lt;br /&gt;
===&amp;lt;tt&amp;gt;--disableHP&amp;lt;/tt&amp;gt;===&lt;br /&gt;
By default, ctfneurod applies a first-order HP filter with a corner frequency of 0.1Hz&lt;br /&gt;
to Acq data prior to transmission. If this is undesired, use the --disableHP option to&lt;br /&gt;
disable HP filtering.&lt;br /&gt;
&lt;br /&gt;
==See also==&lt;br /&gt;
[[Contributions:ADCs]]&lt;br /&gt;
&lt;br /&gt;
[[Category:Contributions]][[Category:Data Acquisition]]&lt;/div&gt;</summary>
		<author><name>Roostenveld</name></author>
	</entry>
	<entry>
		<id>https://www.bci2000.org/mediawiki/index.php?title=Programming_Tutorial:Implementing_a_simple_Matlab-based_Filter&amp;diff=4057</id>
		<title>Programming Tutorial:Implementing a simple Matlab-based Filter</title>
		<link rel="alternate" type="text/html" href="https://www.bci2000.org/mediawiki/index.php?title=Programming_Tutorial:Implementing_a_simple_Matlab-based_Filter&amp;diff=4057"/>
		<updated>2008-08-13T20:30:37Z</updated>

		<summary type="html">&lt;p&gt;Roostenveld: /* An Example Algorithm based on an Existing Matlab Function */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Online Algorithm Verification==&lt;br /&gt;
In the field of BCI signal processing research, novel methods are often proposed and tested on the basis of existing data.&lt;br /&gt;
While this is adequate as a research strategy, it is important to keep in mind that timely feedback of brain signal classification is an essential element of a BCI.&lt;br /&gt;
From this, it is clear that, ideally, a proposed BCI signal processing or classification method should be verified with respect to its viability and usefulness in a true online setting, providing feedback to the participant causally and in real-time.&lt;br /&gt;
&lt;br /&gt;
BCI2000 facilitates the transformation of an existing offline data analysis method into a functional online system.&lt;br /&gt;
*For signal processing components, it provides a convenient, straightforward programming interface.&lt;br /&gt;
*A signal processing component may be implemented as a set of Matlab scripts.&lt;br /&gt;
&lt;br /&gt;
Still, it is easy to underestimate the effort required to transform an existing offline implementation of a signal processing algorithm into a functional online implementation. While BCI2000 tries to make the transformation as simple as possible, it cannot remove the effort required to deal with chunks of data, which implies the need of &lt;br /&gt;
*buffering -- rather than having immediate access to a continuous data set, it may be necessary to maintain an additional data buffer;&lt;br /&gt;
*with the Matlab interface, maintaining a consistent state between subsequent calls to the processing script.&lt;br /&gt;
&lt;br /&gt;
==An Example Algorithm in MATLAB==&lt;br /&gt;
&lt;br /&gt;
In this scenario, we use a simple, straightforward BCI signal processing algorithm, to show the steps necessary to modify the algorithm such &lt;br /&gt;
that it may be used to build an online system in BCI2000.&lt;br /&gt;
&lt;br /&gt;
In the example, signal processing consists of IIR bandpass filtering, followed with RMS envelope computation, and linear classification.&lt;br /&gt;
A typical Matlab implementation of that algorithm might consist of about ten lines:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
function class_result = classify( data, band, classifier );&lt;br /&gt;
&lt;br /&gt;
% Use as&lt;br /&gt;
%   class_result = classify( data, band, classifier )&lt;br /&gt;
%&lt;br /&gt;
% This function takes raw data as a [channels x samples] vector in the &#039;data&#039;&lt;br /&gt;
% input variable. &lt;br /&gt;
%&lt;br /&gt;
% Then, it computes bandpower features for the band specified in the &#039;band&#039;&lt;br /&gt;
% input variable, which is a number between 0 and 0.5, specifying&lt;br /&gt;
% center frequency in terms of the sampling rate.&lt;br /&gt;
%&lt;br /&gt;
% As a last step, it applies the &#039;classifier&#039; matrix to the features in order&lt;br /&gt;
% to obtain a single classification result for each sample. The &#039;classifier&#039; &lt;br /&gt;
% vector specifies a classification weight for each processed channel.&lt;br /&gt;
% &lt;br /&gt;
% The result is a single classification result for each sample.&lt;br /&gt;
%&lt;br /&gt;
% This requires the Matlab signal processing toolbox.&lt;br /&gt;
&lt;br /&gt;
% Design bandpass filters and apply them to the data matrix.&lt;br /&gt;
% The filtered data will contain bandpass filtered data as channels.&lt;br /&gt;
[n,Wn]=buttord(band*[0.9 1.1]/2,band*[0.7 1.4]/2,1,60);&lt;br /&gt;
[b,a]=butter(n,Wn);&lt;br /&gt;
processed_data = filter(b,a,data);&lt;br /&gt;
&lt;br /&gt;
% For demodulation, rectify and apply a low pass.&lt;br /&gt;
[b,a]=butter(1,band/4);&lt;br /&gt;
processed_data = filter(b,a,abs(processed_data));&lt;br /&gt;
&lt;br /&gt;
% Finally, apply the linear classifier.&lt;br /&gt;
class_result = processed_data * classifier;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Note that, to be viable in an online environment, an algorithm must operate &lt;br /&gt;
on its signal in a causal way, i.e. it may not use future samples in order to&lt;br /&gt;
process present samples. (Still, a certain amount of non-causality may be possible&lt;br /&gt;
by windowed operation on buffered data, although this will increase the effective&lt;br /&gt;
delay between input and output data.)&lt;br /&gt;
&lt;br /&gt;
Also note that the &amp;lt;tt&amp;gt;classify&amp;lt;/tt&amp;gt; function omits spatial filtering -- in Matlab, &lt;br /&gt;
this may be done easily by pre-multiplying the data with a spatial filter matrix&lt;br /&gt;
if desired.&lt;br /&gt;
&lt;br /&gt;
==Translating the existing MATLAB code into BCI2000 functions==&lt;br /&gt;
&lt;br /&gt;
For use with BCI2000, the signal processing code needs to be cast into a form&lt;br /&gt;
that suits the BCI2000 filter interface.&lt;br /&gt;
In this event-based model, portions of code are called at certain times to configure&lt;br /&gt;
a filter component&#039;s internal state, and to act upon the signal in chunks on its way through&lt;br /&gt;
the BCI2000 chain of filters.&lt;br /&gt;
&lt;br /&gt;
===Process===&lt;br /&gt;
&lt;br /&gt;
The most central event in the filter interface is the &#039;&#039;Process&#039;&#039; event.&lt;br /&gt;
The &#039;&#039;Process&#039;&#039; event handler receives a signal input, will process this input&lt;br /&gt;
according to the filter component&#039;s role in the signal processing chain, and &lt;br /&gt;
return the result of processing in a signal output variable.&lt;br /&gt;
It is important to understand that the &#039;&#039;Process&#039;&#039; handler is called separately&lt;br /&gt;
for each chunk of data, and thus does not see the signal in its entirety.&lt;br /&gt;
Also, the size of data blocks (chunks) is freely configurable by the user, although&lt;br /&gt;
restricted to limits stemming from recording hardware properties.&lt;br /&gt;
This implies that &#039;&#039;Process&#039;&#039; scripts may not depend on a certain&lt;br /&gt;
data block size, and will sometimes need to maintain their own data buffers when &lt;br /&gt;
the algorithm in question operates on windows of data rather than continuously.&lt;br /&gt;
In the current example, this is not the case, so we need not maintain an internal &lt;br /&gt;
data buffer.&lt;br /&gt;
Still, we need to maintain an internal state between calls to the &#039;&#039;Process&#039;&#039; &lt;br /&gt;
handler in order to preserve the state of IIR filter delays.&lt;br /&gt;
This will allow continuous operation on the signal with the same processing result&lt;br /&gt;
as in the offline version of the algorithm.&lt;br /&gt;
&lt;br /&gt;
The algorithm&#039;s online version in the &amp;lt;tt&amp;gt;bci_Process&amp;lt;/tt&amp;gt; script will thus be:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
function out_signal = bci_Process( in_signal )&lt;br /&gt;
&lt;br /&gt;
% Process a single block of data by applying a filter to in_signal, &lt;br /&gt;
% and return the result in out_signal.&lt;br /&gt;
% Signal dimensions are ( channels x samples ).&lt;br /&gt;
&lt;br /&gt;
% We use global variables to store classifier, &lt;br /&gt;
% filter coefficients and filter state.&lt;br /&gt;
global a b z lpa lpb lpz classifier;&lt;br /&gt;
&lt;br /&gt;
[out_signal, z] = filter( b, a, in_signal, z );&lt;br /&gt;
out_signal = abs( out_signal );&lt;br /&gt;
[out_signal, lpz] = filter( lpb, lpa, out_signal, lpz );&lt;br /&gt;
out_signal = out_signal * classifier;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Initialize=== &lt;br /&gt;
&lt;br /&gt;
Determination of filter coefficients is part of per-run initialization, and occurs in the &lt;br /&gt;
&#039;&#039;Initialize&#039;&#039; event handler:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
function bci_Initialize( in_signal_dims, out_signal_dims )&lt;br /&gt;
&lt;br /&gt;
% Perform configuration for the bci_Process script.&lt;br /&gt;
&lt;br /&gt;
% Parameters and states are global variables.&lt;br /&gt;
global bci_Parameters bci_States;&lt;br /&gt;
&lt;br /&gt;
% We use global variables to store classifier vector, &lt;br /&gt;
% filter coefficients and filter state.&lt;br /&gt;
global a b z lpa lpb lpz classifier;&lt;br /&gt;
&lt;br /&gt;
% Configure the Bandpass filter&lt;br /&gt;
band = str2double( bci_Parameters.Passband ) / str2double( bci_Parameters.SamplingRate );&lt;br /&gt;
[n,Wn]=buttord(band*[0.9 1.1]/2,band*[0.7 1.4]/2,1,60);&lt;br /&gt;
[b,a]=butter(n,Wn);&lt;br /&gt;
z=zeros(max(length(a),length(b))-1,in_signal_dims(1));&lt;br /&gt;
&lt;br /&gt;
% Configure the Lowpass filter&lt;br /&gt;
[lpb,lpa]=butter(1,band/4);&lt;br /&gt;
lpz=zeros(max(length(lpa),length(lpb))-1,in_signal_dims(1));&lt;br /&gt;
&lt;br /&gt;
% Configure the Classifier vector&lt;br /&gt;
classifier = str2double( bci_Parameters.ClassVector );&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===StartRun===&lt;br /&gt;
&lt;br /&gt;
In addition, we need to reset filter state at the beginning of each run, using the &#039;&#039;StartRun&#039;&#039;&lt;br /&gt;
event handler:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
function bci_StartRun&lt;br /&gt;
&lt;br /&gt;
% Reset filter state at the beginning of a run.&lt;br /&gt;
&lt;br /&gt;
global z lpz;&lt;br /&gt;
z   = zeros(size(z));&lt;br /&gt;
lpz = zeros(size(lpz));&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Constructor===&lt;br /&gt;
To complete the Matlab filter code, we need to declare the &#039;&#039;Band&#039;&#039; and &#039;&#039;Classifier&#039;&#039;&lt;br /&gt;
parameters in a &#039;&#039;Constructor&#039;&#039; event handler:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
function [ parameters, states ] = bci_Construct&lt;br /&gt;
&lt;br /&gt;
% Request BCI2000 parameters by returning parameter definition &lt;br /&gt;
% lines as demonstrated below.&lt;br /&gt;
&lt;br /&gt;
parameters = { ...&lt;br /&gt;
  [ &#039;BPClassifier float Passband= 10 10 0 % % // Bandpass frequency in Hz&#039; ] ...&lt;br /&gt;
  [ &#039;BPClassifier matrix ClassVector= 1 1 1 0 % % // Linear classifier vector&#039; ] ...&lt;br /&gt;
};&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Preflight===&lt;br /&gt;
&lt;br /&gt;
Finally, we need to check parameters for consistency in a &#039;&#039;Preflight&#039;&#039;&lt;br /&gt;
script, and declare the size of our filter&#039;s output signal:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
function [ out_signal_dim ] = bci_Preflight( in_signal_dim )&lt;br /&gt;
&lt;br /&gt;
% Check whether parameters are accessible, and whether&lt;br /&gt;
% parameters have values that allow for safe processing by the &lt;br /&gt;
% bci_Process function.&lt;br /&gt;
% Also, report output signal dimensions in the &#039;out_signal_dim&#039; argument.&lt;br /&gt;
&lt;br /&gt;
% Parameters and states are global variables.&lt;br /&gt;
global bci_Parameters bci_States;&lt;br /&gt;
&lt;br /&gt;
band = str2double( bci_Parameters.Passband ) / str2double( bci_Parameters.SamplingRate );&lt;br /&gt;
if( band &amp;lt;= 0 )&lt;br /&gt;
  error( &#039;The passband parameter must be greater zero&#039; );&lt;br /&gt;
elseif( band &amp;gt; 0.5 / 1.4 )&lt;br /&gt;
  error( &#039;The Passband parameter conflicts with the sampling rate&#039; ); &lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
out_signal_dim = [ 1, size( in_signal_dim, 2 ) ];&lt;br /&gt;
if( in_signal_dim( 1 ) ~= size( bci_Parameters.ClassVector, 2 ) )&lt;br /&gt;
  error( &#039;ClassVector length must match the input signal&#039;&#039;s number of channels&#039; );&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==See also==&lt;br /&gt;
&lt;br /&gt;
[[Programming Reference:MatlabFilter]], &lt;br /&gt;
[[User Reference:MatlabFilter]], &lt;br /&gt;
[[Contributions:FieldTripBuffer]]&lt;br /&gt;
&lt;br /&gt;
[[Category:Tutorial]]&lt;br /&gt;
[[Category:Signal Processing]]&lt;br /&gt;
[[Category:Development]]&lt;br /&gt;
[[Category:Matlab]]&lt;br /&gt;
[[Category:External Interfaces]]&lt;/div&gt;</summary>
		<author><name>Roostenveld</name></author>
	</entry>
	<entry>
		<id>https://www.bci2000.org/mediawiki/index.php?title=Programming_Tutorial:Implementing_a_simple_Matlab-based_Filter&amp;diff=4056</id>
		<title>Programming Tutorial:Implementing a simple Matlab-based Filter</title>
		<link rel="alternate" type="text/html" href="https://www.bci2000.org/mediawiki/index.php?title=Programming_Tutorial:Implementing_a_simple_Matlab-based_Filter&amp;diff=4056"/>
		<updated>2008-08-13T20:30:08Z</updated>

		<summary type="html">&lt;p&gt;Roostenveld: /* An Example Algorithm */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Online Algorithm Verification==&lt;br /&gt;
In the field of BCI signal processing research, novel methods are often proposed and tested on the basis of existing data.&lt;br /&gt;
While this is adequate as a research strategy, it is important to keep in mind that timely feedback of brain signal classification is an essential element of a BCI.&lt;br /&gt;
From this, it is clear that, ideally, a proposed BCI signal processing or classification method should be verified with respect to its viability and usefulness in a true online setting, providing feedback to the participant causally and in real-time.&lt;br /&gt;
&lt;br /&gt;
BCI2000 facilitates the transformation of an existing offline data analysis method into a functional online system.&lt;br /&gt;
*For signal processing components, it provides a convenient, straightforward programming interface.&lt;br /&gt;
*A signal processing component may be implemented as a set of Matlab scripts.&lt;br /&gt;
&lt;br /&gt;
Still, it is easy to underestimate the effort required to transform an existing offline implementation of a signal processing algorithm into a functional online implementation. While BCI2000 tries to make the transformation as simple as possible, it cannot remove the effort required to deal with chunks of data, which implies the need of &lt;br /&gt;
*buffering -- rather than having immediate access to a continuous data set, it may be necessary to maintain an additional data buffer;&lt;br /&gt;
*with the Matlab interface, maintaining a consistent state between subsequent calls to the processing script.&lt;br /&gt;
&lt;br /&gt;
==An Example Algorithm based on an Existing Matlab Function==&lt;br /&gt;
&lt;br /&gt;
In this scenario, we use a simple, straightforward BCI signal processing algorithm, to show the steps necessary to modify the algorithm such &lt;br /&gt;
that it may be used to build an online system in BCI2000.&lt;br /&gt;
&lt;br /&gt;
In the example, signal processing consists of IIR bandpass filtering, followed with RMS envelope computation, and linear classification.&lt;br /&gt;
A typical Matlab implementation of that algorithm might consist of about ten lines:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
function class_result = classify( data, band, classifier );&lt;br /&gt;
&lt;br /&gt;
% Use as&lt;br /&gt;
%   class_result = classify( data, band, classifier )&lt;br /&gt;
%&lt;br /&gt;
% This function takes raw data as a [channels x samples] vector in the &#039;data&#039;&lt;br /&gt;
% input variable. &lt;br /&gt;
%&lt;br /&gt;
% Then, it computes bandpower features for the band specified in the &#039;band&#039;&lt;br /&gt;
% input variable, which is a number between 0 and 0.5, specifying&lt;br /&gt;
% center frequency in terms of the sampling rate.&lt;br /&gt;
%&lt;br /&gt;
% As a last step, it applies the &#039;classifier&#039; matrix to the features in order&lt;br /&gt;
% to obtain a single classification result for each sample. The &#039;classifier&#039; &lt;br /&gt;
% vector specifies a classification weight for each processed channel.&lt;br /&gt;
% &lt;br /&gt;
% The result is a single classification result for each sample.&lt;br /&gt;
%&lt;br /&gt;
% This requires the Matlab signal processing toolbox.&lt;br /&gt;
&lt;br /&gt;
% Design bandpass filters and apply them to the data matrix.&lt;br /&gt;
% The filtered data will contain bandpass filtered data as channels.&lt;br /&gt;
[n,Wn]=buttord(band*[0.9 1.1]/2,band*[0.7 1.4]/2,1,60);&lt;br /&gt;
[b,a]=butter(n,Wn);&lt;br /&gt;
processed_data = filter(b,a,data);&lt;br /&gt;
&lt;br /&gt;
% For demodulation, rectify and apply a low pass.&lt;br /&gt;
[b,a]=butter(1,band/4);&lt;br /&gt;
processed_data = filter(b,a,abs(processed_data));&lt;br /&gt;
&lt;br /&gt;
% Finally, apply the linear classifier.&lt;br /&gt;
class_result = processed_data * classifier;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Note that, to be viable in an online environment, an algorithm must operate &lt;br /&gt;
on its signal in a causal way, i.e. it may not use future samples in order to&lt;br /&gt;
process present samples. (Still, a certain amount of non-causality may be possible&lt;br /&gt;
by windowed operation on buffered data, although this will increase the effective&lt;br /&gt;
delay between input and output data.)&lt;br /&gt;
&lt;br /&gt;
Also note that the &amp;lt;tt&amp;gt;classify&amp;lt;/tt&amp;gt; function omits spatial filtering -- in Matlab, &lt;br /&gt;
this may be done easily by pre-multiplying the data with a spatial filter matrix&lt;br /&gt;
if desired.&lt;br /&gt;
&lt;br /&gt;
==Translating the existing MATLAB code into BCI2000 functions==&lt;br /&gt;
&lt;br /&gt;
For use with BCI2000, the signal processing code needs to be cast into a form&lt;br /&gt;
that suits the BCI2000 filter interface.&lt;br /&gt;
In this event-based model, portions of code are called at certain times to configure&lt;br /&gt;
a filter component&#039;s internal state, and to act upon the signal in chunks on its way through&lt;br /&gt;
the BCI2000 chain of filters.&lt;br /&gt;
&lt;br /&gt;
===Process===&lt;br /&gt;
&lt;br /&gt;
The most central event in the filter interface is the &#039;&#039;Process&#039;&#039; event.&lt;br /&gt;
The &#039;&#039;Process&#039;&#039; event handler receives a signal input, will process this input&lt;br /&gt;
according to the filter component&#039;s role in the signal processing chain, and &lt;br /&gt;
return the result of processing in a signal output variable.&lt;br /&gt;
It is important to understand that the &#039;&#039;Process&#039;&#039; handler is called separately&lt;br /&gt;
for each chunk of data, and thus does not see the signal in its entirety.&lt;br /&gt;
Also, the size of data blocks (chunks) is freely configurable by the user, although&lt;br /&gt;
restricted to limits stemming from recording hardware properties.&lt;br /&gt;
This implies that &#039;&#039;Process&#039;&#039; scripts may not depend on a certain&lt;br /&gt;
data block size, and will sometimes need to maintain their own data buffers when &lt;br /&gt;
the algorithm in question operates on windows of data rather than continuously.&lt;br /&gt;
In the current example, this is not the case, so we need not maintain an internal &lt;br /&gt;
data buffer.&lt;br /&gt;
Still, we need to maintain an internal state between calls to the &#039;&#039;Process&#039;&#039; &lt;br /&gt;
handler in order to preserve the state of IIR filter delays.&lt;br /&gt;
This will allow continuous operation on the signal with the same processing result&lt;br /&gt;
as in the offline version of the algorithm.&lt;br /&gt;
&lt;br /&gt;
The algorithm&#039;s online version in the &amp;lt;tt&amp;gt;bci_Process&amp;lt;/tt&amp;gt; script will thus be:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
function out_signal = bci_Process( in_signal )&lt;br /&gt;
&lt;br /&gt;
% Process a single block of data by applying a filter to in_signal, &lt;br /&gt;
% and return the result in out_signal.&lt;br /&gt;
% Signal dimensions are ( channels x samples ).&lt;br /&gt;
&lt;br /&gt;
% We use global variables to store classifier, &lt;br /&gt;
% filter coefficients and filter state.&lt;br /&gt;
global a b z lpa lpb lpz classifier;&lt;br /&gt;
&lt;br /&gt;
[out_signal, z] = filter( b, a, in_signal, z );&lt;br /&gt;
out_signal = abs( out_signal );&lt;br /&gt;
[out_signal, lpz] = filter( lpb, lpa, out_signal, lpz );&lt;br /&gt;
out_signal = out_signal * classifier;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Initialize=== &lt;br /&gt;
&lt;br /&gt;
Determination of filter coefficients is part of per-run initialization, and occurs in the &lt;br /&gt;
&#039;&#039;Initialize&#039;&#039; event handler:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
function bci_Initialize( in_signal_dims, out_signal_dims )&lt;br /&gt;
&lt;br /&gt;
% Perform configuration for the bci_Process script.&lt;br /&gt;
&lt;br /&gt;
% Parameters and states are global variables.&lt;br /&gt;
global bci_Parameters bci_States;&lt;br /&gt;
&lt;br /&gt;
% We use global variables to store classifier vector, &lt;br /&gt;
% filter coefficients and filter state.&lt;br /&gt;
global a b z lpa lpb lpz classifier;&lt;br /&gt;
&lt;br /&gt;
% Configure the Bandpass filter&lt;br /&gt;
band = str2double( bci_Parameters.Passband ) / str2double( bci_Parameters.SamplingRate );&lt;br /&gt;
[n,Wn]=buttord(band*[0.9 1.1]/2,band*[0.7 1.4]/2,1,60);&lt;br /&gt;
[b,a]=butter(n,Wn);&lt;br /&gt;
z=zeros(max(length(a),length(b))-1,in_signal_dims(1));&lt;br /&gt;
&lt;br /&gt;
% Configure the Lowpass filter&lt;br /&gt;
[lpb,lpa]=butter(1,band/4);&lt;br /&gt;
lpz=zeros(max(length(lpa),length(lpb))-1,in_signal_dims(1));&lt;br /&gt;
&lt;br /&gt;
% Configure the Classifier vector&lt;br /&gt;
classifier = str2double( bci_Parameters.ClassVector );&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===StartRun===&lt;br /&gt;
&lt;br /&gt;
In addition, we need to reset filter state at the beginning of each run, using the &#039;&#039;StartRun&#039;&#039;&lt;br /&gt;
event handler:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
function bci_StartRun&lt;br /&gt;
&lt;br /&gt;
% Reset filter state at the beginning of a run.&lt;br /&gt;
&lt;br /&gt;
global z lpz;&lt;br /&gt;
z   = zeros(size(z));&lt;br /&gt;
lpz = zeros(size(lpz));&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Constructor===&lt;br /&gt;
To complete the Matlab filter code, we need to declare the &#039;&#039;Band&#039;&#039; and &#039;&#039;Classifier&#039;&#039;&lt;br /&gt;
parameters in a &#039;&#039;Constructor&#039;&#039; event handler:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
function [ parameters, states ] = bci_Construct&lt;br /&gt;
&lt;br /&gt;
% Request BCI2000 parameters by returning parameter definition &lt;br /&gt;
% lines as demonstrated below.&lt;br /&gt;
&lt;br /&gt;
parameters = { ...&lt;br /&gt;
  [ &#039;BPClassifier float Passband= 10 10 0 % % // Bandpass frequency in Hz&#039; ] ...&lt;br /&gt;
  [ &#039;BPClassifier matrix ClassVector= 1 1 1 0 % % // Linear classifier vector&#039; ] ...&lt;br /&gt;
};&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Preflight===&lt;br /&gt;
&lt;br /&gt;
Finally, we need to check parameters for consistency in a &#039;&#039;Preflight&#039;&#039;&lt;br /&gt;
script, and declare the size of our filter&#039;s output signal:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
function [ out_signal_dim ] = bci_Preflight( in_signal_dim )&lt;br /&gt;
&lt;br /&gt;
% Check whether parameters are accessible, and whether&lt;br /&gt;
% parameters have values that allow for safe processing by the &lt;br /&gt;
% bci_Process function.&lt;br /&gt;
% Also, report output signal dimensions in the &#039;out_signal_dim&#039; argument.&lt;br /&gt;
&lt;br /&gt;
% Parameters and states are global variables.&lt;br /&gt;
global bci_Parameters bci_States;&lt;br /&gt;
&lt;br /&gt;
band = str2double( bci_Parameters.Passband ) / str2double( bci_Parameters.SamplingRate );&lt;br /&gt;
if( band &amp;lt;= 0 )&lt;br /&gt;
  error( &#039;The passband parameter must be greater zero&#039; );&lt;br /&gt;
elseif( band &amp;gt; 0.5 / 1.4 )&lt;br /&gt;
  error( &#039;The Passband parameter conflicts with the sampling rate&#039; ); &lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
out_signal_dim = [ 1, size( in_signal_dim, 2 ) ];&lt;br /&gt;
if( in_signal_dim( 1 ) ~= size( bci_Parameters.ClassVector, 2 ) )&lt;br /&gt;
  error( &#039;ClassVector length must match the input signal&#039;&#039;s number of channels&#039; );&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==See also==&lt;br /&gt;
&lt;br /&gt;
[[Programming Reference:MatlabFilter]], &lt;br /&gt;
[[User Reference:MatlabFilter]], &lt;br /&gt;
[[Contributions:FieldTripBuffer]]&lt;br /&gt;
&lt;br /&gt;
[[Category:Tutorial]]&lt;br /&gt;
[[Category:Signal Processing]]&lt;br /&gt;
[[Category:Development]]&lt;br /&gt;
[[Category:Matlab]]&lt;br /&gt;
[[Category:External Interfaces]]&lt;/div&gt;</summary>
		<author><name>Roostenveld</name></author>
	</entry>
	<entry>
		<id>https://www.bci2000.org/mediawiki/index.php?title=Programming_Tutorial:Implementing_a_simple_Matlab-based_Filter&amp;diff=4055</id>
		<title>Programming Tutorial:Implementing a simple Matlab-based Filter</title>
		<link rel="alternate" type="text/html" href="https://www.bci2000.org/mediawiki/index.php?title=Programming_Tutorial:Implementing_a_simple_Matlab-based_Filter&amp;diff=4055"/>
		<updated>2008-08-13T20:28:54Z</updated>

		<summary type="html">&lt;p&gt;Roostenveld: /* Transformation into BCI2000 Matlab Scripts */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Online Algorithm Verification==&lt;br /&gt;
In the field of BCI signal processing research, novel methods are often proposed and tested on the basis of existing data.&lt;br /&gt;
While this is adequate as a research strategy, it is important to keep in mind that timely feedback of brain signal classification is an essential element of a BCI.&lt;br /&gt;
From this, it is clear that, ideally, a proposed BCI signal processing or classification method should be verified with respect to its viability and usefulness in a true online setting, providing feedback to the participant causally and in real-time.&lt;br /&gt;
&lt;br /&gt;
BCI2000 facilitates the transformation of an existing offline data analysis method into a functional online system.&lt;br /&gt;
*For signal processing components, it provides a convenient, straightforward programming interface.&lt;br /&gt;
*A signal processing component may be implemented as a set of Matlab scripts.&lt;br /&gt;
&lt;br /&gt;
Still, it is easy to underestimate the effort required to transform an existing offline implementation of a signal processing algorithm into a functional online implementation. While BCI2000 tries to make the transformation as simple as possible, it cannot remove the effort required to deal with chunks of data, which implies the need of &lt;br /&gt;
*buffering -- rather than having immediate access to a continuous data set, it may be necessary to maintain an additional data buffer;&lt;br /&gt;
*with the Matlab interface, maintaining a consistent state between subsequent calls to the processing script.&lt;br /&gt;
&lt;br /&gt;
==An Example Algorithm==&lt;br /&gt;
In this scenario, we use a simple, straightforward BCI signal processing algorithm, to show the steps necessary to modify the algorithm such &lt;br /&gt;
that it may be used to build an online system in BCI2000.&lt;br /&gt;
&lt;br /&gt;
In the example, signal processing consists of IIR bandpass filtering, followed with RMS envelope computation, and linear classification.&lt;br /&gt;
A typical Matlab implementation of that algorithm might consist of about ten lines:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
function class_result = classify( data, band, classifier );&lt;br /&gt;
&lt;br /&gt;
% Use as&lt;br /&gt;
%   class_result = classify( data, band, classifier )&lt;br /&gt;
%&lt;br /&gt;
% This function takes raw data as a [channels x samples] vector in the &#039;data&#039;&lt;br /&gt;
% input variable. &lt;br /&gt;
%&lt;br /&gt;
% Then, it computes bandpower features for the band specified in the &#039;band&#039;&lt;br /&gt;
% input variable, which is a number between 0 and 0.5, specifying&lt;br /&gt;
% center frequency in terms of the sampling rate.&lt;br /&gt;
%&lt;br /&gt;
% As a last step, it applies the &#039;classifier&#039; matrix to the features in order&lt;br /&gt;
% to obtain a single classification result for each sample. The &#039;classifier&#039; &lt;br /&gt;
% vector specifies a classification weight for each processed channel.&lt;br /&gt;
% &lt;br /&gt;
% The result is a single classification result for each sample.&lt;br /&gt;
%&lt;br /&gt;
% This requires the Matlab signal processing toolbox.&lt;br /&gt;
&lt;br /&gt;
% Design bandpass filters and apply them to the data matrix.&lt;br /&gt;
% The filtered data will contain bandpass filtered data as channels.&lt;br /&gt;
[n,Wn]=buttord(band*[0.9 1.1]/2,band*[0.7 1.4]/2,1,60);&lt;br /&gt;
[b,a]=butter(n,Wn);&lt;br /&gt;
processed_data = filter(b,a,data);&lt;br /&gt;
&lt;br /&gt;
% For demodulation, rectify and apply a low pass.&lt;br /&gt;
[b,a]=butter(1,band/4);&lt;br /&gt;
processed_data = filter(b,a,abs(processed_data));&lt;br /&gt;
&lt;br /&gt;
% Finally, apply the linear classifier.&lt;br /&gt;
class_result = processed_data * classifier;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Note that, to be viable in an online environment, an algorithm must operate &lt;br /&gt;
on its signal in a causal way, i.e. it may not use future samples in order to&lt;br /&gt;
process present samples. (Still, a certain amount of non-causality may be possible&lt;br /&gt;
by windowed operation on buffered data, although this will increase the effective&lt;br /&gt;
delay between input and output data.)&lt;br /&gt;
&lt;br /&gt;
Also note that the &amp;lt;tt&amp;gt;classify&amp;lt;/tt&amp;gt; function omits spatial filtering -- in Matlab, &lt;br /&gt;
this may be done easily by pre-multiplying the data with a spatial filter matrix&lt;br /&gt;
if desired.&lt;br /&gt;
&lt;br /&gt;
==Translating the existing MATLAB code into BCI2000 functions==&lt;br /&gt;
&lt;br /&gt;
For use with BCI2000, the signal processing code needs to be cast into a form&lt;br /&gt;
that suits the BCI2000 filter interface.&lt;br /&gt;
In this event-based model, portions of code are called at certain times to configure&lt;br /&gt;
a filter component&#039;s internal state, and to act upon the signal in chunks on its way through&lt;br /&gt;
the BCI2000 chain of filters.&lt;br /&gt;
&lt;br /&gt;
===Process===&lt;br /&gt;
&lt;br /&gt;
The most central event in the filter interface is the &#039;&#039;Process&#039;&#039; event.&lt;br /&gt;
The &#039;&#039;Process&#039;&#039; event handler receives a signal input, will process this input&lt;br /&gt;
according to the filter component&#039;s role in the signal processing chain, and &lt;br /&gt;
return the result of processing in a signal output variable.&lt;br /&gt;
It is important to understand that the &#039;&#039;Process&#039;&#039; handler is called separately&lt;br /&gt;
for each chunk of data, and thus does not see the signal in its entirety.&lt;br /&gt;
Also, the size of data blocks (chunks) is freely configurable by the user, although&lt;br /&gt;
restricted to limits stemming from recording hardware properties.&lt;br /&gt;
This implies that &#039;&#039;Process&#039;&#039; scripts may not depend on a certain&lt;br /&gt;
data block size, and will sometimes need to maintain their own data buffers when &lt;br /&gt;
the algorithm in question operates on windows of data rather than continuously.&lt;br /&gt;
In the current example, this is not the case, so we need not maintain an internal &lt;br /&gt;
data buffer.&lt;br /&gt;
Still, we need to maintain an internal state between calls to the &#039;&#039;Process&#039;&#039; &lt;br /&gt;
handler in order to preserve the state of IIR filter delays.&lt;br /&gt;
This will allow continuous operation on the signal with the same processing result&lt;br /&gt;
as in the offline version of the algorithm.&lt;br /&gt;
&lt;br /&gt;
The algorithm&#039;s online version in the &amp;lt;tt&amp;gt;bci_Process&amp;lt;/tt&amp;gt; script will thus be:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
function out_signal = bci_Process( in_signal )&lt;br /&gt;
&lt;br /&gt;
% Process a single block of data by applying a filter to in_signal, &lt;br /&gt;
% and return the result in out_signal.&lt;br /&gt;
% Signal dimensions are ( channels x samples ).&lt;br /&gt;
&lt;br /&gt;
% We use global variables to store classifier, &lt;br /&gt;
% filter coefficients and filter state.&lt;br /&gt;
global a b z lpa lpb lpz classifier;&lt;br /&gt;
&lt;br /&gt;
[out_signal, z] = filter( b, a, in_signal, z );&lt;br /&gt;
out_signal = abs( out_signal );&lt;br /&gt;
[out_signal, lpz] = filter( lpb, lpa, out_signal, lpz );&lt;br /&gt;
out_signal = out_signal * classifier;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Initialize=== &lt;br /&gt;
&lt;br /&gt;
Determination of filter coefficients is part of per-run initialization, and occurs in the &lt;br /&gt;
&#039;&#039;Initialize&#039;&#039; event handler:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
function bci_Initialize( in_signal_dims, out_signal_dims )&lt;br /&gt;
&lt;br /&gt;
% Perform configuration for the bci_Process script.&lt;br /&gt;
&lt;br /&gt;
% Parameters and states are global variables.&lt;br /&gt;
global bci_Parameters bci_States;&lt;br /&gt;
&lt;br /&gt;
% We use global variables to store classifier vector, &lt;br /&gt;
% filter coefficients and filter state.&lt;br /&gt;
global a b z lpa lpb lpz classifier;&lt;br /&gt;
&lt;br /&gt;
% Configure the Bandpass filter&lt;br /&gt;
band = str2double( bci_Parameters.Passband ) / str2double( bci_Parameters.SamplingRate );&lt;br /&gt;
[n,Wn]=buttord(band*[0.9 1.1]/2,band*[0.7 1.4]/2,1,60);&lt;br /&gt;
[b,a]=butter(n,Wn);&lt;br /&gt;
z=zeros(max(length(a),length(b))-1,in_signal_dims(1));&lt;br /&gt;
&lt;br /&gt;
% Configure the Lowpass filter&lt;br /&gt;
[lpb,lpa]=butter(1,band/4);&lt;br /&gt;
lpz=zeros(max(length(lpa),length(lpb))-1,in_signal_dims(1));&lt;br /&gt;
&lt;br /&gt;
% Configure the Classifier vector&lt;br /&gt;
classifier = str2double( bci_Parameters.ClassVector );&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===StartRun===&lt;br /&gt;
&lt;br /&gt;
In addition, we need to reset filter state at the beginning of each run, using the &#039;&#039;StartRun&#039;&#039;&lt;br /&gt;
event handler:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
function bci_StartRun&lt;br /&gt;
&lt;br /&gt;
% Reset filter state at the beginning of a run.&lt;br /&gt;
&lt;br /&gt;
global z lpz;&lt;br /&gt;
z   = zeros(size(z));&lt;br /&gt;
lpz = zeros(size(lpz));&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Constructor===&lt;br /&gt;
To complete the Matlab filter code, we need to declare the &#039;&#039;Band&#039;&#039; and &#039;&#039;Classifier&#039;&#039;&lt;br /&gt;
parameters in a &#039;&#039;Constructor&#039;&#039; event handler:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
function [ parameters, states ] = bci_Construct&lt;br /&gt;
&lt;br /&gt;
% Request BCI2000 parameters by returning parameter definition &lt;br /&gt;
% lines as demonstrated below.&lt;br /&gt;
&lt;br /&gt;
parameters = { ...&lt;br /&gt;
  [ &#039;BPClassifier float Passband= 10 10 0 % % // Bandpass frequency in Hz&#039; ] ...&lt;br /&gt;
  [ &#039;BPClassifier matrix ClassVector= 1 1 1 0 % % // Linear classifier vector&#039; ] ...&lt;br /&gt;
};&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Preflight===&lt;br /&gt;
&lt;br /&gt;
Finally, we need to check parameters for consistency in a &#039;&#039;Preflight&#039;&#039;&lt;br /&gt;
script, and declare the size of our filter&#039;s output signal:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
function [ out_signal_dim ] = bci_Preflight( in_signal_dim )&lt;br /&gt;
&lt;br /&gt;
% Check whether parameters are accessible, and whether&lt;br /&gt;
% parameters have values that allow for safe processing by the &lt;br /&gt;
% bci_Process function.&lt;br /&gt;
% Also, report output signal dimensions in the &#039;out_signal_dim&#039; argument.&lt;br /&gt;
&lt;br /&gt;
% Parameters and states are global variables.&lt;br /&gt;
global bci_Parameters bci_States;&lt;br /&gt;
&lt;br /&gt;
band = str2double( bci_Parameters.Passband ) / str2double( bci_Parameters.SamplingRate );&lt;br /&gt;
if( band &amp;lt;= 0 )&lt;br /&gt;
  error( &#039;The passband parameter must be greater zero&#039; );&lt;br /&gt;
elseif( band &amp;gt; 0.5 / 1.4 )&lt;br /&gt;
  error( &#039;The Passband parameter conflicts with the sampling rate&#039; ); &lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
out_signal_dim = [ 1, size( in_signal_dim, 2 ) ];&lt;br /&gt;
if( in_signal_dim( 1 ) ~= size( bci_Parameters.ClassVector, 2 ) )&lt;br /&gt;
  error( &#039;ClassVector length must match the input signal&#039;&#039;s number of channels&#039; );&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==See also==&lt;br /&gt;
&lt;br /&gt;
[[Programming Reference:MatlabFilter]], &lt;br /&gt;
[[User Reference:MatlabFilter]], &lt;br /&gt;
[[Contributions:FieldTripBuffer]]&lt;br /&gt;
&lt;br /&gt;
[[Category:Tutorial]]&lt;br /&gt;
[[Category:Signal Processing]]&lt;br /&gt;
[[Category:Development]]&lt;br /&gt;
[[Category:Matlab]]&lt;br /&gt;
[[Category:External Interfaces]]&lt;/div&gt;</summary>
		<author><name>Roostenveld</name></author>
	</entry>
	<entry>
		<id>https://www.bci2000.org/mediawiki/index.php?title=Contributions:SignalProcessing&amp;diff=4040</id>
		<title>Contributions:SignalProcessing</title>
		<link rel="alternate" type="text/html" href="https://www.bci2000.org/mediawiki/index.php?title=Contributions:SignalProcessing&amp;diff=4040"/>
		<updated>2008-08-11T15:28:24Z</updated>

		<summary type="html">&lt;p&gt;Roostenveld: added fieldtripbuffer&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;The following signal processing filters are available in the Contributions section of BCI2000: &lt;br /&gt;
&lt;br /&gt;
*[[Contributions:FIRFilter]]: A finite impulse response filter.&lt;br /&gt;
*[[Contributions:PeakDetector]]: A peak detector filter.&lt;br /&gt;
*[[Contributions:SigfriedSigProc]]: Signal processing module SIGFRIED.&lt;br /&gt;
*[[Contributions:Statistics]]: Original statistics filtering from version 1.&lt;br /&gt;
*[[Contributions:SWSignalProcessing]]: Signal processing module for slow wave (SCP) feedback.&lt;br /&gt;
*[[Contributions:BCPy2000]]: Signal processing and application module for calling custom Python functions.&lt;br /&gt;
*[[Contributions:FieldTripBuffer]]: Realtime analysis of EEG data in MATLAB using the FieldTrip toolbox.&lt;br /&gt;
&lt;br /&gt;
==See also==&lt;br /&gt;
[[User Reference:Filters]]&lt;br /&gt;
&lt;br /&gt;
[[Category:Signal Processing]][[Category:Contents]]&lt;/div&gt;</summary>
		<author><name>Roostenveld</name></author>
	</entry>
	<entry>
		<id>https://www.bci2000.org/mediawiki/index.php?title=MARIO:Contents&amp;diff=4039</id>
		<title>MARIO:Contents</title>
		<link rel="alternate" type="text/html" href="https://www.bci2000.org/mediawiki/index.php?title=MARIO:Contents&amp;diff=4039"/>
		<updated>2008-08-11T15:25:25Z</updated>

		<summary type="html">&lt;p&gt;Roostenveld: added category matlab&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;MARIO is a collection of Matlab programs for off-line BCI2000 data analysis and prototyping of processing algorithms.&lt;br /&gt;
The following information is available about MARIO:&lt;br /&gt;
*[[MARIO Technical Documentation]]&lt;br /&gt;
:A conceptual description of the MARIO software and its design.&lt;br /&gt;
*[[Mu Rhythm Off-line Analysis Tutorial]]&lt;br /&gt;
:Introduction to the use of the MARIO tool to analyze recorded sessions of mu rhythm modulation&lt;br /&gt;
*[[P300 Off-line Analysis Tutorial]]&lt;br /&gt;
:Introduction to the use of the MARIO tool to analyze recorded sessions of mu P300 letter spelling&lt;br /&gt;
&lt;br /&gt;
[[Category:Contents]] &lt;br /&gt;
[[Category:Matlab]]&lt;br /&gt;
[[Category:Data Analysis Tools]]&lt;/div&gt;</summary>
		<author><name>Roostenveld</name></author>
	</entry>
	<entry>
		<id>https://www.bci2000.org/mediawiki/index.php?title=User_talk:Roostenveld&amp;diff=3996</id>
		<title>User talk:Roostenveld</title>
		<link rel="alternate" type="text/html" href="https://www.bci2000.org/mediawiki/index.php?title=User_talk:Roostenveld&amp;diff=3996"/>
		<updated>2008-07-14T13:30:53Z</updated>

		<summary type="html">&lt;p&gt;Roostenveld: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;TODO:&lt;br /&gt;
* Programming_Tutorial:Implementing_a_Matlab-based_Filter&lt;br /&gt;
* Contributions:FieldTripBuffer&lt;/div&gt;</summary>
		<author><name>Roostenveld</name></author>
	</entry>
	<entry>
		<id>https://www.bci2000.org/mediawiki/index.php?title=Programming_Tutorial:Implementing_a_simple_Matlab-based_Filter&amp;diff=3995</id>
		<title>Programming Tutorial:Implementing a simple Matlab-based Filter</title>
		<link rel="alternate" type="text/html" href="https://www.bci2000.org/mediawiki/index.php?title=Programming_Tutorial:Implementing_a_simple_Matlab-based_Filter&amp;diff=3995"/>
		<updated>2008-07-14T13:23:37Z</updated>

		<summary type="html">&lt;p&gt;Roostenveld: /* See also */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Online Algorithm Verification==&lt;br /&gt;
In the field of BCI signal processing research, novel methods are often proposed and tested on the basis of existing data.&lt;br /&gt;
While this is adequate as a research strategy, it is important to keep in mind that timely feedback of brain signal classification is an essential element of a BCI.&lt;br /&gt;
From this, it is clear that, ideally, a proposed BCI signal processing or classification method should be verified with respect to its viability and usefulness in a true online setting, providing feedback to the participant causally and in real-time.&lt;br /&gt;
&lt;br /&gt;
BCI2000 facilitates the transformation of an existing offline data analysis method into a functional online system.&lt;br /&gt;
*For signal processing components, it provides a convenient, straightforward programming interface.&lt;br /&gt;
*A signal processing component may be implemented as a set of Matlab scripts.&lt;br /&gt;
&lt;br /&gt;
Still, it is easy to underestimate the effort required to transform an existing offline implementation of a signal processing algorithm into a functional online implementation. While BCI2000 tries to make the transformation as simple as possible, it cannot remove the effort required to deal with chunks of data, which implies the need of &lt;br /&gt;
*buffering -- rather than having immediate access to a continuous data set, it may be necessary to maintain an additional data buffer;&lt;br /&gt;
*with the Matlab interface, maintaining a consistent state between subsequent calls to the processing script.&lt;br /&gt;
&lt;br /&gt;
==An Example Algorithm==&lt;br /&gt;
In this scenario, we use a simple, straightforward BCI signal processing algorithm, to show the steps necessary to modify the algorithm such &lt;br /&gt;
that it may be used to build an online system in BCI2000.&lt;br /&gt;
&lt;br /&gt;
In the example, signal processing consists of IIR bandpass filtering, followed with RMS envelope computation, and linear classification.&lt;br /&gt;
A typical Matlab implementation of that algorithm might consist of about ten lines:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
function class_result = classify( data, band, classifier );&lt;br /&gt;
&lt;br /&gt;
% Use as&lt;br /&gt;
%   class_result = classify( data, band, classifier )&lt;br /&gt;
%&lt;br /&gt;
% This function takes raw data as a [channels x samples] vector in the &#039;data&#039;&lt;br /&gt;
% input variable. &lt;br /&gt;
%&lt;br /&gt;
% Then, it computes bandpower features for the band specified in the &#039;band&#039;&lt;br /&gt;
% input variable, which is a number between 0 and 0.5, specifying&lt;br /&gt;
% center frequency in terms of the sampling rate.&lt;br /&gt;
%&lt;br /&gt;
% As a last step, it applies the &#039;classifier&#039; matrix to the features in order&lt;br /&gt;
% to obtain a single classification result for each sample. The &#039;classifier&#039; &lt;br /&gt;
% vector specifies a classification weight for each processed channel.&lt;br /&gt;
% &lt;br /&gt;
% The result is a single classification result for each sample.&lt;br /&gt;
%&lt;br /&gt;
% This requires the Matlab signal processing toolbox.&lt;br /&gt;
&lt;br /&gt;
% Design bandpass filters and apply them to the data matrix.&lt;br /&gt;
% The filtered data will contain bandpass filtered data as channels.&lt;br /&gt;
[n,Wn]=buttord(band*[0.9 1.1]/2,band*[0.7 1.4]/2,1,60);&lt;br /&gt;
[b,a]=butter(n,Wn);&lt;br /&gt;
processed_data = filter(b,a,data);&lt;br /&gt;
&lt;br /&gt;
% For demodulation, rectify and apply a low pass.&lt;br /&gt;
[b,a]=butter(1,band/4);&lt;br /&gt;
processed_data = filter(b,a,abs(processed_data));&lt;br /&gt;
&lt;br /&gt;
% Finally, apply the linear classifier.&lt;br /&gt;
class_result = processed_data * classifier;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Note that, to be viable in an online environment, an algorithm must operate &lt;br /&gt;
on its signal in a causal way, i.e. it may not use future samples in order to&lt;br /&gt;
process present samples. (Still, a certain amount of non-causality may be possible&lt;br /&gt;
by windowed operation on buffered data, although this will increase the effective&lt;br /&gt;
delay between input and output data.)&lt;br /&gt;
&lt;br /&gt;
Also note that the &amp;lt;tt&amp;gt;classify&amp;lt;/tt&amp;gt; function omits spatial filtering -- in Matlab, &lt;br /&gt;
this may be done easily by pre-multiplying the data with a spatial filter matrix&lt;br /&gt;
if desired.&lt;br /&gt;
&lt;br /&gt;
==Transformation into BCI2000 Matlab Scripts==&lt;br /&gt;
&lt;br /&gt;
For use with BCI2000, the signal processing code needs to be cast into a form&lt;br /&gt;
that suits the BCI2000 filter interface.&lt;br /&gt;
In this event-based model, portions of code are called at certain times to configure&lt;br /&gt;
a filter component&#039;s internal state, and to act upon the signal in chunks on its way through&lt;br /&gt;
the BCI2000 chain of filters.&lt;br /&gt;
&lt;br /&gt;
===Process===&lt;br /&gt;
&lt;br /&gt;
The most central event in the filter interface is the &#039;&#039;Process&#039;&#039; event.&lt;br /&gt;
The &#039;&#039;Process&#039;&#039; event handler receives a signal input, will process this input&lt;br /&gt;
according to the filter component&#039;s role in the signal processing chain, and &lt;br /&gt;
return the result of processing in a signal output variable.&lt;br /&gt;
It is important to understand that the &#039;&#039;Process&#039;&#039; handler is called separately&lt;br /&gt;
for each chunk of data, and thus does not see the signal in its entirety.&lt;br /&gt;
Also, the size of data blocks (chunks) is freely configurable by the user, although&lt;br /&gt;
restricted to limits stemming from recording hardware properties.&lt;br /&gt;
This implies that &#039;&#039;Process&#039;&#039; scripts may not depend on a certain&lt;br /&gt;
data block size, and will sometimes need to maintain their own data buffers when &lt;br /&gt;
the algorithm in question operates on windows of data rather than continuously.&lt;br /&gt;
In the current example, this is not the case, so we need not maintain an internal &lt;br /&gt;
data buffer.&lt;br /&gt;
Still, we need to maintain an internal state between calls to the &#039;&#039;Process&#039;&#039; &lt;br /&gt;
handler in order to preserve the state of IIR filter delays.&lt;br /&gt;
This will allow continuous operation on the signal with the same processing result&lt;br /&gt;
as in the offline version of the algorithm.&lt;br /&gt;
&lt;br /&gt;
The algorithm&#039;s online version in the &amp;lt;tt&amp;gt;bci_Process&amp;lt;/tt&amp;gt; script will thus be:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
function out_signal = bci_Process( in_signal )&lt;br /&gt;
&lt;br /&gt;
% Process a single block of data by applying a filter to in_signal, &lt;br /&gt;
% and return the result in out_signal.&lt;br /&gt;
% Signal dimensions are ( channels x samples ).&lt;br /&gt;
&lt;br /&gt;
% We use global variables to store classifier, &lt;br /&gt;
% filter coefficients and filter state.&lt;br /&gt;
global a b z lpa lpb lpz classifier;&lt;br /&gt;
&lt;br /&gt;
[out_signal, z] = filter( b, a, in_signal, z );&lt;br /&gt;
out_signal = abs( out_signal );&lt;br /&gt;
[out_signal, lpz] = filter( lpb, lpa, out_signal, lpz );&lt;br /&gt;
out_signal = out_signal * classifier;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Initialize=== &lt;br /&gt;
&lt;br /&gt;
Determination of filter coefficients is part of per-run initialization, and occurs in the &lt;br /&gt;
&#039;&#039;Initialize&#039;&#039; event handler:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
function bci_Initialize( in_signal_dims, out_signal_dims )&lt;br /&gt;
&lt;br /&gt;
% Perform configuration for the bci_Process script.&lt;br /&gt;
&lt;br /&gt;
% Parameters and states are global variables.&lt;br /&gt;
global bci_Parameters bci_States;&lt;br /&gt;
&lt;br /&gt;
% We use global variables to store classifier vector, &lt;br /&gt;
% filter coefficients and filter state.&lt;br /&gt;
global a b z lpa lpb lpz classifier;&lt;br /&gt;
&lt;br /&gt;
% Configure the Bandpass filter&lt;br /&gt;
band = str2double( bci_Parameters.Passband ) / str2double( bci_Parameters.SamplingRate );&lt;br /&gt;
[n,Wn]=buttord(band*[0.9 1.1]/2,band*[0.7 1.4]/2,1,60);&lt;br /&gt;
[b,a]=butter(n,Wn);&lt;br /&gt;
z=zeros(max(length(a),length(b))-1,in_signal_dims(1));&lt;br /&gt;
&lt;br /&gt;
% Configure the Lowpass filter&lt;br /&gt;
[lpb,lpa]=butter(1,band/4);&lt;br /&gt;
lpz=zeros(max(length(lpa),length(lpb))-1,in_signal_dims(1));&lt;br /&gt;
&lt;br /&gt;
% Configure the Classifier vector&lt;br /&gt;
classifier = str2double( bci_Parameters.ClassVector );&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===StartRun===&lt;br /&gt;
&lt;br /&gt;
Finally, we need to reset filter state at the beginning of each run, using the &#039;&#039;StartRun&#039;&#039;&lt;br /&gt;
event handler:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
function bci_StartRun&lt;br /&gt;
&lt;br /&gt;
% Reset filter state at the beginning of a run.&lt;br /&gt;
&lt;br /&gt;
global z lpz;&lt;br /&gt;
z   = zeros(size(z));&lt;br /&gt;
lpz = zeros(size(lpz));&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Constructor===&lt;br /&gt;
To complete the Matlab filter code, we need to declare the &#039;&#039;Band&#039;&#039; and &#039;&#039;Classifier&#039;&#039;&lt;br /&gt;
parameters in a &#039;&#039;Constructor&#039;&#039; event handler:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
function [ parameters, states ] = bci_Construct&lt;br /&gt;
&lt;br /&gt;
% Request BCI2000 parameters by returning parameter definition &lt;br /&gt;
% lines as demonstrated below.&lt;br /&gt;
&lt;br /&gt;
parameters = { ...&lt;br /&gt;
  [ &#039;BPClassifier float Passband= 10 10 0 % % // Bandpass frequency in Hz&#039; ] ...&lt;br /&gt;
  [ &#039;BPClassifier matrix ClassVector= 1 1 1 0 % % // Linear classifier vector&#039; ] ...&lt;br /&gt;
};&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Preflight===&lt;br /&gt;
&lt;br /&gt;
Finally, we need to check parameters for consistency in a &#039;&#039;Preflight&#039;&#039;&lt;br /&gt;
script, and declare the size of our filter&#039;s output signal:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
function [ out_signal_dim ] = bci_Preflight( in_signal_dim )&lt;br /&gt;
&lt;br /&gt;
% Check whether parameters are accessible, and whether&lt;br /&gt;
% parameters have values that allow for safe processing by the &lt;br /&gt;
% bci_Process function.&lt;br /&gt;
% Also, report output signal dimensions in the &#039;out_signal_dim&#039; argument.&lt;br /&gt;
&lt;br /&gt;
% Parameters and states are global variables.&lt;br /&gt;
global bci_Parameters bci_States;&lt;br /&gt;
&lt;br /&gt;
band = str2double( bci_Parameters.Passband ) / str2double( bci_Parameters.SamplingRate );&lt;br /&gt;
if( band &amp;lt;= 0 )&lt;br /&gt;
  error( &#039;The passband parameter must be greater zero&#039; );&lt;br /&gt;
elseif( band &amp;gt; 0.5 / 1.4 )&lt;br /&gt;
  error( &#039;The Passband parameter conflicts with the sampling rate&#039; ); &lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
out_signal_dim = [ 1, size( in_signal_dim, 2 ) ];&lt;br /&gt;
if( in_signal_dim( 1 ) ~= size( bci_Parameters.ClassVector, 2 ) )&lt;br /&gt;
  error( &#039;ClassVector length must match the input signal&#039;&#039;s number of channels&#039; );&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==See also==&lt;br /&gt;
&lt;br /&gt;
[[Programming Reference:MatlabFilter]], &lt;br /&gt;
[[User Reference:MatlabFilter]], &lt;br /&gt;
[[Contributions:FieldTripBuffer]]&lt;br /&gt;
&lt;br /&gt;
[[Category:Tutorial]]&lt;br /&gt;
[[Category:Signal Processing]]&lt;br /&gt;
[[Category:Development]]&lt;br /&gt;
[[Category:Matlab]]&lt;br /&gt;
[[Category:External Interfaces]]&lt;/div&gt;</summary>
		<author><name>Roostenveld</name></author>
	</entry>
	<entry>
		<id>https://www.bci2000.org/mediawiki/index.php?title=Programming_Tutorial:Implementing_a_simple_Matlab-based_Filter&amp;diff=3994</id>
		<title>Programming Tutorial:Implementing a simple Matlab-based Filter</title>
		<link rel="alternate" type="text/html" href="https://www.bci2000.org/mediawiki/index.php?title=Programming_Tutorial:Implementing_a_simple_Matlab-based_Filter&amp;diff=3994"/>
		<updated>2008-07-14T13:23:07Z</updated>

		<summary type="html">&lt;p&gt;Roostenveld: /* See also */ added link to fieldtripfilter&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Online Algorithm Verification==&lt;br /&gt;
In the field of BCI signal processing research, novel methods are often proposed and tested on the basis of existing data.&lt;br /&gt;
While this is adequate as a research strategy, it is important to keep in mind that timely feedback of brain signal classification is an essential element of a BCI.&lt;br /&gt;
From this, it is clear that, ideally, a proposed BCI signal processing or classification method should be verified with respect to its viability and usefulness in a true online setting, providing feedback to the participant causally and in real-time.&lt;br /&gt;
&lt;br /&gt;
BCI2000 facilitates the transformation of an existing offline data analysis method into a functional online system.&lt;br /&gt;
*For signal processing components, it provides a convenient, straightforward programming interface.&lt;br /&gt;
*A signal processing component may be implemented as a set of Matlab scripts.&lt;br /&gt;
&lt;br /&gt;
Still, it is easy to underestimate the effort required to transform an existing offline implementation of a signal processing algorithm into a functional online implementation. While BCI2000 tries to make the transformation as simple as possible, it cannot remove the effort required to deal with chunks of data, which implies the need of &lt;br /&gt;
*buffering -- rather than having immediate access to a continuous data set, it may be necessary to maintain an additional data buffer;&lt;br /&gt;
*with the Matlab interface, maintaining a consistent state between subsequent calls to the processing script.&lt;br /&gt;
&lt;br /&gt;
==An Example Algorithm==&lt;br /&gt;
In this scenario, we use a simple, straightforward BCI signal processing algorithm, to show the steps necessary to modify the algorithm such &lt;br /&gt;
that it may be used to build an online system in BCI2000.&lt;br /&gt;
&lt;br /&gt;
In the example, signal processing consists of IIR bandpass filtering, followed with RMS envelope computation, and linear classification.&lt;br /&gt;
A typical Matlab implementation of that algorithm might consist of about ten lines:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
function class_result = classify( data, band, classifier );&lt;br /&gt;
&lt;br /&gt;
% Use as&lt;br /&gt;
%   class_result = classify( data, band, classifier )&lt;br /&gt;
%&lt;br /&gt;
% This function takes raw data as a [channels x samples] vector in the &#039;data&#039;&lt;br /&gt;
% input variable. &lt;br /&gt;
%&lt;br /&gt;
% Then, it computes bandpower features for the band specified in the &#039;band&#039;&lt;br /&gt;
% input variable, which is a number between 0 and 0.5, specifying&lt;br /&gt;
% center frequency in terms of the sampling rate.&lt;br /&gt;
%&lt;br /&gt;
% As a last step, it applies the &#039;classifier&#039; matrix to the features in order&lt;br /&gt;
% to obtain a single classification result for each sample. The &#039;classifier&#039; &lt;br /&gt;
% vector specifies a classification weight for each processed channel.&lt;br /&gt;
% &lt;br /&gt;
% The result is a single classification result for each sample.&lt;br /&gt;
%&lt;br /&gt;
% This requires the Matlab signal processing toolbox.&lt;br /&gt;
&lt;br /&gt;
% Design bandpass filters and apply them to the data matrix.&lt;br /&gt;
% The filtered data will contain bandpass filtered data as channels.&lt;br /&gt;
[n,Wn]=buttord(band*[0.9 1.1]/2,band*[0.7 1.4]/2,1,60);&lt;br /&gt;
[b,a]=butter(n,Wn);&lt;br /&gt;
processed_data = filter(b,a,data);&lt;br /&gt;
&lt;br /&gt;
% For demodulation, rectify and apply a low pass.&lt;br /&gt;
[b,a]=butter(1,band/4);&lt;br /&gt;
processed_data = filter(b,a,abs(processed_data));&lt;br /&gt;
&lt;br /&gt;
% Finally, apply the linear classifier.&lt;br /&gt;
class_result = processed_data * classifier;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Note that, to be viable in an online environment, an algorithm must operate &lt;br /&gt;
on its signal in a causal way, i.e. it may not use future samples in order to&lt;br /&gt;
process present samples. (Still, a certain amount of non-causality may be possible&lt;br /&gt;
by windowed operation on buffered data, although this will increase the effective&lt;br /&gt;
delay between input and output data.)&lt;br /&gt;
&lt;br /&gt;
Also note that the &amp;lt;tt&amp;gt;classify&amp;lt;/tt&amp;gt; function omits spatial filtering -- in Matlab, &lt;br /&gt;
this may be done easily by pre-multiplying the data with a spatial filter matrix&lt;br /&gt;
if desired.&lt;br /&gt;
&lt;br /&gt;
==Transformation into BCI2000 Matlab Scripts==&lt;br /&gt;
&lt;br /&gt;
For use with BCI2000, the signal processing code needs to be cast into a form&lt;br /&gt;
that suits the BCI2000 filter interface.&lt;br /&gt;
In this event-based model, portions of code are called at certain times to configure&lt;br /&gt;
a filter component&#039;s internal state, and to act upon the signal in chunks on its way through&lt;br /&gt;
the BCI2000 chain of filters.&lt;br /&gt;
&lt;br /&gt;
===Process===&lt;br /&gt;
&lt;br /&gt;
The most central event in the filter interface is the &#039;&#039;Process&#039;&#039; event.&lt;br /&gt;
The &#039;&#039;Process&#039;&#039; event handler receives a signal input, will process this input&lt;br /&gt;
according to the filter component&#039;s role in the signal processing chain, and &lt;br /&gt;
return the result of processing in a signal output variable.&lt;br /&gt;
It is important to understand that the &#039;&#039;Process&#039;&#039; handler is called separately&lt;br /&gt;
for each chunk of data, and thus does not see the signal in its entirety.&lt;br /&gt;
Also, the size of data blocks (chunks) is freely configurable by the user, although&lt;br /&gt;
restricted to limits stemming from recording hardware properties.&lt;br /&gt;
This implies that &#039;&#039;Process&#039;&#039; scripts may not depend on a certain&lt;br /&gt;
data block size, and will sometimes need to maintain their own data buffers when &lt;br /&gt;
the algorithm in question operates on windows of data rather than continuously.&lt;br /&gt;
In the current example, this is not the case, so we need not maintain an internal &lt;br /&gt;
data buffer.&lt;br /&gt;
Still, we need to maintain an internal state between calls to the &#039;&#039;Process&#039;&#039; &lt;br /&gt;
handler in order to preserve the state of IIR filter delays.&lt;br /&gt;
This will allow continuous operation on the signal with the same processing result&lt;br /&gt;
as in the offline version of the algorithm.&lt;br /&gt;
&lt;br /&gt;
The algorithm&#039;s online version in the &amp;lt;tt&amp;gt;bci_Process&amp;lt;/tt&amp;gt; script will thus be:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
function out_signal = bci_Process( in_signal )&lt;br /&gt;
&lt;br /&gt;
% Process a single block of data by applying a filter to in_signal, &lt;br /&gt;
% and return the result in out_signal.&lt;br /&gt;
% Signal dimensions are ( channels x samples ).&lt;br /&gt;
&lt;br /&gt;
% We use global variables to store classifier, &lt;br /&gt;
% filter coefficients and filter state.&lt;br /&gt;
global a b z lpa lpb lpz classifier;&lt;br /&gt;
&lt;br /&gt;
[out_signal, z] = filter( b, a, in_signal, z );&lt;br /&gt;
out_signal = abs( out_signal );&lt;br /&gt;
[out_signal, lpz] = filter( lpb, lpa, out_signal, lpz );&lt;br /&gt;
out_signal = out_signal * classifier;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Initialize=== &lt;br /&gt;
&lt;br /&gt;
Determination of filter coefficients is part of per-run initialization, and occurs in the &lt;br /&gt;
&#039;&#039;Initialize&#039;&#039; event handler:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
function bci_Initialize( in_signal_dims, out_signal_dims )&lt;br /&gt;
&lt;br /&gt;
% Perform configuration for the bci_Process script.&lt;br /&gt;
&lt;br /&gt;
% Parameters and states are global variables.&lt;br /&gt;
global bci_Parameters bci_States;&lt;br /&gt;
&lt;br /&gt;
% We use global variables to store classifier vector, &lt;br /&gt;
% filter coefficients and filter state.&lt;br /&gt;
global a b z lpa lpb lpz classifier;&lt;br /&gt;
&lt;br /&gt;
% Configure the Bandpass filter&lt;br /&gt;
band = str2double( bci_Parameters.Passband ) / str2double( bci_Parameters.SamplingRate );&lt;br /&gt;
[n,Wn]=buttord(band*[0.9 1.1]/2,band*[0.7 1.4]/2,1,60);&lt;br /&gt;
[b,a]=butter(n,Wn);&lt;br /&gt;
z=zeros(max(length(a),length(b))-1,in_signal_dims(1));&lt;br /&gt;
&lt;br /&gt;
% Configure the Lowpass filter&lt;br /&gt;
[lpb,lpa]=butter(1,band/4);&lt;br /&gt;
lpz=zeros(max(length(lpa),length(lpb))-1,in_signal_dims(1));&lt;br /&gt;
&lt;br /&gt;
% Configure the Classifier vector&lt;br /&gt;
classifier = str2double( bci_Parameters.ClassVector );&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===StartRun===&lt;br /&gt;
&lt;br /&gt;
Finally, we need to reset filter state at the beginning of each run, using the &#039;&#039;StartRun&#039;&#039;&lt;br /&gt;
event handler:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
function bci_StartRun&lt;br /&gt;
&lt;br /&gt;
% Reset filter state at the beginning of a run.&lt;br /&gt;
&lt;br /&gt;
global z lpz;&lt;br /&gt;
z   = zeros(size(z));&lt;br /&gt;
lpz = zeros(size(lpz));&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Constructor===&lt;br /&gt;
To complete the Matlab filter code, we need to declare the &#039;&#039;Band&#039;&#039; and &#039;&#039;Classifier&#039;&#039;&lt;br /&gt;
parameters in a &#039;&#039;Constructor&#039;&#039; event handler:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
function [ parameters, states ] = bci_Construct&lt;br /&gt;
&lt;br /&gt;
% Request BCI2000 parameters by returning parameter definition &lt;br /&gt;
% lines as demonstrated below.&lt;br /&gt;
&lt;br /&gt;
parameters = { ...&lt;br /&gt;
  [ &#039;BPClassifier float Passband= 10 10 0 % % // Bandpass frequency in Hz&#039; ] ...&lt;br /&gt;
  [ &#039;BPClassifier matrix ClassVector= 1 1 1 0 % % // Linear classifier vector&#039; ] ...&lt;br /&gt;
};&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Preflight===&lt;br /&gt;
&lt;br /&gt;
Finally, we need to check parameters for consistency in a &#039;&#039;Preflight&#039;&#039;&lt;br /&gt;
script, and declare the size of our filter&#039;s output signal:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
function [ out_signal_dim ] = bci_Preflight( in_signal_dim )&lt;br /&gt;
&lt;br /&gt;
% Check whether parameters are accessible, and whether&lt;br /&gt;
% parameters have values that allow for safe processing by the &lt;br /&gt;
% bci_Process function.&lt;br /&gt;
% Also, report output signal dimensions in the &#039;out_signal_dim&#039; argument.&lt;br /&gt;
&lt;br /&gt;
% Parameters and states are global variables.&lt;br /&gt;
global bci_Parameters bci_States;&lt;br /&gt;
&lt;br /&gt;
band = str2double( bci_Parameters.Passband ) / str2double( bci_Parameters.SamplingRate );&lt;br /&gt;
if( band &amp;lt;= 0 )&lt;br /&gt;
  error( &#039;The passband parameter must be greater zero&#039; );&lt;br /&gt;
elseif( band &amp;gt; 0.5 / 1.4 )&lt;br /&gt;
  error( &#039;The Passband parameter conflicts with the sampling rate&#039; ); &lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
out_signal_dim = [ 1, size( in_signal_dim, 2 ) ];&lt;br /&gt;
if( in_signal_dim( 1 ) ~= size( bci_Parameters.ClassVector, 2 ) )&lt;br /&gt;
  error( &#039;ClassVector length must match the input signal&#039;&#039;s number of channels&#039; );&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==See also==&lt;br /&gt;
&lt;br /&gt;
[[Programming Reference:MatlabFilter]], &lt;br /&gt;
[[User Reference:MatlabFilter]], &lt;br /&gt;
[[Contributions:FieldTripFilter]]&lt;br /&gt;
&lt;br /&gt;
[[Category:Tutorial]]&lt;br /&gt;
[[Category:Signal Processing]]&lt;br /&gt;
[[Category:Development]]&lt;br /&gt;
[[Category:Matlab]]&lt;br /&gt;
[[Category:External Interfaces]]&lt;/div&gt;</summary>
		<author><name>Roostenveld</name></author>
	</entry>
	<entry>
		<id>https://www.bci2000.org/mediawiki/index.php?title=Talk:Programming_Tutorial:Implementing_a_simple_Matlab-based_Filter&amp;diff=3993</id>
		<title>Talk:Programming Tutorial:Implementing a simple Matlab-based Filter</title>
		<link rel="alternate" type="text/html" href="https://www.bci2000.org/mediawiki/index.php?title=Talk:Programming_Tutorial:Implementing_a_simple_Matlab-based_Filter&amp;diff=3993"/>
		<updated>2008-07-14T13:21:52Z</updated>

		<summary type="html">&lt;p&gt;Roostenveld: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Robert - 14 July 2008:&lt;br /&gt;
&lt;br /&gt;
I suggest to remove the section &amp;quot;Online Algorithm Verification&amp;quot; and to have a discussion on Matlab for offline and online processing somewhere else.&lt;/div&gt;</summary>
		<author><name>Roostenveld</name></author>
	</entry>
	<entry>
		<id>https://www.bci2000.org/mediawiki/index.php?title=Talk:Programming_Tutorial:Implementing_a_simple_Matlab-based_Filter&amp;diff=3992</id>
		<title>Talk:Programming Tutorial:Implementing a simple Matlab-based Filter</title>
		<link rel="alternate" type="text/html" href="https://www.bci2000.org/mediawiki/index.php?title=Talk:Programming_Tutorial:Implementing_a_simple_Matlab-based_Filter&amp;diff=3992"/>
		<updated>2008-07-14T13:20:58Z</updated>

		<summary type="html">&lt;p&gt;Roostenveld: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;I suggest to remove the section &amp;quot;Online Algorithm Verification&amp;quot; and to have a discussion on Matlab for offline and online processing somewhere else.&lt;/div&gt;</summary>
		<author><name>Roostenveld</name></author>
	</entry>
	<entry>
		<id>https://www.bci2000.org/mediawiki/index.php?title=Programming_Reference:MatlabFilter&amp;diff=3991</id>
		<title>Programming Reference:MatlabFilter</title>
		<link rel="alternate" type="text/html" href="https://www.bci2000.org/mediawiki/index.php?title=Programming_Reference:MatlabFilter&amp;diff=3991"/>
		<updated>2008-07-14T13:19:08Z</updated>

		<summary type="html">&lt;p&gt;Roostenveld: /* See also */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Overview==&lt;br /&gt;
&lt;br /&gt;
The MatlabFilter implements a mechanism for using the Matlab engine within the BCI2000 pipeline. The &#039;&#039;MatlabFilter&#039;&#039; calls the Matlab engine to act upon signals, parameters, and states. It provides the full BCI2000 [[Programming Reference:GenericFilter Class|filter interface]] to a Matlab filter implementation.&lt;br /&gt;
&lt;br /&gt;
While BCI2000 is running, each block of data is pushed to the Matlab engine and a well-specified Matlab function (i.e. *.m file) is executed. As explained in more detail in the Programming reference for the  [[Programming_Reference:GenericFilter_Class|GenericFilter Class]], all BCI2000 filters consist of a set of functions. The MatlabFilter will, upon execution of each of these functions, contact the Matlab engine and execure the corresponding Matlab function equivalent. Furthermore, the MatlabFilter copies the input signal and the states to the Matlab engine, and copies the output signal and states from the engine after processing the signal.&lt;br /&gt;
&lt;br /&gt;
{| style=&amp;quot;color:black;background-color:#ffffcc;&amp;quot; cellpadding=&amp;quot;5&amp;quot; cellspacing=&amp;quot;0&amp;quot; border=&amp;quot;2&amp;quot; &lt;br /&gt;
!BCI2000 function&lt;br /&gt;
!Matlab equivalent&lt;br /&gt;
|-&lt;br /&gt;
|[[Programming_Reference:GenericFilter_Class#Constructor|Constructor()]]&lt;br /&gt;
|bci_Construct&lt;br /&gt;
|-&lt;br /&gt;
|[[Programming_Reference:GenericFilter_Class#Preflight|Preflight()]]&lt;br /&gt;
|bci_Preflight&lt;br /&gt;
|-&lt;br /&gt;
|[[Programming_Reference:GenericFilter_Class#Initialize|Initialize()]]&lt;br /&gt;
|bci_Initialize&lt;br /&gt;
|-&lt;br /&gt;
|[[Programming_Reference:GenericFilter_Class#StartRun|StartRun()]]&lt;br /&gt;
|bci_StartRun&lt;br /&gt;
|-&lt;br /&gt;
|[[Programming_Reference:GenericFilter_Class#Process|Process()]]&lt;br /&gt;
|bci_Process&lt;br /&gt;
|-&lt;br /&gt;
|[[Programming_Reference:GenericFilter_Class#StopRun|StopRun()]]&lt;br /&gt;
|bci_StopRun&lt;br /&gt;
|-&lt;br /&gt;
|[[Programming_Reference:GenericFilter_Class#Resting|Resting()]]&lt;br /&gt;
|bci_Resting&lt;br /&gt;
|-&lt;br /&gt;
|[[Programming_Reference:GenericFilter_Class#Halt|Halt()]]&lt;br /&gt;
|bci_Halt&lt;br /&gt;
|-&lt;br /&gt;
|[[Programming_Reference:GenericFilter_Class#Destructor|Destructor()]]&lt;br /&gt;
|bci_Destruct&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Existence of the above-listed Matlab functions is not mandatory, the Matlab &#039;exist&#039; command will be used to determine whether a given function is available, and will not call the Matlab engine when this is not the case. If either of the bci_Preflight, bci_Initialize, or bci_Process functions is not available, a warning will be displayed to the user.&lt;br /&gt;
&lt;br /&gt;
Most of these Matlab functions do not take input arguments, except for&lt;br /&gt;
  function [parameters, states] = bci_Construct;&lt;br /&gt;
  &lt;br /&gt;
  function [out_signal_dim] = bci_Preflight( in_signal_dim );&lt;br /&gt;
  &lt;br /&gt;
  function bci_Initialize( in_signal_dim, out_signal_dim );&lt;br /&gt;
  &lt;br /&gt;
  function [out_signal] = bci_Process( in_signal );&lt;br /&gt;
&lt;br /&gt;
The most important functions is &amp;lt;tt&amp;gt;bci_Process&amp;lt;/tt&amp;gt;, which is executed every time a new block of data is passed through the pipeline. The &amp;lt;tt&amp;gt;bci_Construct&amp;lt;/tt&amp;gt; function is executed at the beginning and determines the parameters and states. The &amp;lt;tt&amp;gt;bci_Initialize&amp;lt;/tt&amp;gt; function determines whether all requirements are met. The &amp;lt;tt&amp;gt;bci_StartRun&amp;lt;/tt&amp;gt; function prepares the computation that is done during processing (e.g. computing filter parameters). Communication between these Matlab functions is done using global variables.&lt;br /&gt;
&lt;br /&gt;
==Parameters and States==&lt;br /&gt;
&lt;br /&gt;
Parameters and states are accessible via global Matlab structs called  &#039;bci_Parameters&#039; and &#039;bci_States&#039;. Parameters may be changed from &amp;lt;tt&amp;gt;bci_StopRun&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;bci_Resting&amp;lt;/tt&amp;gt;, and will automatically be propagated to the other modules. State values may be modified from the &amp;lt;tt&amp;gt;bci_Process&amp;lt;/tt&amp;gt; function. &lt;br /&gt;
&lt;br /&gt;
To add parameters and states to the BCI2000 list of states, the &amp;lt;tt&amp;gt;bci_Construct&amp;lt;/tt&amp;gt; function may return non-empty cell arrays of strings in its parameters  and states return values. The strings constituting these cell arrays are [[Technical Reference:Parameter Definition|parameter]] and [[Technical Reference:State Definition|state definitions]].&lt;br /&gt;
&lt;br /&gt;
Your added parameters will appear in the [[User Reference:Operator Module|operator module&#039;s]] parameter configuration dialog. Your parameter definition&#039;s section name will be used to direct its position in the configuration dialog&#039;s register cards:&lt;br /&gt;
 MyFilter int MyParam= 2 0 0 2 // ...&lt;br /&gt;
will display the &#039;&#039;MyParam&#039;&#039; parameter on separate register card named &#039;&#039;MyFilter&#039;&#039;.&lt;br /&gt;
 Filtering:MyFilter int MyParam= 2 0 0 2 // ...&lt;br /&gt;
will display the parameter on the &#039;&#039;Filtering&#039;&#039; register card, inside a group called &#039;&#039;MyFilter&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
==Input and Output Signal Format==&lt;br /&gt;
&lt;br /&gt;
BCI2000 signals are mapped to Matlab matrices with channel index first, and sample (element) index second. The signal dimension arguments of &amp;lt;tt&amp;gt;bci_Preflight&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;bci_Initialize&amp;lt;/tt&amp;gt; are integer vectors  of size 1x2 with &amp;lt;tt&amp;gt;[n_channels n_elements]&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
==Error Reporting==&lt;br /&gt;
&lt;br /&gt;
To report errors from Matlab functions, use Matlab&#039;s &amp;lt;tt&amp;gt;error&amp;lt;/tt&amp;gt; command.&lt;br /&gt;
Your error messages will be displayed to the user from the [[User Reference:Operator Module|operator module]].&lt;br /&gt;
&lt;br /&gt;
==Combining the MatlabFilter with other Signal Processing Filters==&lt;br /&gt;
&lt;br /&gt;
In the BCI2000 binary distribution, the &#039;&#039;MatlabFilter&#039;&#039; is shipped inside the &#039;&#039;MatlabSignalProcessing&#039;&#039; executable. There, the signal processing chain consists of the [[User Reference:SpatialFilter|SpatialFilter]] and the &#039;&#039;MatlabFilter&#039;&#039;. However, by editing &amp;lt;tt&amp;gt;src/core/SignalProcessing/Matlab/PipeDefinition.cpp&amp;lt;/tt&amp;gt;, you may add as many [[User Reference:Filters|signal processing filters]] as you wish.&lt;br /&gt;
&lt;br /&gt;
See [[Programming Reference:Filter Chain]] for information about defining a filter chain. Modification of the filter chain requires access to the [[Programming Reference:BCI2000 Source Code|BCI2000 source code]]. You will need to rebuild the &#039;&#039;MatlabSignalProcessing&#039;&#039; executable.&lt;br /&gt;
&lt;br /&gt;
==Command Line Options==&lt;br /&gt;
===MatlabWD===&lt;br /&gt;
By default, the Matlab engine&#039;s working directory is set to the signal processing module&#039;s working directory at startup. When using the Matlab filter for signal processing, your filter&#039;s code will be contained in the &amp;lt;tt&amp;gt;bci_Process&amp;lt;/tt&amp;gt; and associated Matlab functions, which will typically be located inside a dedicated directory. Thus, you may switch between Matlab-based filter implementations by selecting a Matlab working directory at startup.&lt;br /&gt;
This may be done by specifying the &amp;lt;tt&amp;gt;MatlabWD&amp;lt;/tt&amp;gt; parameter from the command line when starting the MatlabSignalProcessing module:&lt;br /&gt;
&amp;lt;pre&amp;gt;start MatlabSignalProcessing.exe --MatlabWD=&amp;quot;./matlab&amp;quot; 127.0.0.1&amp;lt;/pre&amp;gt;&lt;br /&gt;
will change Matlab&#039;s working directory to a directory called &amp;quot;matlab&amp;quot; inside the BCI2000 &amp;quot;prog&amp;quot; directory at startup, and execute &amp;lt;tt&amp;gt;bci_Process&amp;lt;/tt&amp;gt; and associated functions from there.&lt;br /&gt;
&lt;br /&gt;
This parameter must be set from the command line; later changes will have no effect.&lt;br /&gt;
&lt;br /&gt;
Using the &amp;lt;tt&amp;gt;--MatlabWD&amp;lt;/tt&amp;gt; command line option, you may easily switch between multiple Matlab-based BCI2000 configurations.&lt;br /&gt;
Simply create a separate startup batch file for each configuration, and specify the respective configuration&#039;s path on the command line that starts up the MatlabFilter.&lt;br /&gt;
&lt;br /&gt;
===MatlabStayOpen===&lt;br /&gt;
By default, the Matlab instance associated with the Matlab engine will be closed when BCI2000 quits.&lt;br /&gt;
Generally, this behavior is desired to ensure a well-defined BCI2000 system state after each startup.&lt;br /&gt;
For debugging purposes, though, it is sometimes useful to have the engine&#039;s command window available even after BCI2000 has been quit.&lt;br /&gt;
This behavior is controlled by another command-line parameter, &amp;lt;tt&amp;gt;MatlabStayOpen&amp;lt;/tt&amp;gt;, which may take the following values:&lt;br /&gt;
*0 is the default (no change in behavior),&lt;br /&gt;
*1 keeps the Matlab engine&#039;s command window open but clears variables used to transmit information forth and back between BCI2000 and Matlab,&lt;br /&gt;
*2 also preserves intermediate variables (such as bci_Parameters and bci_States) in the Matlab engine&#039;s workspace.&lt;br /&gt;
&lt;br /&gt;
To set this parameter to 2, use this command line to start up the signal processing module:&lt;br /&gt;
&amp;lt;pre&amp;gt;start MatlabSignalProcessing.exe --MatlabStayOpen=2 127.0.0.1&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Once the &amp;lt;tt&amp;gt;MatlabStayOpen&amp;lt;/tt&amp;gt; parameter has been set from the command line, it will be listed on the Operator Module&#039;s parameter configuration dialog&#039;s &amp;quot;system&amp;quot; tab, and may be modified from there. The change will be applied when clicking &#039;&#039;Set Config&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
==Troubleshooting==&lt;br /&gt;
===Debugging Matlab functions===&lt;br /&gt;
#Start BCI2000. The &#039;&#039;MatlabFilter&#039;&#039; will open a Matlab instance in its own window in minimized state.&lt;br /&gt;
#Switch to the minimized Matlab instance.&lt;br /&gt;
#Type, e.g.,&amp;lt;br /&amp;gt;&amp;lt;code&amp;gt;edit bci_Process.m&amp;lt;/code&amp;gt;&amp;lt;br /&amp;gt;to open a Matlab editor window connected to the Matlab interactive window.&lt;br /&gt;
#In the editor window, set breakpoints as you would normally.&lt;br /&gt;
#Execution will be paused, and you may examine variables moving the mouse over them.&lt;br /&gt;
&lt;br /&gt;
===Matlab doesn&#039;t find your functions===&lt;br /&gt;
Make sure to&lt;br /&gt;
*either set Matlab&#039;s working directory to the directory containing your functions, using the &amp;lt;tt&amp;gt;--MatlabWD&amp;lt;/tt&amp;gt; command line option described above,&lt;br /&gt;
*or add the respective directory to your Matlab path.&lt;br /&gt;
Generally, the first option is recommended over the second one.&lt;br /&gt;
&lt;br /&gt;
===There is no Matlab engine started up===&lt;br /&gt;
Execute&amp;lt;br /&amp;gt;&amp;lt;code&amp;gt;matlab /regserver&amp;lt;/code&amp;gt;&amp;lt;br /&amp;gt;from the command line when logged in with administrative privileges.&lt;br /&gt;
===Changes to M-files appear to have no effect===&lt;br /&gt;
Switch to the Matlab engine&#039;s command window, and enter&amp;lt;br /&amp;gt;&amp;lt;code&amp;gt;clear;&amp;lt;/code&amp;gt;&amp;lt;br /&amp;gt; or &amp;lt;br /&amp;gt;&amp;lt;code&amp;gt;clear scriptname;&amp;lt;/code&amp;gt;&amp;lt;br /&amp;gt;each time you change a Matlab function.&lt;br /&gt;
&lt;br /&gt;
===You get linker errors when rebuilding &#039;&#039;MatlabSignalProcessing&#039;&#039;===&lt;br /&gt;
If you get linker errors after editing &amp;lt;tt&amp;gt;PipeDefinition.cpp&amp;lt;/tt&amp;gt;, make sure that all filters&#039; cpp files are part of the &#039;&#039;MatlabSignalProcessing&#039;&#039; project.&lt;br /&gt;
===You get a message window with an &amp;quot;Entry Point Not Found&amp;quot; message===&lt;br /&gt;
This error is likely to indicate confusion of multiple installations of Matlab on your machine.&lt;br /&gt;
Check your Windows &#039;&#039;%PATH%&#039;&#039; environment variable, e.g. by opening a command console window and typing&lt;br /&gt;
 echo %PATH%&lt;br /&gt;
When it contains references to more than a single Matlab installation, remove all apart from the reference to the installation you are going to use with BCI2000. Also, make sure to execute&lt;br /&gt;
 matlab /regserver&lt;br /&gt;
for the Matlab installation you intend to use.&lt;br /&gt;
&lt;br /&gt;
==See also==&lt;br /&gt;
[[Technical Reference:Core Modules#Signal Processing Module]], &lt;br /&gt;
[[Technical Reference:Parameter Definition]], &lt;br /&gt;
[[Technical Reference:State Definition]], &lt;br /&gt;
[[Programming Reference:GenericFilter Class]], &lt;br /&gt;
[[Programming Reference:Filter Chain]], &lt;br /&gt;
[[Contributions:FieldTripBuffer]]&lt;br /&gt;
&lt;br /&gt;
* Matlab tutorial for [[Programming_Tutorial:Implementing_a_Matlab-based_Filter|IIR bandpass filtering, followed with RMS envelope computation, and linear classification]]&lt;br /&gt;
* Matlab tutorial for [[Programming_Tutorial:Implementing_another_Matlab-based_Filter|adaptive spatial filtering using a beamformer, power computation and normalization]]&lt;br /&gt;
&lt;br /&gt;
[[Category:Filters]]&lt;br /&gt;
[[Category:Signal Processing]]&lt;br /&gt;
[[Category:Matlab]]&lt;br /&gt;
[[Category:Development]]&lt;br /&gt;
[[Category:External Interfaces]]&lt;/div&gt;</summary>
		<author><name>Roostenveld</name></author>
	</entry>
	<entry>
		<id>https://www.bci2000.org/mediawiki/index.php?title=User_Reference:Filters&amp;diff=3990</id>
		<title>User Reference:Filters</title>
		<link rel="alternate" type="text/html" href="https://www.bci2000.org/mediawiki/index.php?title=User_Reference:Filters&amp;diff=3990"/>
		<updated>2008-07-14T13:18:06Z</updated>

		<summary type="html">&lt;p&gt;Roostenveld: /* Signal Processing Module */ moved matlabfilter ref&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&#039;&#039;Filters&#039;&#039; are the most important building blocks of a BCI2000 system.&lt;br /&gt;
This document describes the &#039;&#039;filter&#039;&#039; concept for users who will configure BCI2000 for their own experiments, and provides links to the documentation pages of individual filters, describing each filter&#039;s purpose and parameterization.&lt;br /&gt;
&lt;br /&gt;
==Data Processing as a Pipe==&lt;br /&gt;
Each of the three BCI2000 core modules contains a &#039;&#039;chain of filters&#039;&#039;, i.e. a sequence of filters forming a pipe where, basically, brain signal data enter on one side, and a processed version of these data leaves on the other side.&lt;br /&gt;
&lt;br /&gt;
The notion of a &#039;&#039;pipe&#039;&#039; implies that, for each portion of data entering on the input side, there will be exactly one portion of output data on the output side.&lt;br /&gt;
This is analogous to a water pipe: unlike a water stream (a brook, or a river), it is impossible to insert or remove water from inside the pipe without breaking it.&lt;br /&gt;
Similarly, albeit signal portions may change their shape on their way through the pipe (filter chain), it is impossible to insert or remove any of them.&lt;br /&gt;
Thus, each data portion acquired by the data acquisition module will run through the entire BCI2000 system, being processed by a sequence of filters.&lt;br /&gt;
&lt;br /&gt;
==Configuration Overview==&lt;br /&gt;
In principle, these filters may be placed in any order by the writer (programmer) of a BCI2000 module, and it is [[Programming Reference:Filter Chain|technically easy]] to change the number, and order, of filters. &lt;br /&gt;
However, not all combinations or orderings of filters make sense.&lt;br /&gt;
&lt;br /&gt;
The table shows modules and filters for BCI2000 configurations included in the core distribution:&lt;br /&gt;
&lt;br /&gt;
{| border=&amp;quot;1&amp;quot; style=&amp;quot;text-align:center&amp;quot;&lt;br /&gt;
! &amp;amp;nbsp; || colspan=&amp;quot;3&amp;quot; | Data Acquisition &lt;br /&gt;
! colspan=&amp;quot;5&amp;quot; | Signal Processing &lt;br /&gt;
! colspan=&amp;quot;3&amp;quot; | Application&lt;br /&gt;
|-&lt;br /&gt;
! SMR Feedback&lt;br /&gt;
&lt;br /&gt;
| rowspan=&amp;quot;3&amp;quot; |&lt;br /&gt;
{| border=&amp;quot;0&amp;quot; style=&amp;quot;text-align:center&amp;quot; &lt;br /&gt;
| colspan=&amp;quot;2&amp;quot; | [[User Reference:DataIOFilter|DataIOFilter]] &lt;br /&gt;
|-&lt;br /&gt;
| ADC || FileWriter&lt;br /&gt;
|} &lt;br /&gt;
| rowspan=&amp;quot;3&amp;quot; | [[User Reference:AlignmentFilter|AlignmentFilter]] &lt;br /&gt;
| rowspan=&amp;quot;3&amp;quot; | [[User Reference:TransmissionFilter|TransmissionFilter]]&lt;br /&gt;
| rowspan=&amp;quot;3&amp;quot; | [[User Reference:SpatialFilter|SpatialFilter]]&lt;br /&gt;
| [[User Reference:ARFilter|ARFilter]] or [[User Reference:FFTFilter|FFTFilter]] &lt;br /&gt;
| rowspan=&amp;quot;3&amp;quot; | [[User Reference:LinearClassifier|LinearClassifier]] &lt;br /&gt;
| rowspan=&amp;quot;3&amp;quot; | [[User Reference:LPFilter|LPFilter]] &lt;br /&gt;
| rowspan=&amp;quot;3&amp;quot; | [[User Reference:Normalizer|Normalizer]] &lt;br /&gt;
| rowspan=&amp;quot;3&amp;quot; | [[User Reference:ConnectorInput|ConnectorInput]] &lt;br /&gt;
| [[User Reference:CursorTask|CursorTask]] &lt;br /&gt;
| rowspan=&amp;quot;3&amp;quot; | [[User Reference:ConnectorOutput|ConnectorOutput]]&lt;br /&gt;
|-&lt;br /&gt;
! P3 Speller&lt;br /&gt;
| rowspan=&amp;quot;2&amp;quot; | [[User Reference:P3TemporalFilter|P3TemporalFilter]]&lt;br /&gt;
| [[User Reference:P3SpellerTask|P3SpellerTask]]&lt;br /&gt;
|-&lt;br /&gt;
! Stimulus Presentation&lt;br /&gt;
| [[User Reference:StimulusPresentationTask|StimulusPresentationTask]]&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==Data Acquisition Module==&lt;br /&gt;
In the data acquisition (source) module, the DataIOFilter manages data acquisition and storage in a general manner.&lt;br /&gt;
Actual acquisition of data is provided by ADC filters,&lt;br /&gt;
actual writing into data files is done by FileWriter filters representing various [[User Reference:Data File Formats|Data Formats]].&lt;br /&gt;
In addition to managing the operation of ADC and FileWriter filters, the DataIOFilter handles signal calibration into physical units (typically, &amp;lt;math&amp;gt;\mu V&amp;lt;/math&amp;gt;), and visualization of the source signal.&lt;br /&gt;
&lt;br /&gt;
Filter documentation for source modules is available for&lt;br /&gt;
===General Filters===&lt;br /&gt;
*[[User Reference:DataIOFilter]]: Storage and acquisition management, calibration.&lt;br /&gt;
*[[User Reference:SourceFilter]]: HP/LP/notch filtering prior to storage.&lt;br /&gt;
*[[User Reference:AlignmentFilter]]: Temporal alignment.&lt;br /&gt;
*[[User Reference:TransmissionFilter]]: Subset selection for online processing.&lt;br /&gt;
&lt;br /&gt;
===Data Acquisition===&lt;br /&gt;
*[[User Reference:SignalGeneratorADC]]: A signal generator for testing purposes.&lt;br /&gt;
*[[User Reference:gUSBampADC]]: Interface to the g.tec gUSBamp amplifier.&lt;br /&gt;
*[[User Reference:gMOBIlabADC]]: Interface to the g.tec gMOBIlab amplifier.&lt;br /&gt;
*[[Contributions:ADCs]]: Contributed code interfacing to other hardware.&lt;br /&gt;
&lt;br /&gt;
===Data Storage===&lt;br /&gt;
*[[User Reference:BCI2000FileWriter]]: BCI2000 data format.&lt;br /&gt;
*[[User Reference:EDFFileWriter]]: European Data Format.&lt;br /&gt;
*[[User Reference:GDFFileWriter]]: General Data Format for Biosignals.&lt;br /&gt;
*[[User Reference:NullFileWriter]]: Suppressing file output.&lt;br /&gt;
*[[Contributions:FileWriters]]: Contributed code supporting other output formats.&lt;br /&gt;
&lt;br /&gt;
==Signal Processing Module==&lt;br /&gt;
In the signal processing module, brain signals are filtered spatially and temporally, resulting in a set of extracted features. In the Classifier, these features are used to differentiate amongst a small number of mental states (classes). Finally, the Normalizer adjusts the Classifier&#039;s output to zero mean and unit variance.&lt;br /&gt;
&lt;br /&gt;
Filter documentation for signal processing filters is available for&lt;br /&gt;
===Feature Extraction===&lt;br /&gt;
*[[User Reference:SpatialFilter]]: Instantaneous (i.e., sample-wise) linear transformation of brain signal input.&lt;br /&gt;
*[[User Reference:ARFilter]]: Auto-regression based spectral amplitude estimator.&lt;br /&gt;
*[[User Reference:FFTFilter]]: Short-term FFT for display or feature extraction purposes.&lt;br /&gt;
*[[User Reference:P3TemporalFilter]]: Segmenting and averaging for ERP feature extraction.&lt;br /&gt;
&lt;br /&gt;
===Translation Algorithm===&lt;br /&gt;
*[[User Reference:LinearClassifier]]: Linear projection of signal features onto low-dimensional classification space (control signal).&lt;br /&gt;
*[[User Reference:Normalizer]]: Adjustment of control signal to zero mean/unit variance.&lt;br /&gt;
&lt;br /&gt;
===Miscellaneous Filters===&lt;br /&gt;
*[[User Reference:LPFilter]]: Temporal low-pass filtering at any stage of processing.&lt;br /&gt;
*[[User Reference:ConditionalIntegrator]]: An offline replacement for cursor movement in an online task.&lt;br /&gt;
*[[User Reference:StateTransform]]: An offline replacement for hitting targets in an online task.&lt;br /&gt;
*[[User Reference:ExpressionFilter]]: Computes filter output using arithmetic expressions.&lt;br /&gt;
*[[User Reference:MatlabFilter]]: Using MATLAB to implement online processing in the pipeline.&lt;br /&gt;
&lt;br /&gt;
==Application Module==&lt;br /&gt;
Basically, the application module contains a single filter that handles trial sequencing and brain signal feedback.&lt;br /&gt;
In the standard configuration, this &#039;&#039;task&#039;&#039; filter is surrounded by ConnectorInput and ConnectorOutput filters. These connector filters allow for exchanging data with external software over a UDP based socket protocol.&lt;br /&gt;
&lt;br /&gt;
Filter documentation of application module filters is available for&lt;br /&gt;
===Stimulus Presentation and Feedback===&lt;br /&gt;
*[[User Reference:CursorTask]]: Feedback of brain signals in form of up to 3-dimensional cursor movement.&lt;br /&gt;
*[[User Reference:StimulusPresentationTask]]: Sequential presentation of stimuli.&lt;br /&gt;
*[[User Reference:P3SpellerTask]]: A P300 speller matrix application.&lt;br /&gt;
===External Interfaces===&lt;br /&gt;
*[[User Reference:ConnectorOutput]]: Reports state information to a UDP socket.&lt;br /&gt;
*[[User Reference:ConnectorInput]]: Sets state information according to input from a UDP socket.&lt;br /&gt;
*[[User Reference:KeystrokeFilter]]: Translates state information into simulated keyboard input.&lt;br /&gt;
*[[User Reference:JoystickFilter]]: Records joystick movements into a state.&lt;br /&gt;
*[[User Reference:KeyLogFilter]]: Records keyboard and mouse key presses into a state.&lt;br /&gt;
*[[User Reference:MouseFilter]]: Records mouse pointer coordinates into a state.&lt;br /&gt;
&lt;br /&gt;
===Internationalization===&lt;br /&gt;
*[[User Reference:Localization]]: Translating subject-visible messages.&lt;br /&gt;
&lt;br /&gt;
==See also==&lt;br /&gt;
[[Technical Reference:Core Modules]]&lt;br /&gt;
&lt;br /&gt;
[[Category:Contents]][[Category:Filters]]&lt;/div&gt;</summary>
		<author><name>Roostenveld</name></author>
	</entry>
	<entry>
		<id>https://www.bci2000.org/mediawiki/index.php?title=Programming_Reference:MatlabFilter&amp;diff=3989</id>
		<title>Programming Reference:MatlabFilter</title>
		<link rel="alternate" type="text/html" href="https://www.bci2000.org/mediawiki/index.php?title=Programming_Reference:MatlabFilter&amp;diff=3989"/>
		<updated>2008-07-14T13:16:17Z</updated>

		<summary type="html">&lt;p&gt;Roostenveld: /* Overview */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Overview==&lt;br /&gt;
&lt;br /&gt;
The MatlabFilter implements a mechanism for using the Matlab engine within the BCI2000 pipeline. The &#039;&#039;MatlabFilter&#039;&#039; calls the Matlab engine to act upon signals, parameters, and states. It provides the full BCI2000 [[Programming Reference:GenericFilter Class|filter interface]] to a Matlab filter implementation.&lt;br /&gt;
&lt;br /&gt;
While BCI2000 is running, each block of data is pushed to the Matlab engine and a well-specified Matlab function (i.e. *.m file) is executed. As explained in more detail in the Programming reference for the  [[Programming_Reference:GenericFilter_Class|GenericFilter Class]], all BCI2000 filters consist of a set of functions. The MatlabFilter will, upon execution of each of these functions, contact the Matlab engine and execure the corresponding Matlab function equivalent. Furthermore, the MatlabFilter copies the input signal and the states to the Matlab engine, and copies the output signal and states from the engine after processing the signal.&lt;br /&gt;
&lt;br /&gt;
{| style=&amp;quot;color:black;background-color:#ffffcc;&amp;quot; cellpadding=&amp;quot;5&amp;quot; cellspacing=&amp;quot;0&amp;quot; border=&amp;quot;2&amp;quot; &lt;br /&gt;
!BCI2000 function&lt;br /&gt;
!Matlab equivalent&lt;br /&gt;
|-&lt;br /&gt;
|[[Programming_Reference:GenericFilter_Class#Constructor|Constructor()]]&lt;br /&gt;
|bci_Construct&lt;br /&gt;
|-&lt;br /&gt;
|[[Programming_Reference:GenericFilter_Class#Preflight|Preflight()]]&lt;br /&gt;
|bci_Preflight&lt;br /&gt;
|-&lt;br /&gt;
|[[Programming_Reference:GenericFilter_Class#Initialize|Initialize()]]&lt;br /&gt;
|bci_Initialize&lt;br /&gt;
|-&lt;br /&gt;
|[[Programming_Reference:GenericFilter_Class#StartRun|StartRun()]]&lt;br /&gt;
|bci_StartRun&lt;br /&gt;
|-&lt;br /&gt;
|[[Programming_Reference:GenericFilter_Class#Process|Process()]]&lt;br /&gt;
|bci_Process&lt;br /&gt;
|-&lt;br /&gt;
|[[Programming_Reference:GenericFilter_Class#StopRun|StopRun()]]&lt;br /&gt;
|bci_StopRun&lt;br /&gt;
|-&lt;br /&gt;
|[[Programming_Reference:GenericFilter_Class#Resting|Resting()]]&lt;br /&gt;
|bci_Resting&lt;br /&gt;
|-&lt;br /&gt;
|[[Programming_Reference:GenericFilter_Class#Halt|Halt()]]&lt;br /&gt;
|bci_Halt&lt;br /&gt;
|-&lt;br /&gt;
|[[Programming_Reference:GenericFilter_Class#Destructor|Destructor()]]&lt;br /&gt;
|bci_Destruct&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Existence of the above-listed Matlab functions is not mandatory, the Matlab &#039;exist&#039; command will be used to determine whether a given function is available, and will not call the Matlab engine when this is not the case. If either of the bci_Preflight, bci_Initialize, or bci_Process functions is not available, a warning will be displayed to the user.&lt;br /&gt;
&lt;br /&gt;
Most of these Matlab functions do not take input arguments, except for&lt;br /&gt;
  function [parameters, states] = bci_Construct;&lt;br /&gt;
  &lt;br /&gt;
  function [out_signal_dim] = bci_Preflight( in_signal_dim );&lt;br /&gt;
  &lt;br /&gt;
  function bci_Initialize( in_signal_dim, out_signal_dim );&lt;br /&gt;
  &lt;br /&gt;
  function [out_signal] = bci_Process( in_signal );&lt;br /&gt;
&lt;br /&gt;
The most important functions is &amp;lt;tt&amp;gt;bci_Process&amp;lt;/tt&amp;gt;, which is executed every time a new block of data is passed through the pipeline. The &amp;lt;tt&amp;gt;bci_Construct&amp;lt;/tt&amp;gt; function is executed at the beginning and determines the parameters and states. The &amp;lt;tt&amp;gt;bci_Initialize&amp;lt;/tt&amp;gt; function determines whether all requirements are met. The &amp;lt;tt&amp;gt;bci_StartRun&amp;lt;/tt&amp;gt; function prepares the computation that is done during processing (e.g. computing filter parameters). Communication between these Matlab functions is done using global variables.&lt;br /&gt;
&lt;br /&gt;
==Parameters and States==&lt;br /&gt;
&lt;br /&gt;
Parameters and states are accessible via global Matlab structs called  &#039;bci_Parameters&#039; and &#039;bci_States&#039;. Parameters may be changed from &amp;lt;tt&amp;gt;bci_StopRun&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;bci_Resting&amp;lt;/tt&amp;gt;, and will automatically be propagated to the other modules. State values may be modified from the &amp;lt;tt&amp;gt;bci_Process&amp;lt;/tt&amp;gt; function. &lt;br /&gt;
&lt;br /&gt;
To add parameters and states to the BCI2000 list of states, the &amp;lt;tt&amp;gt;bci_Construct&amp;lt;/tt&amp;gt; function may return non-empty cell arrays of strings in its parameters  and states return values. The strings constituting these cell arrays are [[Technical Reference:Parameter Definition|parameter]] and [[Technical Reference:State Definition|state definitions]].&lt;br /&gt;
&lt;br /&gt;
Your added parameters will appear in the [[User Reference:Operator Module|operator module&#039;s]] parameter configuration dialog. Your parameter definition&#039;s section name will be used to direct its position in the configuration dialog&#039;s register cards:&lt;br /&gt;
 MyFilter int MyParam= 2 0 0 2 // ...&lt;br /&gt;
will display the &#039;&#039;MyParam&#039;&#039; parameter on separate register card named &#039;&#039;MyFilter&#039;&#039;.&lt;br /&gt;
 Filtering:MyFilter int MyParam= 2 0 0 2 // ...&lt;br /&gt;
will display the parameter on the &#039;&#039;Filtering&#039;&#039; register card, inside a group called &#039;&#039;MyFilter&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
==Input and Output Signal Format==&lt;br /&gt;
&lt;br /&gt;
BCI2000 signals are mapped to Matlab matrices with channel index first, and sample (element) index second. The signal dimension arguments of &amp;lt;tt&amp;gt;bci_Preflight&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;bci_Initialize&amp;lt;/tt&amp;gt; are integer vectors  of size 1x2 with &amp;lt;tt&amp;gt;[n_channels n_elements]&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
==Error Reporting==&lt;br /&gt;
&lt;br /&gt;
To report errors from Matlab functions, use Matlab&#039;s &amp;lt;tt&amp;gt;error&amp;lt;/tt&amp;gt; command.&lt;br /&gt;
Your error messages will be displayed to the user from the [[User Reference:Operator Module|operator module]].&lt;br /&gt;
&lt;br /&gt;
==Combining the MatlabFilter with other Signal Processing Filters==&lt;br /&gt;
&lt;br /&gt;
In the BCI2000 binary distribution, the &#039;&#039;MatlabFilter&#039;&#039; is shipped inside the &#039;&#039;MatlabSignalProcessing&#039;&#039; executable. There, the signal processing chain consists of the [[User Reference:SpatialFilter|SpatialFilter]] and the &#039;&#039;MatlabFilter&#039;&#039;. However, by editing &amp;lt;tt&amp;gt;src/core/SignalProcessing/Matlab/PipeDefinition.cpp&amp;lt;/tt&amp;gt;, you may add as many [[User Reference:Filters|signal processing filters]] as you wish.&lt;br /&gt;
&lt;br /&gt;
See [[Programming Reference:Filter Chain]] for information about defining a filter chain. Modification of the filter chain requires access to the [[Programming Reference:BCI2000 Source Code|BCI2000 source code]]. You will need to rebuild the &#039;&#039;MatlabSignalProcessing&#039;&#039; executable.&lt;br /&gt;
&lt;br /&gt;
==Command Line Options==&lt;br /&gt;
===MatlabWD===&lt;br /&gt;
By default, the Matlab engine&#039;s working directory is set to the signal processing module&#039;s working directory at startup. When using the Matlab filter for signal processing, your filter&#039;s code will be contained in the &amp;lt;tt&amp;gt;bci_Process&amp;lt;/tt&amp;gt; and associated Matlab functions, which will typically be located inside a dedicated directory. Thus, you may switch between Matlab-based filter implementations by selecting a Matlab working directory at startup.&lt;br /&gt;
This may be done by specifying the &amp;lt;tt&amp;gt;MatlabWD&amp;lt;/tt&amp;gt; parameter from the command line when starting the MatlabSignalProcessing module:&lt;br /&gt;
&amp;lt;pre&amp;gt;start MatlabSignalProcessing.exe --MatlabWD=&amp;quot;./matlab&amp;quot; 127.0.0.1&amp;lt;/pre&amp;gt;&lt;br /&gt;
will change Matlab&#039;s working directory to a directory called &amp;quot;matlab&amp;quot; inside the BCI2000 &amp;quot;prog&amp;quot; directory at startup, and execute &amp;lt;tt&amp;gt;bci_Process&amp;lt;/tt&amp;gt; and associated functions from there.&lt;br /&gt;
&lt;br /&gt;
This parameter must be set from the command line; later changes will have no effect.&lt;br /&gt;
&lt;br /&gt;
Using the &amp;lt;tt&amp;gt;--MatlabWD&amp;lt;/tt&amp;gt; command line option, you may easily switch between multiple Matlab-based BCI2000 configurations.&lt;br /&gt;
Simply create a separate startup batch file for each configuration, and specify the respective configuration&#039;s path on the command line that starts up the MatlabFilter.&lt;br /&gt;
&lt;br /&gt;
===MatlabStayOpen===&lt;br /&gt;
By default, the Matlab instance associated with the Matlab engine will be closed when BCI2000 quits.&lt;br /&gt;
Generally, this behavior is desired to ensure a well-defined BCI2000 system state after each startup.&lt;br /&gt;
For debugging purposes, though, it is sometimes useful to have the engine&#039;s command window available even after BCI2000 has been quit.&lt;br /&gt;
This behavior is controlled by another command-line parameter, &amp;lt;tt&amp;gt;MatlabStayOpen&amp;lt;/tt&amp;gt;, which may take the following values:&lt;br /&gt;
*0 is the default (no change in behavior),&lt;br /&gt;
*1 keeps the Matlab engine&#039;s command window open but clears variables used to transmit information forth and back between BCI2000 and Matlab,&lt;br /&gt;
*2 also preserves intermediate variables (such as bci_Parameters and bci_States) in the Matlab engine&#039;s workspace.&lt;br /&gt;
&lt;br /&gt;
To set this parameter to 2, use this command line to start up the signal processing module:&lt;br /&gt;
&amp;lt;pre&amp;gt;start MatlabSignalProcessing.exe --MatlabStayOpen=2 127.0.0.1&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Once the &amp;lt;tt&amp;gt;MatlabStayOpen&amp;lt;/tt&amp;gt; parameter has been set from the command line, it will be listed on the Operator Module&#039;s parameter configuration dialog&#039;s &amp;quot;system&amp;quot; tab, and may be modified from there. The change will be applied when clicking &#039;&#039;Set Config&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
==Troubleshooting==&lt;br /&gt;
===Debugging Matlab functions===&lt;br /&gt;
#Start BCI2000. The &#039;&#039;MatlabFilter&#039;&#039; will open a Matlab instance in its own window in minimized state.&lt;br /&gt;
#Switch to the minimized Matlab instance.&lt;br /&gt;
#Type, e.g.,&amp;lt;br /&amp;gt;&amp;lt;code&amp;gt;edit bci_Process.m&amp;lt;/code&amp;gt;&amp;lt;br /&amp;gt;to open a Matlab editor window connected to the Matlab interactive window.&lt;br /&gt;
#In the editor window, set breakpoints as you would normally.&lt;br /&gt;
#Execution will be paused, and you may examine variables moving the mouse over them.&lt;br /&gt;
&lt;br /&gt;
===Matlab doesn&#039;t find your functions===&lt;br /&gt;
Make sure to&lt;br /&gt;
*either set Matlab&#039;s working directory to the directory containing your functions, using the &amp;lt;tt&amp;gt;--MatlabWD&amp;lt;/tt&amp;gt; command line option described above,&lt;br /&gt;
*or add the respective directory to your Matlab path.&lt;br /&gt;
Generally, the first option is recommended over the second one.&lt;br /&gt;
&lt;br /&gt;
===There is no Matlab engine started up===&lt;br /&gt;
Execute&amp;lt;br /&amp;gt;&amp;lt;code&amp;gt;matlab /regserver&amp;lt;/code&amp;gt;&amp;lt;br /&amp;gt;from the command line when logged in with administrative privileges.&lt;br /&gt;
===Changes to M-files appear to have no effect===&lt;br /&gt;
Switch to the Matlab engine&#039;s command window, and enter&amp;lt;br /&amp;gt;&amp;lt;code&amp;gt;clear;&amp;lt;/code&amp;gt;&amp;lt;br /&amp;gt; or &amp;lt;br /&amp;gt;&amp;lt;code&amp;gt;clear scriptname;&amp;lt;/code&amp;gt;&amp;lt;br /&amp;gt;each time you change a Matlab function.&lt;br /&gt;
&lt;br /&gt;
===You get linker errors when rebuilding &#039;&#039;MatlabSignalProcessing&#039;&#039;===&lt;br /&gt;
If you get linker errors after editing &amp;lt;tt&amp;gt;PipeDefinition.cpp&amp;lt;/tt&amp;gt;, make sure that all filters&#039; cpp files are part of the &#039;&#039;MatlabSignalProcessing&#039;&#039; project.&lt;br /&gt;
===You get a message window with an &amp;quot;Entry Point Not Found&amp;quot; message===&lt;br /&gt;
This error is likely to indicate confusion of multiple installations of Matlab on your machine.&lt;br /&gt;
Check your Windows &#039;&#039;%PATH%&#039;&#039; environment variable, e.g. by opening a command console window and typing&lt;br /&gt;
 echo %PATH%&lt;br /&gt;
When it contains references to more than a single Matlab installation, remove all apart from the reference to the installation you are going to use with BCI2000. Also, make sure to execute&lt;br /&gt;
 matlab /regserver&lt;br /&gt;
for the Matlab installation you intend to use.&lt;br /&gt;
&lt;br /&gt;
==See also==&lt;br /&gt;
[[Technical Reference:Core Modules#Signal Processing Module]], &lt;br /&gt;
[[Technical Reference:Parameter Definition]], &lt;br /&gt;
[[Technical Reference:State Definition]], &lt;br /&gt;
[[Programming Reference:GenericFilter Class]], &lt;br /&gt;
[[Programming Reference:Filter Chain]], &lt;br /&gt;
[[Contributions:FieldTripBuffer]]&lt;br /&gt;
&lt;br /&gt;
Tutorials:&lt;br /&gt;
* [[Programming_Tutorial:Implementing_a_Matlab-based_Filter|IIR bandpass filtering, followed with RMS envelope computation, and linear classification]]&lt;br /&gt;
* [[Programming_Tutorial:Implementing_another_Matlab-based_Filter|adaptive spatial filtering using a beamformer, power computation and normalization]]&lt;br /&gt;
&lt;br /&gt;
[[Category:Filters]]&lt;br /&gt;
[[Category:Signal Processing]]&lt;br /&gt;
[[Category:Matlab]]&lt;br /&gt;
[[Category:Development]]&lt;br /&gt;
[[Category:External Interfaces]]&lt;/div&gt;</summary>
		<author><name>Roostenveld</name></author>
	</entry>
	<entry>
		<id>https://www.bci2000.org/mediawiki/index.php?title=Programming_Reference:MatlabFilter&amp;diff=3988</id>
		<title>Programming Reference:MatlabFilter</title>
		<link rel="alternate" type="text/html" href="https://www.bci2000.org/mediawiki/index.php?title=Programming_Reference:MatlabFilter&amp;diff=3988"/>
		<updated>2008-07-14T13:14:44Z</updated>

		<summary type="html">&lt;p&gt;Roostenveld: /* Overview */ typo&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Overview==&lt;br /&gt;
&lt;br /&gt;
The MatlabFilter implements a mechanism for using the Matlab engine within the BCI2000 pipeline. The &#039;&#039;MatlabFilter&#039;&#039; calls the Matlab engine to act upon signals, parameters, and states. It provides the full BCI2000 [[Programming Reference:GenericFilter Class|filter interface]] to a Matlab filter implementation.&lt;br /&gt;
&lt;br /&gt;
While BCI2000 is running, each block of data is pushed to the Matlab engine and a well-specified Matlab function (i.e. *.m file) is executed. As explained in more detail in the Programming reference for the  [[Programming_Reference:GenericFilter_Class|GenericFilter Class]], all BCI2000 filters consist of a set of functions. The MatlabFilter will, upon execution of each of these functions, contact the Matlab engine and execure the corresponding Matlab function equivalent. Furthermore, the MatlabFilter copies the input signal and the states to the Matlab engine, and copies the output signal and states from the engine after processing the signal.&lt;br /&gt;
&lt;br /&gt;
{| style=&amp;quot;color:black;background-color:#ffffcc;&amp;quot; cellpadding=&amp;quot;5&amp;quot; cellspacing=&amp;quot;0&amp;quot; border=&amp;quot;2&amp;quot; &lt;br /&gt;
!BCI2000 function&lt;br /&gt;
!Matlab equivalent&lt;br /&gt;
|-&lt;br /&gt;
|[[Programming_Reference:GenericFilter_Class#Constructor|Constructor()]]&lt;br /&gt;
|bci_Construct&lt;br /&gt;
|-&lt;br /&gt;
|[[Programming_Reference:GenericFilter_Class#Preflight|Preflight()]]&lt;br /&gt;
|bci_Preflight&lt;br /&gt;
|-&lt;br /&gt;
|[[Programming_Reference:GenericFilter_Class#Initialize|Initialize()]]&lt;br /&gt;
|bci_Initialize&lt;br /&gt;
|-&lt;br /&gt;
|[[Programming_Reference:GenericFilter_Class#StartRun|StartRun()]]&lt;br /&gt;
|bci_StartRun&lt;br /&gt;
|-&lt;br /&gt;
|[[Programming_Reference:GenericFilter_Class#Process|Process()]]&lt;br /&gt;
|bci_Process&lt;br /&gt;
|-&lt;br /&gt;
|[[Programming_Reference:GenericFilter_Class#StopRun|StopRun()]]&lt;br /&gt;
|bci_StopRun&lt;br /&gt;
|-&lt;br /&gt;
|[[Programming_Reference:GenericFilter_Class#Resting|Resting()]]&lt;br /&gt;
|bci_Resting&lt;br /&gt;
|-&lt;br /&gt;
|[[Programming_Reference:GenericFilter_Class#Halt|Halt()]]&lt;br /&gt;
|bci_Halt&lt;br /&gt;
|-&lt;br /&gt;
|[[Programming_Reference:GenericFilter_Class#Destructor|Destructor()]]&lt;br /&gt;
|bci_Destruct&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Existence of the above-listed Matlab functions is not mandatory, the Matlab &#039;exist&#039; command will be used to determine whether a given function is available, and will not call the Matlab engine when this is not the case. If either of the bci_Preflight, bci_Initialize, or bci_Process functions is not available, a warning will be displayed to the user.&lt;br /&gt;
&lt;br /&gt;
Most of these Matlab functions do not take input arguments, except for&lt;br /&gt;
  function [parameters, states] = bci_Construct;&lt;br /&gt;
  &lt;br /&gt;
  function [out_signal_dim] = bci_Preflight( in_signal_dim );&lt;br /&gt;
  &lt;br /&gt;
  function bci_Initialize( in_signal_dim, out_signal_dim );&lt;br /&gt;
  &lt;br /&gt;
  function [out_signal] = bci_Process( in_signal );&lt;br /&gt;
&lt;br /&gt;
The most important functions is &amp;lt;tt&amp;gt;bci_Process&amp;lt;/tt&amp;gt;, which is executed every time a new block of data is passed through the pipeline. Furthermore &amp;lt;tt&amp;gt;bci_Construct&amp;lt;/tt&amp;gt; is executed at the beginning and determines the parameters and states, &amp;lt;tt&amp;gt;bci_Initialize&amp;lt;/tt&amp;gt; determines whether all requirements are met, and &amp;lt;tt&amp;gt;bci_StartRun&amp;lt;/tt&amp;gt; prepares the computation that is done during processing (e.g. computing filter parameters). Communication between these Matlab functions is done using global variables.&lt;br /&gt;
&lt;br /&gt;
==Parameters and States==&lt;br /&gt;
&lt;br /&gt;
Parameters and states are accessible via global Matlab structs called  &#039;bci_Parameters&#039; and &#039;bci_States&#039;. Parameters may be changed from &amp;lt;tt&amp;gt;bci_StopRun&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;bci_Resting&amp;lt;/tt&amp;gt;, and will automatically be propagated to the other modules. State values may be modified from the &amp;lt;tt&amp;gt;bci_Process&amp;lt;/tt&amp;gt; function. &lt;br /&gt;
&lt;br /&gt;
To add parameters and states to the BCI2000 list of states, the &amp;lt;tt&amp;gt;bci_Construct&amp;lt;/tt&amp;gt; function may return non-empty cell arrays of strings in its parameters  and states return values. The strings constituting these cell arrays are [[Technical Reference:Parameter Definition|parameter]] and [[Technical Reference:State Definition|state definitions]].&lt;br /&gt;
&lt;br /&gt;
Your added parameters will appear in the [[User Reference:Operator Module|operator module&#039;s]] parameter configuration dialog. Your parameter definition&#039;s section name will be used to direct its position in the configuration dialog&#039;s register cards:&lt;br /&gt;
 MyFilter int MyParam= 2 0 0 2 // ...&lt;br /&gt;
will display the &#039;&#039;MyParam&#039;&#039; parameter on separate register card named &#039;&#039;MyFilter&#039;&#039;.&lt;br /&gt;
 Filtering:MyFilter int MyParam= 2 0 0 2 // ...&lt;br /&gt;
will display the parameter on the &#039;&#039;Filtering&#039;&#039; register card, inside a group called &#039;&#039;MyFilter&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
==Input and Output Signal Format==&lt;br /&gt;
&lt;br /&gt;
BCI2000 signals are mapped to Matlab matrices with channel index first, and sample (element) index second. The signal dimension arguments of &amp;lt;tt&amp;gt;bci_Preflight&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;bci_Initialize&amp;lt;/tt&amp;gt; are integer vectors  of size 1x2 with &amp;lt;tt&amp;gt;[n_channels n_elements]&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
==Error Reporting==&lt;br /&gt;
&lt;br /&gt;
To report errors from Matlab functions, use Matlab&#039;s &amp;lt;tt&amp;gt;error&amp;lt;/tt&amp;gt; command.&lt;br /&gt;
Your error messages will be displayed to the user from the [[User Reference:Operator Module|operator module]].&lt;br /&gt;
&lt;br /&gt;
==Combining the MatlabFilter with other Signal Processing Filters==&lt;br /&gt;
&lt;br /&gt;
In the BCI2000 binary distribution, the &#039;&#039;MatlabFilter&#039;&#039; is shipped inside the &#039;&#039;MatlabSignalProcessing&#039;&#039; executable. There, the signal processing chain consists of the [[User Reference:SpatialFilter|SpatialFilter]] and the &#039;&#039;MatlabFilter&#039;&#039;. However, by editing &amp;lt;tt&amp;gt;src/core/SignalProcessing/Matlab/PipeDefinition.cpp&amp;lt;/tt&amp;gt;, you may add as many [[User Reference:Filters|signal processing filters]] as you wish.&lt;br /&gt;
&lt;br /&gt;
See [[Programming Reference:Filter Chain]] for information about defining a filter chain. Modification of the filter chain requires access to the [[Programming Reference:BCI2000 Source Code|BCI2000 source code]]. You will need to rebuild the &#039;&#039;MatlabSignalProcessing&#039;&#039; executable.&lt;br /&gt;
&lt;br /&gt;
==Command Line Options==&lt;br /&gt;
===MatlabWD===&lt;br /&gt;
By default, the Matlab engine&#039;s working directory is set to the signal processing module&#039;s working directory at startup. When using the Matlab filter for signal processing, your filter&#039;s code will be contained in the &amp;lt;tt&amp;gt;bci_Process&amp;lt;/tt&amp;gt; and associated Matlab functions, which will typically be located inside a dedicated directory. Thus, you may switch between Matlab-based filter implementations by selecting a Matlab working directory at startup.&lt;br /&gt;
This may be done by specifying the &amp;lt;tt&amp;gt;MatlabWD&amp;lt;/tt&amp;gt; parameter from the command line when starting the MatlabSignalProcessing module:&lt;br /&gt;
&amp;lt;pre&amp;gt;start MatlabSignalProcessing.exe --MatlabWD=&amp;quot;./matlab&amp;quot; 127.0.0.1&amp;lt;/pre&amp;gt;&lt;br /&gt;
will change Matlab&#039;s working directory to a directory called &amp;quot;matlab&amp;quot; inside the BCI2000 &amp;quot;prog&amp;quot; directory at startup, and execute &amp;lt;tt&amp;gt;bci_Process&amp;lt;/tt&amp;gt; and associated functions from there.&lt;br /&gt;
&lt;br /&gt;
This parameter must be set from the command line; later changes will have no effect.&lt;br /&gt;
&lt;br /&gt;
Using the &amp;lt;tt&amp;gt;--MatlabWD&amp;lt;/tt&amp;gt; command line option, you may easily switch between multiple Matlab-based BCI2000 configurations.&lt;br /&gt;
Simply create a separate startup batch file for each configuration, and specify the respective configuration&#039;s path on the command line that starts up the MatlabFilter.&lt;br /&gt;
&lt;br /&gt;
===MatlabStayOpen===&lt;br /&gt;
By default, the Matlab instance associated with the Matlab engine will be closed when BCI2000 quits.&lt;br /&gt;
Generally, this behavior is desired to ensure a well-defined BCI2000 system state after each startup.&lt;br /&gt;
For debugging purposes, though, it is sometimes useful to have the engine&#039;s command window available even after BCI2000 has been quit.&lt;br /&gt;
This behavior is controlled by another command-line parameter, &amp;lt;tt&amp;gt;MatlabStayOpen&amp;lt;/tt&amp;gt;, which may take the following values:&lt;br /&gt;
*0 is the default (no change in behavior),&lt;br /&gt;
*1 keeps the Matlab engine&#039;s command window open but clears variables used to transmit information forth and back between BCI2000 and Matlab,&lt;br /&gt;
*2 also preserves intermediate variables (such as bci_Parameters and bci_States) in the Matlab engine&#039;s workspace.&lt;br /&gt;
&lt;br /&gt;
To set this parameter to 2, use this command line to start up the signal processing module:&lt;br /&gt;
&amp;lt;pre&amp;gt;start MatlabSignalProcessing.exe --MatlabStayOpen=2 127.0.0.1&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Once the &amp;lt;tt&amp;gt;MatlabStayOpen&amp;lt;/tt&amp;gt; parameter has been set from the command line, it will be listed on the Operator Module&#039;s parameter configuration dialog&#039;s &amp;quot;system&amp;quot; tab, and may be modified from there. The change will be applied when clicking &#039;&#039;Set Config&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
==Troubleshooting==&lt;br /&gt;
===Debugging Matlab functions===&lt;br /&gt;
#Start BCI2000. The &#039;&#039;MatlabFilter&#039;&#039; will open a Matlab instance in its own window in minimized state.&lt;br /&gt;
#Switch to the minimized Matlab instance.&lt;br /&gt;
#Type, e.g.,&amp;lt;br /&amp;gt;&amp;lt;code&amp;gt;edit bci_Process.m&amp;lt;/code&amp;gt;&amp;lt;br /&amp;gt;to open a Matlab editor window connected to the Matlab interactive window.&lt;br /&gt;
#In the editor window, set breakpoints as you would normally.&lt;br /&gt;
#Execution will be paused, and you may examine variables moving the mouse over them.&lt;br /&gt;
&lt;br /&gt;
===Matlab doesn&#039;t find your functions===&lt;br /&gt;
Make sure to&lt;br /&gt;
*either set Matlab&#039;s working directory to the directory containing your functions, using the &amp;lt;tt&amp;gt;--MatlabWD&amp;lt;/tt&amp;gt; command line option described above,&lt;br /&gt;
*or add the respective directory to your Matlab path.&lt;br /&gt;
Generally, the first option is recommended over the second one.&lt;br /&gt;
&lt;br /&gt;
===There is no Matlab engine started up===&lt;br /&gt;
Execute&amp;lt;br /&amp;gt;&amp;lt;code&amp;gt;matlab /regserver&amp;lt;/code&amp;gt;&amp;lt;br /&amp;gt;from the command line when logged in with administrative privileges.&lt;br /&gt;
===Changes to M-files appear to have no effect===&lt;br /&gt;
Switch to the Matlab engine&#039;s command window, and enter&amp;lt;br /&amp;gt;&amp;lt;code&amp;gt;clear;&amp;lt;/code&amp;gt;&amp;lt;br /&amp;gt; or &amp;lt;br /&amp;gt;&amp;lt;code&amp;gt;clear scriptname;&amp;lt;/code&amp;gt;&amp;lt;br /&amp;gt;each time you change a Matlab function.&lt;br /&gt;
&lt;br /&gt;
===You get linker errors when rebuilding &#039;&#039;MatlabSignalProcessing&#039;&#039;===&lt;br /&gt;
If you get linker errors after editing &amp;lt;tt&amp;gt;PipeDefinition.cpp&amp;lt;/tt&amp;gt;, make sure that all filters&#039; cpp files are part of the &#039;&#039;MatlabSignalProcessing&#039;&#039; project.&lt;br /&gt;
===You get a message window with an &amp;quot;Entry Point Not Found&amp;quot; message===&lt;br /&gt;
This error is likely to indicate confusion of multiple installations of Matlab on your machine.&lt;br /&gt;
Check your Windows &#039;&#039;%PATH%&#039;&#039; environment variable, e.g. by opening a command console window and typing&lt;br /&gt;
 echo %PATH%&lt;br /&gt;
When it contains references to more than a single Matlab installation, remove all apart from the reference to the installation you are going to use with BCI2000. Also, make sure to execute&lt;br /&gt;
 matlab /regserver&lt;br /&gt;
for the Matlab installation you intend to use.&lt;br /&gt;
&lt;br /&gt;
==See also==&lt;br /&gt;
[[Technical Reference:Core Modules#Signal Processing Module]], &lt;br /&gt;
[[Technical Reference:Parameter Definition]], &lt;br /&gt;
[[Technical Reference:State Definition]], &lt;br /&gt;
[[Programming Reference:GenericFilter Class]], &lt;br /&gt;
[[Programming Reference:Filter Chain]], &lt;br /&gt;
[[Contributions:FieldTripBuffer]]&lt;br /&gt;
&lt;br /&gt;
Tutorials:&lt;br /&gt;
* [[Programming_Tutorial:Implementing_a_Matlab-based_Filter|IIR bandpass filtering, followed with RMS envelope computation, and linear classification]]&lt;br /&gt;
* [[Programming_Tutorial:Implementing_another_Matlab-based_Filter|adaptive spatial filtering using a beamformer, power computation and normalization]]&lt;br /&gt;
&lt;br /&gt;
[[Category:Filters]]&lt;br /&gt;
[[Category:Signal Processing]]&lt;br /&gt;
[[Category:Matlab]]&lt;br /&gt;
[[Category:Development]]&lt;br /&gt;
[[Category:External Interfaces]]&lt;/div&gt;</summary>
		<author><name>Roostenveld</name></author>
	</entry>
	<entry>
		<id>https://www.bci2000.org/mediawiki/index.php?title=Programming_Reference:GenericFilter_Class&amp;diff=3987</id>
		<title>Programming Reference:GenericFilter Class</title>
		<link rel="alternate" type="text/html" href="https://www.bci2000.org/mediawiki/index.php?title=Programming_Reference:GenericFilter_Class&amp;diff=3987"/>
		<updated>2008-07-14T13:14:19Z</updated>

		<summary type="html">&lt;p&gt;Roostenveld: /* Initialize */ changed heading from level 2 to 3&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==The &amp;lt;tt&amp;gt;GenericFilter&amp;lt;/tt&amp;gt; class==&lt;br /&gt;
&amp;lt;tt&amp;gt;GenericFilter&amp;lt;/tt&amp;gt; is a base class that provides a programming&lt;br /&gt;
interface for all user code inside the core modules of this BCI2000&lt;br /&gt;
implementation.&lt;br /&gt;
Programming your own data acquisition module, your own filter inside&lt;br /&gt;
Signal Processing, or your own application, implies deriving your&lt;br /&gt;
own&lt;br /&gt;
class from &amp;lt;tt&amp;gt;GenericFilter&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&amp;lt;tt&amp;gt;GenericFilter&amp;lt;/tt&amp;gt;&#039;s member functions represent the various&lt;br /&gt;
initialization and processing events that occur during system startup&lt;br /&gt;
and operation (see [[Technical Reference:States of Operation]]).&lt;br /&gt;
Your own filter code &#039;&#039;must&#039;&#039;  implement its own&lt;br /&gt;
versions of some of these member functions:&lt;br /&gt;
&lt;br /&gt;
==Required Members==&lt;br /&gt;
===Constructor===&lt;br /&gt;
The &#039;&#039;Constructor&#039;&#039;, besides its general purpose of initializing member data, declares the parameters and states your filter wants to introduce into the system (the &amp;lt;tt&amp;gt;BEGIN_...&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;END_...&amp;lt;/tt&amp;gt; macros handle the actual function calls): &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 MyFilter::MyFilter()&lt;br /&gt;
 : mMyParam( 1 ),&lt;br /&gt;
   mMyOtherParam( 0.1 ),&lt;br /&gt;
   mCount( 0 )&lt;br /&gt;
 {&lt;br /&gt;
   BEGIN_PARAMETER_DEFINITIONS&lt;br /&gt;
     &amp;quot;MySection int MyParam= 1 &amp;quot;&lt;br /&gt;
       &amp;quot;0 0 3 // This is range-checked between 0 and 3&amp;quot;,&lt;br /&gt;
     &amp;quot;MySection float MyOtherParam= 0.1 &amp;quot;&lt;br /&gt;
       &amp;quot;0 % % // This is not automatically range-checked&amp;quot;,&lt;br /&gt;
   END_PARAMETER_DEFINITIONS&lt;br /&gt;
&lt;br /&gt;
   BEGIN_STATE_DEFINITIONS&lt;br /&gt;
     &amp;quot;MyState 1 0 0 0&amp;quot;,&lt;br /&gt;
   END_STATE_DEFINITIONS&lt;br /&gt;
 } &amp;lt;/pre&amp;gt;&lt;br /&gt;
The data between the &amp;lt;code&amp;gt;BEGIN_...&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;END_...&amp;lt;/code&amp;gt; macros are lists of comma-separated C-strings, which are additionally broken across lines to improve readability. For the compiler, anything is fine here that can go between the curly braces in&lt;br /&gt;
 const char* array[] = { &amp;quot;entry 1&amp;quot;, &amp;quot;entry 2&amp;quot;, &amp;quot;entry 3&amp;quot;, };&lt;br /&gt;
For successful filter instantiation at runtime, the strings must also be valid [[Technical Reference:Parameter Definition|parameter]] and [[Technical Reference:State Definition|state definitions]].&lt;br /&gt;
&lt;br /&gt;
===Preflight===&lt;br /&gt;
The &amp;lt;tt&amp;gt;Preflight&amp;lt;/tt&amp;gt; function checks whether the preconditions for successful operation are met. This function is called whenever parameter values are re- applied, i.e., whenever the user presses &amp;quot;Set Config&amp;quot;, &amp;quot;Start&amp;quot;, or &amp;quot;Resume&amp;quot; in the operator window. If &amp;lt;tt&amp;gt;Preflight&amp;lt;/tt&amp;gt; does not report an error via &amp;lt;tt&amp;gt;bcierr&amp;lt;/tt&amp;gt;, this counts as a statement that &amp;lt;tt&amp;gt;Initialize&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;Process&amp;lt;/tt&amp;gt; will work properly with the current parameters. The first argument to &amp;lt;tt&amp;gt;Preflight&amp;lt;/tt&amp;gt; will inform you about what kind of input signal your filter is going to receive, and your filter is expected to report the properties of its output signal via the second parameter:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 void MyFilter::Preflight( const SignalProperties&amp;amp; Input,&lt;br /&gt;
                                 SignalProperties&amp;amp; Output ) const&lt;br /&gt;
 {&lt;br /&gt;
   PreflightCondition( Parameter( &amp;quot;MyOtherParam&amp;quot; ) &amp;gt; 0.0 );&lt;br /&gt;
   PreflightCondition( Input.Channels() &amp;gt; 0 );&lt;br /&gt;
   PreflightCondition( Output.Elements( 0 ) &amp;gt; 0 );&lt;br /&gt;
   Output = Input;&lt;br /&gt;
 } &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Note that the &amp;lt;tt&amp;gt;const&amp;lt;/tt&amp;gt; keyword following the function argument list forbids altering any data member of your filter object. This avoids diffusion of initialization code from &amp;lt;tt&amp;gt;Initialize&amp;lt;/tt&amp;gt; into &amp;lt;tt&amp;gt;Preflight&amp;lt;/tt&amp;gt;. If you have your own sub-objects instantiated and maintained by your filter, you should provide them with their own &amp;lt;tt&amp;gt;Preflight() const&amp;lt;/tt&amp;gt; member functions, and call these from your filter&#039;s &amp;lt;tt&amp;gt;Preflight&amp;lt;/tt&amp;gt;. &lt;br /&gt;
&lt;br /&gt;
===Initialize===&lt;br /&gt;
&amp;lt;tt&amp;gt;Initialize&amp;lt;/tt&amp;gt; is called after a successful &amp;lt;tt&amp;gt;Preflight&amp;lt;/tt&amp;gt;. Thus, it may safely omit all checks related to parameter consistency. In &amp;lt;tt&amp;gt;Initialize&amp;lt;/tt&amp;gt;, your filter&#039;s data members are set to the values implied by the user&#039;s choices, or to the initial values required for the filter&#039;s operation:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 void MyFilter::Initialize( const SignalProperties&amp;amp; Input,&lt;br /&gt;
                            const SignalProperties&amp;amp; Output )&lt;br /&gt;
 {&lt;br /&gt;
   mMyParam = Parameter( &amp;quot;MyParam&amp;quot; );&lt;br /&gt;
   mMyOtherParam = Parameter( &amp;quot;MyOtherParam&amp;quot; );&lt;br /&gt;
   mCount = 0;&lt;br /&gt;
 } &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Process===&lt;br /&gt;
The &amp;lt;tt&amp;gt;Process&amp;lt;/tt&amp;gt; function is called once for each block of EEG data. It receives an input in its first argument, and sets the output signal to values resulting from the filter operation. In the current BCI2000 implementation, there is a single chain of filters; one filter&#039;s output signal is the next filter&#039;s input. A filter which does not perform any modification to the signal (e.g., a statistics filter) needs to copy its input signal into the output signal, as in the example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 void MyFilter::Process( const GenericSignal&amp;amp; Input,&lt;br /&gt;
                               GenericSignal&amp;amp; Output )&lt;br /&gt;
 {&lt;br /&gt;
   if( Input( 0, 0 ) &amp;gt; mMyOtherParam )&lt;br /&gt;
     ++mCount;&lt;br /&gt;
   Output = Input;&lt;br /&gt;
 } &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;tt&amp;gt;Process&amp;lt;/tt&amp;gt; function should not acquire or release resources (opening/closing files, allocating memory, etc). Natural places for such operations are the &amp;lt;tt&amp;gt;Initialize&amp;lt;/tt&amp;gt;, &amp;lt;tt&amp;gt;StartRun&amp;lt;/tt&amp;gt;, and &amp;lt;tt&amp;gt;StopRun&amp;lt;/tt&amp;gt; member functions.&lt;br /&gt;
&lt;br /&gt;
==Optional Member Functions==&lt;br /&gt;
Other member functions are &#039;&#039;optional;&#039;&#039;  you may decide whether&lt;br /&gt;
you&lt;br /&gt;
override their default implementation with your own version,&lt;br /&gt;
depending on what your filter needs to do:&lt;br /&gt;
  &lt;br /&gt;
===StartRun===&lt;br /&gt;
&amp;lt;tt&amp;gt;StartRun&amp;lt;/tt&amp;gt; is called when the system enters the running state. As opposed to &amp;lt;tt&amp;gt;Initialize&amp;lt;/tt&amp;gt;, which is the place for tasks that need to be performed on each parameter change, &amp;lt;tt&amp;gt;StartRun&amp;lt;/tt&amp;gt; is provided for tasks that need to be performed each time the user clicks &amp;quot;Run&amp;quot; or &amp;quot;Resume&amp;quot; in the operator window. As a canonical example, the &amp;lt;tt&amp;gt;DataIOFilter&amp;lt;/tt&amp;gt; opens a new &amp;lt;tt&amp;gt;.dat&amp;lt;/tt&amp;gt; file from its &amp;lt;tt&amp;gt;StartRun&amp;lt;/tt&amp;gt; member function.&lt;br /&gt;
&lt;br /&gt;
===StopRun===&lt;br /&gt;
&amp;lt;tt&amp;gt;StopRun&amp;lt;/tt&amp;gt; is called each time the system leaves the running state, entering the suspended state. Typically, this happens whenever the user clicks &amp;quot;Suspend&amp;quot; in the operator window. The &amp;lt;tt&amp;gt;DataIOFilter&amp;lt;/tt&amp;gt; provides an example for its usage: This filter closes the current &amp;lt;tt&amp;gt;.dat&amp;lt;/tt&amp;gt; file from its &amp;lt;tt&amp;gt;StopRun&amp;lt;/tt&amp;gt; member function.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;tt&amp;gt;StopRun&amp;lt;/tt&amp;gt; is also the only function from which a filter may change a parameter value. Any parameter changes inside &amp;lt;tt&amp;gt;StopRun&amp;lt;/tt&amp;gt; will propagate to the other modules without any explicit request from your side.&lt;br /&gt;
&lt;br /&gt;
===Resting===&lt;br /&gt;
&amp;lt;tt&amp;gt;Resting&amp;lt;/tt&amp;gt; is called instead of &amp;lt;tt&amp;gt;Process&amp;lt;/tt&amp;gt; while the system is in suspended state. &lt;br /&gt;
&lt;br /&gt;
===Halt===&lt;br /&gt;
The &amp;lt;tt&amp;gt;Halt&amp;lt;/tt&amp;gt; function is called before any reconfiguration of the system takes place. If your filter initiates asynchronous operations such as playing a sound file, acquiring EEG data, or executing threads, its &amp;lt;tt&amp;gt;Halt&amp;lt;/tt&amp;gt; member function should terminate all such operations immediately. Failure to do so might result in a non-responding module, or in other errors difficult to track down. For descendants of &amp;lt;tt&amp;gt;GenericADC&amp;lt;/tt&amp;gt;, implementing the &amp;lt;tt&amp;gt;Halt&amp;lt;/tt&amp;gt; function is mandatory. &lt;br /&gt;
&lt;br /&gt;
===Destructor===&lt;br /&gt;
Your filter&#039;s &#039;&#039;Destructor&#039;&#039;  should free all resources acquired in the Constructor or in &amp;lt;tt&amp;gt;Initialize&amp;lt;/tt&amp;gt;. In many cases, freeing of resources will be done automatically if you use direct members instead of pointers, removing the need for a destructor. However, if your filter has a non-empty &amp;lt;tt&amp;gt;Halt&amp;lt;/tt&amp;gt; function, it needs a destructor that calls &amp;lt;tt&amp;gt;Halt&amp;lt;/tt&amp;gt; - this can not be done from the base class destructor because overridden virtual functions cannot be called from base class constructors or destructors.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 MyFilter::~MyFilter()&lt;br /&gt;
 {&lt;br /&gt;
   Halt();&lt;br /&gt;
 } &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===AllowsVisualization===&lt;br /&gt;
Unlike the previous members of &amp;lt;code&amp;gt;GenericFilter&amp;lt;/code&amp;gt;, this function represents a property rather than an event.&lt;br /&gt;
Through its return value, a filter can decide whether its output may be offered for visualization by the framework, i.e. whether it appears in the Processing Stages section in the operator&#039;s Visualize tab.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;AllowsVisualization&amp;lt;/code&amp;gt; is called once at instantiation, and then after each Initialize() that occurs.&lt;br /&gt;
If it returns false at instantiation, no visualization parameter is created for the filter.&lt;br /&gt;
If it returns false at initialization time, no default visualization is displayed,&lt;br /&gt;
independently of the visualization parameter&#039;s value.&lt;br /&gt;
The filter itself may still maintain its own visualization parameters and [[Programming Reference:GenericVisualization Class|GenericVisualization]] object.&lt;br /&gt;
&lt;br /&gt;
==Rules for Parameter and State access==&lt;br /&gt;
For consistent and robust overall system operation, it is crucial that individual filters actually check [[BCI2000 Glossary#Parameter|parameter]] values and [[BCI2000 Glossary#States|state]] accessibility from their &amp;lt;code&amp;gt;Preflight()&amp;lt;/code&amp;gt; function. To support this idea, the BCI2000 framework imposes a set of rules to parameter and state access:&lt;br /&gt;
*Parameters and states accessed during initialization or processing &#039;&#039;must&#039;&#039; be accessed during the preflight phase. Otherwise, an error message will be displayed when the first access occurs.&lt;br /&gt;
*To avoid pointless cludging of the &amp;lt;code&amp;gt;Preflight()&amp;lt;/code&amp;gt; function, there are some exceptions to this rule:&lt;br /&gt;
**States defined in a filter&#039;s constructor are known to be present, and thus need not be accessed from that filter&#039;s &amp;lt;code&amp;gt;Preflight()&amp;lt;/code&amp;gt; function.&lt;br /&gt;
**Parameters defined in a filter&#039;s constructor will be range-checked automatically if any of their &amp;lt;code&amp;gt;LowRange&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;HighRange&amp;lt;/code&amp;gt; [[Technical Reference:Parameter Definition|properties]] is set, and need not be accessed explicitly from &amp;lt;code&amp;gt;Preflight()&amp;lt;/code&amp;gt;.&lt;br /&gt;
**To disable automatic range checking, set both these properties to the empty string in your [[Technical Reference:Parameter Definition|parameter definition line]]:&amp;lt;br /&amp;gt;&amp;lt;code&amp;gt;MySection int MyParam= 1 0 % % // a parameter that is not automatically range-checked&amp;lt;/code&amp;gt;&amp;lt;br /&amp;gt;This implies that you need to implement your own range-check in &amp;lt;code&amp;gt;Preflight()&amp;lt;/code&amp;gt;. If there are no actual constraints on the parameter&#039;s value, just add a line &amp;lt;code&amp;gt;Parameter( &amp;quot;MyParam&amp;quot; );&amp;lt;/code&amp;gt; to the filter&#039;s &amp;lt;code&amp;gt;Preflight()&amp;lt;/code&amp;gt; function.&lt;br /&gt;
*[[User Reference:Expression Syntax|Expressions]] may involve state names, and thus need to be evaluated in order to check whether referred-to states are present. Just add a line &amp;lt;br /&amp;gt;&amp;lt;code&amp;gt;Expression( Parameter( &amp;quot;MyExpressionParam&amp;quot; ) ).Evaluate();&amp;lt;/code&amp;gt;&amp;lt;br /&amp;gt; to the filter&#039;s &amp;lt;code&amp;gt;Preflight()&amp;lt;/code&amp;gt; function.&lt;br /&gt;
&lt;br /&gt;
[[Category:Framework API]][[Category:Development]]&lt;/div&gt;</summary>
		<author><name>Roostenveld</name></author>
	</entry>
	<entry>
		<id>https://www.bci2000.org/mediawiki/index.php?title=Programming_Reference:GenericFilter_Class&amp;diff=3986</id>
		<title>Programming Reference:GenericFilter Class</title>
		<link rel="alternate" type="text/html" href="https://www.bci2000.org/mediawiki/index.php?title=Programming_Reference:GenericFilter_Class&amp;diff=3986"/>
		<updated>2008-07-14T13:13:54Z</updated>

		<summary type="html">&lt;p&gt;Roostenveld: added initialize heading&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==The &amp;lt;tt&amp;gt;GenericFilter&amp;lt;/tt&amp;gt; class==&lt;br /&gt;
&amp;lt;tt&amp;gt;GenericFilter&amp;lt;/tt&amp;gt; is a base class that provides a programming&lt;br /&gt;
interface for all user code inside the core modules of this BCI2000&lt;br /&gt;
implementation.&lt;br /&gt;
Programming your own data acquisition module, your own filter inside&lt;br /&gt;
Signal Processing, or your own application, implies deriving your&lt;br /&gt;
own&lt;br /&gt;
class from &amp;lt;tt&amp;gt;GenericFilter&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&amp;lt;tt&amp;gt;GenericFilter&amp;lt;/tt&amp;gt;&#039;s member functions represent the various&lt;br /&gt;
initialization and processing events that occur during system startup&lt;br /&gt;
and operation (see [[Technical Reference:States of Operation]]).&lt;br /&gt;
Your own filter code &#039;&#039;must&#039;&#039;  implement its own&lt;br /&gt;
versions of some of these member functions:&lt;br /&gt;
&lt;br /&gt;
==Required Members==&lt;br /&gt;
===Constructor===&lt;br /&gt;
The &#039;&#039;Constructor&#039;&#039;, besides its general purpose of initializing member data, declares the parameters and states your filter wants to introduce into the system (the &amp;lt;tt&amp;gt;BEGIN_...&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;END_...&amp;lt;/tt&amp;gt; macros handle the actual function calls): &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 MyFilter::MyFilter()&lt;br /&gt;
 : mMyParam( 1 ),&lt;br /&gt;
   mMyOtherParam( 0.1 ),&lt;br /&gt;
   mCount( 0 )&lt;br /&gt;
 {&lt;br /&gt;
   BEGIN_PARAMETER_DEFINITIONS&lt;br /&gt;
     &amp;quot;MySection int MyParam= 1 &amp;quot;&lt;br /&gt;
       &amp;quot;0 0 3 // This is range-checked between 0 and 3&amp;quot;,&lt;br /&gt;
     &amp;quot;MySection float MyOtherParam= 0.1 &amp;quot;&lt;br /&gt;
       &amp;quot;0 % % // This is not automatically range-checked&amp;quot;,&lt;br /&gt;
   END_PARAMETER_DEFINITIONS&lt;br /&gt;
&lt;br /&gt;
   BEGIN_STATE_DEFINITIONS&lt;br /&gt;
     &amp;quot;MyState 1 0 0 0&amp;quot;,&lt;br /&gt;
   END_STATE_DEFINITIONS&lt;br /&gt;
 } &amp;lt;/pre&amp;gt;&lt;br /&gt;
The data between the &amp;lt;code&amp;gt;BEGIN_...&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;END_...&amp;lt;/code&amp;gt; macros are lists of comma-separated C-strings, which are additionally broken across lines to improve readability. For the compiler, anything is fine here that can go between the curly braces in&lt;br /&gt;
 const char* array[] = { &amp;quot;entry 1&amp;quot;, &amp;quot;entry 2&amp;quot;, &amp;quot;entry 3&amp;quot;, };&lt;br /&gt;
For successful filter instantiation at runtime, the strings must also be valid [[Technical Reference:Parameter Definition|parameter]] and [[Technical Reference:State Definition|state definitions]].&lt;br /&gt;
&lt;br /&gt;
===Preflight===&lt;br /&gt;
The &amp;lt;tt&amp;gt;Preflight&amp;lt;/tt&amp;gt; function checks whether the preconditions for successful operation are met. This function is called whenever parameter values are re- applied, i.e., whenever the user presses &amp;quot;Set Config&amp;quot;, &amp;quot;Start&amp;quot;, or &amp;quot;Resume&amp;quot; in the operator window. If &amp;lt;tt&amp;gt;Preflight&amp;lt;/tt&amp;gt; does not report an error via &amp;lt;tt&amp;gt;bcierr&amp;lt;/tt&amp;gt;, this counts as a statement that &amp;lt;tt&amp;gt;Initialize&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;Process&amp;lt;/tt&amp;gt; will work properly with the current parameters. The first argument to &amp;lt;tt&amp;gt;Preflight&amp;lt;/tt&amp;gt; will inform you about what kind of input signal your filter is going to receive, and your filter is expected to report the properties of its output signal via the second parameter:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 void MyFilter::Preflight( const SignalProperties&amp;amp; Input,&lt;br /&gt;
                                 SignalProperties&amp;amp; Output ) const&lt;br /&gt;
 {&lt;br /&gt;
   PreflightCondition( Parameter( &amp;quot;MyOtherParam&amp;quot; ) &amp;gt; 0.0 );&lt;br /&gt;
   PreflightCondition( Input.Channels() &amp;gt; 0 );&lt;br /&gt;
   PreflightCondition( Output.Elements( 0 ) &amp;gt; 0 );&lt;br /&gt;
   Output = Input;&lt;br /&gt;
 } &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Note that the &amp;lt;tt&amp;gt;const&amp;lt;/tt&amp;gt; keyword following the function argument list forbids altering any data member of your filter object. This avoids diffusion of initialization code from &amp;lt;tt&amp;gt;Initialize&amp;lt;/tt&amp;gt; into &amp;lt;tt&amp;gt;Preflight&amp;lt;/tt&amp;gt;. If you have your own sub-objects instantiated and maintained by your filter, you should provide them with their own &amp;lt;tt&amp;gt;Preflight() const&amp;lt;/tt&amp;gt; member functions, and call these from your filter&#039;s &amp;lt;tt&amp;gt;Preflight&amp;lt;/tt&amp;gt;. &lt;br /&gt;
&lt;br /&gt;
==Initialize==&lt;br /&gt;
&amp;lt;tt&amp;gt;Initialize&amp;lt;/tt&amp;gt; is called after a successful &amp;lt;tt&amp;gt;Preflight&amp;lt;/tt&amp;gt;. Thus, it may safely omit all checks related to parameter consistency. In &amp;lt;tt&amp;gt;Initialize&amp;lt;/tt&amp;gt;, your filter&#039;s data members are set to the values implied by the user&#039;s choices, or to the initial values required for the filter&#039;s operation:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 void MyFilter::Initialize( const SignalProperties&amp;amp; Input,&lt;br /&gt;
                            const SignalProperties&amp;amp; Output )&lt;br /&gt;
 {&lt;br /&gt;
   mMyParam = Parameter( &amp;quot;MyParam&amp;quot; );&lt;br /&gt;
   mMyOtherParam = Parameter( &amp;quot;MyOtherParam&amp;quot; );&lt;br /&gt;
   mCount = 0;&lt;br /&gt;
 } &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Process===&lt;br /&gt;
The &amp;lt;tt&amp;gt;Process&amp;lt;/tt&amp;gt; function is called once for each block of EEG data. It receives an input in its first argument, and sets the output signal to values resulting from the filter operation. In the current BCI2000 implementation, there is a single chain of filters; one filter&#039;s output signal is the next filter&#039;s input. A filter which does not perform any modification to the signal (e.g., a statistics filter) needs to copy its input signal into the output signal, as in the example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 void MyFilter::Process( const GenericSignal&amp;amp; Input,&lt;br /&gt;
                               GenericSignal&amp;amp; Output )&lt;br /&gt;
 {&lt;br /&gt;
   if( Input( 0, 0 ) &amp;gt; mMyOtherParam )&lt;br /&gt;
     ++mCount;&lt;br /&gt;
   Output = Input;&lt;br /&gt;
 } &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;tt&amp;gt;Process&amp;lt;/tt&amp;gt; function should not acquire or release resources (opening/closing files, allocating memory, etc). Natural places for such operations are the &amp;lt;tt&amp;gt;Initialize&amp;lt;/tt&amp;gt;, &amp;lt;tt&amp;gt;StartRun&amp;lt;/tt&amp;gt;, and &amp;lt;tt&amp;gt;StopRun&amp;lt;/tt&amp;gt; member functions.&lt;br /&gt;
&lt;br /&gt;
==Optional Member Functions==&lt;br /&gt;
Other member functions are &#039;&#039;optional;&#039;&#039;  you may decide whether&lt;br /&gt;
you&lt;br /&gt;
override their default implementation with your own version,&lt;br /&gt;
depending on what your filter needs to do:&lt;br /&gt;
  &lt;br /&gt;
===StartRun===&lt;br /&gt;
&amp;lt;tt&amp;gt;StartRun&amp;lt;/tt&amp;gt; is called when the system enters the running state. As opposed to &amp;lt;tt&amp;gt;Initialize&amp;lt;/tt&amp;gt;, which is the place for tasks that need to be performed on each parameter change, &amp;lt;tt&amp;gt;StartRun&amp;lt;/tt&amp;gt; is provided for tasks that need to be performed each time the user clicks &amp;quot;Run&amp;quot; or &amp;quot;Resume&amp;quot; in the operator window. As a canonical example, the &amp;lt;tt&amp;gt;DataIOFilter&amp;lt;/tt&amp;gt; opens a new &amp;lt;tt&amp;gt;.dat&amp;lt;/tt&amp;gt; file from its &amp;lt;tt&amp;gt;StartRun&amp;lt;/tt&amp;gt; member function.&lt;br /&gt;
&lt;br /&gt;
===StopRun===&lt;br /&gt;
&amp;lt;tt&amp;gt;StopRun&amp;lt;/tt&amp;gt; is called each time the system leaves the running state, entering the suspended state. Typically, this happens whenever the user clicks &amp;quot;Suspend&amp;quot; in the operator window. The &amp;lt;tt&amp;gt;DataIOFilter&amp;lt;/tt&amp;gt; provides an example for its usage: This filter closes the current &amp;lt;tt&amp;gt;.dat&amp;lt;/tt&amp;gt; file from its &amp;lt;tt&amp;gt;StopRun&amp;lt;/tt&amp;gt; member function.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;tt&amp;gt;StopRun&amp;lt;/tt&amp;gt; is also the only function from which a filter may change a parameter value. Any parameter changes inside &amp;lt;tt&amp;gt;StopRun&amp;lt;/tt&amp;gt; will propagate to the other modules without any explicit request from your side.&lt;br /&gt;
&lt;br /&gt;
===Resting===&lt;br /&gt;
&amp;lt;tt&amp;gt;Resting&amp;lt;/tt&amp;gt; is called instead of &amp;lt;tt&amp;gt;Process&amp;lt;/tt&amp;gt; while the system is in suspended state. &lt;br /&gt;
&lt;br /&gt;
===Halt===&lt;br /&gt;
The &amp;lt;tt&amp;gt;Halt&amp;lt;/tt&amp;gt; function is called before any reconfiguration of the system takes place. If your filter initiates asynchronous operations such as playing a sound file, acquiring EEG data, or executing threads, its &amp;lt;tt&amp;gt;Halt&amp;lt;/tt&amp;gt; member function should terminate all such operations immediately. Failure to do so might result in a non-responding module, or in other errors difficult to track down. For descendants of &amp;lt;tt&amp;gt;GenericADC&amp;lt;/tt&amp;gt;, implementing the &amp;lt;tt&amp;gt;Halt&amp;lt;/tt&amp;gt; function is mandatory. &lt;br /&gt;
&lt;br /&gt;
===Destructor===&lt;br /&gt;
Your filter&#039;s &#039;&#039;Destructor&#039;&#039;  should free all resources acquired in the Constructor or in &amp;lt;tt&amp;gt;Initialize&amp;lt;/tt&amp;gt;. In many cases, freeing of resources will be done automatically if you use direct members instead of pointers, removing the need for a destructor. However, if your filter has a non-empty &amp;lt;tt&amp;gt;Halt&amp;lt;/tt&amp;gt; function, it needs a destructor that calls &amp;lt;tt&amp;gt;Halt&amp;lt;/tt&amp;gt; - this can not be done from the base class destructor because overridden virtual functions cannot be called from base class constructors or destructors.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 MyFilter::~MyFilter()&lt;br /&gt;
 {&lt;br /&gt;
   Halt();&lt;br /&gt;
 } &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===AllowsVisualization===&lt;br /&gt;
Unlike the previous members of &amp;lt;code&amp;gt;GenericFilter&amp;lt;/code&amp;gt;, this function represents a property rather than an event.&lt;br /&gt;
Through its return value, a filter can decide whether its output may be offered for visualization by the framework, i.e. whether it appears in the Processing Stages section in the operator&#039;s Visualize tab.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;AllowsVisualization&amp;lt;/code&amp;gt; is called once at instantiation, and then after each Initialize() that occurs.&lt;br /&gt;
If it returns false at instantiation, no visualization parameter is created for the filter.&lt;br /&gt;
If it returns false at initialization time, no default visualization is displayed,&lt;br /&gt;
independently of the visualization parameter&#039;s value.&lt;br /&gt;
The filter itself may still maintain its own visualization parameters and [[Programming Reference:GenericVisualization Class|GenericVisualization]] object.&lt;br /&gt;
&lt;br /&gt;
==Rules for Parameter and State access==&lt;br /&gt;
For consistent and robust overall system operation, it is crucial that individual filters actually check [[BCI2000 Glossary#Parameter|parameter]] values and [[BCI2000 Glossary#States|state]] accessibility from their &amp;lt;code&amp;gt;Preflight()&amp;lt;/code&amp;gt; function. To support this idea, the BCI2000 framework imposes a set of rules to parameter and state access:&lt;br /&gt;
*Parameters and states accessed during initialization or processing &#039;&#039;must&#039;&#039; be accessed during the preflight phase. Otherwise, an error message will be displayed when the first access occurs.&lt;br /&gt;
*To avoid pointless cludging of the &amp;lt;code&amp;gt;Preflight()&amp;lt;/code&amp;gt; function, there are some exceptions to this rule:&lt;br /&gt;
**States defined in a filter&#039;s constructor are known to be present, and thus need not be accessed from that filter&#039;s &amp;lt;code&amp;gt;Preflight()&amp;lt;/code&amp;gt; function.&lt;br /&gt;
**Parameters defined in a filter&#039;s constructor will be range-checked automatically if any of their &amp;lt;code&amp;gt;LowRange&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;HighRange&amp;lt;/code&amp;gt; [[Technical Reference:Parameter Definition|properties]] is set, and need not be accessed explicitly from &amp;lt;code&amp;gt;Preflight()&amp;lt;/code&amp;gt;.&lt;br /&gt;
**To disable automatic range checking, set both these properties to the empty string in your [[Technical Reference:Parameter Definition|parameter definition line]]:&amp;lt;br /&amp;gt;&amp;lt;code&amp;gt;MySection int MyParam= 1 0 % % // a parameter that is not automatically range-checked&amp;lt;/code&amp;gt;&amp;lt;br /&amp;gt;This implies that you need to implement your own range-check in &amp;lt;code&amp;gt;Preflight()&amp;lt;/code&amp;gt;. If there are no actual constraints on the parameter&#039;s value, just add a line &amp;lt;code&amp;gt;Parameter( &amp;quot;MyParam&amp;quot; );&amp;lt;/code&amp;gt; to the filter&#039;s &amp;lt;code&amp;gt;Preflight()&amp;lt;/code&amp;gt; function.&lt;br /&gt;
*[[User Reference:Expression Syntax|Expressions]] may involve state names, and thus need to be evaluated in order to check whether referred-to states are present. Just add a line &amp;lt;br /&amp;gt;&amp;lt;code&amp;gt;Expression( Parameter( &amp;quot;MyExpressionParam&amp;quot; ) ).Evaluate();&amp;lt;/code&amp;gt;&amp;lt;br /&amp;gt; to the filter&#039;s &amp;lt;code&amp;gt;Preflight()&amp;lt;/code&amp;gt; function.&lt;br /&gt;
&lt;br /&gt;
[[Category:Framework API]][[Category:Development]]&lt;/div&gt;</summary>
		<author><name>Roostenveld</name></author>
	</entry>
	<entry>
		<id>https://www.bci2000.org/mediawiki/index.php?title=Programming_Reference:MatlabFilter&amp;diff=3985</id>
		<title>Programming Reference:MatlabFilter</title>
		<link rel="alternate" type="text/html" href="https://www.bci2000.org/mediawiki/index.php?title=Programming_Reference:MatlabFilter&amp;diff=3985"/>
		<updated>2008-07-14T13:11:54Z</updated>

		<summary type="html">&lt;p&gt;Roostenveld: /* Overview */ fixed typo in link&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Overview==&lt;br /&gt;
&lt;br /&gt;
The MatlabFilter implements a mechanism for using the Matlab engine within the BCI2000 pipeline. The &#039;&#039;MatlabFilter&#039;&#039; calls the Matlab engine to act upon signals, parameters, and states. It provides the full BCI2000 [[Programming Reference:GenericFilter Class|filter interface]] to a Matlab filter implementation.&lt;br /&gt;
&lt;br /&gt;
While BCI2000 is running, each block of data is pushed to the Matlab engine and a well-specified Matlab function (i.e. *.m file) is executed. As explained in more detail in the Programming reference for the  [[Programming_Reference:GenericFilter_Class|GenericFilter Class]], all BCI2000 filters consist of a set of functions. The MatlabFilter will, upon execution of each of these functions, contact the Matlab engine and execure the corresponding Matlab function equivalent. Furthermore, the MatlabFilter copies the input signal and the states to the Matlab engine, and copies the output signal and states from the engine after processing the signal.&lt;br /&gt;
&lt;br /&gt;
{| style=&amp;quot;color:black;background-color:#ffffcc;&amp;quot; cellpadding=&amp;quot;5&amp;quot; cellspacing=&amp;quot;0&amp;quot; border=&amp;quot;2&amp;quot; &lt;br /&gt;
!BCI2000 function&lt;br /&gt;
!Matlab equivalent&lt;br /&gt;
|-&lt;br /&gt;
|[[Programming_Reference:GenericFilter_Class#Constructor|Constructor()]]&lt;br /&gt;
|bci_Construct&lt;br /&gt;
|-&lt;br /&gt;
|[[Programming_Reference:GenericFilter_Class#Prefligh|Preflight()]]&lt;br /&gt;
|bci_Preflight&lt;br /&gt;
|-&lt;br /&gt;
|[[Programming_Reference:GenericFilter_Class#Initialize|Initialize()]]&lt;br /&gt;
|bci_Initialize&lt;br /&gt;
|-&lt;br /&gt;
|[[Programming_Reference:GenericFilter_Class#StartRun|StartRun()]]&lt;br /&gt;
|bci_StartRun&lt;br /&gt;
|-&lt;br /&gt;
|[[Programming_Reference:GenericFilter_Class#Process|Process()]]&lt;br /&gt;
|bci_Process&lt;br /&gt;
|-&lt;br /&gt;
|[[Programming_Reference:GenericFilter_Class#StopRun|StopRun()]]&lt;br /&gt;
|bci_StopRun&lt;br /&gt;
|-&lt;br /&gt;
|[[Programming_Reference:GenericFilter_Class#Resting|Resting()]]&lt;br /&gt;
|bci_Resting&lt;br /&gt;
|-&lt;br /&gt;
|[[Programming_Reference:GenericFilter_Class#Halt|Halt()]]&lt;br /&gt;
|bci_Halt&lt;br /&gt;
|-&lt;br /&gt;
|[[Programming_Reference:GenericFilter_Class#Destructor|Destructor()]]&lt;br /&gt;
|bci_Destruct&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Existence of the above-listed Matlab functions is not mandatory, the Matlab &#039;exist&#039; command will be used to determine whether a given function is available, and will not call the Matlab engine when this is not the case. If either of the bci_Preflight, bci_Initialize, or bci_Process functions is not available, a warning will be displayed to the user.&lt;br /&gt;
&lt;br /&gt;
Most of these Matlab functions do not take input arguments, except for&lt;br /&gt;
  function [parameters, states] = bci_Construct;&lt;br /&gt;
  &lt;br /&gt;
  function [out_signal_dim] = bci_Preflight( in_signal_dim );&lt;br /&gt;
  &lt;br /&gt;
  function bci_Initialize( in_signal_dim, out_signal_dim );&lt;br /&gt;
  &lt;br /&gt;
  function [out_signal] = bci_Process( in_signal );&lt;br /&gt;
&lt;br /&gt;
The most important functions is &amp;lt;tt&amp;gt;bci_Process&amp;lt;/tt&amp;gt;, which is executed every time a new block of data is passed through the pipeline. Furthermore &amp;lt;tt&amp;gt;bci_Construct&amp;lt;/tt&amp;gt; is executed at the beginning and determines the parameters and states, &amp;lt;tt&amp;gt;bci_Initialize&amp;lt;/tt&amp;gt; determines whether all requirements are met, and &amp;lt;tt&amp;gt;bci_StartRun&amp;lt;/tt&amp;gt; prepares the computation that is done during processing (e.g. computing filter parameters). Communication between these Matlab functions is done using global variables.&lt;br /&gt;
&lt;br /&gt;
==Parameters and States==&lt;br /&gt;
&lt;br /&gt;
Parameters and states are accessible via global Matlab structs called  &#039;bci_Parameters&#039; and &#039;bci_States&#039;. Parameters may be changed from &amp;lt;tt&amp;gt;bci_StopRun&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;bci_Resting&amp;lt;/tt&amp;gt;, and will automatically be propagated to the other modules. State values may be modified from the &amp;lt;tt&amp;gt;bci_Process&amp;lt;/tt&amp;gt; function. &lt;br /&gt;
&lt;br /&gt;
To add parameters and states to the BCI2000 list of states, the &amp;lt;tt&amp;gt;bci_Construct&amp;lt;/tt&amp;gt; function may return non-empty cell arrays of strings in its parameters  and states return values. The strings constituting these cell arrays are [[Technical Reference:Parameter Definition|parameter]] and [[Technical Reference:State Definition|state definitions]].&lt;br /&gt;
&lt;br /&gt;
Your added parameters will appear in the [[User Reference:Operator Module|operator module&#039;s]] parameter configuration dialog. Your parameter definition&#039;s section name will be used to direct its position in the configuration dialog&#039;s register cards:&lt;br /&gt;
 MyFilter int MyParam= 2 0 0 2 // ...&lt;br /&gt;
will display the &#039;&#039;MyParam&#039;&#039; parameter on separate register card named &#039;&#039;MyFilter&#039;&#039;.&lt;br /&gt;
 Filtering:MyFilter int MyParam= 2 0 0 2 // ...&lt;br /&gt;
will display the parameter on the &#039;&#039;Filtering&#039;&#039; register card, inside a group called &#039;&#039;MyFilter&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
==Input and Output Signal Format==&lt;br /&gt;
&lt;br /&gt;
BCI2000 signals are mapped to Matlab matrices with channel index first, and sample (element) index second. The signal dimension arguments of &amp;lt;tt&amp;gt;bci_Preflight&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;bci_Initialize&amp;lt;/tt&amp;gt; are integer vectors  of size 1x2 with &amp;lt;tt&amp;gt;[n_channels n_elements]&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
==Error Reporting==&lt;br /&gt;
&lt;br /&gt;
To report errors from Matlab functions, use Matlab&#039;s &amp;lt;tt&amp;gt;error&amp;lt;/tt&amp;gt; command.&lt;br /&gt;
Your error messages will be displayed to the user from the [[User Reference:Operator Module|operator module]].&lt;br /&gt;
&lt;br /&gt;
==Combining the MatlabFilter with other Signal Processing Filters==&lt;br /&gt;
&lt;br /&gt;
In the BCI2000 binary distribution, the &#039;&#039;MatlabFilter&#039;&#039; is shipped inside the &#039;&#039;MatlabSignalProcessing&#039;&#039; executable. There, the signal processing chain consists of the [[User Reference:SpatialFilter|SpatialFilter]] and the &#039;&#039;MatlabFilter&#039;&#039;. However, by editing &amp;lt;tt&amp;gt;src/core/SignalProcessing/Matlab/PipeDefinition.cpp&amp;lt;/tt&amp;gt;, you may add as many [[User Reference:Filters|signal processing filters]] as you wish.&lt;br /&gt;
&lt;br /&gt;
See [[Programming Reference:Filter Chain]] for information about defining a filter chain. Modification of the filter chain requires access to the [[Programming Reference:BCI2000 Source Code|BCI2000 source code]]. You will need to rebuild the &#039;&#039;MatlabSignalProcessing&#039;&#039; executable.&lt;br /&gt;
&lt;br /&gt;
==Command Line Options==&lt;br /&gt;
===MatlabWD===&lt;br /&gt;
By default, the Matlab engine&#039;s working directory is set to the signal processing module&#039;s working directory at startup. When using the Matlab filter for signal processing, your filter&#039;s code will be contained in the &amp;lt;tt&amp;gt;bci_Process&amp;lt;/tt&amp;gt; and associated Matlab functions, which will typically be located inside a dedicated directory. Thus, you may switch between Matlab-based filter implementations by selecting a Matlab working directory at startup.&lt;br /&gt;
This may be done by specifying the &amp;lt;tt&amp;gt;MatlabWD&amp;lt;/tt&amp;gt; parameter from the command line when starting the MatlabSignalProcessing module:&lt;br /&gt;
&amp;lt;pre&amp;gt;start MatlabSignalProcessing.exe --MatlabWD=&amp;quot;./matlab&amp;quot; 127.0.0.1&amp;lt;/pre&amp;gt;&lt;br /&gt;
will change Matlab&#039;s working directory to a directory called &amp;quot;matlab&amp;quot; inside the BCI2000 &amp;quot;prog&amp;quot; directory at startup, and execute &amp;lt;tt&amp;gt;bci_Process&amp;lt;/tt&amp;gt; and associated functions from there.&lt;br /&gt;
&lt;br /&gt;
This parameter must be set from the command line; later changes will have no effect.&lt;br /&gt;
&lt;br /&gt;
Using the &amp;lt;tt&amp;gt;--MatlabWD&amp;lt;/tt&amp;gt; command line option, you may easily switch between multiple Matlab-based BCI2000 configurations.&lt;br /&gt;
Simply create a separate startup batch file for each configuration, and specify the respective configuration&#039;s path on the command line that starts up the MatlabFilter.&lt;br /&gt;
&lt;br /&gt;
===MatlabStayOpen===&lt;br /&gt;
By default, the Matlab instance associated with the Matlab engine will be closed when BCI2000 quits.&lt;br /&gt;
Generally, this behavior is desired to ensure a well-defined BCI2000 system state after each startup.&lt;br /&gt;
For debugging purposes, though, it is sometimes useful to have the engine&#039;s command window available even after BCI2000 has been quit.&lt;br /&gt;
This behavior is controlled by another command-line parameter, &amp;lt;tt&amp;gt;MatlabStayOpen&amp;lt;/tt&amp;gt;, which may take the following values:&lt;br /&gt;
*0 is the default (no change in behavior),&lt;br /&gt;
*1 keeps the Matlab engine&#039;s command window open but clears variables used to transmit information forth and back between BCI2000 and Matlab,&lt;br /&gt;
*2 also preserves intermediate variables (such as bci_Parameters and bci_States) in the Matlab engine&#039;s workspace.&lt;br /&gt;
&lt;br /&gt;
To set this parameter to 2, use this command line to start up the signal processing module:&lt;br /&gt;
&amp;lt;pre&amp;gt;start MatlabSignalProcessing.exe --MatlabStayOpen=2 127.0.0.1&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Once the &amp;lt;tt&amp;gt;MatlabStayOpen&amp;lt;/tt&amp;gt; parameter has been set from the command line, it will be listed on the Operator Module&#039;s parameter configuration dialog&#039;s &amp;quot;system&amp;quot; tab, and may be modified from there. The change will be applied when clicking &#039;&#039;Set Config&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
==Troubleshooting==&lt;br /&gt;
===Debugging Matlab functions===&lt;br /&gt;
#Start BCI2000. The &#039;&#039;MatlabFilter&#039;&#039; will open a Matlab instance in its own window in minimized state.&lt;br /&gt;
#Switch to the minimized Matlab instance.&lt;br /&gt;
#Type, e.g.,&amp;lt;br /&amp;gt;&amp;lt;code&amp;gt;edit bci_Process.m&amp;lt;/code&amp;gt;&amp;lt;br /&amp;gt;to open a Matlab editor window connected to the Matlab interactive window.&lt;br /&gt;
#In the editor window, set breakpoints as you would normally.&lt;br /&gt;
#Execution will be paused, and you may examine variables moving the mouse over them.&lt;br /&gt;
&lt;br /&gt;
===Matlab doesn&#039;t find your functions===&lt;br /&gt;
Make sure to&lt;br /&gt;
*either set Matlab&#039;s working directory to the directory containing your functions, using the &amp;lt;tt&amp;gt;--MatlabWD&amp;lt;/tt&amp;gt; command line option described above,&lt;br /&gt;
*or add the respective directory to your Matlab path.&lt;br /&gt;
Generally, the first option is recommended over the second one.&lt;br /&gt;
&lt;br /&gt;
===There is no Matlab engine started up===&lt;br /&gt;
Execute&amp;lt;br /&amp;gt;&amp;lt;code&amp;gt;matlab /regserver&amp;lt;/code&amp;gt;&amp;lt;br /&amp;gt;from the command line when logged in with administrative privileges.&lt;br /&gt;
===Changes to M-files appear to have no effect===&lt;br /&gt;
Switch to the Matlab engine&#039;s command window, and enter&amp;lt;br /&amp;gt;&amp;lt;code&amp;gt;clear;&amp;lt;/code&amp;gt;&amp;lt;br /&amp;gt; or &amp;lt;br /&amp;gt;&amp;lt;code&amp;gt;clear scriptname;&amp;lt;/code&amp;gt;&amp;lt;br /&amp;gt;each time you change a Matlab function.&lt;br /&gt;
&lt;br /&gt;
===You get linker errors when rebuilding &#039;&#039;MatlabSignalProcessing&#039;&#039;===&lt;br /&gt;
If you get linker errors after editing &amp;lt;tt&amp;gt;PipeDefinition.cpp&amp;lt;/tt&amp;gt;, make sure that all filters&#039; cpp files are part of the &#039;&#039;MatlabSignalProcessing&#039;&#039; project.&lt;br /&gt;
===You get a message window with an &amp;quot;Entry Point Not Found&amp;quot; message===&lt;br /&gt;
This error is likely to indicate confusion of multiple installations of Matlab on your machine.&lt;br /&gt;
Check your Windows &#039;&#039;%PATH%&#039;&#039; environment variable, e.g. by opening a command console window and typing&lt;br /&gt;
 echo %PATH%&lt;br /&gt;
When it contains references to more than a single Matlab installation, remove all apart from the reference to the installation you are going to use with BCI2000. Also, make sure to execute&lt;br /&gt;
 matlab /regserver&lt;br /&gt;
for the Matlab installation you intend to use.&lt;br /&gt;
&lt;br /&gt;
==See also==&lt;br /&gt;
[[Technical Reference:Core Modules#Signal Processing Module]], &lt;br /&gt;
[[Technical Reference:Parameter Definition]], &lt;br /&gt;
[[Technical Reference:State Definition]], &lt;br /&gt;
[[Programming Reference:GenericFilter Class]], &lt;br /&gt;
[[Programming Reference:Filter Chain]], &lt;br /&gt;
[[Contributions:FieldTripBuffer]]&lt;br /&gt;
&lt;br /&gt;
Tutorials:&lt;br /&gt;
* [[Programming_Tutorial:Implementing_a_Matlab-based_Filter|IIR bandpass filtering, followed with RMS envelope computation, and linear classification]]&lt;br /&gt;
* [[Programming_Tutorial:Implementing_another_Matlab-based_Filter|adaptive spatial filtering using a beamformer, power computation and normalization]]&lt;br /&gt;
&lt;br /&gt;
[[Category:Filters]]&lt;br /&gt;
[[Category:Signal Processing]]&lt;br /&gt;
[[Category:Matlab]]&lt;br /&gt;
[[Category:Development]]&lt;br /&gt;
[[Category:External Interfaces]]&lt;/div&gt;</summary>
		<author><name>Roostenveld</name></author>
	</entry>
	<entry>
		<id>https://www.bci2000.org/mediawiki/index.php?title=Programming_Reference:MatlabFilter&amp;diff=3984</id>
		<title>Programming Reference:MatlabFilter</title>
		<link rel="alternate" type="text/html" href="https://www.bci2000.org/mediawiki/index.php?title=Programming_Reference:MatlabFilter&amp;diff=3984"/>
		<updated>2008-07-14T13:11:30Z</updated>

		<summary type="html">&lt;p&gt;Roostenveld: /* Overview */ added lins in table&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Overview==&lt;br /&gt;
&lt;br /&gt;
The MatlabFilter implements a mechanism for using the Matlab engine within the BCI2000 pipeline. The &#039;&#039;MatlabFilter&#039;&#039; calls the Matlab engine to act upon signals, parameters, and states. It provides the full BCI2000 [[Programming Reference:GenericFilter Class|filter interface]] to a Matlab filter implementation.&lt;br /&gt;
&lt;br /&gt;
While BCI2000 is running, each block of data is pushed to the Matlab engine and a well-specified Matlab function (i.e. *.m file) is executed. As explained in more detail in the Programming reference for the  [[Programming_Reference:GenericFilter_Class|GenericFilter Class]], all BCI2000 filters consist of a set of functions. The MatlabFilter will, upon execution of each of these functions, contact the Matlab engine and execure the corresponding Matlab function equivalent. Furthermore, the MatlabFilter copies the input signal and the states to the Matlab engine, and copies the output signal and states from the engine after processing the signal.&lt;br /&gt;
&lt;br /&gt;
{| style=&amp;quot;color:black;background-color:#ffffcc;&amp;quot; cellpadding=&amp;quot;5&amp;quot; cellspacing=&amp;quot;0&amp;quot; border=&amp;quot;2&amp;quot; &lt;br /&gt;
!BCI2000 function&lt;br /&gt;
!Matlab equivalent&lt;br /&gt;
|-&lt;br /&gt;
|[[Programming_Reference:GenericFilter_Class#Constructor|Constructor()]]&lt;br /&gt;
|bci_Construct&lt;br /&gt;
|-&lt;br /&gt;
|[[Programming_Reference:GenericFilter_Class#Prefligh|Preflight()]]&lt;br /&gt;
|bci_Preflight&lt;br /&gt;
|-&lt;br /&gt;
|[[Programming_Reference:GenericFilter_Class#Initialize|Initialize()]]&lt;br /&gt;
|bci_Initialize&lt;br /&gt;
|-&lt;br /&gt;
|[[Programming_Reference:GenericFilter_Class#StartRun|StartRun()]]&lt;br /&gt;
|bci_StartRun&lt;br /&gt;
|-&lt;br /&gt;
|[[Programming_Reference:GenericFilter_Class#Process|Process()]]&lt;br /&gt;
|bci_Process&lt;br /&gt;
|-&lt;br /&gt;
|[[Programming_Reference:GenericFilter_Class#StopRun|StopRun()]]&lt;br /&gt;
|bci_StopRun&lt;br /&gt;
|-&lt;br /&gt;
|[[Programming_Reference:GenericFilter_ClassResting|#Resting()]]&lt;br /&gt;
|bci_Resting&lt;br /&gt;
|-&lt;br /&gt;
|[[Programming_Reference:GenericFilter_Class#Halt|Halt()]]&lt;br /&gt;
|bci_Halt&lt;br /&gt;
|-&lt;br /&gt;
|[[Programming_Reference:GenericFilter_Class#Destructor|Destructor()]]&lt;br /&gt;
|bci_Destruct&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Existence of the above-listed Matlab functions is not mandatory, the Matlab &#039;exist&#039; command will be used to determine whether a given function is available, and will not call the Matlab engine when this is not the case. If either of the bci_Preflight, bci_Initialize, or bci_Process functions is not available, a warning will be displayed to the user.&lt;br /&gt;
&lt;br /&gt;
Most of these Matlab functions do not take input arguments, except for&lt;br /&gt;
  function [parameters, states] = bci_Construct;&lt;br /&gt;
  &lt;br /&gt;
  function [out_signal_dim] = bci_Preflight( in_signal_dim );&lt;br /&gt;
  &lt;br /&gt;
  function bci_Initialize( in_signal_dim, out_signal_dim );&lt;br /&gt;
  &lt;br /&gt;
  function [out_signal] = bci_Process( in_signal );&lt;br /&gt;
&lt;br /&gt;
The most important functions is &amp;lt;tt&amp;gt;bci_Process&amp;lt;/tt&amp;gt;, which is executed every time a new block of data is passed through the pipeline. Furthermore &amp;lt;tt&amp;gt;bci_Construct&amp;lt;/tt&amp;gt; is executed at the beginning and determines the parameters and states, &amp;lt;tt&amp;gt;bci_Initialize&amp;lt;/tt&amp;gt; determines whether all requirements are met, and &amp;lt;tt&amp;gt;bci_StartRun&amp;lt;/tt&amp;gt; prepares the computation that is done during processing (e.g. computing filter parameters). Communication between these Matlab functions is done using global variables.&lt;br /&gt;
&lt;br /&gt;
==Parameters and States==&lt;br /&gt;
&lt;br /&gt;
Parameters and states are accessible via global Matlab structs called  &#039;bci_Parameters&#039; and &#039;bci_States&#039;. Parameters may be changed from &amp;lt;tt&amp;gt;bci_StopRun&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;bci_Resting&amp;lt;/tt&amp;gt;, and will automatically be propagated to the other modules. State values may be modified from the &amp;lt;tt&amp;gt;bci_Process&amp;lt;/tt&amp;gt; function. &lt;br /&gt;
&lt;br /&gt;
To add parameters and states to the BCI2000 list of states, the &amp;lt;tt&amp;gt;bci_Construct&amp;lt;/tt&amp;gt; function may return non-empty cell arrays of strings in its parameters  and states return values. The strings constituting these cell arrays are [[Technical Reference:Parameter Definition|parameter]] and [[Technical Reference:State Definition|state definitions]].&lt;br /&gt;
&lt;br /&gt;
Your added parameters will appear in the [[User Reference:Operator Module|operator module&#039;s]] parameter configuration dialog. Your parameter definition&#039;s section name will be used to direct its position in the configuration dialog&#039;s register cards:&lt;br /&gt;
 MyFilter int MyParam= 2 0 0 2 // ...&lt;br /&gt;
will display the &#039;&#039;MyParam&#039;&#039; parameter on separate register card named &#039;&#039;MyFilter&#039;&#039;.&lt;br /&gt;
 Filtering:MyFilter int MyParam= 2 0 0 2 // ...&lt;br /&gt;
will display the parameter on the &#039;&#039;Filtering&#039;&#039; register card, inside a group called &#039;&#039;MyFilter&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
==Input and Output Signal Format==&lt;br /&gt;
&lt;br /&gt;
BCI2000 signals are mapped to Matlab matrices with channel index first, and sample (element) index second. The signal dimension arguments of &amp;lt;tt&amp;gt;bci_Preflight&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;bci_Initialize&amp;lt;/tt&amp;gt; are integer vectors  of size 1x2 with &amp;lt;tt&amp;gt;[n_channels n_elements]&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
==Error Reporting==&lt;br /&gt;
&lt;br /&gt;
To report errors from Matlab functions, use Matlab&#039;s &amp;lt;tt&amp;gt;error&amp;lt;/tt&amp;gt; command.&lt;br /&gt;
Your error messages will be displayed to the user from the [[User Reference:Operator Module|operator module]].&lt;br /&gt;
&lt;br /&gt;
==Combining the MatlabFilter with other Signal Processing Filters==&lt;br /&gt;
&lt;br /&gt;
In the BCI2000 binary distribution, the &#039;&#039;MatlabFilter&#039;&#039; is shipped inside the &#039;&#039;MatlabSignalProcessing&#039;&#039; executable. There, the signal processing chain consists of the [[User Reference:SpatialFilter|SpatialFilter]] and the &#039;&#039;MatlabFilter&#039;&#039;. However, by editing &amp;lt;tt&amp;gt;src/core/SignalProcessing/Matlab/PipeDefinition.cpp&amp;lt;/tt&amp;gt;, you may add as many [[User Reference:Filters|signal processing filters]] as you wish.&lt;br /&gt;
&lt;br /&gt;
See [[Programming Reference:Filter Chain]] for information about defining a filter chain. Modification of the filter chain requires access to the [[Programming Reference:BCI2000 Source Code|BCI2000 source code]]. You will need to rebuild the &#039;&#039;MatlabSignalProcessing&#039;&#039; executable.&lt;br /&gt;
&lt;br /&gt;
==Command Line Options==&lt;br /&gt;
===MatlabWD===&lt;br /&gt;
By default, the Matlab engine&#039;s working directory is set to the signal processing module&#039;s working directory at startup. When using the Matlab filter for signal processing, your filter&#039;s code will be contained in the &amp;lt;tt&amp;gt;bci_Process&amp;lt;/tt&amp;gt; and associated Matlab functions, which will typically be located inside a dedicated directory. Thus, you may switch between Matlab-based filter implementations by selecting a Matlab working directory at startup.&lt;br /&gt;
This may be done by specifying the &amp;lt;tt&amp;gt;MatlabWD&amp;lt;/tt&amp;gt; parameter from the command line when starting the MatlabSignalProcessing module:&lt;br /&gt;
&amp;lt;pre&amp;gt;start MatlabSignalProcessing.exe --MatlabWD=&amp;quot;./matlab&amp;quot; 127.0.0.1&amp;lt;/pre&amp;gt;&lt;br /&gt;
will change Matlab&#039;s working directory to a directory called &amp;quot;matlab&amp;quot; inside the BCI2000 &amp;quot;prog&amp;quot; directory at startup, and execute &amp;lt;tt&amp;gt;bci_Process&amp;lt;/tt&amp;gt; and associated functions from there.&lt;br /&gt;
&lt;br /&gt;
This parameter must be set from the command line; later changes will have no effect.&lt;br /&gt;
&lt;br /&gt;
Using the &amp;lt;tt&amp;gt;--MatlabWD&amp;lt;/tt&amp;gt; command line option, you may easily switch between multiple Matlab-based BCI2000 configurations.&lt;br /&gt;
Simply create a separate startup batch file for each configuration, and specify the respective configuration&#039;s path on the command line that starts up the MatlabFilter.&lt;br /&gt;
&lt;br /&gt;
===MatlabStayOpen===&lt;br /&gt;
By default, the Matlab instance associated with the Matlab engine will be closed when BCI2000 quits.&lt;br /&gt;
Generally, this behavior is desired to ensure a well-defined BCI2000 system state after each startup.&lt;br /&gt;
For debugging purposes, though, it is sometimes useful to have the engine&#039;s command window available even after BCI2000 has been quit.&lt;br /&gt;
This behavior is controlled by another command-line parameter, &amp;lt;tt&amp;gt;MatlabStayOpen&amp;lt;/tt&amp;gt;, which may take the following values:&lt;br /&gt;
*0 is the default (no change in behavior),&lt;br /&gt;
*1 keeps the Matlab engine&#039;s command window open but clears variables used to transmit information forth and back between BCI2000 and Matlab,&lt;br /&gt;
*2 also preserves intermediate variables (such as bci_Parameters and bci_States) in the Matlab engine&#039;s workspace.&lt;br /&gt;
&lt;br /&gt;
To set this parameter to 2, use this command line to start up the signal processing module:&lt;br /&gt;
&amp;lt;pre&amp;gt;start MatlabSignalProcessing.exe --MatlabStayOpen=2 127.0.0.1&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Once the &amp;lt;tt&amp;gt;MatlabStayOpen&amp;lt;/tt&amp;gt; parameter has been set from the command line, it will be listed on the Operator Module&#039;s parameter configuration dialog&#039;s &amp;quot;system&amp;quot; tab, and may be modified from there. The change will be applied when clicking &#039;&#039;Set Config&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
==Troubleshooting==&lt;br /&gt;
===Debugging Matlab functions===&lt;br /&gt;
#Start BCI2000. The &#039;&#039;MatlabFilter&#039;&#039; will open a Matlab instance in its own window in minimized state.&lt;br /&gt;
#Switch to the minimized Matlab instance.&lt;br /&gt;
#Type, e.g.,&amp;lt;br /&amp;gt;&amp;lt;code&amp;gt;edit bci_Process.m&amp;lt;/code&amp;gt;&amp;lt;br /&amp;gt;to open a Matlab editor window connected to the Matlab interactive window.&lt;br /&gt;
#In the editor window, set breakpoints as you would normally.&lt;br /&gt;
#Execution will be paused, and you may examine variables moving the mouse over them.&lt;br /&gt;
&lt;br /&gt;
===Matlab doesn&#039;t find your functions===&lt;br /&gt;
Make sure to&lt;br /&gt;
*either set Matlab&#039;s working directory to the directory containing your functions, using the &amp;lt;tt&amp;gt;--MatlabWD&amp;lt;/tt&amp;gt; command line option described above,&lt;br /&gt;
*or add the respective directory to your Matlab path.&lt;br /&gt;
Generally, the first option is recommended over the second one.&lt;br /&gt;
&lt;br /&gt;
===There is no Matlab engine started up===&lt;br /&gt;
Execute&amp;lt;br /&amp;gt;&amp;lt;code&amp;gt;matlab /regserver&amp;lt;/code&amp;gt;&amp;lt;br /&amp;gt;from the command line when logged in with administrative privileges.&lt;br /&gt;
===Changes to M-files appear to have no effect===&lt;br /&gt;
Switch to the Matlab engine&#039;s command window, and enter&amp;lt;br /&amp;gt;&amp;lt;code&amp;gt;clear;&amp;lt;/code&amp;gt;&amp;lt;br /&amp;gt; or &amp;lt;br /&amp;gt;&amp;lt;code&amp;gt;clear scriptname;&amp;lt;/code&amp;gt;&amp;lt;br /&amp;gt;each time you change a Matlab function.&lt;br /&gt;
&lt;br /&gt;
===You get linker errors when rebuilding &#039;&#039;MatlabSignalProcessing&#039;&#039;===&lt;br /&gt;
If you get linker errors after editing &amp;lt;tt&amp;gt;PipeDefinition.cpp&amp;lt;/tt&amp;gt;, make sure that all filters&#039; cpp files are part of the &#039;&#039;MatlabSignalProcessing&#039;&#039; project.&lt;br /&gt;
===You get a message window with an &amp;quot;Entry Point Not Found&amp;quot; message===&lt;br /&gt;
This error is likely to indicate confusion of multiple installations of Matlab on your machine.&lt;br /&gt;
Check your Windows &#039;&#039;%PATH%&#039;&#039; environment variable, e.g. by opening a command console window and typing&lt;br /&gt;
 echo %PATH%&lt;br /&gt;
When it contains references to more than a single Matlab installation, remove all apart from the reference to the installation you are going to use with BCI2000. Also, make sure to execute&lt;br /&gt;
 matlab /regserver&lt;br /&gt;
for the Matlab installation you intend to use.&lt;br /&gt;
&lt;br /&gt;
==See also==&lt;br /&gt;
[[Technical Reference:Core Modules#Signal Processing Module]], &lt;br /&gt;
[[Technical Reference:Parameter Definition]], &lt;br /&gt;
[[Technical Reference:State Definition]], &lt;br /&gt;
[[Programming Reference:GenericFilter Class]], &lt;br /&gt;
[[Programming Reference:Filter Chain]], &lt;br /&gt;
[[Contributions:FieldTripBuffer]]&lt;br /&gt;
&lt;br /&gt;
Tutorials:&lt;br /&gt;
* [[Programming_Tutorial:Implementing_a_Matlab-based_Filter|IIR bandpass filtering, followed with RMS envelope computation, and linear classification]]&lt;br /&gt;
* [[Programming_Tutorial:Implementing_another_Matlab-based_Filter|adaptive spatial filtering using a beamformer, power computation and normalization]]&lt;br /&gt;
&lt;br /&gt;
[[Category:Filters]]&lt;br /&gt;
[[Category:Signal Processing]]&lt;br /&gt;
[[Category:Matlab]]&lt;br /&gt;
[[Category:Development]]&lt;br /&gt;
[[Category:External Interfaces]]&lt;/div&gt;</summary>
		<author><name>Roostenveld</name></author>
	</entry>
	<entry>
		<id>https://www.bci2000.org/mediawiki/index.php?title=Programming_Reference:MatlabFilter&amp;diff=3983</id>
		<title>Programming Reference:MatlabFilter</title>
		<link rel="alternate" type="text/html" href="https://www.bci2000.org/mediawiki/index.php?title=Programming_Reference:MatlabFilter&amp;diff=3983"/>
		<updated>2008-07-14T13:08:55Z</updated>

		<summary type="html">&lt;p&gt;Roostenveld: general cleanup&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Overview==&lt;br /&gt;
&lt;br /&gt;
The MatlabFilter implements a mechanism for using the Matlab engine within the BCI2000 pipeline. The &#039;&#039;MatlabFilter&#039;&#039; calls the Matlab engine to act upon signals, parameters, and states. It provides the full BCI2000 [[Programming Reference:GenericFilter Class|filter interface]] to a Matlab filter implementation.&lt;br /&gt;
&lt;br /&gt;
While BCI2000 is running, each block of data is pushed to the Matlab engine and a well-specified Matlab function (i.e. *.m file) is executed. As explained in more detail in the Programming reference for the  [[Programming_Reference:GenericFilter_Class|GenericFilter Class]], all BCI2000 filters consist of a set of functions. The MatlabFilter will, upon execution of each of these functions, contact the Matlab engine and execure the corresponding Matlab function equivalent. Furthermore, the MatlabFilter copies the input signal and the states to the Matlab engine, and copies the output signal and states from the engine after processing the signal.&lt;br /&gt;
&lt;br /&gt;
{| style=&amp;quot;color:black;background-color:#ffffcc;&amp;quot; cellpadding=&amp;quot;5&amp;quot; cellspacing=&amp;quot;0&amp;quot; border=&amp;quot;2&amp;quot; &lt;br /&gt;
!BCI2000 function&lt;br /&gt;
!Matlab equivalent&lt;br /&gt;
|-&lt;br /&gt;
|[[Programming_Reference:GenericFilter_Class#Constructor|Constructor()]]&lt;br /&gt;
|bci_Construct&lt;br /&gt;
|-&lt;br /&gt;
|Preflight() &lt;br /&gt;
|bci_Preflight&lt;br /&gt;
|-&lt;br /&gt;
|Initialize() &lt;br /&gt;
|bci_Initialize&lt;br /&gt;
|-&lt;br /&gt;
|StartRun() &lt;br /&gt;
|bci_StartRun&lt;br /&gt;
|-&lt;br /&gt;
|Process() &lt;br /&gt;
|bci_Process&lt;br /&gt;
|-&lt;br /&gt;
|StopRun() &lt;br /&gt;
|bci_StopRun&lt;br /&gt;
|-&lt;br /&gt;
|Resting()&lt;br /&gt;
|bci_Resting&lt;br /&gt;
|-&lt;br /&gt;
|Halt()&lt;br /&gt;
|bci_Halt&lt;br /&gt;
|-&lt;br /&gt;
|Destructor()&lt;br /&gt;
|bci_Destruct&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Existence of the above-listed Matlab functions is not mandatory, the Matlab &#039;exist&#039; command will be used to determine whether a given function is available, and will not call the Matlab engine when this is not the case. If either of the bci_Preflight, bci_Initialize, or bci_Process functions is not available, a warning will be displayed to the user.&lt;br /&gt;
&lt;br /&gt;
Most of these Matlab functions do not take input arguments, except for&lt;br /&gt;
  function [parameters, states] = bci_Construct;&lt;br /&gt;
  &lt;br /&gt;
  function [out_signal_dim] = bci_Preflight( in_signal_dim );&lt;br /&gt;
  &lt;br /&gt;
  function bci_Initialize( in_signal_dim, out_signal_dim );&lt;br /&gt;
  &lt;br /&gt;
  function [out_signal] = bci_Process( in_signal );&lt;br /&gt;
&lt;br /&gt;
The most important functions is &amp;lt;tt&amp;gt;bci_Process&amp;lt;/tt&amp;gt;, which is executed every time a new block of data is passed through the pipeline. Furthermore &amp;lt;tt&amp;gt;bci_Construct&amp;lt;/tt&amp;gt; is executed at the beginning and determines the parameters and states, &amp;lt;tt&amp;gt;bci_Initialize&amp;lt;/tt&amp;gt; determines whether all requirements are met, and &amp;lt;tt&amp;gt;bci_StartRun&amp;lt;/tt&amp;gt; prepares the computation that is done during processing (e.g. computing filter parameters). Communication between these Matlab functions is done using global variables. &lt;br /&gt;
&lt;br /&gt;
==Parameters and States==&lt;br /&gt;
&lt;br /&gt;
Parameters and states are accessible via global Matlab structs called  &#039;bci_Parameters&#039; and &#039;bci_States&#039;. Parameters may be changed from &amp;lt;tt&amp;gt;bci_StopRun&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;bci_Resting&amp;lt;/tt&amp;gt;, and will automatically be propagated to the other modules. State values may be modified from the &amp;lt;tt&amp;gt;bci_Process&amp;lt;/tt&amp;gt; function. &lt;br /&gt;
&lt;br /&gt;
To add parameters and states to the BCI2000 list of states, the &amp;lt;tt&amp;gt;bci_Construct&amp;lt;/tt&amp;gt; function may return non-empty cell arrays of strings in its parameters  and states return values. The strings constituting these cell arrays are [[Technical Reference:Parameter Definition|parameter]] and [[Technical Reference:State Definition|state definitions]].&lt;br /&gt;
&lt;br /&gt;
Your added parameters will appear in the [[User Reference:Operator Module|operator module&#039;s]] parameter configuration dialog. Your parameter definition&#039;s section name will be used to direct its position in the configuration dialog&#039;s register cards:&lt;br /&gt;
 MyFilter int MyParam= 2 0 0 2 // ...&lt;br /&gt;
will display the &#039;&#039;MyParam&#039;&#039; parameter on separate register card named &#039;&#039;MyFilter&#039;&#039;.&lt;br /&gt;
 Filtering:MyFilter int MyParam= 2 0 0 2 // ...&lt;br /&gt;
will display the parameter on the &#039;&#039;Filtering&#039;&#039; register card, inside a group called &#039;&#039;MyFilter&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
==Input and Output Signal Format==&lt;br /&gt;
&lt;br /&gt;
BCI2000 signals are mapped to Matlab matrices with channel index first, and sample (element) index second. The signal dimension arguments of &amp;lt;tt&amp;gt;bci_Preflight&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;bci_Initialize&amp;lt;/tt&amp;gt; are integer vectors  of size 1x2 with &amp;lt;tt&amp;gt;[n_channels n_elements]&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
==Error Reporting==&lt;br /&gt;
&lt;br /&gt;
To report errors from Matlab functions, use Matlab&#039;s &amp;lt;tt&amp;gt;error&amp;lt;/tt&amp;gt; command.&lt;br /&gt;
Your error messages will be displayed to the user from the [[User Reference:Operator Module|operator module]].&lt;br /&gt;
&lt;br /&gt;
==Combining the MatlabFilter with other Signal Processing Filters==&lt;br /&gt;
&lt;br /&gt;
In the BCI2000 binary distribution, the &#039;&#039;MatlabFilter&#039;&#039; is shipped inside the &#039;&#039;MatlabSignalProcessing&#039;&#039; executable. There, the signal processing chain consists of the [[User Reference:SpatialFilter|SpatialFilter]] and the &#039;&#039;MatlabFilter&#039;&#039;. However, by editing &amp;lt;tt&amp;gt;src/core/SignalProcessing/Matlab/PipeDefinition.cpp&amp;lt;/tt&amp;gt;, you may add as many [[User Reference:Filters|signal processing filters]] as you wish.&lt;br /&gt;
&lt;br /&gt;
See [[Programming Reference:Filter Chain]] for information about defining a filter chain. Modification of the filter chain requires access to the [[Programming Reference:BCI2000 Source Code|BCI2000 source code]]. You will need to rebuild the &#039;&#039;MatlabSignalProcessing&#039;&#039; executable.&lt;br /&gt;
&lt;br /&gt;
==Command Line Options==&lt;br /&gt;
===MatlabWD===&lt;br /&gt;
By default, the Matlab engine&#039;s working directory is set to the signal processing module&#039;s working directory at startup. When using the Matlab filter for signal processing, your filter&#039;s code will be contained in the &amp;lt;tt&amp;gt;bci_Process&amp;lt;/tt&amp;gt; and associated Matlab functions, which will typically be located inside a dedicated directory. Thus, you may switch between Matlab-based filter implementations by selecting a Matlab working directory at startup.&lt;br /&gt;
This may be done by specifying the &amp;lt;tt&amp;gt;MatlabWD&amp;lt;/tt&amp;gt; parameter from the command line when starting the MatlabSignalProcessing module:&lt;br /&gt;
&amp;lt;pre&amp;gt;start MatlabSignalProcessing.exe --MatlabWD=&amp;quot;./matlab&amp;quot; 127.0.0.1&amp;lt;/pre&amp;gt;&lt;br /&gt;
will change Matlab&#039;s working directory to a directory called &amp;quot;matlab&amp;quot; inside the BCI2000 &amp;quot;prog&amp;quot; directory at startup, and execute &amp;lt;tt&amp;gt;bci_Process&amp;lt;/tt&amp;gt; and associated functions from there.&lt;br /&gt;
&lt;br /&gt;
This parameter must be set from the command line; later changes will have no effect.&lt;br /&gt;
&lt;br /&gt;
Using the &amp;lt;tt&amp;gt;--MatlabWD&amp;lt;/tt&amp;gt; command line option, you may easily switch between multiple Matlab-based BCI2000 configurations.&lt;br /&gt;
Simply create a separate startup batch file for each configuration, and specify the respective configuration&#039;s path on the command line that starts up the MatlabFilter.&lt;br /&gt;
&lt;br /&gt;
===MatlabStayOpen===&lt;br /&gt;
By default, the Matlab instance associated with the Matlab engine will be closed when BCI2000 quits.&lt;br /&gt;
Generally, this behavior is desired to ensure a well-defined BCI2000 system state after each startup.&lt;br /&gt;
For debugging purposes, though, it is sometimes useful to have the engine&#039;s command window available even after BCI2000 has been quit.&lt;br /&gt;
This behavior is controlled by another command-line parameter, &amp;lt;tt&amp;gt;MatlabStayOpen&amp;lt;/tt&amp;gt;, which may take the following values:&lt;br /&gt;
*0 is the default (no change in behavior),&lt;br /&gt;
*1 keeps the Matlab engine&#039;s command window open but clears variables used to transmit information forth and back between BCI2000 and Matlab,&lt;br /&gt;
*2 also preserves intermediate variables (such as bci_Parameters and bci_States) in the Matlab engine&#039;s workspace.&lt;br /&gt;
&lt;br /&gt;
To set this parameter to 2, use this command line to start up the signal processing module:&lt;br /&gt;
&amp;lt;pre&amp;gt;start MatlabSignalProcessing.exe --MatlabStayOpen=2 127.0.0.1&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Once the &amp;lt;tt&amp;gt;MatlabStayOpen&amp;lt;/tt&amp;gt; parameter has been set from the command line, it will be listed on the Operator Module&#039;s parameter configuration dialog&#039;s &amp;quot;system&amp;quot; tab, and may be modified from there. The change will be applied when clicking &#039;&#039;Set Config&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
==Troubleshooting==&lt;br /&gt;
===Debugging Matlab functions===&lt;br /&gt;
#Start BCI2000. The &#039;&#039;MatlabFilter&#039;&#039; will open a Matlab instance in its own window in minimized state.&lt;br /&gt;
#Switch to the minimized Matlab instance.&lt;br /&gt;
#Type, e.g.,&amp;lt;br /&amp;gt;&amp;lt;code&amp;gt;edit bci_Process.m&amp;lt;/code&amp;gt;&amp;lt;br /&amp;gt;to open a Matlab editor window connected to the Matlab interactive window.&lt;br /&gt;
#In the editor window, set breakpoints as you would normally.&lt;br /&gt;
#Execution will be paused, and you may examine variables moving the mouse over them.&lt;br /&gt;
&lt;br /&gt;
===Matlab doesn&#039;t find your functions===&lt;br /&gt;
Make sure to&lt;br /&gt;
*either set Matlab&#039;s working directory to the directory containing your functions, using the &amp;lt;tt&amp;gt;--MatlabWD&amp;lt;/tt&amp;gt; command line option described above,&lt;br /&gt;
*or add the respective directory to your Matlab path.&lt;br /&gt;
Generally, the first option is recommended over the second one.&lt;br /&gt;
&lt;br /&gt;
===There is no Matlab engine started up===&lt;br /&gt;
Execute&amp;lt;br /&amp;gt;&amp;lt;code&amp;gt;matlab /regserver&amp;lt;/code&amp;gt;&amp;lt;br /&amp;gt;from the command line when logged in with administrative privileges.&lt;br /&gt;
===Changes to M-files appear to have no effect===&lt;br /&gt;
Switch to the Matlab engine&#039;s command window, and enter&amp;lt;br /&amp;gt;&amp;lt;code&amp;gt;clear;&amp;lt;/code&amp;gt;&amp;lt;br /&amp;gt; or &amp;lt;br /&amp;gt;&amp;lt;code&amp;gt;clear scriptname;&amp;lt;/code&amp;gt;&amp;lt;br /&amp;gt;each time you change a Matlab function.&lt;br /&gt;
&lt;br /&gt;
===You get linker errors when rebuilding &#039;&#039;MatlabSignalProcessing&#039;&#039;===&lt;br /&gt;
If you get linker errors after editing &amp;lt;tt&amp;gt;PipeDefinition.cpp&amp;lt;/tt&amp;gt;, make sure that all filters&#039; cpp files are part of the &#039;&#039;MatlabSignalProcessing&#039;&#039; project.&lt;br /&gt;
===You get a message window with an &amp;quot;Entry Point Not Found&amp;quot; message===&lt;br /&gt;
This error is likely to indicate confusion of multiple installations of Matlab on your machine.&lt;br /&gt;
Check your Windows &#039;&#039;%PATH%&#039;&#039; environment variable, e.g. by opening a command console window and typing&lt;br /&gt;
 echo %PATH%&lt;br /&gt;
When it contains references to more than a single Matlab installation, remove all apart from the reference to the installation you are going to use with BCI2000. Also, make sure to execute&lt;br /&gt;
 matlab /regserver&lt;br /&gt;
for the Matlab installation you intend to use.&lt;br /&gt;
&lt;br /&gt;
==See also==&lt;br /&gt;
[[Technical Reference:Core Modules#Signal Processing Module]], &lt;br /&gt;
[[Technical Reference:Parameter Definition]], &lt;br /&gt;
[[Technical Reference:State Definition]], &lt;br /&gt;
[[Programming Reference:GenericFilter Class]], &lt;br /&gt;
[[Programming Reference:Filter Chain]], &lt;br /&gt;
[[Contributions:FieldTripBuffer]]&lt;br /&gt;
&lt;br /&gt;
Tutorials:&lt;br /&gt;
* [[Programming_Tutorial:Implementing_a_Matlab-based_Filter|IIR bandpass filtering, followed with RMS envelope computation, and linear classification]]&lt;br /&gt;
* [[Programming_Tutorial:Implementing_another_Matlab-based_Filter|adaptive spatial filtering using a beamformer, power computation and normalization]]&lt;br /&gt;
&lt;br /&gt;
[[Category:Filters]]&lt;br /&gt;
[[Category:Signal Processing]]&lt;br /&gt;
[[Category:Matlab]]&lt;br /&gt;
[[Category:Development]]&lt;br /&gt;
[[Category:External Interfaces]]&lt;/div&gt;</summary>
		<author><name>Roostenveld</name></author>
	</entry>
	<entry>
		<id>https://www.bci2000.org/mediawiki/index.php?title=User_Reference:MatlabFilter&amp;diff=3981</id>
		<title>User Reference:MatlabFilter</title>
		<link rel="alternate" type="text/html" href="https://www.bci2000.org/mediawiki/index.php?title=User_Reference:MatlabFilter&amp;diff=3981"/>
		<updated>2008-07-14T12:46:06Z</updated>

		<summary type="html">&lt;p&gt;Roostenveld: /* Detailled explanation */ fixed link&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Synopsis==&lt;br /&gt;
&lt;br /&gt;
The Matlab filter implements an interface between BCI2000 and a standalone Matlab engine. It allows you to implement and/or use Matlab functions for filtering data within the BCI2000 pipeline.&lt;br /&gt;
&lt;br /&gt;
==Parameters==&lt;br /&gt;
&lt;br /&gt;
The Matlab filter does not have a set of default parameters that it uses. Instead, the user-supplied Matlab functions that are executed by the Matlab filter specify the parameters. After initialization, these parameters are displayed in the [[User_Reference:Operator_Module|Operator]] and can be modified there.&lt;br /&gt;
&lt;br /&gt;
==States==&lt;br /&gt;
&lt;br /&gt;
The Matlab filter does not have a set of default states that it uses. Instead, the user-supplied Matlab functions that are executed by the Matlab filter specify the states. &lt;br /&gt;
&lt;br /&gt;
==Detailled explanation==&lt;br /&gt;
&lt;br /&gt;
The Matlab filter does not implement any algorithms itself, but instead allows you to implement the algorithms of your choice in Matlab.&lt;br /&gt;
&lt;br /&gt;
Once BCI2000 is running with the Matlab filter, you will see that a very simple Matlab command line window has openend. In that command line window you can type commands that show you the variables that BCI2000 communicates to the Matlab engine. For example, you can do&lt;br /&gt;
&lt;br /&gt;
  % show the variables&lt;br /&gt;
  whos&lt;br /&gt;
  &lt;br /&gt;
  % plot the first channel of the data (see below)&lt;br /&gt;
  plot(bci_InSignal(1,:))&lt;br /&gt;
  &lt;br /&gt;
  % plot the first channel of the data and continuously update the plot (see below)&lt;br /&gt;
  while(1); plot(bci_InSignal(1,:)); pause(0.01); end&lt;br /&gt;
&lt;br /&gt;
If you try these examples above like this, you will notice that Matlab takes quite a considerable amount of time to open a new figure. While the figure is being openend by Matlab, the engine is blocked , which causes the MatlabFilter to fail writing new data to the engine. Hence the MatlabFilter will return an error and you will have to restart BCI2000. The reason is that the Matlab engine gives you access through the command window, but that the Matlab engine is not capable of executing the commands that it receives from BCI2000 and the commands that it receives from you through the command window simultaneously. &lt;br /&gt;
&lt;br /&gt;
To get the above example to run correctly, you will have to open the figure /prior/ to starting BCI2000 by typing &amp;lt;tt&amp;gt;figure&amp;lt;/tt&amp;gt; (this will open an empty figure). Subsequently, once BCI2000 is running, the content of the figure can be updated. &lt;br /&gt;
&lt;br /&gt;
However, the above example already shows that the Matlab engine is not intended to be used concurrently from the BCI2000 MatlabFilter and from the command window. Instead of typing your own commands in the command window, you will have to give full control of command execution to the MatlabFilter to ensure that timing is controlled. The way that BCI2000 and Matlab interact is that each of the components of a standard filter is mapped onto a corresponding Matlab function. If the Matlab filter is running within BCI2000, it will call the Matlab engine and execute the required Matlab function. The easiest way of getting started with the Matlab filter is by using one of the provided programming tutorials: &lt;br /&gt;
* [[Programming_Tutorial:Implementing_a_Matlab-based_Filter|IIR bandpass filtering, followed with RMS envelope computation, and linear classification]]&lt;br /&gt;
* [[Programming_Tutorial:Implementing_another_Matlab-based_Filter|adaptive spatial filtering using a beamformer, power computation and normalization]]&lt;br /&gt;
&lt;br /&gt;
A detailled description of how to implement your own signal processing functions in Matlab can be found in the [[Programming_Reference:MatlabFilter|Programming reference for the Matlab filter]].&lt;br /&gt;
&lt;br /&gt;
==Troubleshooting:==&lt;br /&gt;
&lt;br /&gt;
===Matlab doesn&#039;t find your functions===&lt;br /&gt;
Make sure to&lt;br /&gt;
*either set Matlab&#039;s working directory to the directory containing your functions, using the &amp;lt;tt&amp;gt;--MatlabWD&amp;lt;/tt&amp;gt; command line option described above,&lt;br /&gt;
*or add the respective directory to your Matlab path.&lt;br /&gt;
Generally, the first option is recommended over the second one.&lt;br /&gt;
&lt;br /&gt;
===There is no Matlab engine started up===&lt;br /&gt;
Execute&amp;lt;br /&amp;gt;&amp;lt;code&amp;gt;matlab /regserver&amp;lt;/code&amp;gt;&amp;lt;br /&amp;gt;from the command line when logged in with administrative privileges.&lt;br /&gt;
&lt;br /&gt;
==See also==&lt;br /&gt;
[[Programming_Reference:MatlabFilter]],&lt;br /&gt;
[[Contributions:FieldTripBuffer]]&lt;br /&gt;
&lt;br /&gt;
[[Category:Filters]][[Category:Signal Processing]][[Category:Matlab]][[Category:Development]]&lt;/div&gt;</summary>
		<author><name>Roostenveld</name></author>
	</entry>
	<entry>
		<id>https://www.bci2000.org/mediawiki/index.php?title=Contributions:FieldTripBuffer&amp;diff=3980</id>
		<title>Contributions:FieldTripBuffer</title>
		<link rel="alternate" type="text/html" href="https://www.bci2000.org/mediawiki/index.php?title=Contributions:FieldTripBuffer&amp;diff=3980"/>
		<updated>2008-07-14T12:45:20Z</updated>

		<summary type="html">&lt;p&gt;Roostenveld: /* See also */ fixed links&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Synopsis==&lt;br /&gt;
&lt;br /&gt;
==Location==&lt;br /&gt;
&lt;br /&gt;
==Parameters==&lt;br /&gt;
&lt;br /&gt;
==States==&lt;br /&gt;
&lt;br /&gt;
==See also==&lt;br /&gt;
[[User_Reference:MatlabFilter]], [[Programming_Reference:MatlabFilter]]&lt;br /&gt;
&lt;br /&gt;
[[Category:Filters]]&lt;br /&gt;
[[Category:Signal Processing]]&lt;br /&gt;
[[Category:Matlab]]&lt;br /&gt;
[[Category:Development]]&lt;br /&gt;
[[Category:External Interfaces]]&lt;/div&gt;</summary>
		<author><name>Roostenveld</name></author>
	</entry>
	<entry>
		<id>https://www.bci2000.org/mediawiki/index.php?title=Programming_Reference:Rapid_Development&amp;diff=3979</id>
		<title>Programming Reference:Rapid Development</title>
		<link rel="alternate" type="text/html" href="https://www.bci2000.org/mediawiki/index.php?title=Programming_Reference:Rapid_Development&amp;diff=3979"/>
		<updated>2008-07-14T12:43:55Z</updated>

		<summary type="html">&lt;p&gt;Roostenveld: /* Matlab Integration */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This page describes elements of BCI2000 intended to simplify a programmer&#039;s task when implementing a new BCI2000 filter, or application.&lt;br /&gt;
&lt;br /&gt;
==Templates==&lt;br /&gt;
*The [[Programming Reference:FeedbackDemo Application]] may serve as a starting point for a trial-based application.&lt;br /&gt;
*The [[Programming Reference:IIRFilter Class]] filter stub allows you to easily implement IIR filters.&lt;br /&gt;
&lt;br /&gt;
==Helper Classes==&lt;br /&gt;
*[[Programming Reference:WavePlayer Class]] plays back audio files.&lt;br /&gt;
*[[Programming Reference:TextToSpeech Class]] provides synthetic speech.&lt;br /&gt;
*[[Programming Reference:MidiPlayer Class]] plays back MIDI data.&lt;br /&gt;
*[[Programming Reference:TrialStatistics Class]] computes accuracies and bit rates.&lt;br /&gt;
*[[Programming Reference:LogFile Class]] transparently maintains a log file in the current data directory.&lt;br /&gt;
&lt;br /&gt;
==Matlab Integration==&lt;br /&gt;
*The [[Programming Reference:MatlabFilter]] lets Matlab code act upon BCI2000 data.&lt;br /&gt;
*There is also a [[Programming Tutorial:Implementing a Matlab-based Filter|tutorial on implementing a Matlab-based tilter]] for temporal filtering, and [[Programming Tutorial:Implementing another Matlab-based Filter| another tutorial]] on implementing an adaptive spatial filter using beamforming.&lt;br /&gt;
&lt;br /&gt;
[[Category:Contents]]&lt;br /&gt;
[[Category:Development]]&lt;/div&gt;</summary>
		<author><name>Roostenveld</name></author>
	</entry>
	<entry>
		<id>https://www.bci2000.org/mediawiki/index.php?title=Programming_Reference:Contents&amp;diff=3978</id>
		<title>Programming Reference:Contents</title>
		<link rel="alternate" type="text/html" href="https://www.bci2000.org/mediawiki/index.php?title=Programming_Reference:Contents&amp;diff=3978"/>
		<updated>2008-07-14T12:39:24Z</updated>

		<summary type="html">&lt;p&gt;Roostenveld: /* Tutorials */ added another matlab demo&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This part of the reference provides background information which you need in order&lt;br /&gt;
to understand, modify, or create code that depends on the BCI2000 framework.&lt;br /&gt;
&lt;br /&gt;
You should read it before writing your own BCI2000 module, or modifying an existing&lt;br /&gt;
one as presented in the tutorial examples.&lt;br /&gt;
&lt;br /&gt;
==Getting Started==&lt;br /&gt;
*[[Programming Howto:Building BCI2000]]&lt;br /&gt;
*[[Programming Howto:SVN Client Setup]]&lt;br /&gt;
*[[Programming Howto:Using TortoiseSVN]]&lt;br /&gt;
*[[Programming FAQ]]&lt;br /&gt;
&lt;br /&gt;
==Conceptual Overview==&lt;br /&gt;
*[[Programming Reference:Filter Chain]]&lt;br /&gt;
*[[Programming Reference:Signals]]&lt;br /&gt;
*[[Programming Reference:Error Handling]]&lt;br /&gt;
*[[Programming Reference:Localization]]&lt;br /&gt;
*[[Programming Reference:Rapid Development]]&lt;br /&gt;
&lt;br /&gt;
==Programming Interface Documentation==&lt;br /&gt;
&lt;br /&gt;
===Generic Filter Interface===&lt;br /&gt;
*[[Programming Reference:GenericFilter Class]]&lt;br /&gt;
*[[Programming Reference:Environment Class]]&lt;br /&gt;
*[[Programming Reference:GenericVisualization Class]]&lt;br /&gt;
&lt;br /&gt;
===Text Messages===&lt;br /&gt;
*[[Programming Reference:Errors and Warnings]]&lt;br /&gt;
*[[Programming Reference:Debug Output]]&lt;br /&gt;
*[[Programming Reference:LogFile Class]]&lt;br /&gt;
&lt;br /&gt;
===Data I/O===&lt;br /&gt;
*[[Programming Reference:GenericFileWriter Class]]&lt;br /&gt;
*[[Programming Reference:GenericADC Class]]&lt;br /&gt;
*[[Programming Reference:BCI2000FileReader Class]]&lt;br /&gt;
&lt;br /&gt;
===Signal Processing Modules===&lt;br /&gt;
*[[Programming Reference:IIRFilter Class]]&lt;br /&gt;
&lt;br /&gt;
===Application Modules===&lt;br /&gt;
*[[Programming Reference:ApplicationBase Class]]&lt;br /&gt;
*[[Programming Reference:FeedbackTask Class]]&lt;br /&gt;
*[[Programming Reference:StimulusTask Class]]&lt;br /&gt;
&lt;br /&gt;
====Visual Presentation====&lt;br /&gt;
*[[Programming Reference:3D API]]&lt;br /&gt;
&lt;br /&gt;
====Auditory Presentation====&lt;br /&gt;
*[[Programming Reference:MidiPlayer Class]]&lt;br /&gt;
*[[Programming Reference:WavePlayer Class]]&lt;br /&gt;
*[[Programming Reference:TextToSpeech Class]]&lt;br /&gt;
&lt;br /&gt;
==Tutorials==&lt;br /&gt;
*[[Programming Tutorial:Implementing a Data Acquisition Module]]&lt;br /&gt;
*[[Programming Tutorial:Implementing a Signal Processing Filter]]&lt;br /&gt;
*[[Programming Tutorial:Implementing a Matlab-based Filter]]&lt;br /&gt;
*[[Programming Tutorial:Implementing another Matlab-based Filter]]&lt;br /&gt;
&lt;br /&gt;
==See also==&lt;br /&gt;
[[Technical Reference:Contents]], [[User Reference:Contents]]&lt;br /&gt;
&lt;br /&gt;
[[Category:Contents]]&lt;/div&gt;</summary>
		<author><name>Roostenveld</name></author>
	</entry>
	<entry>
		<id>https://www.bci2000.org/mediawiki/index.php?title=Programming_Reference:MatlabFilter&amp;diff=3977</id>
		<title>Programming Reference:MatlabFilter</title>
		<link rel="alternate" type="text/html" href="https://www.bci2000.org/mediawiki/index.php?title=Programming_Reference:MatlabFilter&amp;diff=3977"/>
		<updated>2008-07-14T12:38:14Z</updated>

		<summary type="html">&lt;p&gt;Roostenveld: /* See also */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;The &#039;&#039;MatlabFilter&#039;&#039; calls the Matlab engine to act upon signals,&lt;br /&gt;
parameters, and states. &lt;br /&gt;
It provides the full BCI2000 [[Programming Reference:GenericFilter Class|filter interface]]&lt;br /&gt;
to a Matlab filter implementation.&lt;br /&gt;
&lt;br /&gt;
==Filter Interface==&lt;br /&gt;
For each BCI2000 filter member function, there is a corresponding Matlab&lt;br /&gt;
function as follows:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
GenericFilter member      Matlab function syntax&lt;br /&gt;
====================      ======================&lt;br /&gt;
Constructor               [parameters, states] = bci_Construct&lt;br /&gt;
Destructor                bci_Destruct&lt;br /&gt;
Preflight                 out_signal_dim = bci_Preflight( in_signal_dim )&lt;br /&gt;
Initialize                bci_Initialize( in_signal_dim, out_signal_dim )&lt;br /&gt;
Process                   out_signal = bci_Process( in_signal )&lt;br /&gt;
StartRun                  bci_StartRun&lt;br /&gt;
StopRun                   bci_StopRun&lt;br /&gt;
Resting                   bci_Resting&lt;br /&gt;
Halt                      bci_Halt&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Existence of the above-listed Matlab functions is not mandatory.&lt;br /&gt;
The MatlabFilter uses the Matlab &amp;lt;tt&amp;gt;exist&amp;lt;/tt&amp;gt; command to determine whether a&lt;br /&gt;
given function is available, and will not try to call that function when this&lt;br /&gt;
is not the case.&lt;br /&gt;
&lt;br /&gt;
If either of the &amp;lt;tt&amp;gt;bci_Preflight&amp;lt;/tt&amp;gt;, &amp;lt;tt&amp;gt;bci_Initialize&amp;lt;/tt&amp;gt;, or &amp;lt;tt&amp;gt;bci_Process&amp;lt;/tt&amp;gt; functions&lt;br /&gt;
is not available, a warning will be displayed to the user.&lt;br /&gt;
&lt;br /&gt;
==Accessing Parameters and States==&lt;br /&gt;
Parameters and states are accessible via global Matlab structs called&lt;br /&gt;
&amp;lt;tt&amp;gt;bci_Parameters&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;bci_States&amp;lt;/tt&amp;gt;. In Matlab, write&lt;br /&gt;
&lt;br /&gt;
  global bci_Parameters bci_States;&lt;br /&gt;
  my_sampling_rate = bci_Parameters.SamplingRate;&lt;br /&gt;
&lt;br /&gt;
*Parameter values may be changed from inside the &amp;lt;tt&amp;gt;bci_StopRun&amp;lt;/tt&amp;gt; script, and will automatically be propagated to the other modules.&lt;br /&gt;
&lt;br /&gt;
*State values may be modified from the &amp;lt;tt&amp;gt;bci_Process&amp;lt;/tt&amp;gt; function.&lt;br /&gt;
&lt;br /&gt;
==Declaring Parameters and States==&lt;br /&gt;
To add parameters and states to the BCI2000 list of states, the &amp;lt;tt&amp;gt;bci_Construct&amp;lt;/tt&amp;gt;&lt;br /&gt;
function may return non-empty cell arrays of strings in its &amp;lt;tt&amp;gt;parameters&amp;lt;/tt&amp;gt;&lt;br /&gt;
and &amp;lt;tt&amp;gt;states&amp;lt;/tt&amp;gt; return values. The strings constituting these cell arrays are [[Technical Reference:Parameter Definition|parameter]] and [[Technical Reference:State Definition|state definitions]].&lt;br /&gt;
&lt;br /&gt;
Your added parameters will appear in the [[User Reference:Operator Module|operator module&#039;s]] parameter configuration dialog. Your parameter definition&#039;s section name will be used to direct its position in the configuration dialog&#039;s register cards:&lt;br /&gt;
 MyFilter int MyParam= 2 0 0 2 // ...&lt;br /&gt;
will display the &#039;&#039;MyParam&#039;&#039; parameter on separate register card named &#039;&#039;MyFilter&#039;&#039;.&lt;br /&gt;
 Filtering:MyFilter int MyParam= 2 0 0 2 // ...&lt;br /&gt;
will display the parameter on the &#039;&#039;Filtering&#039;&#039; register card, inside a group called &#039;&#039;MyFilter&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
==Signal Format==&lt;br /&gt;
BCI2000 signals are mapped to Matlab matrices with channel index first,&lt;br /&gt;
and sample (element) index second.&lt;br /&gt;
Signal dimension arguments of &amp;lt;tt&amp;gt;bci_Preflight&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;bci_Initialize&amp;lt;/tt&amp;gt; are&lt;br /&gt;
vectors of integers (1x2 matrices) as in &amp;lt;tt&amp;gt;[n_channels n_elements]&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
==Error Reporting==&lt;br /&gt;
To report errors from Matlab functions, use Matlab&#039;s &amp;lt;tt&amp;gt;error&amp;lt;/tt&amp;gt; command.&lt;br /&gt;
Your error messages will be displayed to the user from the [[User Reference:Operator Module|operator module]].&lt;br /&gt;
&lt;br /&gt;
==Combining with BCI2000 Signal Processing Filters==&lt;br /&gt;
In the BCI2000 binary distribution, the &#039;&#039;MatlabFilter&#039;&#039; is shipped inside the &#039;&#039;MatlabSignalProcessing&#039;&#039; executable.&lt;br /&gt;
There, the signal processing chain consists of the [[User Reference:SpatialFilter|SpatialFilter]] and the &#039;&#039;MatlabFilter&#039;&#039;.&lt;br /&gt;
However, by editing &amp;lt;tt&amp;gt;src/core/SignalProcessing/Matlab/PipeDefinition.cpp&amp;lt;/tt&amp;gt;, you may add as many [[User Reference:Filters|signal processing filters]] as you wish.&lt;br /&gt;
(See [[Programming Reference:Filter Chain]] for information about defining a filter chain.)&lt;br /&gt;
&lt;br /&gt;
This modification requires access to the [[Programming Reference:BCI2000 Source Code|BCI2000 source code]]. You will need to rebuild the &#039;&#039;MatlabSignalProcessing&#039;&#039; executable.&lt;br /&gt;
&lt;br /&gt;
==Command Line Options==&lt;br /&gt;
===MatlabWD===&lt;br /&gt;
By default, the Matlab engine&#039;s working directory is set to the signal processing module&#039;s working directory at startup. When using the Matlab filter for signal processing, your filter&#039;s code will be contained in the &amp;lt;tt&amp;gt;bci_Process&amp;lt;/tt&amp;gt; and associated Matlab functions, which will typically be located inside a dedicated directory. Thus, you may switch between Matlab-based filter implementations by selecting a Matlab working directory at startup.&lt;br /&gt;
This may be done by specifying the &amp;lt;tt&amp;gt;MatlabWD&amp;lt;/tt&amp;gt; parameter from the command line when starting the MatlabSignalProcessing module:&lt;br /&gt;
&amp;lt;pre&amp;gt;start MatlabSignalProcessing.exe --MatlabWD=&amp;quot;./matlab&amp;quot; 127.0.0.1&amp;lt;/pre&amp;gt;&lt;br /&gt;
will change Matlab&#039;s working directory to a directory called &amp;quot;matlab&amp;quot; inside the BCI2000 &amp;quot;prog&amp;quot; directory at startup, and execute &amp;lt;tt&amp;gt;bci_Process&amp;lt;/tt&amp;gt; and associated functions from there.&lt;br /&gt;
&lt;br /&gt;
This parameter must be set from the command line; later changes will have no effect.&lt;br /&gt;
&lt;br /&gt;
Using the &amp;lt;tt&amp;gt;--MatlabWD&amp;lt;/tt&amp;gt; command line option, you may easily switch between multiple Matlab-based BCI2000 configurations.&lt;br /&gt;
Simply create a separate startup batch file for each configuration, and specify the respective configuration&#039;s path on the command line that starts up the MatlabFilter.&lt;br /&gt;
&lt;br /&gt;
===MatlabStayOpen===&lt;br /&gt;
By default, the Matlab instance associated with the Matlab engine will be closed when BCI2000 quits.&lt;br /&gt;
Generally, this behavior is desired to ensure a well-defined BCI2000 system state after each startup.&lt;br /&gt;
For debugging purposes, though, it is sometimes useful to have the engine&#039;s command window available even after BCI2000 has been quit.&lt;br /&gt;
This behavior is controlled by another command-line parameter, &amp;lt;tt&amp;gt;MatlabStayOpen&amp;lt;/tt&amp;gt;, which may take the following values:&lt;br /&gt;
*0 is the default (no change in behavior),&lt;br /&gt;
*1 keeps the Matlab engine&#039;s command window open but clears variables used to transmit information forth and back between BCI2000 and Matlab,&lt;br /&gt;
*2 also preserves intermediate variables (such as bci_Parameters and bci_States) in the Matlab engine&#039;s workspace.&lt;br /&gt;
&lt;br /&gt;
To set this parameter to 2, use this command line to start up the signal processing module:&lt;br /&gt;
&amp;lt;pre&amp;gt;start MatlabSignalProcessing.exe --MatlabStayOpen=2 127.0.0.1&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Once the &amp;lt;tt&amp;gt;MatlabStayOpen&amp;lt;/tt&amp;gt; parameter has been set from the command line, it will be listed on the Operator Module&#039;s parameter configuration dialog&#039;s &amp;quot;system&amp;quot; tab, and may be modified from there. The change will be applied when clicking &#039;&#039;Set Config&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
==Troubleshooting==&lt;br /&gt;
===Debugging Matlab functions===&lt;br /&gt;
#Start BCI2000. The &#039;&#039;MatlabFilter&#039;&#039; will open a Matlab instance in its own window in minimized state.&lt;br /&gt;
#Switch to the minimized Matlab instance.&lt;br /&gt;
#Type, e.g.,&amp;lt;br /&amp;gt;&amp;lt;code&amp;gt;edit bci_Process.m&amp;lt;/code&amp;gt;&amp;lt;br /&amp;gt;to open a Matlab editor window connected to the Matlab interactive window.&lt;br /&gt;
#In the editor window, set breakpoints as you would normally.&lt;br /&gt;
#Execution will be paused, and you may examine variables moving the mouse over them.&lt;br /&gt;
&lt;br /&gt;
===Matlab doesn&#039;t find your functions===&lt;br /&gt;
Make sure to&lt;br /&gt;
*either set Matlab&#039;s working directory to the directory containing your functions, using the &amp;lt;tt&amp;gt;--MatlabWD&amp;lt;/tt&amp;gt; command line option described above,&lt;br /&gt;
*or add the respective directory to your Matlab path.&lt;br /&gt;
Generally, the first option is recommended over the second one.&lt;br /&gt;
&lt;br /&gt;
===There is no Matlab engine started up===&lt;br /&gt;
Execute&amp;lt;br /&amp;gt;&amp;lt;code&amp;gt;matlab /regserver&amp;lt;/code&amp;gt;&amp;lt;br /&amp;gt;from the command line when logged in with administrative privileges.&lt;br /&gt;
===Changes to M-files appear to have no effect===&lt;br /&gt;
Switch to the Matlab engine&#039;s command window, and enter&amp;lt;br /&amp;gt;&amp;lt;code&amp;gt;clear;&amp;lt;/code&amp;gt;&amp;lt;br /&amp;gt; or &amp;lt;br /&amp;gt;&amp;lt;code&amp;gt;clear scriptname;&amp;lt;/code&amp;gt;&amp;lt;br /&amp;gt;each time you change a Matlab function.&lt;br /&gt;
&lt;br /&gt;
===You get linker errors when rebuilding &#039;&#039;MatlabSignalProcessing&#039;&#039;===&lt;br /&gt;
If you get linker errors after editing &amp;lt;tt&amp;gt;PipeDefinition.cpp&amp;lt;/tt&amp;gt;, make sure that all filters&#039; cpp files are part of the &#039;&#039;MatlabSignalProcessing&#039;&#039; project.&lt;br /&gt;
===You get a message window with an &amp;quot;Entry Point Not Found&amp;quot; message===&lt;br /&gt;
This error is likely to indicate confusion of multiple installations of Matlab on your machine.&lt;br /&gt;
Check your Windows &#039;&#039;%PATH%&#039;&#039; environment variable, e.g. by opening a command console window and typing&lt;br /&gt;
 echo %PATH%&lt;br /&gt;
When it contains references to more than a single Matlab installation, remove all apart from the reference to the installation you are going to use with BCI2000. Also, make sure to execute&lt;br /&gt;
 matlab /regserver&lt;br /&gt;
for the Matlab installation you intend to use.&lt;br /&gt;
&lt;br /&gt;
==See also==&lt;br /&gt;
[[Technical Reference:Core Modules#Signal Processing Module]], &lt;br /&gt;
[[Technical Reference:Parameter Definition]], &lt;br /&gt;
[[Technical Reference:State Definition]], &lt;br /&gt;
[[Programming Reference:GenericFilter Class]], &lt;br /&gt;
[[Programming Reference:Filter Chain]], &lt;br /&gt;
[[Programming Tutorial:Implementing a Matlab-based Filter]]&lt;br /&gt;
[[Programming Tutorial:Implementing another Matlab-based Filter]],&lt;br /&gt;
[[Contributions:FieldTripBuffer]]&lt;br /&gt;
&lt;br /&gt;
[[Category:Development]]&lt;br /&gt;
[[Category:External Interfaces]]&lt;/div&gt;</summary>
		<author><name>Roostenveld</name></author>
	</entry>
	<entry>
		<id>https://www.bci2000.org/mediawiki/index.php?title=User_Reference:MatlabFilter&amp;diff=3976</id>
		<title>User Reference:MatlabFilter</title>
		<link rel="alternate" type="text/html" href="https://www.bci2000.org/mediawiki/index.php?title=User_Reference:MatlabFilter&amp;diff=3976"/>
		<updated>2008-07-14T12:36:41Z</updated>

		<summary type="html">&lt;p&gt;Roostenveld: /* See also */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Synopsis==&lt;br /&gt;
&lt;br /&gt;
The Matlab filter implements an interface between BCI2000 and a standalone Matlab engine. It allows you to implement and/or use Matlab functions for filtering data within the BCI2000 pipeline.&lt;br /&gt;
&lt;br /&gt;
==Parameters==&lt;br /&gt;
&lt;br /&gt;
The Matlab filter does not have a set of default parameters that it uses. Instead, the user-supplied Matlab functions that are executed by the Matlab filter specify the parameters. After initialization, these parameters are displayed in the [[User_Reference:Operator_Module|Operator]] and can be modified there.&lt;br /&gt;
&lt;br /&gt;
==States==&lt;br /&gt;
&lt;br /&gt;
The Matlab filter does not have a set of default states that it uses. Instead, the user-supplied Matlab functions that are executed by the Matlab filter specify the states. &lt;br /&gt;
&lt;br /&gt;
==Detailled explanation==&lt;br /&gt;
&lt;br /&gt;
The Matlab filter does not implement any algorithms itself, but instead allows you to implement the algorithms of your choice in Matlab.&lt;br /&gt;
&lt;br /&gt;
Once BCI2000 is running with the Matlab filter, you will see that a very simple Matlab command line window has openend. In that command line window you can type commands that show you the variables that BCI2000 communicates to the Matlab engine. For example, you can do&lt;br /&gt;
&lt;br /&gt;
  % show the variables&lt;br /&gt;
  whos&lt;br /&gt;
  &lt;br /&gt;
  % plot the first channel of the data (see below)&lt;br /&gt;
  plot(bci_InSignal(1,:))&lt;br /&gt;
  &lt;br /&gt;
  % plot the first channel of the data and continuously update the plot (see below)&lt;br /&gt;
  while(1); plot(bci_InSignal(1,:)); pause(0.01); end&lt;br /&gt;
&lt;br /&gt;
If you try these examples above like this, you will notice that Matlab takes quite a considerable amount of time to open a new figure. While the figure is being openend by Matlab, the engine is blocked , which causes the MatlabFilter to fail writing new data to the engine. Hence the MatlabFilter will return an error and you will have to restart BCI2000. The reason is that the Matlab engine gives you access through the command window, but that the Matlab engine is not capable of executing the commands that it receives from BCI2000 and the commands that it receives from you through the command window simultaneously. &lt;br /&gt;
&lt;br /&gt;
To get the above example to run correctly, you will have to open the figure /prior/ to starting BCI2000 by typing &amp;lt;tt&amp;gt;figure&amp;lt;/tt&amp;gt; (this will open an empty figure). Subsequently, once BCI2000 is running, the content of the figure can be updated. &lt;br /&gt;
&lt;br /&gt;
However, the above example already shows that the Matlab engine is not intended to be used concurrently from the BCI2000 MatlabFilter and from the command window. Instead of typing your own commands in the command window, you will have to give full control of command execution to the MatlabFilter to ensure that timing is controlled. The way that BCI2000 and Matlab interact is that each of the components of a standard filter is mapped onto a corresponding Matlab function. If the Matlab filter is running within BCI2000, it will call the Matlab engine and execute the required Matlab function. The easiest way of getting started with the Matlab filter is by using one of the provided programming tutorials: &lt;br /&gt;
* [[Programming_Tutorial:Implementing_a_Matlab-based_Filter|IIR bandpass filtering, followed with RMS envelope computation, and linear classification]]&lt;br /&gt;
* [[Programming_Tutorial:Implementing_another_Matlab-based_Filter|adaptive spatial filtering using a beamformer, power computation and normalization]]&lt;br /&gt;
&lt;br /&gt;
A detailled description of how to implement your own signal processing functions in Matlab can be found in the [[Programming_Reference:Matlab|Programming reference for the Matlab filter]].&lt;br /&gt;
&lt;br /&gt;
==Troubleshooting:==&lt;br /&gt;
&lt;br /&gt;
===Matlab doesn&#039;t find your functions===&lt;br /&gt;
Make sure to&lt;br /&gt;
*either set Matlab&#039;s working directory to the directory containing your functions, using the &amp;lt;tt&amp;gt;--MatlabWD&amp;lt;/tt&amp;gt; command line option described above,&lt;br /&gt;
*or add the respective directory to your Matlab path.&lt;br /&gt;
Generally, the first option is recommended over the second one.&lt;br /&gt;
&lt;br /&gt;
===There is no Matlab engine started up===&lt;br /&gt;
Execute&amp;lt;br /&amp;gt;&amp;lt;code&amp;gt;matlab /regserver&amp;lt;/code&amp;gt;&amp;lt;br /&amp;gt;from the command line when logged in with administrative privileges.&lt;br /&gt;
&lt;br /&gt;
==See also==&lt;br /&gt;
[[Programming_Reference:MatlabFilter]],&lt;br /&gt;
[[Contributions:FieldTripBuffer]]&lt;br /&gt;
&lt;br /&gt;
[[Category:Filters]][[Category:Signal Processing]][[Category:Matlab]][[Category:Development]]&lt;/div&gt;</summary>
		<author><name>Roostenveld</name></author>
	</entry>
	<entry>
		<id>https://www.bci2000.org/mediawiki/index.php?title=User_Reference:MatlabFilter&amp;diff=3975</id>
		<title>User Reference:MatlabFilter</title>
		<link rel="alternate" type="text/html" href="https://www.bci2000.org/mediawiki/index.php?title=User_Reference:MatlabFilter&amp;diff=3975"/>
		<updated>2008-07-14T12:36:14Z</updated>

		<summary type="html">&lt;p&gt;Roostenveld: /* See also */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Synopsis==&lt;br /&gt;
&lt;br /&gt;
The Matlab filter implements an interface between BCI2000 and a standalone Matlab engine. It allows you to implement and/or use Matlab functions for filtering data within the BCI2000 pipeline.&lt;br /&gt;
&lt;br /&gt;
==Parameters==&lt;br /&gt;
&lt;br /&gt;
The Matlab filter does not have a set of default parameters that it uses. Instead, the user-supplied Matlab functions that are executed by the Matlab filter specify the parameters. After initialization, these parameters are displayed in the [[User_Reference:Operator_Module|Operator]] and can be modified there.&lt;br /&gt;
&lt;br /&gt;
==States==&lt;br /&gt;
&lt;br /&gt;
The Matlab filter does not have a set of default states that it uses. Instead, the user-supplied Matlab functions that are executed by the Matlab filter specify the states. &lt;br /&gt;
&lt;br /&gt;
==Detailled explanation==&lt;br /&gt;
&lt;br /&gt;
The Matlab filter does not implement any algorithms itself, but instead allows you to implement the algorithms of your choice in Matlab.&lt;br /&gt;
&lt;br /&gt;
Once BCI2000 is running with the Matlab filter, you will see that a very simple Matlab command line window has openend. In that command line window you can type commands that show you the variables that BCI2000 communicates to the Matlab engine. For example, you can do&lt;br /&gt;
&lt;br /&gt;
  % show the variables&lt;br /&gt;
  whos&lt;br /&gt;
  &lt;br /&gt;
  % plot the first channel of the data (see below)&lt;br /&gt;
  plot(bci_InSignal(1,:))&lt;br /&gt;
  &lt;br /&gt;
  % plot the first channel of the data and continuously update the plot (see below)&lt;br /&gt;
  while(1); plot(bci_InSignal(1,:)); pause(0.01); end&lt;br /&gt;
&lt;br /&gt;
If you try these examples above like this, you will notice that Matlab takes quite a considerable amount of time to open a new figure. While the figure is being openend by Matlab, the engine is blocked , which causes the MatlabFilter to fail writing new data to the engine. Hence the MatlabFilter will return an error and you will have to restart BCI2000. The reason is that the Matlab engine gives you access through the command window, but that the Matlab engine is not capable of executing the commands that it receives from BCI2000 and the commands that it receives from you through the command window simultaneously. &lt;br /&gt;
&lt;br /&gt;
To get the above example to run correctly, you will have to open the figure /prior/ to starting BCI2000 by typing &amp;lt;tt&amp;gt;figure&amp;lt;/tt&amp;gt; (this will open an empty figure). Subsequently, once BCI2000 is running, the content of the figure can be updated. &lt;br /&gt;
&lt;br /&gt;
However, the above example already shows that the Matlab engine is not intended to be used concurrently from the BCI2000 MatlabFilter and from the command window. Instead of typing your own commands in the command window, you will have to give full control of command execution to the MatlabFilter to ensure that timing is controlled. The way that BCI2000 and Matlab interact is that each of the components of a standard filter is mapped onto a corresponding Matlab function. If the Matlab filter is running within BCI2000, it will call the Matlab engine and execute the required Matlab function. The easiest way of getting started with the Matlab filter is by using one of the provided programming tutorials: &lt;br /&gt;
* [[Programming_Tutorial:Implementing_a_Matlab-based_Filter|IIR bandpass filtering, followed with RMS envelope computation, and linear classification]]&lt;br /&gt;
* [[Programming_Tutorial:Implementing_another_Matlab-based_Filter|adaptive spatial filtering using a beamformer, power computation and normalization]]&lt;br /&gt;
&lt;br /&gt;
A detailled description of how to implement your own signal processing functions in Matlab can be found in the [[Programming_Reference:Matlab|Programming reference for the Matlab filter]].&lt;br /&gt;
&lt;br /&gt;
==Troubleshooting:==&lt;br /&gt;
&lt;br /&gt;
===Matlab doesn&#039;t find your functions===&lt;br /&gt;
Make sure to&lt;br /&gt;
*either set Matlab&#039;s working directory to the directory containing your functions, using the &amp;lt;tt&amp;gt;--MatlabWD&amp;lt;/tt&amp;gt; command line option described above,&lt;br /&gt;
*or add the respective directory to your Matlab path.&lt;br /&gt;
Generally, the first option is recommended over the second one.&lt;br /&gt;
&lt;br /&gt;
===There is no Matlab engine started up===&lt;br /&gt;
Execute&amp;lt;br /&amp;gt;&amp;lt;code&amp;gt;matlab /regserver&amp;lt;/code&amp;gt;&amp;lt;br /&amp;gt;from the command line when logged in with administrative privileges.&lt;br /&gt;
&lt;br /&gt;
==See also==&lt;br /&gt;
[[Contributions:FieldTripBuffer]], [[Programming_Reference:MatlabFilter]]&lt;br /&gt;
&lt;br /&gt;
[[Category:Filters]][[Category:Signal Processing]][[Category:Matlab]][[Category:Development]]&lt;/div&gt;</summary>
		<author><name>Roostenveld</name></author>
	</entry>
	<entry>
		<id>https://www.bci2000.org/mediawiki/index.php?title=User_Reference:MatlabFilter&amp;diff=3974</id>
		<title>User Reference:MatlabFilter</title>
		<link rel="alternate" type="text/html" href="https://www.bci2000.org/mediawiki/index.php?title=User_Reference:MatlabFilter&amp;diff=3974"/>
		<updated>2008-07-14T12:35:29Z</updated>

		<summary type="html">&lt;p&gt;Roostenveld: moved text over from matlab to matlabfilter&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Synopsis==&lt;br /&gt;
&lt;br /&gt;
The Matlab filter implements an interface between BCI2000 and a standalone Matlab engine. It allows you to implement and/or use Matlab functions for filtering data within the BCI2000 pipeline.&lt;br /&gt;
&lt;br /&gt;
==Parameters==&lt;br /&gt;
&lt;br /&gt;
The Matlab filter does not have a set of default parameters that it uses. Instead, the user-supplied Matlab functions that are executed by the Matlab filter specify the parameters. After initialization, these parameters are displayed in the [[User_Reference:Operator_Module|Operator]] and can be modified there.&lt;br /&gt;
&lt;br /&gt;
==States==&lt;br /&gt;
&lt;br /&gt;
The Matlab filter does not have a set of default states that it uses. Instead, the user-supplied Matlab functions that are executed by the Matlab filter specify the states. &lt;br /&gt;
&lt;br /&gt;
==Detailled explanation==&lt;br /&gt;
&lt;br /&gt;
The Matlab filter does not implement any algorithms itself, but instead allows you to implement the algorithms of your choice in Matlab.&lt;br /&gt;
&lt;br /&gt;
Once BCI2000 is running with the Matlab filter, you will see that a very simple Matlab command line window has openend. In that command line window you can type commands that show you the variables that BCI2000 communicates to the Matlab engine. For example, you can do&lt;br /&gt;
&lt;br /&gt;
  % show the variables&lt;br /&gt;
  whos&lt;br /&gt;
  &lt;br /&gt;
  % plot the first channel of the data (see below)&lt;br /&gt;
  plot(bci_InSignal(1,:))&lt;br /&gt;
  &lt;br /&gt;
  % plot the first channel of the data and continuously update the plot (see below)&lt;br /&gt;
  while(1); plot(bci_InSignal(1,:)); pause(0.01); end&lt;br /&gt;
&lt;br /&gt;
If you try these examples above like this, you will notice that Matlab takes quite a considerable amount of time to open a new figure. While the figure is being openend by Matlab, the engine is blocked , which causes the MatlabFilter to fail writing new data to the engine. Hence the MatlabFilter will return an error and you will have to restart BCI2000. The reason is that the Matlab engine gives you access through the command window, but that the Matlab engine is not capable of executing the commands that it receives from BCI2000 and the commands that it receives from you through the command window simultaneously. &lt;br /&gt;
&lt;br /&gt;
To get the above example to run correctly, you will have to open the figure /prior/ to starting BCI2000 by typing &amp;lt;tt&amp;gt;figure&amp;lt;/tt&amp;gt; (this will open an empty figure). Subsequently, once BCI2000 is running, the content of the figure can be updated. &lt;br /&gt;
&lt;br /&gt;
However, the above example already shows that the Matlab engine is not intended to be used concurrently from the BCI2000 MatlabFilter and from the command window. Instead of typing your own commands in the command window, you will have to give full control of command execution to the MatlabFilter to ensure that timing is controlled. The way that BCI2000 and Matlab interact is that each of the components of a standard filter is mapped onto a corresponding Matlab function. If the Matlab filter is running within BCI2000, it will call the Matlab engine and execute the required Matlab function. The easiest way of getting started with the Matlab filter is by using one of the provided programming tutorials: &lt;br /&gt;
* [[Programming_Tutorial:Implementing_a_Matlab-based_Filter|IIR bandpass filtering, followed with RMS envelope computation, and linear classification]]&lt;br /&gt;
* [[Programming_Tutorial:Implementing_another_Matlab-based_Filter|adaptive spatial filtering using a beamformer, power computation and normalization]]&lt;br /&gt;
&lt;br /&gt;
A detailled description of how to implement your own signal processing functions in Matlab can be found in the [[Programming_Reference:Matlab|Programming reference for the Matlab filter]].&lt;br /&gt;
&lt;br /&gt;
==Troubleshooting:==&lt;br /&gt;
&lt;br /&gt;
===Matlab doesn&#039;t find your functions===&lt;br /&gt;
Make sure to&lt;br /&gt;
*either set Matlab&#039;s working directory to the directory containing your functions, using the &amp;lt;tt&amp;gt;--MatlabWD&amp;lt;/tt&amp;gt; command line option described above,&lt;br /&gt;
*or add the respective directory to your Matlab path.&lt;br /&gt;
Generally, the first option is recommended over the second one.&lt;br /&gt;
&lt;br /&gt;
===There is no Matlab engine started up===&lt;br /&gt;
Execute&amp;lt;br /&amp;gt;&amp;lt;code&amp;gt;matlab /regserver&amp;lt;/code&amp;gt;&amp;lt;br /&amp;gt;from the command line when logged in with administrative privileges.&lt;br /&gt;
&lt;br /&gt;
==See also==&lt;br /&gt;
[[Contributions:FieldTripBuffer]]&lt;br /&gt;
&lt;br /&gt;
[[Category:Filters]][[Category:Signal Processing]][[Category:Matlab]][[Category:Development]]&lt;/div&gt;</summary>
		<author><name>Roostenveld</name></author>
	</entry>
</feed>