Sunday, January 19, 2014

Blinky Lights - Visual Entrainment

In talking up my EEG hacking with some friends, I found a buddy who was really interested.  In particular, he was interested these smartphone apps that claim to affect your sleep state.  My friend wanted to know if these apps actually did anything to the brain.  That's a pretty cool question, and very similar to the question that I had about meditators (see their results here and here).  To figure out if his sleep-modifying apps were doing anything to his brain wave, he volunteered to be my guinea pig.  What a guy!

A Willing Guinea Pig Meets the Red EEG Cap


This post shows some of the data that I collected...though not yet when subject to the sleep app.  I decided to start simple and record how his particular brain responds to sensory entrainment.  Entrainment is how these sleep apps work, so if we understand how he responds to entrainment in general, we'll be well-positioned to understand his response to the sleep apps.  So

Background:  It is my understanding that the sleep apps work by playing specially-constructed sounds into your ears via headphones.  They're trying to induce certain brain rhythms (Delta, Theta, Alpha, Beta, etc) by playing audio into your ears at the same frequency as the desired brain rhythm.  Put most simply, they play a 10 Hz tone into your ears and hope to get brain waves at 10 Hz (ie, Alpha rhythm).  This is called entrainment and is a long-known phenomenon in EEG.  Personally, I'm not too familiar with this type of auditory entrainment, but I do know that visual entrainment, so I'm going to start there.

Setup:  I'm using the same setup as I used for my recordings of meditators.  I used an EEG electrode cap (this is the first time using the red-colored cap, though...exciting!) with the EEG electrode gel that came with the electrode cap kit (ECI Electro-Gel).  We used the same electrode montage (see figures below), the same reference electrode (near FPz/AFz) and the same ground/bias electrode (right mastoid).  For electronics, I used an OpenBCI V1 board with an Arduino streaming data to my PC running our full GUI that was written in Processing.

Baseline, Eyes-Closed Alpha:  Since I had never recorded my friend's EEG before, I decided to start with the most basic recording -- I had him close his eyes so that we could see his Alpha-wave posterior dominant rhythm (PDR).  The spectrograms in the montage below show his response...it is very normal.  Note the energy in the Alpha band (~10 Hz) that shows up most strongly in the back of his head and not at all in the front of his head.  As I said, very normal.

Spectrograms of EEG Signals Recorded With the Eyes Closed.
Notice the Strong (and typical) Energy in the Alpha Frequencies.
Click to Zoom.

In the figure below, I summarize this PDR Alpha response across the eight electrodes.  It shows that his Alpha peaks at about 10.25 Hz.  His Alpha are a bit stronger on the left side of his head (channel 7, green) than on the right (channel 8, blue).  That's also what happens with me.  I've always wondered if this asymmetric Alpha response is related to handedness.  I'm right handed.  I don't know handedness my friend is.  It would be interesting to record a lefty and see what happens!

Average EEG Amplitude Recorded With Eyes Closed and Relaxing.
Notice the Strong Peak in the Alpha Band (~10 Hz).
Finally, the last thing that I'd like to examine with his eyes-closed Alpha data is the spectral coherence of the EEG signals from neighboring electrodes.  This is a quantity that I first analyzed in this post on my second meditator.  It shows how strongly related (how correlated) are the signals between two electrodes.  I use this type of analysis to estimate whether the different physical areas of the brain are working together or independently.

Below are the cross-channel coherence plots for my friend sitting with his eyes closed.  Like with my meditating friend, he shows very little coherence in the front of they head (those areas must be acting independently relative to each other) and more coherence towards the back of the head.  Looking specifically at the Alpha band, it looks like the Alpha seen between electrodes 5 and 7 (ie, back left) are strongly related to each other.  Same with the Alpha seen between electrodes 6 and 8 (ie, back right).  In the very back of the head (7 and 8), the 10 Hz energy is not very coherent between the two hemispheres, even though they are physically closer together that 5/7 or 6/8.  This is so interesting to me.  It is also the same result that we saw with my meditator friend when he was not meditating.

