										
function[Ar,Cr,aa,bb,cc,dd,V1]=aggreg(A,C,G,D,R,r)
%function[Ar,Cr,aa,bb,cc,dd,V1]=aggreg(A,C,G,D,R,r)
%  Does aggregation over time for a state space model.
%  The model is of the form
%
%      x(t+1) = A*x(t) + C*w(t+1)
%        y(t) = G * x(t)
%
%   However, observations (possibly error ridden) on y are available only
%   every r periods, where r is an integer greater than one.
%   The state space model that generates the observations is
%
%      x(s+1) = Ar*x(s) + Cr* er(s+1)
%        y(s) = G * x(s) + v(s)
%      v(s+1) = D * v(s) + u(s+1)
%
%    where E u(s)*u(s)'=R, E er(s)*er(s)'=Vr, and Cr = I, and where
%    s=t*r.  The program forms Ar, Cr, and Vr and uses them as inputs to
%    INNOV3.M in order to construct an innovations representation
%    for the sampled process y(s), s=1, r+1, 2r+1,... .  The innovations
%    representation is returned in the matrices aa,bb,cc,dd.  The covariance
%    matrix of the innovations is returned in V1.  Once the
%    outputs of AGGREG are in memory, VARMA2 can be used to compute
%    an arma representation for the sampled y process.
n=max(size(A));
Vr=C*C'
for i=1:r-1
Vr=Vr+(A^i)*C*C'*(A^i)';
end
Ar=A^r;
Cr=eye(n);
[aa,bb,cc,dd,V1]=innov3(Ar,Cr,Vr,G,D,R);
										
										
