Plotting signal sequences on Matlab

Discussion in 'Homework Help' started by tquiva, Aug 27, 2011.

  1. tquiva

    Thread Starter Member

    Oct 19, 2010
    176
    1
    Hi, could someone please help me with the attached problem.
    I'm trying to plot these signals onto Matlab but I'm not exactly sure how to.


    The problem says that a, b, and c are chosen so that the sequences have unit energy. I'm not sure how to go about doing this, but here's my Matlab code so far. I get an error that my variable n is undefined. How would I define it and all the other variables?


    Code ( (Unknown Language)):
    1. % Signal bbox
    2. n=4; a=1;
    3. star=-15:0.01:15;        
    4. for x=1:length(star);
    5.     if abs(star(x))<=n
    6.         bbox(x)=a;
    7.     else
    8.         bbox(x)=0;
    9.     end;
    10. end;
    11. plot(star,bbox),xlabel('*'),ylabel('bbox[*]'),title('Signal bbox[*]')
    12. ;
    13.  
    14. % Signal cox
    15. n=4; b=1;
    16. star=-15:0.01:15;        
    17. for x=1:length(star);
    18.     if abs(star(x))<=n
    19.         cox(x)=b*cos(pi*star/n);
    20.     else
    21.         cox(x)=0;
    22.     end;
    23. end;
    24. plot(star,cox),xlabel('*'),ylabel('cox[*]'),title('Signal cox[*]')
    25.  
    26. % Signal c2ox
    27. n=4; c=1;
    28. star=-15:0.01:15;        
    29. for x=1:length(star);
    30.     if abs(star(x))<=n
    31.         c2ox(x)=c*(cos(pi*star/n))^2;
    32.     else
    33.         c2ox(x)=0;
    34.     end;
    35. end;
    36. plot(star,c2ox),xlabel('*'),ylabel('c2ox[*]'),title('Signal c2ox[*]')
    37.  
     
    Last edited: Aug 28, 2011
  2. t_n_k

    AAC Fanatic!

    Mar 6, 2009
    5,448
    782
    A couple of comments.

    The variable 'n' is to be a positive integer. As you have defined the array 'star' with 0.01 increments from -15 onwards to +15 the elements of 'star' will not all be positive integers - some being negative and / or non-integer. I'm assuming these are meant to be discrete signal sequences - although I may have misinterpreted the notation.

    You've also got a couple of errors such as in the line

    cox(x)=b*cos(pi*star/n);

    which should be

    cox(x)=b*cos(pi*star(x)/n);

    and also the line

    c2ox(x)=c*(cos(pi*star/n))^2;

    which should be

    c2ox(x)=c*(cos(pi*star(x)/n))^2;

    To set the variables a, b & c to achieve unit energy you would have to do something like equating the function

    \int_0^{n'} (f(x))^2dx=1

    for each case

    My guess is that (in one or more of the three cases) increasing values of n produce increasingly better agreement between the discretely sampled energy case and the continuous energy case - as exemplified in the generic integral function above. There is an exception to this statement regarding one of the three cases which I think you might readily deduce yourself.
     
    Last edited: Aug 29, 2011
  3. tquiva

    Thread Starter Member

    Oct 19, 2010
    176
    1
    Thank you for the tips on my Matlab script. However, I'm still confused on how to choose the a, b, c values to obtain unit energy.

    So for the function cox_n
    [*] , I would square this and integrate it? But what does the limits of 0 to n' mean? I'm still a little lost. Could you please help me out? I really appreciate your help.

    I integrated the cox_n function with the given integral, and received the following answer:

    [​IMG]

    I didn't use any limits, but this is a rather length answer. What am I doing wrong?

    But for the function, bbox, a=1? Is that correct?
     
    Last edited: Aug 30, 2011
  4. t_n_k

    AAC Fanatic!

    Mar 6, 2009
    5,448
    782
    Take the case of ....


    cox_{n}<br />
[*]=\{^{bcos(\pi * /n), \  \ |*| \leq n}_{0, \ \ \ |*|\gt n^{'}}

    change the variable n' to x (to avoid confusion with n & n' terms) such that

    f(x)=bcos(\frac{\pi x}{n})

    Now evaluate the energy of f(x) ...

    E\_{f(x)}=\int^{x=n}_{0}{(f(x))^2 dx}


    E\_{f(x)}=\int^{x=n}_{0}{(bcos(\frac{\pi x}{n})^2 dx}

    E\_{f(x)}=\int^{x=n}_{0}{b^2cos^2(\frac{\pi x}{n}) dx}

    E\_{f(x)}=\int^{x=n}_{0}{b^2(\frac{1+cos(\frac{2 \pi x}{n})}{2}) dx}

    which after integration on the limits reduces to

    E\_{f(x)}=\frac{nb^2}{2}

    equating this to unit energy (or 1) gives

    b=sqrt{\frac{2}{n}}

    In a similar manner it would be true that for the bbox function

    a=\frac{1}{sqrt{n}}

    Finding c is a bit more complex - you should try to show that for unit energy

    c=sqrt{\frac{8}{3n}}

    In your Matlab code you could add computations for the three sequences using the a, b & c values above to evaluate the energy content for each sequence. The values should converge to '1' as n increases - you assign the n values as you wish. If you include n=0 in the energy evaluation, then you would possibly need to replace the 'n' value in the energy computation functions with (n+1) in each case. See how it goes anyway.
     
  5. tquiva

    Thread Starter Member

    Oct 19, 2010
    176
    1
    thank you so much. I worked through all the steps, and I finally understand how to obtain the a,b,c values. I inputted these into Matlab code, and this is now my most up-to-date script:

    Code ( (Unknown Language)):
    1. % Signal bbox
    2. n=4; a=1/sqrt(n);
    3. star=0:0.01:15;        
    4. for x=1:length(star);
    5.     if abs(star(x))<=n
    6.         bbox(x)=a;
    7.     else
    8.         bbox(x)=0;
    9.     end;
    10. end;
    11. plot(star,bbox),xlabel('*'),ylabel('bbox
    12. [*]'),title('Signal bbox
    13. [*]')
    14.  
    15. % Signal cox
    16. n=4; b=sqrt(2/n);      
    17. for x=1:length(star);
    18.     if abs(star(x))<=n
    19.         cox(x)=b*cos(pi*star(x)/n);
    20.     else
    21.         cox(x)=0;
    22.     end;
    23. end;
    24. plot(star,cox),xlabel('*'),ylabel('cox
    25. [*]'),title('Signal cox
    26. [*]')
    27.  
    28. % Signal c2ox
    29. n=4; c=sqrt(8/(3*n));    
    30. for x=1:length(star);
    31.     if abs(star(x))<=n
    32.         c2ox(x)=c*(cos(pi*star(x)/n))^2;
    33.     else
    34.         c2ox(x)=0;
    35.     end;
    36. end;
    37. plot(star,c2ox),xlabel('*'),ylabel('c2ox
    38. [*]'),title('Signal c2ox
    39. [*]')
    Did I do it correctly?

    For part b of the problem, I am asked measure the dissimilarity between a pair of signals. If I'm not mistaken, this means that I would find the difference in energies of the signals? Such as using the integration that you suggested for each signal to obtain the energies, then subtracting the from another... Is my conclusion correct?
     
  6. t_n_k

    AAC Fanatic!

    Mar 6, 2009
    5,448
    782
    Consider the code snippet ....

    n=4; a=1/sqrt(n);
    star=0:0.01:15;
    for x=1:length(star);
    if abs(star(x))<=n
    bbox(x)=a;
    else
    bbox(x)=0;
    end;
    end;

    I would re-cast it as

    n=1000; a=1/sqrt(n+1);
    star=0:1:n+500;
    for x=1:length(star);
    if abs(star(x))<=n
    bbox(x)=a;
    else
    bbox(x)=0;
    end;
    end;

    The energy would be found from something like ....

    sum(bbox.*bbox)

    where ".*" means element by element multiplication - i.e. basically squaring the individual element values. I'm not sure if sum() is a Matlab function - I don't use Matlab. By sum() I just mean adding up all the elements of the squaring process.

    In reality you don't need to concern yourself with the sequence values outside of values greater than 'n', but it is informative to note the sequence forms over a range greater than 'n'.

    As I said in previous post you set 'n' to whatever you wish in your code.
     
  7. t_n_k

    AAC Fanatic!

    Mar 6, 2009
    5,448
    782
    The energy values can probably be computed by a simpler relationship such as

    E_bbox=sum(bbox^2)

    if Matlab allows that overall function ...

    and for the cox sequence ...

    E_cox=sum(cox^2)

    and so on.
     
  8. tquiva

    Thread Starter Member

    Oct 19, 2010
    176
    1
    I was able to finish the entire problem. Thank you for all your help!

    Cheers
     
Loading...