Spectral Coherence Between Neighboring Electrodes.   Strong coherence (red) implies coordinated
EEG activity whereas low coherence (blue) implies independent EEG activity.
Click to Zoom.

Visual Entrainment:  Now we start to do something new.  To see how entrainment works, I started with the easiest sensory entrainment that I know about -- visual entrainment.  The idea here is that you blink a light at a certain speed and you look for brain rhythms at that same frequency.  Truth-be-told, I wasn't actually planning on doing this test, so I didn't have a good light prepared.  But I do have a nice new, really-bright hiking headlamp that has a blink setting.  I don't know exactly what speed it is, but I counted blinks and it's less than 5 Hz.  Sadly, it's blinking rate isn't as steady as I might like.  But, when you're EEG hacking, sometimes you gotta be quick and dirty.

[WARNING!  Be careful doing this kind of test at home!  Blinking lights like this can induce seizures!  Proceed at your own risk!]

To do my visual entrainment test, I darkened the room and had my friend sit in a chair, like before.  I held the blinking light about a foot and a half from his face (see picture below).  We did part of a recording where his eyes were open and looking at the blinking light (so bright!), then he closed his eyes while the blinking continued, then he opened his eyes again.  It turns out that only the eyes-closed portion gave decent results, so that data is what I'm going to focus on.

Attempting Visual Entrainment Using a Blinking LED Hiking Headlamp

If we start with the spectrograms (below, you might want to click on the figure to see it bigger), you'll see that we got a nice line of energy down at the low frequencies (~4 Hz).  The line only appears when both the light was blinking and when his eyes were closed.  Note that it shows up in all EEG channels, but it appears to be a bit stronger on the right side of his head.  These lines in the spectrograms mean that his brain waves were indeed being induced to oscillate at the same rate as the blinking light.  It's a well known effect, but I still think that's kinda cool.

Spectrograms of EEG Signals Recording With Eyes Closed with a Bright Blinking Light.
Click to Zoom.

These spectrograms are summarized in the single spectrum plot below.  It shows a peak at 3.9 Hz, which is most likely the blinking rate of my head lamp.  The amplitude of the entrained waves is quite strong --  note that it is similar in amplitude as the eyes-closed baseline Alpha waves that we recorded earlier.  This graph also confirms that the entrained waves are a bit stronger on the right side (channel 8, blue) versus the left (channel 7, green).  If you remember from above, his baseline eyes-closed alpha waves were the opposite -- they were stronger on the left.  Finally, perhaps most surprising of all is that there are no Alpha waves at all.  Remember, his eyes are closed just like before.  Yet, there are no Alpha waves.  The presence of the blinking light apparently suppresses his natural rhythms (the Alpha) and entrains a rhythm at its own blink rate (the 3.9 Hz signal).

Average EEG Amplitude Recorded With Eyes Closed and A Bright Light Blinking
Notice the Strong Peak at 3.9 Hz (the Blink Rate) and the Absence of Alpha Waves.

Finally, let's look at the spectral coherence across neighboring EEG channels.  The plot below shows strong coherence at these low frequencies (3.9 Hz) across all pairs of channels except for the 1/3 pair (front left) and the 2./4 pair (front right).  Why are these not coherent yet the others are?  I don't know.  The 1/3 pair and the 2/4 pair do have the largest physical spacing of any of the pairs, but I still find it surprising.  I mean, even the cross-hemisphere pairs of electrodes (the 1/2 pair in front and the 7/8 pair in back) show good coherence, but not these 1/3 and 2/4 pairs.  I'm not sure what it means (the front's response is independent of the whole rest of the brain?) but I'll be sure to keep an eye on the 1/3 and 2/4 coherence in the future to see if there is a trend.

Spectral Coherence Between Neighboring Electrodes During the Eyes-Closed Blinking Light Test.
Click to Zoom.

