Vous êtes sur la page 1sur 7

Lund University

Centre for Mathematical Sciences

Mathematical Statistics

Projects in Stationary stochastic processes

VT 2009

Computer Exercise
1 Preparations
Carefully read through the entire computer exercise. Read Chapter 5.1-5.2, 5.5 and 6.1
in the compendium. Answer the exercises in the question dictionary below; you are
expected to be able to answer these questions during the exercise.

1.1 Question dictionary

1. What is the connection between the spectral densities for input and output processes in a linear filter?
2. Define the quadratic coherence spectrum. What is the minimum and maximum
value of the function?
3. The cross spectrum of the input process X(t) and the output process Y (t) from
the filter with frequency function H( f ) is
RX,Y ( f ) = H( f )RX ( f ).
For a white noise input, RX ( f ) = 1 and RX,Y ( f ) = H( f ). What is the quadratic
coherence spectrum X,Y ( f )?
4. Let an uncorrelated
disturbance process Z(t) be added to the filtered process,
i.e., Y (t) = h(t u)X(u)du + Z(t), where X(t) is white noise. What is RX,Y ( f )
and X,Y ( f ) in this case?

2 To start the exercise

During the exercise, you will use your temporary computer account1 . Log on to the
account, open a shell-window (icon with shell) and initiate the Matlab environment by
matstat/mc1> initmstat
matstat/mc1> matlab
< M A T L A B S T A R T A R>
>> fms045mas210
WAFO toolbox paths set: normal initiation

This account will be removed after the course. Do not store files you wish to retain on this account.

It is often convenient to gather Matlab commands in a program script; the Matlab

editor can be used for this purpose. This is started with the command edit in the
Matlab window.

3 ARMA(p,q)-models in Matlab
An ARMA(p, q)-model
X(t) + a1 X(t 1) + + a p X(t p) = e(t) + c1 e(t 1) + + cq e(t q)
can also be written as
A(z1 )X(t) = C(z1 )e(t),

A(z1 ) = 1 + a1 z1 + + a p zp
C(z1 ) = 1 + c1 z1 + + cq zq .

The operator z1 is the time delay unit. If you choose C(z) 1 the process becomes
an AR(p)-process, and if you choose A(z) 1 it becomes an MA(q)-process.
In Matlab, polynomials are represented as row matrices with coefficients; e.g. z p A(z1 ) =
z p + + a p1 z + a p is represented with the row matrix [1 a1 ... ap].

3.1 Spectral density and covariance function

Initiate the row vectors of the ARMA(4,2)-model
X(t) 0.9X(t 1) + 0.8X(t 2) X(t 3) + 0.8X(t 4) = e(t) + 0.4e(t 1) + 0.5e(t 2)
by writing
>> A = [1 -0.9 0.8 -1 0.8];
>> C = [1 0.4 0.5];
in Matlab. To compute spectral density for this ARMA-process, one approach is to use
the commands
>> [H,w]=freqz(C,A);
>> R=abs(H).^2;
where freqz is a Matlab-function that calculates the frequency function corresponding
to the polynomials C and A in a number of equally spaced frequency values (default
512) between 0 < where = 2 f . The squared absolute value gives us the
spectral density S. Visualize using
>> plot(w,R)

The command semilogy(w,R) gives a logarithmic scale in y-axis.

To calculate the covariance function the inverse Fourier transform of the spectral density is computed. The frequency range must be the whole period, i.e., 0.5 f < 0.5
alternatively 0 f < 1 when the command ifft is used. The frequency function is
computed for this range using whole in the function freqz,


where the 128 first values of the covariance function is plotted. Observe that the vector
element r(1) is equal to the covariance value at zero, i.e. rX (0) = r(1), and more
generally rX (k) = r(k+1) in Matlab. If you instead are interested in the correlation
function () = r()/r(0) you obtain it by writing
>> stem([0:127], r(1:128)/r(1),filled)

3.2 Simulation of ARMA-processes

To be able to simulate an ARMA-model one need a sequence of independent normally2
distributed {e(t)} stochastic variables. Generate such a sequence with variance 1 by
>> m = 0;
>> sigma = 1;
>> e = normrnd(m, sigma, 1, n);
>> e = randn(1,n);
Each call generate a row-matrix e with n normally random numbers, choose n 400.
The simulation is done with
>> x = filter(C, A, e);
which result in the vector Y with the simulated time-series as the result, where A and
C are defined as earlier. If you want to plot the result you can write e.g.
>> plot(x)

In general e(t) does not need to be normally distributed.

