edan

some projects

Shell Script for Zooming In to the EEG Data

leave a comment »

To make things a bit easier, I wrote the following shell script.

Copy and paste it to a plain text file, save that as plot-zoom.sh.

Put it in the folder that has your LOGGERnn.CSV files.

Now open up a command line prompt (Terminal, on a Mac), use cd to navigate to the directory that has the LOGGERnn.CSV files and plot-zoom.sh, and run the script with:

./plot-zoom.sh LOGGER00.CSV

(if you want to use e.g LOGGER01.CSV, then replace the file name with that.)

Here’s the script:
(if you want to see it with pretty formatting, check out the copy on Pastebin.


#! /bin/sh
echo "\n************ \n"
echo "Plots EEG data for a zoomed-in file region\n"
echo "This script expects to be run from within a folder\n containing the LOGGERNN.CSV file from the Arduino."
echo "It expects the pathname of the LOGGERNN.CSV\n file as a command line argument, e.g:"
echo "\t plot.sh LOGGER09.CSV \n"
echo "************ \n"

START_MINUTES=00
START_MSEC=00
LENGTH_MINUTES=00
LENGTH_MSEC=00

FILE=$1
echo "Using data from" $FILE


# ask for the offset
echo "\n"
head -n 2 "$FILE"
read -p "What's the offset? (millis value above) " OFFSET
# for testing:
#OFFSET=222
echo "\n"



read -p "How many minutes into the file does the selection start? (msec comes next) " START_MINUTES

read -p "How many milliseconds on top of that? " START_MSEC

read -p "How many minutes does the selection last? " LENGTH_MINUTES

read -p "How many milliseconds on top of that? " LENGTH_MSEC



# calculate starting offset

# for testing
 # START_MINUTES=33;START_MSEC=44;OFFSET=55;LENGTH_MINUTES=66;LENGTH_MSEC=77;

START_OFFSET=$(( ($START_MINUTES * 60 * 1000) + $START_MSEC + $OFFSET))

echo "Using start offset" $START_OFFSET "msec"

# calculate ending offset
END_OFFSET=$(( ($LENGTH_MINUTES  * 60 * 1000) + $LENGTH_MSEC + $START_OFFSET))

echo "Using end offset" $END_OFFSET "msec"

XSIZE=15
YSIZE=4

# testing

OUTFILE="$FILE-${START_MINUTES}m${START_MSEC}msec_for_${LENGTH_MINUTES}m${LENGTH_MSEC}msec.pdf"

echo "Writing output to" $OUTFILE

gnuplot -e "file=file='$FILE'; set datafile separator \",\"; set key autotitle columnhead; set yrange [0:1000000]; set y2range [0:100]; set parametric; set label 1 \"File $FILE starting at $START_MINUTES min $START_MSEC msec for duration $LENGTH_MINUTES min $LENGTH_MSEC msec\" at graph 0.5,1.02 center;set xrange [$START_OFFSET:$END_OFFSET]; set terminal pdfcairo color size $XSIZE,$YSIZE; set output '$OUTFILE'; plot for [i=6:13] file using 1:i with lines smooth bezier, file using 1:3 with lines axis x1y2 smooth csplines"

Written by 333dan

June 9, 2012 at 9:53 pm

Posted in Uncategorized

Zooming In: How to Look at a Specific Range of Time on the EEG Graph

leave a comment »

Quick into to loooking at a particular bit of the dataset using gnuplot… Let’s consider a particular point of interest. (say, someone turning on a light while the subject was sleeping)
The point of interest in this case starts 19655000 msec into the recording, more or less.

Here’s how to calculate that, by the way, if it had happened, say, 3 hours 45 minutes 7 seconds into the recording:

(3 hours * 60min/hr * 60sec/min * 1000msec/sec) + (45 minutes * 60 sec/min * 1000msec/sec) + (7 seconds * 1000msec/sec)

What’s the duration of the point of interest? About 19min, in my case.

That’s a length of (19min * 60 sec/min * 1000 msec/sec) = 1140000 msec

So the ending offset is 19655000 msec + 1140000 msec = 20795000 msec

Ok, what options does gnuplot give us for finding those offsets?

Try xrange… add

set xrange [19655000:20795000]

to the gpl file and call it ‘eeg-demo-xrange-test.gpl’

fire up gnuplot…

gnuplot -e “file=’LOGGER00.CSV'” eeg-demo-xrange-test.gpl

Hey neat! clear change in delta… but this sucks, having to hand-edit xrange every time… try editing the gpl to make it take xstart= and xend= on the command line

<gpl file start>

set datafile separator “,”
set key autotitle columnhead
set autoscale x
set yrange [0:1500000]
set xrange [xstart:xend]
set y2range [0:100]
set parametric

plot for [i=6:13] file using 1:i with lines smooth bezier, file using 1:3 with lines axis x1y2 smooth csplines #, for [i=4:5] file using 1:i with lines lw 2 axis x1y2 smooth bezier

<gpl file ends>

try that…

gnuplot -e “file=’LOGGER00.CSV'; xstart=19655000; xend=20795000″ eeg-demo-xrange-test.gpl

Sweet…

But what I really want is to look at the point of interest in context, with say 19 minutes before and after as well… so subtract 19 minutes from the beginning and add 19 minutes to the ending offset:
19655000 msec – 1140000 msec = 18515000
20795000 msec + 1140000 msec = 21935000

so feed it the new offsets on the command line

gnuplot -e “file=’LOGGER00.CSV'; xstart=18515000; xend=21935000″ /Volumes/DOCS/00-Working-Text-Files/eeg/eeg-demo-xrange-test.gpl

There we go!
I wish there was a smoothing algorithm in between bezier (too smooth) and csplines (not smooth enough).

Written by 333dan

June 7, 2012 at 9:35 pm

Posted in Uncategorized

DIY Sleep Monitor / Logging EEG

leave a comment »

Figure 1. First try with Arduino sleep monitor. In the middle of the graph you can see when the MindFlex headset fell off (!) during the night, and when I put it back on. (Note the signal quality goes from 'fantastic' (near 0) to 'no signal' (200) at that point.)

 

Building blocks of this project:

http://arduino.cc/en/Main/ArduinoBoardUno

Adafruit Data Logging Shield

Mattel MindFlex EEG toy headset

I based most of my work on Eric Mika’s “How to Hack Toy EEGs,” including using his Arduino Brain library to interpret the data from the MindFlex:

http://frontiernerds.com/brain-hack

Arduino Brain Library (required if you want to duplicate this)

(Wire from the T pin on the NeuroSky board to the Arduino RX pin)

I did make one adjustment — I put a 6N138 optoisolator between the MindFlex headset and the Arduino. This means that not only is there no question of noise/ground loops from the Arduino messing with the EEG signal, but there’s also no longer the chance of frying my brain if I plug the Arduino into a wall-powered laptop.

6N138 connections are as follows:

Mindflex V+ to 6N138 LED anode (pin 2)
Mindflex T pin to 470 ohm resistor to 6N138 LED cathode (pin 3)
Arduino 5V to 6N138 Vcc (pin 8)
1K resistor between 6N138 Vb (pin 7) and GND (pin 5)
Arduino RX pin to 6N138 Vo (pin 6)
Arduino GND to 6N138 GND (pin 5)

After a lot of messing about with the demo code for the Data Logger Shield, I found an incompatibility between the Real Time Clock libraries and the Brain library. So I left out the RTC. Here’s the resulting Arduino sketch: http://pastebin.com/Ye43V8ca

That creates a LOGGERnn.CSV file on the SD card, with all the values from the EEG.

As a test, I used the Arduino/MindFlex as a sleep monitor last night, producing a 3.4MB CSV data file spanning ~9 hours of sleep.

To generate a graph from the CSV file with gnuplot, I created a command file <eeg.gpl> containing the following instructions: (remove the # to graph attention/meditation values, I found them superfluous)

set datafile separator “,”
set key autotitle columnhead
set autoscale x
set yrange [0:1500000]
set y2range [0:100]
set parametric

plot for [i=6:13] file using 1:i with lines smooth bezier, file using 1:3 with lines axis x1y2 smooth csplines #, for [i=4:5] file using 1:i with lines lw 2 axis x1y2 smooth bezier

I then called gnuplot from the command line with the following command:

gnuplot -e “file=’/Path/to/LOGGER00.CSV'” /Path/to/eeg.gpl

The resulting graph (generated through AquaTerm, since I’m on a Mac), looks like this:

Image

In the middle of the graph you can see when the MindFlex headset fell off (!) during the night, and when I put it back on.

Written by 333dan

May 27, 2012 at 7:33 pm

Posted in Uncategorized

Follow

Get every new post delivered to your Inbox.