Monday, September 30, 2013

A journey with brain wave experiments - the beginning

Access raw EEG data from Mindwave Mobile

I ordered a Mindwave mobile from Amazon instead of Neurosky because I already have an Amazon Prime account and it would be much easier to return to Amazon if I don't like it.  The device arrived in two days as expected.  Have been playing with it for a few days, now I can read the raw EEG data at 512 samples/sec and observe how my brainwave changes when I blink my eyes.  Below is a screen snap shot of the raw EEG data with two eye blinks, a partial one in the beginning, and another one in the middle.  The home brew program will keep rolling the real time plot to the left as new data arrives.

Overall the Neurosky Mindwave Mobile is reasonable and price is ok. But I find the device a bit challenge to wear.  Not only it interferes with my glasses, but also my ear lobe hurts in just a few minutes. Will have to address these later.  During the exercise, there are quite a few things need to be ironed out. This blog is essentially the lab log. Shall it be called lablog, or la-blog?

Details

C-compiler


My programming machine is now a MacBook Pro with OS X 10.8.5. I still love Linux and can use a cheaper notebook for the development. But since I received this nice machine from my wife, so it's time  to try Unix on Mac to see how well it runs Linux tools, so here I am.

First signed up as a Mac developer for free. This is required to get the developer tools. Installed Xcode (Ver 5.0) and its command line tools from Apple. The Xcode package is about 1.5GB and took a while to download.

After install these, the following is needed if you want to compile the code in old fashioned command line.  I put them in ~/.bash_profile.

DEVELOPER_DIR="/Applications/Xcode.app/Contents/Developer/"  
export DEVELOPER_DIR


Plotting Tool


I need a graphic tool to plot the EEG waveform. Look around, there are so many options that I got lost.  Since I plan to recreate my Linux environment, I looked for gnuplot first, and downloaded it.  While poking around gnu plot for Mac, I came across Aquaterm, so that was installed as well.  After Aquaterm v1.1.1 was installed, there are three demo programs, and all ran fine. After seeing the demo code, I decided to use it as my graphic engine.  A simple code that will plot input data, and keep rolling the data until seeing an end indicator was done in about two hours. This was done with the builtin emacs and command line gcc.

One thing to note here is that aqtClosePlot used in the demo code will clear all color map, as well as create unnecessary flashes during multiple rendering. To work around that, aqtEraseRect was used, which works fine and smoothly.

Read Ones Mind


Starting with Neurosky's example code, I got the EEG raw data to print on screen fairly quickly.  This requires me to learn a little bit of Xcode environment. I have to say it is quite well done, and is a joy to use. After using Xcode, I started to dream about writing an IOS app ;-)

A few things to note:
  • On my Mac, the Bluetooth slave is mounted at /dev/tty.MindWaveMobile-DevA 
    This is different from the document.
  • The idea of "bundle" is new to me. And the Neurosky documents are for older Xcode. It took me a while to figure out how to add the "CoreFoundation Framework".  In Xcode 5, this can be  done at "TARGETS" of the project, under "Build Phases" tab, in the "Link Binary with Library" group.
  • The Neurosky ThinkGear bundle is 32-b, need to change the compiler option. In Xcode, this is under "build settings" tab, "Architecture" group. 

Put Things Together


The rolling EEG plot came smoothly after the above two.  The Mindwave reader output the raw EEG data to stdout, and the other info to stderr.  This is piped to the EEG viewer, which plots the data. Of course, a few tweaking are required.
  • The raw EEG data rate was is only ~50 points per second, not the stated 512 samples/sec.  After a few poking, one has to change the -1 parameter to +1
    • numPackets = TG_ReadPackets(connectionID, 1);
That's it for now. I plan to polish the codes a bit before publish them.