Inconsistent rate at which samples are received in Matlab [resolved]
Hey all,
I am currently streaming raw eeg data to Matlab from OpenBCI GUI using the LSL library. However, when receiving data in Matlab I see that there is some inconsistency in the rate at which I obtain samples (see figs). This is a huge problem for me. I have tried streaming through both WiFi and Bluetooth. Any help?



Thanks in advance.
Device: Cyton + Daisy 16 channel
Transmission: using Bluetooth dongle
OS: Windows 10
PS I have already changed FTDI settings on my Windows PC
Comments
PPS I power the Cyton board with 4 x 1.5V AA batteries.
And the "1 second long" duration for which I collect samples in Matlab is by using the timer in Matlab.
Please post your Matlab code so that others can try to replicate these results.
It is nearly the same code as the example given in the Documentation.
Now when i time the
[vec, ts] = inlet.pull_sample();statement to record for 1second , the length of total samples received would be varying alot. (It should be 250Hz when I have set it to that value, but as you can see in the plot above, it is inconsistent).You probably need to try
pull_chunk, because data is sent out in chunks. Please provide the FULL Matlab code to replicate these results and plot graphs just as you did.I did try
pull_chunk, and I saw similar inconsistency.Here is the code I used to generate my results.
BTW, Is there a known reliable way to get accurate streaming, like using Python instead of GUI, etc?
@pgatti, hi. These are just buffering delays. The sample rate of the data stream is CONSTANT at your chosen rate, 500, 1000, or 250 Hz. This is set by the sample clock on the Cyton. Buffering delays due to networking, process scheduling, Matlab interpreter, etc. -- are all normal and to be expected.
You mention, "...This is a huge problem for me..." Can you elaborate? Your signal processing should always assume that the data is flowing in at a constant sample rate, regardless of buffering delays.
Please read some of the comments on this other thread:
https://openbci.com/forum/index.php?p=/discussion/2329/how-steady-is-cytons-sampling-rate
Regards, William
Thanks for the reply @wjcroft. I understand now that the delays are due to buffering and not in the Cyton board itself.
My purpose is a real-time neurofeedback task. My "buffering" function in Matlab takes 500ms of data ( ie 125 samples when I am streaming at 250Hz) and does processing on this. If there are delays in getting these samples, my timing in the task would be off, as there is also my presentation system sending triggers to the processing scripts.
hence my question -- is there an established reliable way I can receive the stream in my PC with the same sampling rate as the Cyton board is sampling them? (ie without/minimum Buffering delays)?
Much of the buffering delay likely comes from Matlab itself. This is a huge program in an interpreted language. Is there any way you can write your app in another language besides Matlab? There are many frameworks for doing signal processing / DSP in other languages, and some of those are compiled vs interpreted. Additionally, the GUI and Hub are also very large programs. GUI is written in Processing, which is a dialect of Java (generally JIT compiled, but also interpreted.) The Hub is due to be replaced / eliminated very soon. Sometime this spring. It currently is written in Electron / Javascript, which is interpreted / compiled.
I've written neurofeedback applications myself, generally using VPL frameworks such as BioEra, BrainBay, Bioexplorer. In these dataflow languages, certain DSP filtering operations such as bandpass filters can take a variable amount of time to output / process their inputs. A couple hundred milliseconds (.2 to .3 second) is not unusual, and is to be expected. Thus it might be a half second or so before some audio or visual feedback can be returned to the subject from the neurofeedback session. This is never a problem because the central nervous system is intelligent enough understand the delay factor and interpret accordingly.
The 'delays' you show in your 1000 Hz and 500 Hz look like they could be also possibly be aggravated by glitches in the Wifi Shield. Are you running the Shield on a separate AA battery pack? That is recommended for Cyton. UDPx3 is also recommended over TCP. Distance away from other sources of wifi / Bluetooth will also improve radio link reception.
Most neurofeedback is done with 4 or less channels. Some QEEG based / source localization (3D voxel, sLoreta) neurofeedback uses 19 or more channels, but is generally out of the reach of most because of the expensive apps. Can you mention why you need the Daisy for neurofeedback? If you use the plain Cyton 8 channels with Bluetooth dongle, that gives you 250 Hz and lowest possible latency.
That’s a lot of points I can try, thanks for the tips.
I would like to know what other language I could use to resolve this, any advice?
Would BioEra, BrainBay, Bioexplorer or OpenVIBE be good options? Can you tell me which of these I can try?
The rest of the points you make are definitely things i will incorporate into my setup.
BrainBay and OpenViBE are both free. OpenViBE has an extensive docs and help wiki,
http://openvibe.inria.fr/documentation-index/
BrainBay tutorial with Cyton:
https://sites.google.com/site/biofeedbackpages/brainbay-openbci
http://www.shifz.org/brainbay/
--
I would start by just running your Matlab program with Cyton 8 channels, using Bluetooth. If you want to bypass GUI / Hub, you can stream LSL from the Python libraries.
Okay, l will try these things today and see how it goes
Replacing GUI LSL streamer with the pyOpenBCI library's streamer seemed to do the trick. I am now receiving a nearly consistent 251Hz (with a gain/loss of 5-6 samples every now and then).
Is there a way to check latency in data stream (from when the data is being sampled at the board to the data being read) in Matlab? Since this is going to be a real-time NFB project, it would help to confirm the latency.