Matlab LSL time signature abnormalities
Hi everyone, I'm having an issue with streaming LSL into Matlab where the time signatures I get from
[chunk, t] = inlet.pull_chunk();
do not increase linearly. Instead, there are discontinuities it the variable 't' between successive calls, where the first sample in one chunk may actually be marked with a time before the last sample in the previous chunk, or sometimes with a large gap between successive chunks. Below is the relevant section of code. It's part of a larger app so I've only included the relevant section:
app.StateSwitch.Value = 'On';
% now load lsl stuff
% 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});
idx = 1;
app.fs = 250;
numChans = 8;
app.Recording = zeros(app.fs * 60 * 60, numChans);
app.time = zeros(app.fs * 60 * 60, 1);
disp('Now receiving chunked data...');
while strcmp(app.StateSwitch.Value, 'On')
% get chunk from the inlet
[chunk, t] = inlet.pull_chunk();
if ~isempty(chunk)
ChunkSize = size(chunk, 2);
% add space to our recording if needed
if idx + ChunkSize > length(app.Recording)
app.Recording = [app.Recording; zeros(app.fs * 60 * 60, numChans)];
app.time = [app.time; zeros(app.fs * 60 *60, 1)];
end
% add chunk into recording
app.Recording(idx:idx + ChunkSize - 1, :) = chunk';
app.time(idx:idx + ChunkSize - 1) = t;
idx = idx + ChunkSize;
count = count + 1;
end
pause(0.00001);
end
%trim zeros
app.Recording = app.Recording(1:idx-1, :);
app.time = app.time(1:idx-1) - app.time(1);
display('Recording done')
The problem can be visualized by making a figure with:
plot(diff(app.time))
And I get something like this:

Moreover, when I look at the actual data from the recording, there seem to be jumpy discontinuities in the voltage values concurrent with the aberrations in the time stamps, which obviously is an issue.
I've used LSL a decent amount in other projects/settings and never had similar problems, so I'm pretty confused about what's going on here. Is there something obvious I'm missing here?
Some specs: I'm using a Cyton powered by 4 AA batteries, and Matlab 2019b, and streaming LSL from the OpenBCI GUI.
Comments
We've actually had the code in the GUI reviewed by an LSL maintainer. This fix is already in GUI 5.0.0beta.
Here is a link to the issue on Github: https://github.com/OpenBCI/OpenBCI_GUI/issues/775
@retiutut I am actually already using GUI version 5.0.0-beta.0, so I'm not sure that's the exact issue
@Jalevitt I would still review the maintainer's comments to see if this sheds light on this issue.
@retiutut I took a look at it. My limited understanding is that this problem could perhaps cause the discontinuity in the timestamps that I observed, but not the corresponding discontinuity in the actual data. So I'm still a bit lost.
Another possibility for comparison: use the Acquisition Server in OpenViBE to read from the Cyton, then a small OpenViBE 'scenario', if necessary to output the LSL stream. OpenViBE could also do some real-time graphical monitoring if needed; but not as full featured as the OpenBCI_GUI.
@wjcroft I just gave it a shot with OpenViBE, and everything seemed to be working properly, which is nice, but also confusing.
@retiutut I've played around with my code more, and I'm not 100% sure, but it seems like the spacing between chunks is affected by how much other stuff I have running on my computer, i.e. if I have a bunch of Chrome tabs open, I get huge gaps; if I have nothing but Matlab and the GUI open, I get mostly negative gaps, as in the figure I posted.
@jalevitt I would try streaming LSL to Matlab using the GUI v5 and Synthetic Data Mode. Next, please share a screenshot of how the GUI is configured when you do this. A console log file might also be nice to share.
Does this produce the same result in Matlab?
So this would seem to possibly indicate some kind of buffering issue during the creation of the LSL stream. More CPU load may result in less frequent or larger buffers.
Another comparison: have you tried running an older GUI than v5? Not that that is a solution, but may point a finger at buffering, which is completely different in v4.
@retiutut I gave it a shot with the synthetic data mode, and got the same odd result with respect to the timestamps. Below is an image of my setup:
I tried to upload a log file, but the forum wouldn't let me, so here's a link : https://github.com/jalevitt/randomfiles/blob/master/Console_2020-07-12_13-40-07.txt
@wjcroft I tried version 4.2. The timestamps from that recording had chunks that were separated by 10s of seconds, which is odd given I only recorded for about 9 seconds total. I agree that there's probably some sort of buffering issue, but it seems strange that you could get negative spacing between chunks.
This is the exact code I've been using to test this, I removed it from the larger app so It could run on its own (it still has a variable called app, but its just a name). It should run on anyone's matlab environment, as long as you have LSL on your path. Any chance anybody is able to replicate my results?