function D1 = append(D1,D2)
% Appends inputs and outputs of two FINITE models.

%   Copyright 2020 The MathWorks, Inc.
nfd1 = length(D1.Delay.Internal);
nfd2 = length(D2.Delay.Internal);
nx1 = size(D1.a,1);
nx2 = size(D2.a,1);

% Compute realization for concatenation
D1.a = ltipack.util.blkdiag(D1.a,D2.a);
D1.e = ltipack.util.blkdiagE(D1.e,D2.e,nx1,nx2);
D1.dx0 = [D1.dx0;D2.dx0];
D1.x0 = [D1.x0;D2.x0];
if nfd1==0 && nfd2==0
   % No internal delays
   D1.b = ltipack.util.blkdiag(D1.b,D2.b);
   D1.c = ltipack.util.blkdiag(D1.c,D2.c);
   D1.d = ltipack.util.blkdiag(D1.d,D2.d);
   D1.u0 = [D1.u0;D2.u0];
   D1.y0 = [D1.y0;D2.y0];
else
   % Account for internal delays
   [rs1,cs1] = size(D1.d);
   [rs2,cs2] = size(D2.d);
   ny1 = rs1-nfd1;  nu1 = cs1-nfd1;
   ny2 = rs2-nfd2;  nu2 = cs2-nfd2;
   D1.b = [ltipack.util.blkdiag(D1.b(:,1:nu1),D2.b(:,1:nu2)) ,...
      ltipack.util.blkdiag(D1.b(:,nu1+1:cs1),D2.b(:,nu2+1:cs2))];
   D1.c = [ltipack.util.blkdiag(D1.c(1:ny1,:),D2.c(1:ny2,:)) ;...
      ltipack.util.blkdiag(D1.c(ny1+1:rs1,:),D2.c(ny2+1:rs2,:))];
   D1.d = [ltipack.util.blkdiag(D1.d(1:ny1,1:nu1),D2.d(1:ny2,1:nu2)) , ...
      ltipack.util.blkdiag(D1.d(1:ny1,nu1+1:cs1),D2.d(1:ny2,nu2+1:cs2)) ; ...
      ltipack.util.blkdiag(D1.d(ny1+1:rs1,1:nu1),D2.d(ny2+1:rs2,1:nu2)) , ...
      ltipack.util.blkdiag(D1.d(ny1+1:rs1,nu1+1:cs1),D2.d(ny2+1:rs2,nu2+1:cs2))];
   D1.u0 = [D1.u0(1:nu1,:);D2.u0(1:nu2,:);D1.u0(nu1+1:cs1,:);D2.u0(nu2+1:cs2,:)];
   D1.y0 = [D1.y0(1:ny1,:);D2.y0(1:ny2);D1.y0(ny1+1:rs1,:);D2.y0(ny2+1:rs2,:)];
   D1.Delay.Internal = [D1.Delay.Internal ; D2.Delay.Internal];
end
D1.Scaled = false;
D1.Delay.Input = [D1.Delay.Input ; D2.Delay.Input];
D1.Delay.Output = [D1.Delay.Output ; D2.Delay.Output];