Conclusion:  OK, what have we learned?  We learned that my buddy looks pretty cool in that red EEG cap.  And we learned that his brain is a mysterious place that emanates lots of cool signals.  His willingness to be my guinea pig gave me lots of data from which I have made lots of nerdy graphs.    Here's what I learned from the graphs:

  • His eyes-closed alpha waves are similar to the others that I've measured
    • Similar frequency (~10 Hz)
    • Similar amplitude (~4 uV RMS)
    • Similar spatial distribution across the head (strongest in the back)
    • Similar coherence pattern (back-left and back-right, but not cross-hemisphere)
  • We successfully induced visual entrainment with the blinking light (3.9 Hz)
    • Similar amplitude as the eyes-closed Alpha waves (~4 uV RMS)
    • Entrained brain waves appear all over the head
    • Entrained brain waves are coherent everywhere except front-left and front-right
    • The blinking light suppressed the PDR Alpha response

But what does it all mean?  Does it mean that the sleep-modification app on his smartphone will do anything?  No, this data and analysis does not speak to that question at all.  The goal here was just to help me (us?) learn about sensory EEG entrainment in general, and about my friend's individual EEG response in particular.  Now, that we've done the easy thing and gotten a bit smarter, we can maybe move on toward the harder thing (auditory entrainment) to try to answer the question as to whether the sleep-modification brainwave app is doing anything.  Now I have a better idea of what to look for.

So, thanks for reading.  This is so fun!  (for me at least...)

Next Steps:  In this follow-on post, I use a computer screen instead of a blinky light.  I show that I can entrain brain waves at a variety of speeds.  This is the first step in making an entrainment-based BCI!

Follow-Up:  I used visual entrainment to control a six-legged walker...with my brain waves!

Saturday, January 18, 2014

EEG Electrode Adapter - Version 2

As many people are aware, many commercially-available EEG electrodes use an unusual connector called a "touch-proof" connector.  If your EEG system does not use these connectors, you need to either replace the connectors on the electrodes, or you need to make an adapter.  While it is a sensible choice to cut up your electrodes, I prefer to make an adapter.  My previous version of such an adapter worked really well, but it was a bit fragile.  So, I decided to try again.  I also decided to work with a friend of mine who's a little smarter about these kinds of things.  Here's what we came up with.

My Revised EEG Electrode Adapter ("V2") attached to an OpenBCI Board

Problems with the Previous Version:  The picture below shows my previous version of the adapter.  The good part was that the adapter was really small.  I liked that.  The bad part of this adapter (in addition to the fact that I never finished all of the connections) was that it was quite fragile. Specifically, the soldering of the wire to the female connector had no strain relief, which meant that mating the connector always threatened to break the wiring.  Another problem was that I was always confusing which electrode I had plugged into which input on the OpenBCI board.  I really needed to remake the adapter so that it was easier to see which were the "special" connections (SRB, Bias, and 8P) versus all of the "normal" connections (1N-8N).  These are the deficiencies that I focused on in this revision.

My First Attempt at an EEG Electrode Adapter ("V1")
Revised Approach:  With this iteration, my friend and I decided that it would be best if the female touch-proof connectors were mounted to some sort of rigid frame instead of merely being attached to the ends of the wires.  This would give the strain relief that we needed when mating and de-mating the connectors.  To address my other issue -- getting the connections confused -- I decided to use color-coded connectors, instead of just the black connectors in my first build.  Easy.  Ok, let's go!

Parts:  The parts are all the same as in the first build.  The female touch proof connectors are P/N 36145 from Plastics One.  The ribbon cable is just a set of female-female jumper wires from Adafruit (P/N 266).  This time, we also used a piece of scrap plastic channel that we had lying around, probably originally from McMaster-Carr.  And, as you'll see, I used a thin scrap piece of plastic sheet and a wide piece of shink tube, both from our generally pile of "goodies for a future project".  None of these pieces (except for the female touch-proof connectors) are particularly critical, so use what you have.

