Inconsistent rate at which samples are received in Matlab [resolved]

pgattipgatti India
edited February 2020 in Software

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

  • pgattipgatti India
    edited February 2020

    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.

  • retiututretiutut Louisiana, USA

    Please post your Matlab code so that others can try to replicate these results.

  • pgattipgatti India
    edited February 2020

    It is nearly the same code as the example given in the Documentation.

    %% instantiate the library
    disp('Loading the library...');
    lib = lsl_loadlib();
    
    % resolve a stream...
    disp('Resolving an EEG stream...');
    result = {};
    while isempty(result)
        result = lsl_resolve_byprop(lib,'type','EEG'); end
    
    % create a new inlet
    disp('Opening an inlet...');
    inlet = lsl_inlet(result{1} , 1.0);  % Here I am setting 'maxbuffered' value of stream inlet to 1.0 seconds (so that the buffer is cleared every 1 seconds) 
    
    disp('Now receiving data...');
    while true
        % get data from the inlet
        [vec,ts] = inlet.pull_sample();
        % and display it
        fprintf('%.2f\t',vec);
        fprintf('%.5f\n',ts);
    end
    
  • 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).

  • retiututretiutut Louisiana, USA
    edited February 2020

    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.

  • pgattipgatti India
    edited February 2020

    I did try pull_chunk, and I saw similar inconsistency.

    Here is the code I used to generate my results.

    %% instantiate the library
    disp('Loading the library...');
    lib = lsl_loadlib();
    
    % resolve a stream...
    disp('Resolving an EEG stream...');
    result = {};
    while isempty(result)
    result = lsl_resolve_byprop(lib,'type','EEG'); end
    
    % create a new inlet
    disp('Opening an inlet...');
    inlet = lsl_inlet(result{1} , 1.0); % Here I am setting 'maxbuffered' value of stream inlet to 1.0 seconds (so that the buffer is cleared every 1 seconds)
    
    % record for 120 seconds and plot sampling rate
    len_vec = [];
    while size(len_vec, 1) ~= 120
        tic;
        total_vec = [];
            while toc <= 1.0
                [vec,ts] = inlet.pull_sample();
                total_vec = [total_vec; vec];
            end
        len_vec  = [len_vec; size(total_vec, 1)];
    end
    
    plot([1:120], len_vec, '-o');
    

    BTW, Is there a known reliable way to get accurate streaming, like using Python instead of GUI, etc?

  • wjcroftwjcroft Mount Shasta, CA

    @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)?

  • wjcroftwjcroft Mount Shasta, CA

    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.

  • pgattipgatti India
    edited February 2020

    That’s a lot of points I can try, thanks for the tips.

    “...Is there any way you can write your app in another language besides Matlab? “

    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.

  • wjcroftwjcroft Mount Shasta, CA

    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.

Sign In or Register to comment.