1. ## Matlab programming

I am given this question

and ask to construct a numerical generative model for an autoregressive oreder two; xt - xt-1 + 0.5xt-2 = et. generate and plot 256 time series then numerically obtain the spectrum and plot them.

where the noise process is assumed to be one.

I have done something but Im not so sure what im doing. is it numerical genrative?

et = 1;
n = 257;
x = zeros(n-1,1);
x(1) = rand(1)*4
x(2) = rand(1)*8
for t = 3:n-1;
x(t) = x(t-1) - 0.5*x(t-2) + et
end

2. I'm don't quite understand what you're trying to accomplish. Could you give an example of something similar?

I do however have a comment on your code. Why not just set n=256 and x=zeros(n,1) and instead of for t=3:n-1 use for t=3:n?

3. Originally Posted by sensitive
I am given this question

and ask to construct a numerical generative model for an autoregressive oreder two; xt - xt-1 + 0.5xt-2 = et. generate and plot 256 time series then numerically obtain the spectrum and plot them.

where the noise process is assumed to be one.

I have done something but Im not so sure what im doing. is it numerical genrative?

et = 1;
n = 257;
x = zeros(n-1,1);
x(1) = rand(1)*4
x(2) = rand(1)*8
for t = 3:n-1;
x(t) = x(t-1) - 0.5*x(t-2) + et
end

Try something like:

Code:
e=randn(1,1024);    #process over-long time series
x=zeros(1,1024);

for idx=3:1024;x(idx)=x(idx-1)-0.5*x(idx-2)+e(idx);end;

xx=x(256:256+255);  #cut 256 points from the centre
#of x to eliminate start
#transient
plot(xx);

sp=fft(xx);         #get spectrum
plot(abs(sp(1:128)));
ZB

4. xx=x(256:256+255); #cut 256 points from the centre
#of x to eliminate start
#transient

I dont quite get wat this code does. please explain

5. xx=x(256:256+255); #cut 256 points from the centre
#of x to eliminate start
#transient

I dont understand why we start plotting from point 256th onwards?

6. Originally Posted by sensitive
xx=x(256:256+255); #cut 256 points from the centre
#of x to eliminate start
#transient

I dont quite get wat this code does. please explain

I have generated a sample x which is 1024 samples long which has a start up transient at the beginning, so I clip out a section from x of length 256 as this is the length of sample you are asked to generate.

ZB

7. Thank you I get it now...Previously I didnt understand the meaning of transient.

Just one quick question what is a rectangular smoothing filter?

I am asked to apply rectangular smoothing filters to the spectrum obtained in previous query. Is there a function in Matlab that I have to know to do this?

Also just a quick ques on my code. It is a simple plotting code. Iam trying to plot the function with respective w values

sigma = 1;
w = 1:1:5
F=(sigma^2)./2*pi*((2+0.5^2)-cos(w)+cos(2*w))
plot(w,F,'.-')
xlabel 'w'
ylabel 'F(w)'
title 'Power Spectrum of AR(2) model'

The plot I obtained for the code is ploting straight lines from one point to another. How do I plot a curve or is there something wrong on how i define my function that should enable me to plot a curve.

Thank you..

8. Originally Posted by sensitive
Thank you I get it now...Previously I didnt understand the meaning of transient.

Just one quick question what is a rectangular smoothing filter?

I am asked to apply rectangular smoothing filters to the spectrum obtained in previous query. Is there a function in Matlab that I have to know to do this?

Also just a quick ques on my code. It is a simple plotting code. Iam trying to plot the function with respective w values

sigma = 1;
w = 1:1:5
F=(sigma^2)./2*pi*((2+0.5^2)-cos(w)+cos(2*w))
plot(w,F,'.-')
xlabel 'w'
ylabel 'F(w)'
title 'Power Spectrum of AR(2) model'

The plot I obtained for the code is ploting straight lines from one point to another. How do I plot a curve or is there something wrong on how i define my function that should enable me to plot a curve.

Thank you..
MATLAB will evaluate the F at each value of w which you have defined as w=1:1:5 which is the same as [1,2,3,4,5]. So F is evaluated at only 5 points and MATLAB just draws straight lines between each point. So to get more points you need to define more values for w. Try this:

w=1:0.1:5 (This gives you a vector from 1 to 5 in increments of 0.1, you can change this to whatever you want if 0.1 isn't small enough still)

or try:

w=linspace(1,5,100) (This gives a vector from 1 to 5 in 100 equally spaced increments, again change whatever you want to get the desired results)

I presonally like linspace more because if you do something strange like w=1:0.3:5 MATLAB will cutoff the vector at 4.9. linspace assures that you get your starting and ending values and it's also easy to know the length of the vector. In the above case it's just 100.

9. Thank you very much... I am very new to Matlab so I forget things sometimes. Ive came across and used linspace before but it really helps now.thank you

I think Im left with my query on what is a rectangular smoothing filter?

I am asked to apply rectangular smoothing filters to the spectrum obtained in previous query. Is there a function in Matlab that I have to know to do this?

Any input is very much appreciated

10. Originally Posted by sensitive
Thank you very much... I am very new to Matlab so I forget things sometimes. Ive came across and used linspace before but it really helps now.thank you

I think Im left with my query on what is a rectangular smoothing filter?

I am asked to apply rectangular smoothing filters to the spectrum obtained in previous query. Is there a function in Matlab that I have to know to do this?

Any input is very much appreciated
It is a moving window averager. So for instance if our window length is $11$ we would have something like:

$x_{out}(i)=\frac{1}{11}\sum_{j=-5}^5 x_{in}(i+j)$

Note the start up will need to be done carefully to avoid negative indices.

ZB

11. Im trying to make a code in matlab for the moving average formula you gave. but im not sure the outputs are correct. I plotted the values of ys(i) and the values before the 50th points are all zeros. shouldnt it smoothed the data instead of
going to zeros..Or does the smoothed data starts from point 50th onwards and neglect the points before that?

n = 100;
N = 40;
l = 2*N + 1;
for i = 50:n
for j = -N:N
ys(i) = sum(xx(i+j))/l
end
end

Could you please correct me where Ive done wrong.thank you

12. Originally Posted by sensitive
Im trying to make a code in matlab for the moving average formula you gave. but im not sure the outputs are correct. I plotted the values of ys(i) and the values before the 50th points are all zeros. shouldnt it smoothed the data instead of
going to zeros..Or does the smoothed data starts from point 50th onwards and neglect the points before that?

n = 100;
N = 40;
l = 2*N + 1;
for i = 50:n
for j = -N:N
ys(i) = sum(xx(i+j))/l
end
end

Could you please correct me where Ive done wrong.thank you
try something like this:

Code:

x=rand(1,256);  #dummy data to filter

lwind=11;       #window length

lpad=floor(lwind/2);  #extra zeros needed to start filter off and end it

end