Assembly:  My buddy who came up with the idea of using the plastic U-channel as a mounting plate did all of the of the assembly.  He started with drilling a bunch of holes in the U-channel into which he pressed in the female connectors.

Touch-Proof Connectors Mated to the U-Channel -- For Strength!

He then took the purchased wires, pulled off (as a group) the number of wires that we needed and cut the existing connectors off one end.

Inexpensive Jumper Wires Used for My Wire Bundle

To keep the individual wires from splitting themselves off the ribbon, he reinforced the ribbon with a flat piece of plastic and a big piece of shrink tube.  Here's him preparing the items.  The red thing is the big shrink tube and the yellow-ish thing is the scrap bit of flat plastic that he'll use for the reinforcement.


Preparing the Shrink Tube (Red) and Scrap Plastic (Yellow)
to Reinforce the Wire Bundle

With the wire bundle prepared, he soldered the individual wires to the back of the touch proof connectors.  He used some normal size shrink tube to protect the individual solder joints on the back of the connectors.  Very nice.

Connecting the Individual Wires to the Back of the
Touch-Proof Connectors.

And that's all there is to it.  A picture of the completed unit is below.  You can also see it attached to an OpenBCI board at the top.  In the picture below, you can see how reinforcing the ribbon cable was an important feature for keeping the wire bundle from falling apart.

The Finished Adapter.

Pin-Out:  For anyone wondering why I used the unusual color scheme for the connectors, the idea is to clearly indicate that some of the electrodes have different functions.  So, if you use this adapter to connect to the OpenBCI V2 board as shown in the zoom'd picture below, or like the one at the top of this post, the order of the connections is this, from left to right:

   Red: Bias  (aka. driven ground)
   Blue: 8N  (the regular input for channel 8)
   Red: 8P  (the reference input for channel 8, if selected in software)
   Blue: 
       7N  (the regular input for channel 7)
       ...
       1N  (the regular input for channel 1)
   Red: SRB2  (the reference input for all channels)

Further Revisions:  After working with this revised piece for a bit, I found that there is still room for improvement.  For example, in connecting the adapter to the OpenBCI board, it is important to connect the wires in the right order.  The right order is shown below.  At first, I thought it was a good idea that I kept the individual female pin connectors on each wire of the adapter.  I thought that I'd like the freedom and fllexibility that this might provide.  I was wrong.  I should have swapped out the individual female pin connectors for a ganged female connector that would have kept them all in the right order all of the time.  I was wrong.  (So, to the EEG hacker that I'm handing this adapter off to, sorry for the annoyance!).

The Correct Order for the Individual Wires.

So that's the story of the hacking of this electrode adapter.  The real solution, of course, would be to have the connectors on the electrodes and on the EEG system (OpenBCI, in my case) be the same.   The easiest thing would be to put female touch proof connectors on the OpenBCI board.  But, the PCB-mount version are almost $2/each, even in quantity!  This is completely incompatible with the price of the OpenBCI board.  So, the next option would be to buy electrodes with a connector system that we could afford.  That would be a really nice solution to this problem of making kludge-y adapters.

Sunday, January 5, 2014

OpenBCI Driver in BrainBay

Recently, I got an email from Chris, the primary developer behind BrainBay.  He had seen my earlier post, where I'd found a way to get OpenBCI to send data to BrainBay for live visualization and processing.  I was able to make it work by forcing OpenBCI to mimic the data format used by OpenEEG.  Since OpenBCI is more capable than OpenEEG (OpenBCI has more channels and higher bit depth), fitting myself into the OpenEEG format was not an optimal solution.  Well, Chris saw an opportunity to remedy the situation, so he wrote an OpenBCI-specific driver for BrainBay.  Now, OpenBCI can send all 8 channels of data in full resolution to BrainBay!  Thanks, Chris!

Recording my ECG into BrainBay Using  the new OpenBCI-Specific Driver.
I'm holding the electrodes between my fingers.

