# 7.4 Buffering Data in SIMULINK

A common question regarding SIMULINK is how to buffer previous values of the input to use at later times. This can be useful for various reasons, one example being the Spectrum Analyzer block in the Extras library. This block buffers some data and then uses fft analysis to provide a frequency plot. So, how can this be done?

A buffering scheme can be created via an S-function. One common S-function trick is to use discrete states to store information. The following two examples are commented S-functions, which realize two variations of a buffering scheme. The first example, sbuffer.m, creates a sliding window over the last three input values and outputs the sum of these values. The second example, sbuffer2.m, stores the last three values in a buffer, and holds the output value constant as the buffer is filling. For example, if the current sum of the buffered values is 5, the output will be held to 5 for the next three integration steps. At that time, the buffer will contain three new values. The sum of these new values will be the new output.

------------------------ sbuffer.m ------------------------

```function [sys,x0] = sbuffer(t,x,u,flag)
%  This S-function stores the past three values of the input in a
%  buffer and outputs the sum of those values.  It demonstrates how
%  discrete states can be used as memory elements that store
%  specified4 values.
if flag == 0,            % 0 ==> return sizes
sys = [0 3 1 1 0 0];   % 3 discrete states to hold the "old" values
x0 = zeros(1,3);
elseif abs(flag) == 2,   % 2 ==> return next discrete state
sys = zeros(3,1);      % initialize sys
sys(3) = x(2);         % here we are setting the values for x(3),
sys(2) = x(1);         % x(2), and x(1) respectively, for the
sys(1) = u;            % NEXT integration step
elseif abs(flag) == 3,   % 3 ==> return outputs
sys = sum( x );        % output the sum of the past three values
else                     % all other flags return an empty set, []
sys = [];
end
```
---------------------- sbuffer2.m ----------------------

```function [sys,x0] = sbuffer2(t,x,u,flag)
%  This S-function stores the input at the last three integration
%  steps in a buffer and also stores the sum of these values.  The
%  output is held constant while the buffer is being filled.  So, if
%  the current sum of the buffered values is 5, the output will be
%  held to 5 for the next three integration steps.  At that time,
%  the buffer will contain three new values.  The sum of these new
%  values will be the new output.
%
%   A convenient method of testing this function is to set the min
%  and max step size to 1 and the stop time to 10.  Use a clock as
%  input and examine the results in the workspace.
if flag == 0,            % 0 ==> return sizes
%  5 discrete states:
%    1st three to store the previous values
%    4th to count the number of integration steps
%    5th to hold the current sum
sys = [0 5 1 1 0 0];
x0 = zeros(1,5);
elseif abs(flag) == 2,  % 2 ==> return next discrete state
sys = zeros(5,1);    % initialize sys
sys(3) = x(2);
sys(2) = x(1);
sys(1) = u;
% count the number of steps and when reach 3, reset to zero
count = x(4) + 1;  %increment counter
if rem(count,3) == 0,
sys(4) = 0;                         % reset counter at 3rd step
sys(5) = sum(x(1:3));   % store the current sum
else
sys(4) = count;     % store the current count
sys(5) = x(5);      % don't change the 5th state
end
elseif abs(flag) == 3,   % 3 ==> return outputs
if x(4) ~= (3-1),
sys = x(5);          % if not the 3rd step, output the old sum
else
sys = sum(x(1:3));  % output the sum of the past
% three values
end
else                      % all other flags return an empty set, []
sys = [];
end
```

(c) Copyright 1994 by The MathWorks, Inc.