Sunday, December 22, 2013

Live Spectrograms in Processing!

For any regular readers of this blog, you know that I really like to visualize EEG data using spectrograms.  Unfortunately, when using OpenBCI, none of our software has a spectrogram display.  As a result, all my spectrograms are made after the tests are done.  While this is OK, it's not as good as having a real-time spectrogram.  So, I finally took the time to modify the OpenBCI GUI to include a live spectrogram.

As an example of how useful a spectrogram can be, look at the data below.  The picture is a screenshot from my newly-modified OpenBCI Processing GUI.  It shows EEG data recorded from the back of my head (O1-Fz).  My eyes were closed for the entire recording.  The screenshot was taken live (in the OpenBCI GUIs, press "m" to take a screenshot)...this is what the GUI was showing while the data was actually being recorded.

Screenshot from the OpenBCI "Simpler" Processing GUI.
Note the newly-added spectrogram plot.
I was recording Alpha waves from the back of my head (O1-Fz).

What does this screenshot show?  It shows alpha waves.  Specifically, because my eyes were closed and because the electrode was on the back of my head, it is showing my posterior dominant rhythm.  Based on the traditional spectrum plot on the left, it appears that my alpha waves are ~9 Hz in this recording.  Looks fine.

Now look at the spectrogram on the right.  it shows the same alpha wave signal, but now you can also see that the amplitude of my alpha increases and decreases through time (the color oscillates between red and light blue).  It is not a steady signal.  I find this very interesting.  The ability to see in both time and in frequency *at the same time* is the beauty of a spectrogram.

As another example, the screenshot below shows some Mu waves.  Here, I put an electrode on the left side of my head (C3) and tried to relax enough to make some Mu waves.  Mu waves are really difficult for me. I cannot get long sustained Mu waves like I can do for Alpha waves.  Instead, I can only seem to get 2-3 second long bits of Mu wave (as seen below).

A short segment of Mu waves recorded from the side of my head (C3-Fz)

With the regular spectrum plot on the left, it is hard to tell how long my Mu waves are sustained.  Now that I have the spectrogram, I can better see how long I'm sustaining my Mu waves, which means that I have better feedback for practicing making Mu waves.  In this way, you could say that it's a tool for neurofeedback.

Since I think that spectrograms are so useful, I've pushed this modified version of the "Simpler" Processing GUI up to the OpenBCI GitHub (link below).  Note that the spectrogram is only in the "Simpler" version of the Processing GUI.  The "Simpler" Processing GUI is designed to visualize just one or two channels of EEG data instead of all 8 channels that OpenBCI allows.

OpenBCI Processing GitHub:

