FFT the raw data using Javascript and NodeJS
in OpenBCI_GUI
Hi!
I am trying to implement neurofeedback using OpenBCI, Javascript, and NodeJS (Before, it was matlab, but things didn't work out well). I successfully streamed the raw data (it's in microvolts) and the values make sense as they correspond to my eye blinks. Now, I need to FFT this raw data to get the peak at certain frequency. I tried to look for Javascript libraries, but didn't really understand how I can feed my data to those libraries. I might have to implement it from scratch? I'd appreciate it if anyone could help me how to FFT this raw data.
Comments
By the way, my sampling rate is 250 and I believe this is necessary for FFT.
Have you seen https://github.com/corbanbrook/dsp.js ? The docs say:
FFT(bufferSize, sampleRate): Fast Fourier Transform
Usage would be for OBCI:
var fft = new FFT(2500, 250); <---- 2500 data points at 250 Hz, so 10 sec data
fft.forward(signal); <--- signal is 2500 data points from 1 channel
var spectrum = fft.spectrum;
Bill, thanks for that link.
Gerard, have you seen Alex Castillo's EEG-Pipes?
https://github.com/neurosity/eeg-pipes
Regards, William
Thanks, Bill. I will check this library. Will this be applicable for real-time analysis as well? I am new to BCI and I can't quite understand how I can feed certain data points to the function.
Thanks, William, for the suggestion. I first used EEG-Pipes to directly access the alpha wave band. However, this band is basically average of all the peaks between 8Hz to 13Hz. I need to see in what frequency the peak is occurring. It seems to me that EEG-Pipes does not have a function for that. In case I am missing something, am I correct on this? If you know any other libraries, please let me know!
Thanks again, @Billh and @wjcroft
re: "peak alpha frequency"
PAF is the name for the peak frequency component in the alpha band. My suggestion would be to run an FFT with "1 Hz" bins, so you would end up with amplitude values for 8,9,10,11,12,13 Hz. You could then further refine with the top two bins and get the fractional value. Note that this may move around somewhat depending on conditions.
EEG-Pipes has a built in FFT. and you could adjust the function if needed to get the 1 Hz bins; if not the default.
Gerard, I believe it should do realtime analysis as well. You would need to set up the callbacks yourself, though.
It sounds as if you may first of all need to find or write the code to take the BCI data and pull out a single signal of a given length. Perhaps getting the BCI data into an Observable stream in EEG-Pipes would be the way to go.
Thank you so much, William and Bill. EEG-pipes has a function for that, it seems like. It's called BufferFFT() and you can select the number of bins. So, each bin is 1Hz, correct? How many bins will it provide? (even though I only need 8Hz, 9Hz, 10Hz, 11Hz, and 12Hz). Thank you for answering many questions of mine. I am a little confused on some concepts such as bins, etc.
The resolution (Hz per bin) for FFT is provided by the DSP library. FFT maps a time series data stream into 'bins', like a histogram. With each bin containing the signal amplitude at that amount of Hz. You will need to consult the documentation for the DSP library to see how to specify what FFT resolution you want.
When you say DSP library, you mean dsp.js, correct?
Hi Gerard. My impression is that there are many DSP libraries available, in all types of computer languages. Just in Javascript I see a number here:
https://www.google.com/search?q=dsp+library+javascript
It looks like some of these are simplified to the extent, you may not be able to specify what resolution you want for the FFT bins. You'll just have to look around and see what are the defaults, and how to get what you want. It's possible some default to 1 Hz bins, in which case you are all set. You could also I believe specify a sub-hertz bin resolution, such as .5 Hz.
https://www.google.com/search?q=fft+bins
Regards, William
Thank you very much, William! I will look into those libraries and see if they work with my project.