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

data = buffer(1:1100,11);

% Take data(:,1) as the first D/A output, containing the first 11 signal samples, data(:,2) as 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 underlap of 2. To do this, you will repeatedly call buffer to operate on each successive input frame, using the opt parameter to maintain consistency in the underlap from one buffer to the next.
%
% Set the buffer parameters. Specify a new frame size of 4 and an underlap of -2. Skip the first input element, x(1), by setting opt to 1. The carryover vector is empty initially.

n = 4;
p = -2;
opt = 1;
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 three 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.