How do I create an S-function that buffers previous values?
3 views (last 30 days)
Show older comments
Is it possible to buffer the input so that I can use it at a later time? I've noticed that the Spectrum Analyzer does this in Simulink.
Accepted Answer
MathWorks Support Team
on 26 Jul 2010
A common question regarding Simulink is how to buffer previous values of the input to use at later times. 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.
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
sys(2) = x(1); % x(3), x(2), and x(1) respectively, for
sys(1) = u; % the 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
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:2))+u; % 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) ~= 0,
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
sys = []; % set, []
end
0 Comments
More Answers (0)
See Also
Categories
Find more on Classical Control Design in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!