4 Filter design
Different filters can be designed directly by placing poles and zeros in armagui. As
the input process of ARMA-processes (as well as AR- and MA-) is white noise, the
spectral density plot will also be the plot of |H( f )|2 as Re ( f ) = 1.
1. Try to make some interesting filters by placing poles and zeros. Free your mind
and try different possibilities! Sort out how your movements of the poles and zeros
effect the frequency function. Create e.g., a low-pass filter, a high-pass filter, a
band-pass filter and a band-stop filter.
2. Try to create a low-pass filter using 6 poles and 6 zeros. The filter should have
a flat spectral density (not necessarily with power one) below 0.25 and as small
energy as possible at frequencies above 0.25. When you have found a model that
you are satisfied with, export it. Thereafter, load the file lowpass.mat Matlab
which contain a Butterworth low-pass filter. (The function butter in the Signal
Processing Toolbox of Matlab can be used.) Then import the model lowpass
to armagui and compare to your own filter. Can you explain why this design
3. Create a notch-filter, which is a band-stop filter for a very small frequency
interval. Use one pair of complex-valued poles and one pair of complex-valued
zeros. (Hint: Put the zeros on the unit circle and move the poles close to the zeros
and investigate the result).

5 Spectral estimation of sea waves

Load the file afric.mat. The file contains measurements of the height of the sea
surface, in meter, outside western Africa. The measurements have been taken during
approximately 40 minutes and there are four measurements for each second.
Import afric to spekgui. Compute a spectral estimate of the sea wave data using
Welchs method with 60 average. Which are the main frequencies of the process? Which
periods correspond to the main frequencies? Do the periodicities seem to be reasonable?
What do sea-waves look like? Is every seventh wave bigger than the others?
Go back to armagui and try to make an ARMA-model with a spectrum similar
to the spectrum of the afric data, i.e., one with two peaks, and with the power
approximately equal to zero for f = 0. (Hint: An ARMA(4,3)-model can be useful.)
Compare the estimated covariance function with the covariance function for your model. What seems to the most appropriate tool for analysing the wave data: the covariance function or the spectrum?

6 Cross spectrum
The cross spectrum of the input signal X(t) and the output signal Y (t) from the filter
with frequency function H( f ) is
RX,Y ( f ) = H( f )RX ( f ).
For a white noise input signal RX ( f ) = 1 and RX,Y ( f ) = H( f ). Use an ARMA(4,2)process
X(t) 0.9X(t 1) + 0.8X(t 2) X(t 3) + 0.8X(t 4) = e(t) + 0.4e(t 1) + 0.5e(t 2)
by writing
A = [1 -0.9 0.8 -1 0.8];
C = [1 0.4 0.5];
in Matlab. Compute the filter for this ARMA-process, and a lowpass AR(1)-process as
the uncorrelated disturbance Z(t) i.e.,
A2=[1 -0.95];
1. Change the location of the pole of the low-pass disturbance process, .e.g, a1 =
0.95. What differs in the resulting coherence spectrum? Why?
2. Change the power of the disturbance process,
where sigma2 can be chosen e.g., 0.01, 0.1, 10 or 100. How does the choice effect
the coherence spectrum? Can you intuitively explain this?

7 Cross-covariance and cross-spectrum estimation

Now shall we examine the relation between left, X1 (t), and right, X2 (t), wheel track
for measurements of the road level somewhere in Sweden. The measurements are made
every 10:th centimeter. Load data with load Road1. Study data for some dependence,

grid, axis square

Examine the cross-covariance between the tracks. The function covf computes covariance and cross-covariance function for the signal and return a matrix with four rows
and M columns,
>> r = covf(Road1,2000);
where r is a matrix with four rows and 2000 columns.The rows contain estimates of the
following covariance functions: C(X1 (t), X1 (t + )), C(X2 (t), X1 (t + )), C(X1 (t), X2 (t +
)) and C(X2 (t), X2 (t +)). We are interested of the rows 2, and 3, the cross covariances.
Plot one of the cross covariances. After how many meters has the cross covariance
passed zero for the first time? Can you see any periodically in the cross covariance.
Now shall we study the cross spectrum of the tracks. One function that estimates the
cross spectrum is etfe,
>> left = etfe(Road1(:,1), 64, 512); % calculates Rx1
>> right = etfe(Road1(:,2), 64, 512); % calculates Rx2
>> ffplot(left,right)
% plot Rx1 and Rx2
The spectral densities RX1 and RX2 are very similar and the coherence spectrum
X1 ,X2 ( f ) can be estimated with the transfer function,
>> both = etfe(Road1, 64, 512);
>> bodeplot(both); subplot(211); set(gca,yscale,linear)
The second argument to etfe is a smoothing factor, lower implies smoother, and the
third argument is the number of frequencies that we estimate the spectrum at. In
the plot you can see the frequency dependency. Does the frequency dependency seems
reasonable for the knowledge you have on roads? Did you draw the correct conclusion
about the periodicity above?

8 MATLABfunctions

function spekgui(action,varargin)
% spekgui opens a window for spectral estimation.
% Import data by putting them into a "structure", write the name in the "Import
% and push the button.
% Example:
% >> litedata.t=linspace(0,50,1001);
% >> litedata.x=sin(2*pi*litedata.t)+randn(1,1001)*0.5;
% The different spectral estimation methods are :
Welch: averaging over m periodogram.
Welch with time window Hanning.
% The covariance function is estimated from data or from
% the spectral density estimate.
% The estimates of the covariance function and spectral density
% is exported to Matlab with the "Export"-button.