Results 1 to 13 of 13

Math Help - Matlab programming

  1. #1
    Newbie
    Joined
    Dec 2007
    Posts
    8

    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

    please help
    Follow Math Help Forum on Facebook and Google+

  2. #2
    Newbie
    Joined
    Dec 2007
    Posts
    6
    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?
    Follow Math Help Forum on Facebook and Google+

  3. #3
    Member
    Joined
    May 2006
    Posts
    244
    Quote Originally Posted by sensitive View Post
    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

    please help
    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
    Follow Math Help Forum on Facebook and Google+

  4. #4
    Newbie
    Joined
    Dec 2007
    Posts
    8
    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
    Follow Math Help Forum on Facebook and Google+

  5. #5
    Newbie
    Joined
    Dec 2007
    Posts
    8
    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?
    Follow Math Help Forum on Facebook and Google+

  6. #6
    Member
    Joined
    May 2006
    Posts
    244
    Quote Originally Posted by sensitive View Post
    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
    Follow Math Help Forum on Facebook and Google+

  7. #7
    Newbie
    Joined
    Dec 2007
    Posts
    8
    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..
    Follow Math Help Forum on Facebook and Google+

  8. #8
    Newbie
    Joined
    Dec 2007
    Posts
    6
    Quote Originally Posted by sensitive View Post
    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.
    Follow Math Help Forum on Facebook and Google+

  9. #9
    Newbie
    Joined
    Dec 2007
    Posts
    8
    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
    Follow Math Help Forum on Facebook and Google+

  10. #10
    Member
    Joined
    May 2006
    Posts
    244
    Quote Originally Posted by sensitive View Post
    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
    Follow Math Help Forum on Facebook and Google+

  11. #11
    Newbie
    Joined
    Dec 2007
    Posts
    8
    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
    Last edited by sensitive; December 11th 2007 at 02:15 PM.
    Follow Math Help Forum on Facebook and Google+

  12. #12
    Member
    Joined
    May 2006
    Posts
    244
    Quote Originally Posted by sensitive View Post
    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
     
    x=[zeros(1,lpad],x,zeros(1,lpad]]
    y=zeros(1,256);
     
    for idx=1:256
      y(idx)=sum(x((idx):(idx+lwind-1));
    end
    Note this will have start and end transients of length lpad

    ZB
    Follow Math Help Forum on Facebook and Google+

  13. #13
    Newbie
    Joined
    Dec 2007
    Posts
    8
    Thx..Ive tried with the code you gave. Now i can see that it smoothens the data. Thank you very much. I appreciate it.
    Follow Math Help Forum on Facebook and Google+

Similar Math Help Forum Discussions

  1. Matlab programming error help
    Posted in the Math Software Forum
    Replies: 7
    Last Post: February 1st 2010, 07:25 PM
  2. MATLAB linear programming question
    Posted in the Math Software Forum
    Replies: 0
    Last Post: January 31st 2009, 02:08 PM
  3. MATLAB linear programming
    Posted in the Math Software Forum
    Replies: 0
    Last Post: January 31st 2009, 02:02 PM
  4. Replies: 1
    Last Post: November 17th 2008, 03:18 AM
  5. C++/matlab programming
    Posted in the Math Software Forum
    Replies: 1
    Last Post: January 21st 2008, 10:09 PM

Search Tags


/mathhelpforum @mathhelpforum