function [ud,mem] = delay(u,h,del,mem)
% delay.m
%
%    delay element
%
%       [ud,mem] = delay(u,h,del,mem)
%
%    input: u: input
%           h: delay
%           del: hardware sampling time
%           mem: memory of past inputs
%    output: ud: delayed input
%            mem: memory of past inputs
%

% written by H.Ichihara   March 4th 2012

[div,m] = size(u);

% delay in plant
N = ceil(h/del);
v = zeros(N*div,m);
S = zeros(N*div,N*div); S(div+1:end,1:(N-1)*div) = eye((N-1)*div);
if isempty(mem)
 U0 = 0;
 mem = U0*ones(N*div,m);
end
if N == 0
 ud = u;
else
 v(1:div,:) = u;
 ud = mem(((N-1)*div+1):end);
 mem = S*mem+v;
end

end
% EOF