Page 1 of 1

How to read and write Matlab data in MatlabFilter.cpp?

Posted: 25 Sep 2011, 09:35
by august
Hi,

I'm a newby in BCI2000, I'm using Matlab signal processing and it was work out well. But I still have a problem that I want to record data after Matlab operation and I found that I would save it into text file at MatlabFilter.cpp. And my code go like these

void
MatlabFilter::Process( const GenericSignal& Input, GenericSignal& Output )
{
StatesToMatlabWS();
MatlabEngine::PutMatrix( IN_SIGNAL, Input );

if( CallMatlab( *mpBci_Process ) )
Output = MatlabEngine::GetMatrix( OUT_SIGNAL );
else
Output = Input;
myfile << Output;
MatlabWSToStates();
}

This code resulted this .txt file below

SignalProperties { % 1 128 float32 0 1 % 0 0 0 1 % 0 127 {0 1 % -1 1 }
}
3.092456
3.071291
3.008183
2.904283
2.761489
2.582401
2.370271
2.128939
1.862758
1.576502
1.275275
0.9644084
0.649355
0.3355827
0.02846679
-0.2668147
-0.5453811
-0.8027421
....down to 128 rows from 128Hz

But what I really want is all data from all 14 channel ( I use Emotiv EPOC ) not only one channel. So would anyone please help me to figure it out? Thank you for your help in this warm social environment :)

Best regard,
Aug

Re: How to read and write Matlab data in MatlabFilter.cpp?

Posted: 26 Sep 2011, 08:13
by mellinger
Hi,

you don't need to save data after individual processing steps for offline analysis. You can always re-compute these data from the BCI2000 data file by running the command-line versions of BCI2000 filters over the data, including the Matlab filter. This can be done conveniently using the bci2000chain command from the BCI2000/tools/matlab directory (it comes with an instructive example).

Independently of that, you can of course write data out the way you do it. In your case, there is only one channel in the output signal because your bci_Preflight() Matlab function requested this for the output signal. When your Matlab processing in bci_Process() results in only a single output channel, then you cannot expect the Matlab filter's output signal to have more channels than that.

If you want access to the raw data recorded from your headset, then use the load_bcidat mex file to read the BCI2000 data file into Matlab.

Juergen

Re: How to read and write Matlab data in MatlabFilter.cpp?

Posted: 15 Oct 2011, 04:57
by august
Dear Juergen,

Thank you very much for your help as always. I have some other question to bother you, as there is only one out_signal variable from MATLAB, how could I receive more than one, let's say three.
I have three difference bandpass filter data from MATLAB, how do I pass these all three variable to MatlabFilter.ccp?

Best Regard,
Aug

Re: How to read and write Matlab data in MatlabFilter.cpp?

Posted: 15 Oct 2011, 10:40
by mellinger
Hi August,

bci_Process() has only one output variable because filters in BCI2000 operate in form of a pipe. Each filter has one input signal, and one output signal. Each filter's input signal is the previous filter's output signal. I'm not sure why you want to have more than one output signal, and how you would further process those signals. When your Matlab code produces output from three bandpass filters, and you want to process all those output signals within BCI2000, you should use a single output signal that has three times as many channels, so it can hold all the data produced by Matlab's filtering.

If you want to split up BCI2000's processing into multiple filters operating in parallel, check the parallel and serial combinations filter template at
http://www.bci2000.org/tracproj/browser ... bination.h
The comment in the header file explains how to use these filter templates to split up processing into parallel streams. For completeness, there is an Identity filter at
http://www.bci2000.org/tracproj/browser ... tyFilter.h
that makes it possible to have both processed and unprocessed data in the output of a parallel filter combination.

If you want to do something else with the output of the Matlab filter, and if you want to do that within a modified MatlabFilter code, then I wonder why you would want to modify the MatlabFilter at all, rather than just doing that "something else" from within bci_Process() in Matlab code? E.g., writing out data into a file is just as easy to do in Matlab as it is in C++, and when doing that from your Matlab code, you would have all your code in one place rather than having to modify MatlabFilter code (and needing to recompile it each time you change something).

Regards,
Juergen