This repository provides a Circular Buffer implementation for MATLAB using a MEX function for efficient data handling. The package is designed to be added as a submodule to your projects and can be used to interact with the Circular Buffer through calls like buf.CircleBufferMex(...)
.
Method Name | Description | Syntax |
---|---|---|
create | Creates a buffer with specified channels and size, initializing with zeros. | buf.CircleBufferMex('create', 'bufferName', numChannels, bufferSize); |
clear | Clears a buffer and frees its memory. | buf.CircleBufferMex('clear', 'bufferName'); |
add | Adds data to the buffer. | buf.CircleBufferMex('add', 'bufferName', data); |
get | Retrieves specific data from a channel starting at a given sample. | buf.CircleBufferMex('get', 'bufferName', numSamples, channel, startSample); |
getMostRecent | Retrieves the most recent samples from the buffer. | buf.CircleBufferMex('getMostRecent', 'bufferName', numSamples); |
You'll want to add this as a submodule to your project as the +buf
package, as shown below.
To add this repository as a submodule to your project:
cd your_project_directory
git submodule add https://github.com/Neuro-Mechatronics-Interfaces/matlab_package__circle_buffer.git +buf
git submodule update --init --recursive
Open MATLAB and navigate to the directory containing the CircleBufferMex.cpp file. Run the following command to compile the MEX file:
mex CircleBufferMex.cpp CircularBuffer.cpp
Once the package is added as a submodule and the MEX file is compiled, you can use the Circular Buffer in your MATLAB code with the +buf package.
To create a buffer:
buf.CircleBufferMex('create', 'buffer1', numChannels, bufferSize);
To add data to the buffer:
data = single(rand(numChannels, dataLength));
buf.CircleBufferMex('add', 'buffer1', data(:));
To retrieve the most recent samples from the buffer:
numSamples = 100;
retrievedData = buf.CircleBufferMex('getMostRecent', 'buffer1', numSamples);
To retrieve specific data from a channel:
channel = 1;
startSample = 0;
retrievedData = buf.CircleBufferMex('get', 'buffer1', numSamples, channel, startSample);
To clear a specific named buffer:
buf.CircleBufferMex('clear', 'buffer1');
Here is a complete example showing how to use the Circular Buffer:
clear;
clc;
% Parameters
numChannels = 64;
bufferSize = 4000;
dataLength = 256;
% Create the buffer
buf.CircleBufferMex('create', 'buffer1', numChannels, bufferSize);
% Generate some data to add to the buffer
data = single(rand(numChannels, dataLength));
% Add data to the buffer
buf.CircleBufferMex('add', 'buffer1', data(:));
% Retrieve the most recent 100 samples from the buffer
numSamples = 100;
retrievedData = buf.CircleBufferMex('getMostRecent', 'buffer1', numSamples);
disp(retrievedData);
This project is licensed under the MIT License - see the LICENSE file for details.
This project is maintained by the Neuro-Mechatronics Interfaces Lab.