%iddemo4

%	Lennart Ljung
%	Copyright (c) 1986-95 by the MathWorks, Inc.
%	$Revision: 1.4 $  $Date: 1995/05/11 14:06:12 $

echo on
%	In this demo we consider spectrum estimation, using Marple's
%	test case (The complex data in L. Marple: S.L. Marple, Jr, 
%	Digital Spectral Analysis with Applications, Prentice-Hall,
%	Englewood Cliffs, NJ 1987.)

load marple

%	Most of the routines in the SITB support complex data.
%       For plotting we examine the real and imaginary parts of
%	the data separately, however.

%	First, take a look at the data:
pause   % Press a key for plot.
subplot(211),plot(real(marple)),title('Real part of data.')
subplot(212),plot(imag(marple)),title('Imaginary part of data.')

pause   % Press a key to continue

%	Let's first check the periodogram of the data;

per = etfe(marple);

ffplot(per)    

pause   % Press a key to continue.

%	The spectrum can also be plotted with logarithmic frequency scale
%	as a bodeplot:

bodeplot(per)   

pause   % Press a key to continue.

%	Since the data record is only 64 samples, and the periodogram is
%	computed for 128 frequencies, we clearly see the oscilla-
%	tions from the narrow frequency window. We therefore apply some
%	smoothing to the periodogram (corresponding to a frequency resolution
%	of 1/32 Hz):

sp = etfe(marple,32);

ffplot([per sp])  

pause   % Press a key to continue.

%	Let's now try the Blackman-Tukey approach to spectrum estimation:

ss = spa(marple);

ffplot([sp ss])    
        % Yellow/solid: Smoothed periodogram.
        % Magneta/dashed: Blackman-Tukey estimate.

pause   % Press a key to continue.

%	The default window length gives a very narrow lag window for this
%	small amount of data. We can choose a larger lag window by

ss20 = spa(marple,20);

ffplot([sp ss20])
        % Yellow/solid: Smoothed periodogram.
        % Magneta/dashed: Blackman-Tukey estimate.

pause   % Press a key to continue.

%	A parametric 5-order AR-model is computed by

t5 = ar(marple,5);

%	and its spectrum is:

s5 = th2ff(t5);

%	Compare with the periodogram estimate:

ffplot([sp s5])   
        % Yellow/solid: Smoothed periodogram.
        % Magneta/dashed: 5th order AR estimate.
pause   % Press a key to continue.

% 	The AR-command in fact covers 20 different methods for
%	spectrum estimation.  The above one was what is known
%	as 'the modified covariance estimate' in Marple's book.
%	Some other well known ones are obtained with:

tb5 = ar(marple,5,'burg');	% Burg's method
ty5 = ar(marple,5,'yw');  	% The Yule-Walker method
sb5 = th2ff(tb5);sy5 = th2ff(ty5);	% The spectra


ffplot([s5 sb5 sy5])
        % solid/yellow: Modified covariance
        % dashed/magneta: Burg
        % dotted/cyan : Yule-Walker
pause   % Press a key to continue.

%	AR-modeling can also be done using the Instrumental
%	Variable approach:

ti = ivar(marple,4);

si = th2ff(ti);
ffplot([s5 si])
        % solid/yellow: Modified covariance
        % dashed/magneta : Instrumental Variable     	
pause   % Press a key to continue.


%	Furthermore, the SITB covers ARMA-modeling of spectra:

ta44 = armax(marple,[4 4]);   % 4 AR-parameters and 4 MA-parameters

sa44 = th2ff(ta44);             % The spectrum

ffplot([s5 sa44])
       % solid/yellow: Modified covariance
       % dashed/magneta : ARMA

echo off
set(gcf,'NextPlot','replace');