Below is a screen shot of the BrainBay hardware setup screen using the new OpenBCI driver.  It's pretty straight-forward...select the COM port and select the baud rate for the communication and you're done.

Setup Screen in BrainBay for the New OpenBCI Driver
 Right now the driver assumes that you are using a 250 Hz sample rate (which is what OpenBCI's Arduino software defaults to), but if you tell OpenBCI to run faster, BrainBay lets you change that.  Simply go to BrainBay's "Options" menu and select "Application Settings".  There, you can change the sample rate to however you've configured your OpenBCI board.

Once Chris made this OpenBCI driver, we had to test it out to make sure that it worked.  Since Chris didn't have an OpenBCI board, he couldn't test it himself.  So, he'd point me to his GitHub, I'd download his latest version of BrainBay, and I'd test it for him.  An important part of testing is doing things repeatably.  So, I added a mode to the OpenBCI software where it would output simple test waveforms instead of the real EEG data. (It is true that the ADS1299 has a bunch of test signals built-in, but none let you do different waveforms per channel.  That's what I coded up for OpenBCI.)

Below is a screen shot of BrainBay after we got the OpenBCI driver all debugged.  The synthetic test waveforms that were being output by OpenBCI are the simple ramp waves shown below.  Once we finally got it to look like this, I was so happy!

Confirming Correct Operation in BrainBay via Synthetically Generated Data from My OpenBCI Board.

With basic operation confirmed, I wanted to test it with real biosignals.  So, I did what I always do as my first test...ECG.  As seen in the picture at the beginning of this post, I used a couple of really basic electrodes connected to an OpenBCI V1 board.  To get my ECG, I simply held those basic between by finger tips (I licked my finger tips to increase their conductivity).  Generally, this is a really bad way to do an ECG...the muscle artifact from actively holding the electrodes can swamp your signal.  But, with a very light tough, it can be good enough to prove that that the system is working.  And it was working.  A screen shot of my ECG is shown below.  This is a pretty decent looking ECG trace...it's got a nice little P-wave, a sharp R-wave, and a nice rounded T-wave.  Very fine.

Recording my ECG From OpenBCI Using the New Driver in BrainBay.  Looks good!

So, unlike my previous time posting about BrainBay, this latest recording is all at OpenBCI's native capabilities.  There was no dumbing it down to fit within OpenEEG's data format.  With Chris' latest version of BrainBay, you can now use all 8-channels that OpenBCI generates at OpenBCI's full 24-bit resolution.

Latest BrainBay on GitHub: https://github.com/ChrisVeigl/BrainBay

Thanks, Chris!

Thursday, January 2, 2014

Breathing Meditation - Alpha Coherence

In my previous post, I recorded EEG from a meditator and I saw that meditation seemed to lower the amplitude of his Alpha waves.  I think that it is interesting that I'm continuing to see objectively measurable changes in EEG in (apparent) response to meditating.  I'd like to dig in a little deeper, though.  In this post, I'd like to see how the Alpha waves in different parts of the head/brain relate to each other.  Are the Alpha waves synchronized ("coherent") across the head, or are they generated independently ("incoherent") in the different brain regions?  Also, does meditation have any effect on their synchronization or independence?  That's what I'm analyzing today.

Measuring Coherence:  I'm going to measure  the spectral coherence of the EEG signals to see which signals are synchronized with each other (if any).  Coherence is a comparison of two signals, so I'll be comparing pairs of EEG signals around the head.

Coherence Compares Amplitude and Phase:  Coherence looks at any changes in amplitude and in phase between the two signals.  For two signals whose amplitudes and phases change together, the coherence could reach up to a value of 1.0 (perfect coherence).  Or, if the amplitude and phase changes are completely independent, it is possible for the coherence to reach down to 0.0 (no coherence).

Coherence vs Frequency:  Coherence is computed in the frequency domain, which means that we can see which frequencies are coherent and which are not.  I'm most interested in what happens to this meditator's Alpha waves (which we saw were centered on 11.7 Hz), so I'll be mainly looking at the coherence around that frequency.

