Matlab / LSL from OpenBCI_GUI (Ganglion) gives weird data

RomzanRomzan Switzerland
edited March 2020 in Ganglion

Dear all,
I am new to the forum, thank you in advance for your help and advises! I will try to give as much details as possible for you to frame the problem.
I am trying to get the LSL stream of data from my ganglion board into Matlab 2019a (I bought the ganglion on the 13 sept. 2017 and never updated the firmware). I am using the OpenBCI_GUI v4.2.0 with the networking widget and the liblsl from matlab. First, I tried with the simulated data from the GUI and managed to get them into matlab without delay. With a test of getting 10 seconds worth of Data, I assumed that the sampling rate given for the synthetic data is 250Hz. Here is the matlab code I used:

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});

disp('Now receiving data...');

% Prepare to receive 10 seconds of data
Fs = 250;
figure
p = plot(linspace(0,5,Fs*5),zeros(1,Fs*5));
[x,y] = deal(zeros(1,Fs*10));

tic

for i = 1:length(x)
    % get data from the inlet
    [vec,ts] = inlet.pull_sample();
    x(i) = ts;
    y(i) = vec(1);
    % and display it every 25 points to avoid lag
    p.YData = [p.YData(2:end),vec(2)];
    if mod(i,25) == 0
        drawnow
    end
    fprintf('%.2f\t',vec);
    fprintf('%.5f\n',ts);
end

toc

And the result:

Now when I try with real data from the ganglion with the exact same code, I just changed to record with a supposed sampling rate at 200Hz for what should be worth of 10 seconds ( Fs = 200; ):

I identified two problems:
1. The data shown are not the one observed in the GUI. It shows a cyclic artifact on all the channels.
2. The data arrived way to fast for what the ganglion is supposed to achieve. In 4 seconds I filled what was supposed to be 10 seconds at 200 Hz.

Any idea what could be the cause ? I remember trying this once with python (pylsl), I was able to read the LSL but the same artifact appeared.

Thank you for your help and tell me if anything is missing from my description.

Best regards,
Romain

Comments

  • wjcroftwjcroft Mount Shasta, CA

    Romain, hi.

    Please take a look at this other recent thread and see if you can bypass LSL entirely, using the BrainFlow library API.

    https://openbci.com/forum/index.php?p=/discussion/2506/how-to-connect-openbci-with-matlab-using-lsl#latest

    It's possible the artifacts you are seeing in the LSL stream, have some relation to the way in which you are calling the LSL functions from Matlab. Or the settings in the GUI Networking widget. Direct access from Matlab (with BrainFlow) should be both faster and easier, less latency.

    Regards, William

  • RomzanRomzan Switzerland

    Hello William,
    Thank you for your answer. Since I observed the same with python and that I am able to get the synthetic data without problem, I narrowed the problem down to the networking widget when getting real data. I will try brainflow as soon as possible.
    Regarding the firmware of my ganglion, would there be a reason to update it or the one of 2017 is still viable? I don't feel so comfortable tweaking in there if I can avoid it.
    Best,
    Romain

  • wjcroftwjcroft Mount Shasta, CA

    Your firmware is fine. Richard @retiutut, is it possible Romain is not configuring LSL correctly? BrainFlow should give you a workaround.

  • retiututretiutut Louisiana, USA


    Please change # Chan to 4, just like when you tried with synthetic data. Turning off channels 3 and 4 should stream data with (x, x, 0, 0).

  • retiututretiutut Louisiana, USA
    edited March 2020

    Thank for sharing the Matlab code, going to try this using Matlab!

  • wjcroftwjcroft Mount Shasta, CA

    Yeah I saw that as well, previously, but figured you would spot any LSL issues in his code or GUI setup.

  • retiututretiutut Louisiana, USA
    edited March 2020

    Also, this "# Chan" textbox is going away soon, because it is confusing. https://github.com/OpenBCI/OpenBCI_GUI/issues/644

    Installing Matlab2020a now :smile:

  • retiututretiutut Louisiana, USA

    @Romzan In the Matlab code, how do we graph all four channels?

  • RomzanRomzan Switzerland
    edited March 2020

    Hello! Thank you for the answer, sorry I did not get the time to answer yet, I'm matlabing for work so much with the confinment that I don't have the time to matlab for fun...
    The #chan = 4 did the trick! It seems I finally get the data properly. It was simple in the end, I'm glad I ask for you help!
    In my code I did not write to plot all 4 but just one at a time. Just change the index of vec() at line 31 if you want to display another channel.
    If you want to plot all 4 I would do it using subplot like this:

    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});
    
    disp('Now receiving data...');
    
    % Prepare to receive 10 seconds of data
    Fs = 250;
    figure
    [p,li] = deal(cell(1,4));
    for i =1:4
        p{i} = subplot(4,1,i);
        li{i} = line(linspace(0,5,Fs*5),zeros(1,Fs*5));
    end
    
    x = zeros(1,Fs*10);
    y = zeros(4,Fs*10);
    
    tic
    for i = 1:length(x)
        % get data from the inlet
        [vec,ts] = inlet.pull_sample();
        x(i) = ts;
        y(:,i) = vec';
        for j = 1:4
            li{j}.YData = [li{j}.YData(2:end),vec(j)];
        end
        % and display it every 25 points to avoid lag
        if mod(i,25) == 0
            drawnow
        end
        fprintf('%.2f\t',vec);
        fprintf('%.5f\n',ts);
    end
    
    toc
    

    Also removing the display in the console gives more time to compute and less risk of lag.
    Thank you again for the nice support!
    best,
    Romain

Sign In or Register to comment.