spectrogram in MATLAB, BrainBay, Python / Power Spectral Density

edited April 2015 in Software
how to create power spectral density vs frequency plot of live eeg data in open bci using brain bay.

Comments

  • wjcroftwjcroft Mount Shasta, CA
    edited February 2017
    There is a spectrogram example in the BrainBay neurofeedback tutorial. It's plotting frequency vs. amplitude (in microvolts). By right clicking on the element block you can change the display into several different plot formats.

    If you want power, just square the incoming amplitude value using an expression evaluator element preceding the spectrogram element.

    Unfortunately the calibration of the scales is not always as clear as it could be, but you can eyeball that by simultaneously looking at amplitude or power vs. time on another graph. Some VPL (Visual Programming Language) spectrograms show nice calibration scales on the edges, such as Bioexplorer or BioEra or OpenViBE. I'm sure LabVIEW does this as well. Obviously you can do this in MATLAB, Python also. Chip uses the latter two quite a bit in his EEGHacker blog posts.


  • Thanks, wjcroft :) does brainbay apply the 50hz notch filter? I don't see it anywhere.

    Now, I have three .txt files that are generated by the OpenBCI GUI after 1 minute trials, which I would like to analyze using MATLAB, my questions are,

    1. Is the saved data, raw EEG data (as in, bias and reference neglected and notch not applied)?
    2. How do I create a spectrogram plot and mean spectrum plot using MATLAB as shown here and here
  • wjcroftwjcroft Mount Shasta, CA
    edited April 2015
    OK, sorry, I posted that last comment without thinking too clearly.

    You CAN square voltage (in microvolts) to get power, IF for example you are using an element like MAGNITUDE to get the voltage amplitude in a certain band (e.g. 8-12 hz). Squaring the output of that element would give you the power in that band in uV^2.

    However PSD in general is a more complex calculation, so there are multiple ways to do it, see some links below. The 'simplest' form of PSD (Periodogram) does appear in fact to be the square of the FFT (output). But as shown in the Wikipedia article, it is probably the oldest algorithm in that list.

    https://en.wikipedia.org/wiki/Spectral_density_estimation
    http://www.mathworks.com/help/signal/ref/pwelch.html
    http://stackoverflow.com/questions/27079289/on-the-use-and-understanding-of-pwelch-in-matlab

    So the short answer is no, PSD cannot be done inside BrainBay. However the FFT/Spectrogram block is what most neurofeedback practitioners use; and is quite suitable for most tasks. If you indeed want PSD, that Welch estimate in MATLAB looks like what many recommend.

  • wjcroftwjcroft Mount Shasta, CA
    Bajji, hi.

    I'm going to mention @chipaudette here in hopes he will address your EEGHacker spectrogram question.

    re: BrainBay notch filter, you are correct. There is no default notch. I usually suggest you put a bandpass filter on each input channel, as the tutorial shows. (.5 to 40 hz). That eliminates both the DC offset and the mains freq.

  • wjcroftwjcroft Mount Shasta, CA
    Forgot your other question, yes the .txt file is the raw unfiltered data. It is referenced and the Bias signal has reduced mains noise somewhat. But you will need to filter the raw data to remove DC offset and any remaining mains.

  • Thanks wjcroft for your prompt and detailed answers :) but you haven't touched on my other question as to how to generate a spectrogram using Matlab. Googling "eeghacker spectrogram" takes me to his blog where @chipaudette describes the generation of a spectrogram using tools other than matlab. I am basically trying to detect mu waves. I'm pretty sure i saw a significant drop in the alpha band and want to make sure by using the spectrogram.
  • I tried to create spectrogram in matlab of the openbci data i recorded. I tried using [S,F,T]=spectrogram(x,window,noverlap,F,fs)  function but i could'nt figure out the correct values for these parameters. what values should I pass for window , noverlap and F. (please forgive my ignorance i am just a newbie).

  • wjcroftwjcroft Mount Shasta, CA
    In his blog posts @chipaudette has mentioned both his use of MATLAB and Python to present graphical results. Have you checked his Github?  https://github.com/chipaudette

    I can't help you with the spectrogram code question, as this is something only Chip can answer; it's his code.
  • wjcroftwjcroft Mount Shasta, CA
    @user93 , hi. I merged your question into this existing thread on MATLAB spectrograms / PSD. @chipaudette has a number of these examples on his EEGHacker blog. I'm hoping he'll comment here.
  • wjcroftwjcroft Mount Shasta, CA
    edited April 2015
    For those still interested in Power Spectral Density, EEGLAB has a builtin function called 'spectopo' that does this. It's based on the 'pwelch' function inside MATLAB. There are a number of relevant links pulled up by this search:


    You should be able to use @jfrey 's LabStreamingLayer plugin for Python, to live stream into EEGLAB / BCILAB / MATLAB.


  • edited April 2015
    Thanks wjcroft I figured out how to do this in python after reading through chips code .I'll be shifting to python.
Sign In or Register to comment.