ECE 3300
MATLAB Assignment 2
This assignment (1) demonstrates how to work with complex-valued signals and periodic signals
in MATLAB, and (2) shows how to obtain energy, power, co
elation, and mean-square e
or.
Complex Numbers and Complex-Valued Lists
a=3+4j sets a to the complex number 3 + 4j. Complex numbers can be used in lists; an example
is b=[3-4j,3j,-4]. abs(x) computes the magnitude, angle(x) finds the phase (in radians),
conj(x) takes the complex conjugate, real(x) determines the real part, and imag(x) obtains
the imaginary part (excluding the j). Each of these commands can be used on lists; for example,
if b is as defined above, abs(b) equals [5,3,4], angle(b) equals [ XXXXXXXXXX1416],
conj(b) equals [3+4j,-3j,-4], real(b) equals [3,0,-4], and imag(b) equals [-4,3,0].
Rounding Numbers
The command round(t) rounds t to the nearest integer. If t is exactly between two integers, it
ounds up to the higher integer. round can be used on lists; for example, if t=[ XXXXXXXXXX
XXXXXXXXXX], round(t)=[ XXXXXXXXXX]. Similarly, the command ceil(t)
ounds t up to the next higher integer, and the command floor(t) rounds t down to the next
lower integer. Both of these commands can also be used on lists. For the example of t above,
ceil(t)=[ XXXXXXXXXX] and floor(t)=[ XXXXXXXXXX].
Periodic Signals
Suppose x̃(t) is a periodic signal with fundamental period T0 and fundamental cycle x(t) and
define y(t) = x(t − T0r(t/T0)), where r(·) is the function round(·). Then we claim that y(t)
equals x̃(t). To prove this result we need to show two facts: (1) y(t) has fundamental cycle x(t).
(2) y(t) is periodic with fundamental period T0. To prove (1), we note that −T0/2 ≤ t < T0/2
implies that − 12 ≤ t/T0
1
2 ; simply divide each term in the first inequality by T0 to see this. It
follows that r(t/T0) = 0, and thus, for −T0/2 ≤ t < T0/2, y(t) = x(t − T0r(t/T0)) = x(t). To
prove (2), we first note that r(t− 1) = r(t)− 1. (To see this, note that r(5.8− 1) = r(4.8) = 5 =
6− 1 = r(5.8)− 1.) It follows that
y(t− T0) = x(t− T0 − T0r( t−T0T0 ))
= x(t− T0 − T0(r( tT0 − 1)))
= x(t− T0 − T0(r( tT0 )− 1))
= x(t− T0 − T0r( tT0 ) + T0)
= x(t− T0r( tT0 ))
= y(t)
Thus we conclude that x̃(t) = x(t − T0r(t/T0)). We can write this as x̃(t) = x(tper) where
tper = t− T0r(t/T0).
To apply this result to MATLAB, suppose we wish to implement a periodic signal x̃(t) with
T0 = 2 and fundamental cycle x(t) = t
2(u(t)−u(t−1)) using a sampling time of 0.01. Further sup-
pose we wish to represent x̃(t) over ten periods, from t = −10 to 10. We first set t=-10:0.01:10
and t per=t-2.*round(t./2). Next we set x=t per.^2.*((t pe
=0)&(t pe
1)). The peri-
odic signal from t = −10 to t = 10 is represented by t and x. To prevent roundoff issues, T0
should be an integer multiple of the sampling time.
Discrete-time periodic signals are defined similarly except the the time values are integers.
For example, suppose N0 = 7 and x[n] =
n+3
2 (u[n + 2] − u[n − 3]), and we wish to repre-
sent x̃[n] from n = −50 to n = 50. We use the following MATLAB commands: n=-50:50,
n per=n-7.*round(n./7), and x=(n per+3)./2.*((n pe
=-2)&(n pe
=2)); the signal is rep-
1
esented from n = −50 to n = 50 by n and x.
PROBLEM STATEMENT: PART ONE
Consider (1) the periodic signals x̃(t) and ỹ(t), each with fundamental period T0 = 10, and with
fundamental cycles x(t) = (9− t2) sin(πt)(u(t+ 3)− u(t− 3)) and y(t) = (9− t2) cos(πt)(u(t+
3)− u(t− 3)). Also consider the non-periodic signal z(t) = x(t) + jy(t) where x(t) and y(t) are
as defined in the previous sentence. In everything below use a sampling time of 0.01.
1. Plot x̃(t) from t = −15 to t = 15.
2. Plot ddt ỹ(t) from t = −15 to t = 15.
3. Plot |z(t)| from t = −15 to t = 15.
4. Plot ∠z(t) from t = −15 to t = 15.
Energy of Discrete-Time Signals
Suppose the signal x[n] is defined over the range a ≤ n ≤ c. Then the energy is given by
Ex =
∑c
n=a |x[n]|2. If x[n] is represented in MATLAB via n=a:c and x, the energy can be
determined via sum(abs(x).^2).
Co
elation and Mean-Square E
or of Discrete-Time Signals
Suppose the signals x[n] and y[n] are both defined over the range a ≤ n ≤ c. Then the cor-
elation is given by Rx,y =
∑c
n=a x[n]y
∗[n] and the mean-square e
or is given by MSEx,y =∑c
n=a |x[n]− y[n]|2. If x[n] and y[n] are represented in MATLAB via n=a:c, x, and y, the cor-
elation can be determined via sum(x.*conj(y)), and the mean-square e
or can be obtained
via sum(abs(x-y).^2).
Energy of Continuous-Time Signals
Suppose the signal x(t) is defined over the range a ≤ t ≤ c. Then the energy is given by
Ex =
∫ c
a
|x(t)|2dt. We can approximate the integral as the sum of the areas of a bunch of
ectangles; the ith such rectangle has width b and height |x(a+ (i− 1)b)|2. Because the area of
a rectangle is the product of its width and height, it follows that, if c = a+ kb,
Ex =
∫ c
a
|x(t)|2dt =
∫ a+k
a
|x(t)|2dt ≈
k∑
i=1
|x(a+ (i− 1)b)|2
Thus, if the sampling time is b, and if x(t) is represented in MATLAB by t=a:b:c and x, then
Ex can be approximated by sum(abs(x).^2).*b.
Co
elation and Mean-Square E
or of Continuous-Time Signals
Suppose the signals x(t) and y(t) are defined over the range a ≤ t ≤ c. Then the co
elation
and mean-square e
or are given by Rx,y =
∫ c
a
x(t)y∗(t)dt and MSEx,y =
∫ c
a
|x(t) − y(t)|2dt.
Approximating the integrals as the sum of the areas of rectangles, we have
Rx,y =
∫ c
a
x(t)y∗(t)dt =
∫ a+k
a
x(t)y∗(t)dt ≈
k∑
i=1
x(a+ (i− 1)b)y∗(a+ (i− 1)b)
and
MSEx,y =
∫ c
a
|x(t)− y(t)|2dt =
∫ a+k
a
|x(t)− y(t)|2dt ≈
k∑
i=1
|x(a+ (i− 1)b)− y(a+ (i− 1)b)|2
2
Thus, if the sampling time is b, and if x(t) and y(t) are represented in MATLAB by t=a:b:c,
x, and y, then Rx,y can be approximated by sum(x.*conj(y)).*b and MSEx,y can be approxi-
mated by sum(abs(x-y).^2).*b.
Periodic Signals
For periodic signals, the power, co
elation, and mean-square e
or can be obtained from the
energy, co
elation, and mean-square e
or of the signals’ co
esponding fundamental cycles by
simply dividing the quantity by the fundamental period.
The following table summarizes the results assuming the starting signal is implemented via n=a:c
(if discrete time) or t=a:b:c (if continuous time) and x.
Quantity Equation t or n MATLAB
Ex
∑∞
n=−∞ |x[n]|2 n=a:c sum(abs(x).^2)
Ex
∫∞
−∞ |x(t)|
2dt t=a:b:c sum(abs(x).^2).*
Rx,y
∑∞
n=−∞ x[n]y
∗[n] n=a:c sum(x.*conj(y))
Rx,y
∫∞
−∞ x(t)y
∗(t)dt t=a:b:c sum(x.*conj(y)).*
MSEx,y
∑∞
n=−∞ |x[n]− y[n]|2 n=a:c sum(abs(x-y).^2)
MSEx,y
∫∞
−∞ |x(t)− y(t)|
2dt t=a:b:c sum(abs(x-y).^2).*
PROBLEM STATEMENT: PART TWO
Consider the non-periodic signals x(t) = (9−t2) sin(πt)(u(t+3)−u(t−3)) and y(t) = sin(πt)(u(t+
3) − u(t − 3)). In everything below use a sampling time of 0.01. For each part, print out the
value of the indicated quantities, including the name of each quantity with the co
esponding
value.
1. Determine Ex, Ey, Rx,y, and ρx,y.
2. Determine MSEx,y two ways – by the direct formula, and by using the equation Ex +Ey−
2Re{Rx,y}.
3. Determine the PERCENTAGE of energy in x(t) in the time interval −1 ≤ t ≤ 1. Also
determine the percentage of energy in y(t) in this same time interval.
3