Being limited to one or two channels means that this GUI might also be useful for OpenEEG (which is a two channel device).  All I have to do is alter the routine that interprets the data from the OpenBCI board so that it instead knows how to interpret the data format used by OpenEEG.  That should be pretty easy.  Look for a future update!


  1. Interesting. Intuitively, I would think expect that the spectograms on the right would yield much clearer peaks on the left... it seems like the left plot has a similar SNR to a single vertical "slice" of the spectogram. Averaging across all of those slices, I'd expect the peak to be much higher relative to the noise.... of course, log plots and jet color maps make it hard to analyze... :)

    I assume that the spectrograms show PSD. is there some measure of "phase lock-in" for alpha- and mu-waves that would not be present in background electrical noise? e.g., if we had a plot of phase next to the spectrogram, would it vary linearly for the alpha waves, and randomly for the rest? If so, are you taking that into account (e.g., by FFTing the entire timeseries at once instead of just summing the PSDs in the spectrogram)? Or, perhaps I have my math wrong...

    1. Hey Rob,

      The traditional spectrum plot on the left has some time-smoothing built-in...precisely to enhance the ability to see EEG signals like Alpha and mu waves. For that purpose, the time-smoothing works great. Unfortunately, it also reduces the ability to see fine amplitude modulations that occur in time. That's the part that I like about these spectrograms...they expose the time-based modulations.

      Your ideas about looking at the phase behavior are sound. The difficulty with looking for phase "lock-in" is that the phase from an FFT is only constant if the true frequency of the signal is perfectly aligned with the center frequency of the FFT bin. If the true frequency is off a little, then the phase from the FFT starts wandering. If the true frequency is totally stable, just not at the center frequency, the FFT phase *rate* will be constant...which is something that we could exploit.

      To look for a consistent phase rate, you'll have to look at the first derivative of phase from FFT block to FFT block. I've done that before, and it is useful in some circumstances. Maybe it would be useful in this one, too. It does require quite a stable signal frequency, though, and I'm not sure that EEG signals are that stable.

      Perhaps a better approach would be to use tow (or more) sensing electrodes spaced around the back of the head. Because eyes-closed alpha waves are widely distributed and because they are indicative of wide areas of your brain getting in sync, the alpha waves measured by the different electrodes should be exactly the same frequency in all electrodes. Depending on what is the source of the EEG "noise", the noise may or may not be the same in all electrodes. We can exploit this difference, and hopefully enhance just the Alpha waves, by looking for just those signals that are coherent between the two electrodes. This should cause the Alpha waves to "pop" right out.

      But, to do that, I'd need to put more electrodes on my head. And that EEG paste is icky. I guess that I'll just have to man up....for SCIENCE!


  2. Robert Thatcher at ANI has a number of papers online on his Phase Shift, Phase Lock, Phase Reset measurement technologies. His current Z-score neurofeedback based on his normative database actually has norms for these metrics. And thus can include them as part of the Z-score training. See:

  3. I utilize the spectrograms with almost all of my neurofeedback work. And the format that Chip is using here is generally the most popular. (Two dimensional JTFA Joint Time Frequency Analysis.) National Instruments has an extensive toolbox in this area:

    Here's question for us brainiacs: why do our EEG spectrograms frequently have areas that look like "swiss cheese", with the dark blue 'holes' surrounded by the light green 'cheese'? :-) This is very familiar to me, yet I think it is also demonstrating some deep operation characteristics of the frequency level communications used by brain. Certain bands of information transfer are "open" or active at any given moment in time and thus light up on the spectrogram. While other frequency domains stay dark and unused in those times.

    Why these inactive areas show this round hole shape, however, is still a mystery to me. It seems to imply that holes represent frequency domains that drop out and expand over time. Then "come back online" and fill in moments later. Curiouser and curiouser, said Alice.

    Is this spectrogram behavior seen in other fields besides EEG? I bet Jay Gunkleman would have an answer to this. I might ask him.

    1. I work with spectrograms in many domains outside of EEG. I feel that I see this "swiss cheese" behavior everywhere that one has noisy signals. If you were to synthesize some noise (Gaussian white noise or whatever) and run it through a spectrogram, I think that you'd find this same kind of behavior.

      The particular size of the holes will have to do with the length of the FFT combined with the amount of overlap between successive FFTs. I've got quite a lot of overlap, so the "holes" are very smoothly rounded. If I had less overlap (or no overlap), the transition between "cheese" and "holes" would be very sharp and would visually remind you more of speckle or static, which would perhaps reinforce the interpretation that it is indeed a reflection of the underlying noisy activity.

      With that said, the "noise" that we're seeing can still be legitimate brain activity...I'm not implying that it is necessarily electronic noise. It could be legitimate brain activity because EEG can really only see the coordinated activity of relatively large sections of your cortex. There's no reason, though, that small regions of your brain couldn't be acting independently. When all of these little contributions are measured together at a given EEG location, it would be difficult to discern any structure in the overall signal, so it would appear as noise. It would still be legitimate brain activity despite having all the properties of noise.

      So, with the "swiss cheese" possibly reflecting legitimate brain activity, I will now keep a sharper eye in future recordings to see if the cheese exhibits different properties for different folks or for a given person under different test conditions.

  4. Some interesting spectrogram examples comparing STFT Short Time Fourier Transform spectrograms to Gabor spectrograms. It's possible that the shape of our swiss cheese 'holes' are related to the transforms being used. In other words, some smearing of the image is due to the FFT windowing being done.



    Of course these fancier spectrograms are MUCH more compute intensive, so are rarely used in EEG work. But Val Brown does use them in his NeurOptimal software (Zengar).

    1. Thanks to the pointers. I have used Wigner-Ville Distribution (WVD) many times before in an attempt to get a higher-resolution time-frequency visualization. Unfortunately, unless the signal being analyzed has only a single component, I'm always disappointed by the results due to the cross-terms that result. The cross-terms are artifacts that do not reflect actual components that are in the signal. I hate those things. Even after applying smoothing techniques (like Gabor), I always end up going back to the STFT.

      STFT has visual artifacts, too. For example, really strong narrowband signals will appear to have sidebands, even when they're pure. These are minimized by using a windowing function such as Hanning. STFT will also have time-smearing that make really sharp events appear to start and end more gradually. Both of these artifacts are serious. It's only because I'm more familiar and comfortable with them that I prefer the STFT over the WVD (and Gabor). I guess that I just prefer the enemy that I know.

      But, back to your first point...yes, I agree that the swiss cheese behavior results from the transform being used. For the STFT, I believe that it is the length of the FFT versus the amount of overlap between FFT blocks that controls the appearance of the holes in the swiss cheese.

  5. Hello Sir, I am also researching in looking at the EEG data, there is one question that is arising- Is spectrogram the best choice for analyzing eeg signals?

  6. Hello. I am a college student and I need spectrogram for one of my projects. I downloaded the code from GitHub and I am running processing 2.2.1. I am getting error when trying to execute the code. It says 'Cannot find anything named "sendToSpectrogram"'. Can you help me please?
    Thank You