circular buffer operator

186 views
Skip to first unread message

jonathan...@gmail.com

unread,
Feb 17, 2021, 4:52:41 PM2/17/21
to Bonsai Users
Hi, 

In the DSP library, there is the Buffer operator which allows aggregation of multiple matrices and numbers. When combined with the Skip input parameter, it can be used to create a sliding window over the input data stream. 

I'm using this to feed a vertex shader to plot ephys traces. However, this results in a scroll plot like behavior where the x axis slides along as new samples are produced. What I really want is a static x axis with elements update circularly, like on an oscilloscope. To do this I need the equivalent of the Buffer operator that results in a circular buffer rather than a sliding buffer. 

- Does this already exist?
- If not can it be made from existing Bonsai operators?
- If not is it worth it try for a PR to the DSP library?

- Jon

João Frazão

unread,
Feb 18, 2021, 5:17:11 AM2/18/21
to jonathan...@gmail.com, Bonsai Users
I do not have a solution, but my two cents on this:
OpenCV?
to do this in a dsp buffer you want to probably do it using the operators defined in openCV, like ADD, OR, XOR, AND, etc that operate with SIMD instructions to handle efficiently memory vectors of values. i would probably think about this as a (smaller) sample buffer that is written (at the right place) in a (bigger) display buffer, the Right place is probably a MOD operation on the index order of the sample address to the address of the bigger buffer.

I highly doubt you want this since i am suspicious why you want this, and the scale that maybe is pretended: the openCV solution to this problem means that you would be sending bigger buffer to the graphic card than you really need; every time you send the total updated display buffer, even the values that did not change.
Shaders?
I think the solution is just to send the most actual sample(s) to the shader, and handle that update to the display buffer in the shader itself. much more performant and maybe easier to implement and scale. it may even be very similar to what is already done in the timeSeries visualizer.

Joao




--
You received this message because you are subscribed to the Google Groups "Bonsai Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to bonsai-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/bonsai-users/1cd36114-2085-4635-9fc7-2020022c2bb6n%40googlegroups.com.


--
Joao Frazao
Intelligent Systems Laboratory
Champalimaud Centre for the Unknown
Av. Brasília, Doca de Pedrouços
1400-038 Lisboa, Portugal
www.neuro.fchampalimaud.org

jonathan...@gmail.com

unread,
Feb 18, 2021, 9:17:35 AM2/18/21
to Bonsai Users
Hi  Joao,

Thanks. Yeah I'm not very worried about performance optimization and using OpenCV it is indeed easy enough to write a circular buffer. I'm trying to avoid it entirely if possible.

I think your point about maintaining the circular buffer in GPU memory rather than using main ram is extremely valid. I have litter to no GLSL skills so it will be an interesting challenge. 

Gonçalo Lopes

unread,
Apr 2, 2021, 8:26:23 PM4/2/21
to jonathan...@gmail.com, Bonsai Users
Hi Jon,

We've previously also done this using the Shaders package by keeping a circular buffer of textures in GPU memory, where each texture is exactly the size of each sample buffer. The advantage of doing it this way is that updating the circular buffer is always simply a copy operation overriding the next GPU buffer, where the buffer index is updated using a modulus over the total number of buffers: next = (current + 1) % numbuffers

In fact, this way it is easy to switch from sliding window to circular buffer, since it all boils down to how you draw the texture buffers and which buffers you override. Since OpenGL doesn't care in which order you draw the buffers, it's effectively the same performance to do one or the other.

There is a working example of this approach on the neuroseeker package source which you could probably adapt to your purposes (see CreateDataModel.cs and DrawDataModel.cs):

Hope this helps.


jonathan...@gmail.com

unread,
Jun 17, 2021, 10:13:05 AM6/17/21
to Bonsai Users
Thanks Goncalo, this is indeed exactly what I'm interested in! I'm using my extremely limited understanding of OpenGL to try and adapt this functionality for my purposes. I think something that would really help is if there was an example of the actual Shaders that were used with these nodes when using them to visualize NeuroSeeker data. Are they available anywhere?

Gonçalo Lopes

unread,
Nov 6, 2021, 9:40:16 AM11/6/21
to jonathan...@gmail.com, Bonsai Users
Hi John,

You probably have already figured this out, but here are some examples from the original recordings:

Hope this helps.

NSK_recordings.zip
Reply all
Reply to author
Forward
0 new messages