EEG BinaryPacket readings decoded using R

By Elliot Noma

The mindwave mobile produced by Neurosky allows one to monitor brain processes over a bluetooth connection. Neurosky publishes manuals on the use, communications protocol and data analysis of the outputs. In this post I describe how I extract the signal from the socket interface using R.

A new record can be retrieved from the socket at one second intervals. If the signal is sufficiently strong, the record is 42 bytes long starting with two sync characters. The record contains three types of information

  • the signal strength from 0 to 200
  • the attention and meditation levels 0 to 100
  • 8 waves from 0 to 2^{22}

1. The socket is initialized using the commands

require(svSocket)

startSocketServer(port = 13854, server.name = “Rserver”, procfun = processSocket, secure = FALSE)
con <- socketConnection(host = “localhost”, port = 13854, blocking = FALSE, open=”rb”)

2. individual bytes are read from the socket using

try(readBin(con, “raw”), silent=TRUE)

3. records are identified by blocks that are started by the two byte sequence

aa aa

4. successfully read records identify the information using the following coding scheme starting with the third byte of the record:

02 indicates the following byte is the signal strength with 0 indicating a strong connection and 200 (byte code c8) indicating a broken connection. Use the as.integer() function to convert from raw to integer format.

04 indicates the following byte is the attention level from 0 to 100 (byte code 64)

05 indicates the following byte is the meditation level from 0 to 100

81 signals the start of the waves fields. The next byte is the number of bytes allocated to the waves. In all cases I have observed there are 32 bytes allocated to the 8 wave levels. The waves are described as 4 byte floating points values To convert the 4 bytes designed x[1:4] to a floating point number, I used the following code

con1 <- rawConnection(raw(0), “r+”)
sapply(1:8 – 1, function(x) writeBin(inbytes[4 * x + 4:1], con1))
seek(con1,0)
float <- sapply(1:8, function(x) readBin(con1, “numeric”, size=4))
close(con1)

All successfully received records I have seen order the information as follows

aa aa 02 <byte> 81 20 <32 bytes> 04 <byte> 05 <byte>

for a total of 42 bytes per record.

Advertisements

Leave a Reply

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

WordPress.com Logo

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

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s

%d bloggers like this: