# Plotting signal sequences on Matlab

#### tquiva

Joined Oct 19, 2010
176
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?

Rich (BB code):
% Signal bbox
n=4; a=1;
star=-15:0.01:15;
for x=1:length(star);
if abs(star(x))<=n
bbox(x)=a;
else
bbox(x)=0;
end;
end;
plot(star,bbox),xlabel('*'),ylabel('bbox[*]'),title('Signal bbox[*]')
;

% Signal cox
n=4; b=1;
star=-15:0.01:15;
for x=1:length(star);
if abs(star(x))<=n
cox(x)=b*cos(pi*star/n);
else
cox(x)=0;
end;
end;
plot(star,cox),xlabel('*'),ylabel('cox[*]'),title('Signal cox[*]')

% Signal c2ox
n=4; c=1;
star=-15:0.01:15;
for x=1:length(star);
if abs(star(x))<=n
c2ox(x)=c*(cos(pi*star/n))^2;
else
c2ox(x)=0;
end;
end;
plot(star,c2ox),xlabel('*'),ylabel('c2ox[*]'),title('Signal c2ox[*]')

Last edited:

#### t_n_k

Joined Mar 6, 2009
5,455

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:

#### tquiva

Joined Oct 19, 2010
176
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.
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: 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:

#### t_n_k

Joined Mar 6, 2009
5,455
Take the case of ....

$$cox_{n} [*]=\{^{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.

#### tquiva

Joined Oct 19, 2010
176
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:

Rich (BB code):
% Signal bbox
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;
plot(star,bbox),xlabel('*'),ylabel('bbox
[*]'),title('Signal bbox
[*]')

% Signal cox
n=4; b=sqrt(2/n);
for x=1:length(star);
if abs(star(x))<=n
cox(x)=b*cos(pi*star(x)/n);
else
cox(x)=0;
end;
end;
plot(star,cox),xlabel('*'),ylabel('cox
[*]'),title('Signal cox
[*]')

% Signal c2ox
n=4; c=sqrt(8/(3*n));
for x=1:length(star);
if abs(star(x))<=n
c2ox(x)=c*(cos(pi*star(x)/n))^2;
else
c2ox(x)=0;
end;
end;
plot(star,c2ox),xlabel('*'),ylabel('c2ox
[*]'),title('Signal c2ox
[*]')
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?

#### t_n_k

Joined Mar 6, 2009
5,455
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.

#### t_n_k

Joined Mar 6, 2009
5,455
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.