Jump to content

Talk:Technical Reference:State Definition

From BCI2000 Wiki
The printable version is no longer supported and may have rendering errors. Please update your browser bookmarks and please use the default browser print function instead.

State Vector

The section on state vectors is totally wrong. After reverse-engineering some data files with the help of the BCI2000Viewer, it appears that the state vectors are actually encoded in reverse-big-endian. In other words, if smaller numbers represent lower order bits, then the state vector layout mentioned in this section would actually be encoded as follows:

State Vector Byte 1 State Vector Byte 2 State Vector Byte 3
0 1 2 3 4 5 6 7 0 1 2 3 4 5 6 7 0 1 2 3 4 5 6 7
Running SourceTime unused
0 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15  

Which on the physical machine actually maps to:

State Vector Byte 1 State Vector Byte 2 State Vector Byte 3
7 6 5 4 3 2 1 0 7 6 5 4 3 2 1 0 7 6 5 4 3 2 1 0
SourceTime Running SourceTime unused SourceTime
6 5 4 3 2 1 0 0 14 13 12 11 10 9 8 7   15

Fixed section on state vector encoding

Thanks for pointing out the error. State vector encoding is actually little-endian (LSB first) rather than big-endian, and its layout on a little-endian machine is straightforward as a concatenation of little-endian encoded state values. I fixed the description accordingly.

Pseudocode reference

Do you think it would be helpful to readers to put (in pseudocode) a way to read the State Definition?

int ExtractStateValue(unsigned char[] vector, int byteoffset, int bitoffset, int numberofbits){

    int numberofbytes = numberofbits / 8 + 1;
    char bytes[numbytes] = vector[ byteoffset : byteoffset + numbytes ];

    int ret = bytes[0] >> bitoffset;
    for ( int i=1; i<numbytes; i++ ){
        ret += bytes[i] << ( 8 * i - bitoffset );
    }

    return ret % ( 1 << numbits );
}

This is not compilable C or C++ code, as far as I know. However, I think it might make things clearer.

Cstocks 21:15, 4 January 2010 (UTC)

Actual C++ code

The actual C++ code is here: http://www.bci2000.org/tracproj/browser/trunk/src/shared/types/StateVectorSample.cpp#L69 (log into trac if you get a message about browser view privileges)

Mellinger 15:00, 5 January 2010 (UTC)

Pseudocode

Thanks for pointing that out, but to someone (like me) who is not terribly familiar with C++ code, seeing the actual code is not very helpful. For example, I would have to start hunting through the code to find out what State::ValueType, mByteLength, mpData, et cetera mean.

Also, I am proud to note that my algorithm is at least 8 times more efficient than the C++ function you pointed me to.