Baseline Coherence:  Let's start by looking at the coherence during the meditator's baseline recording.  This is when he was sitting with his eyes closed and relaxing -- but not meditating.  The figure below shows the coherence for different pairs of EEG electrodes around his head.  Blue is low coherence (0.0) and red is high coherence (1.0).  Again, I'm looking at the figures mostly around the Alpha waves (10-12 Hz).

Mean-Squared Coherence For EEG Signals Recorded with Eyes-Closed But Not Meditating.
Click to Enlarge.

Looking at his Alpha wave frequencies, I see that his Alpha are coherent on back-left side of his brain (between electrodes 5 and 7) and on the back-right side of his brain (between electrodes 6 and 8).  I think that it is interesting that the Alpha waves are not coherent between the left and right sides (between electrodes 7 and 8).  These three findings suggest that the back-left region of his brain is working as a unit (at these frequencies), that the back-right region of his brain is working as a unit, but that the left and right Alpha waves are being generated independently.  Again, this is when he is simply relaxing with his eyes closed.

Coherence While Meditating:  Below is a figure showing the results during meditation.  The color scale is the same as for the plots above for the baseline recording.  In this new figure, note the Alpha coherence on the back-right (electrodes 6 and 8) is still present but that the Alpha coherence on the back-left (electrodes 5 and 7) is now gone!  This suggests to me that the meditation has somehow decoupled the brain centers on the back-left of his brain.  Whoa.  Cool.

Mean-Squared Coherence For EEG Signals Recorded During Eyes-Closed Meditation.
Click to Enlarge.

Quantifying the Change in Coherence:  To make this change in coherence more clear, I collapsed these complicated spectrogram-like plots into a simple plot of coherence versus frequency.  The simpler plots are shown below.  This plot only includes data during the eyes-closed portion of each test.  Because we should really only look at the coherence at frequencies where there is appreciable signal energy (and we should ignore other frequencies), I've highlighted the region of the Alpha waves (the only signal that is consistently present during these recordings) by using thicker lines.

Comparing the Average Coherence Just When the Eyes Are Closed.
The thicker lines highlight the Alpha wave frequencies.
On the left, you see the average coherence when he was relaxing but not meditating.  On the right you see the average coherence when he was meditating.  Note that the red trace (the back-left of the head), clearly drops from a coherence of about 0.9 to a coherence of about 0.7.  Very clear.  Also, unnoticed before, the blue trace shows that the coherence between the left and right (electrodes 8 and 7) drops from about 0,7 down to about 0.5.  This plot clearly suggests that the back-left of the brain was acting more independently during meditation.

Discussion:  In the previous post, we saw Alpha waves throughout the back half of his head.  It would be easy to assume that we were seeing the same Alpha wave throughout the back of his head.  Today's analysis has shown that this is not the case.  When merely relaxing, the coherence measurement suggests that the back-left and the back-right parts of his brain are generating their own Alpha rhythms independently of each other.  When meditating, it looks like the back-left part of his brain further subdivides.  Why?  I don't know.  To what effect?  I don't know.  Is it a good thing or bad thing?  I don't know.  All I know is that it is really cool to be able to objectively measure changes in brain activity due to conscious control.

Next Steps:  I'm thinking that I now want to go back and measure the coherence of the signals that we recorded from the meditator at Maker Faire.  I'm pretty sure that my two meditators were using different meditative techniques, so it would be interesting to see if the changes in coherence are the same or different between the two meditators.  It would also be interesting to repeat these recordings to see if the changes are consistent between meditation sessions.  Finally, these coherence results simply show that the signals became more independent.  It doesn't actually tell me which specific properties of the signals (amplitude?  phase?) became different.  It would be cool to see which aspects of the signals changed due to meditation.  The brain sure is a dark and mysterious place!

Follow-Up: The coherence pattern seen above when not meditating has been confirmed in data from another non-meditating friend.