%% Continuous Overlapping Buffers
% Create a buffer containing 100 frames, each with 11 samples.

data = buffer(1:1100,11);

%
% Take the frames (columns) in the matrix data to be the sequential outputs of a data acquisition board sampling a physical signal: data(:,1) is the first D/A output, containing the first 11 signal samples; data(:,2) is the second output, containing the next 11 signal samples, and so on.
% You want to rebuffer this signal from the acquired frame size of 11 to a frame size of 4 with an overlap of 1. Call buffer to operate on each successive input frame, using the opt parameter to maintain consistency in the overlap from one buffer to the next.
% Set the buffer parameters. Specify a value of -5 for y(1). The carryover vector is empty initially.

n = 4;
p = 1;
opt = -5;
z = [];

%
% Now repeatedly call buffer, each time passing in a new signal frame (column) from data. Overflow samples (returned in z) are carried over and prepended to the input in the subsequent call to buffer.

for i = 1:size(data,2)
   x = data(:,i);
   [y,z,opt] = buffer([z;x],n,p,opt);
end

% Here's what happens during the first four iterations.
%
% The size of the output matrix, y, can vary by a single column from one iteration to the next. This is typical for buffering operations with overlap or underlap.
% Copyright 2012 The MathWorks, Inc.