%   SPECTR1.M
%   Computes spectral density for endogenous variables
%   of a dynamic equilibrium model.
%   The inputs to the program include the matrices ao and c which
%   are computed by "solvea.m".  The user must also specify the
%   matrix "sy" linking observables to the state, as well as
%   the matrices D and R which determine the vector
%   autoregressive process of measurement errors.  The matrices D and R
%   are square with dimension equal to the number of rows of sy.
%   The user must specify "nnc", the row number of the constant in the
%   state vector.
%   The model is represented as
%
%       x(t+1) = A*x(t) + C*w(t+1)
%         y(t) = G*x(t) + v(t)
%       v(t+1) = D*v(t) + e(t+1)
%
%   where E e(t)*e(t)' = R, and where the matrices A, C, and G are determined
%   by deleting the rows and columns of ao, c, and sy, respectively, that
%   correspond to the constant in the state vector.
%
%   The spectral densities and the autocovariograms for each component
%   of y are plotted.
A1=rowd(ao,nnc);
A=cold(A1,nnc);
C=rowd(c,nnc);
G=cold(sy,nnc);
[m1,m2]=size(A);
[m3,m4]=size(G);
R=zeros(m3);
D=zeros(m3);
T=2^8
spectral
S'
[m5,m6]=size(S');
SS=S';
%  Plot spectral density for each observable
for i=1:m6
eval(['string=''spectrum of var ',int2str(i),' '';'])
semilogy(w,real(SS(:,i))),title(string),pause
end


SR=zeros(T,rg);
SR(1:T/2+1,1:rg)=SS;
SR(T/2+2:T,1:rg)=SS(T/2:-1:2,1:rg) ;
%  Create and plot autocovariogram for each observable
%  Create autocovariogram by inverse Fourier transforming spectrum
index=[0:T-1]';
for i=1:m6
eval(['string=''covariogram of var',int2str(i),' '';'])
cc=ifft(SR(:,i));
plot(index,real(cc)),title(string),pause
end
