I\'m starting programming in MATLAB and I have some problems creating a buffer matrix. I\'m trying to do the following:
I\'m continuously obtaining an image from a w
centroidBuffer = [centroidBuffer(:,2:end) centroidData(:)];
This is a nice and simple solution, but it is slow. Every time you add a new vector, matlab has to copy the whole old data except the first entry. If you think about real-time, this is not a good idea.
circBuff(:,:,mod(counter,numFrames)) = newData
This idea does not have the copy-problem, but now you do not have a nice subarray any more, which represents from the first index to the last index the data in chronological order.
I just uploaded my solution for a fast circular buffer which avoids the two problems to
http://www.mathworks.com/matlabcentral/fileexchange/47025-circvbuf-m
The main idea of this circular buffer is constant and fast performance and avoiding copy operations when using the buffer in a program:
% create a circular vector buffer
bufferSz = 1000;
vectorLen= 7;
cvbuf = circVBuf(int64(bufferSz),int64(vectorLen));
% fill buffer with 99 vectors
vecs = zeros(99,vectorLen,'double');
cvbuf.append(vecs);
% loop over lastly appended vectors of the circVBuf:
new = cvbuf.new;
lst = cvbuf.lst;
for ix=new:lst
vec(:) = cvbuf.raw(:,ix);
end
% or direct array operation on lastly appended vectors in the buffer (no copy => fast)
new = cvbuf.new;
lst = cvbuf.lst;
mean = mean(cvbuf.raw(3:7,new:lst));
Check the screenshot to see, that this circular buffer has advantages if the buffer is large, but the size of data to append each time is small as the performance of circVBuf does NOT depend on the buffer size, compared to a simple copy buffer.
The double buffering garanties a predictive time for an append depending on the data to append in any situation. In future this class shall give you a choice for double buffering yes or no - things will speedup, if you do not need the garantied time.
