Lab 2 Additional MATLAB Features, Properties of Signals and Systems, Convolution
Lab 2 Additional MATLAB Features, Properties of Signals and Systems, Convolution
ELEC 342 Lab 2: Additional MATLAB features, Properties of Signals and Systems, Convo-
lution and System Response
Part I of this lab will introduce some more features of the MATLAB programming language:
looping and conditional selection, as well as its array processing features. Various properties of
signals and systems such as: linearity, evenness, oddness will be verified using simple MATLAB
scripts. In Part II, the MATLAB convolution function will be used to determine a system’s
response to an input signal.
PART I
Example 1: The following for loop computes the sum of all the integers between 1 and 10:
The disp command is used to display the value of a variable (or a string enclosed in single
quotes).
Example 2: The following loop finds the sum of the squares of the integers from 1 to 5, sum = 12
+ 22 + 32 + 42 + 52 = 1 + 4 + 9 + 16 + 25 = 55 .
clear;
sum = 0; % initialize sum to 0
for index = 1 : 5 % set up the loop index
square = index ^ 2; % compute square of current loop index and
2
clear;
x = [ 1 : 5 ]; % intialize the x array
y = x .^2 ; % compute the squares of the elements in the
% x array
sum = 0;
for index = y % loop through every element in the y array
sum = sum + index; % and add each element to sum
end
disp(’The sum of the squares of 1 to 5 is’)
disp(sum)
A conditional statement is used to change the flow of control within a script. MATLAB has two
basic conditional statements: the if and the switch. Some examples of the variations of the if
statement are presented.
if ( condition )
statement1
statement2
...
statementn
end
Two conditions may be combined using the & ( logical AND) and the | ( logical OR) operators
to form more complex conditions such as:
If the specified condition is true, the statements within the if are executed, otherwise the state-
ments are not executed.
clear
number = input(’Enter a number ’)
if ( number < 0 )
disp(’Number is negative’)
end
This example also introduce the input statement which displays the specified string and reads
in put from the keyboard and assigns it to the variable specified on the left hand side of the = oper-
ator.
Enter a number 2
number =
Enter a number -4
number =
-4
Number is negative
clear
number = input(’Enter a number ’)
if ( number < 0 )
4
disp(’Number is negative’)
else
disp(’Number is positive’)
end
Only one of the two disp statements will be executed depending upon the value which is pro-
vided as input.
Example 4: When comparing non-integer numbers, one should never compare for direct equality
as round-off errors in the representation of some number may lead to unexpected results as in:
clear
x = 0.1 + 0.1 + 0.1
if ( x == 0.3)
disp( ’ x is equal to 0.3’)
else
disp( ’ x is not equal to 0.3’)
end
x =
0.3000
Rather than comparing for direct equality, we should check that the absolute value of the differ-
ence between x and 0.3 is less than some threshold value (0.001 for example) :
clear
x = 0.1 + 0.1 + 0.1
if ( abs( x - 0.3 ) <= 0.0001)
disp( ’ x is equal to 0.3’)
else
disp( ’ x is not equal to 0.3’)
end
x =
0.3000
x is equal to 0.3
5
The same idea can be used to “compare” whether two vectors are equal:
If for our purposes we consider our accuracy to be 0.01, then we can compute the difference
between the two vectors as:
and then loop through every element in this diff vector and check if:
For the values the given values of first and second, this for loop will not display the ‘not
equal’ output since the difference does not exceed 0.01.
Linearity:
The next example checks to see if a given system is satisfies the additive property of linear sys-
tems:
given an input signal x1[n], the output response produced is y1[n] and
given an input signal x2[n], the output response produced is y2[n],
then for an input signal x3[n] = x1[n] + x2[n], the output response y3[n] = y1[n] + y2[n] 1
Example 1: This example determines whether the system defined by y[n] = 2x[n] gives outputs
which are consistent with a linear system.
% Ted Obuchowicz
% Apr 23, 2012 16:28
6
clear
% define n
n = [ 0 : 4 ]
x3 = x1 + x2 ;
if ( y3 == ( y1 + y2 ) )
disp( ’Outputs are consistent with a linear system’)
else
disp( ’System is not linear’)
end
Even-Odd signals:
The following example determines if the signal sin( (2pi/10) * n ) for n = 0, 1, 2, .. 9 is even or
odd:
% define x1[n]
x1 = sin( 2*pi/10 * n )
subplot(1,2,1)
stem(n, x1)
% define x2 = x1[-n]
x2 = sin( 2*pi/10 * (-n) )
subplot(1,2,2)
stem(-n, x2)
if ( x2 == x1 )
disp(’EVEN’)
elseif ( x2 == (-x1) )
disp(’ODD’)
else
disp(’NEITHER EVEN NOR ODD’)
end
Note that we should be checking that the absolute value of the difference between the elements of
x1 and x2 is less than some threshold in case there are round-off/truncation errors.
Example 2: This example determines the even and odd components of a signal which consists of
unit step function. It illustrates how one overcomes the limitation that MATLAB does not allow
for negative array indices (which are used in the representation of a discrete signal over some span
of intervals such as -n to +n).
Suppose we wish to create a a unit step function ( x1[n]) and it’s mirror image ( x2[n] = x1[-n])
as shown in Figure 1:
8
0.8
0.6
x[n]
0.4
0.2
0
−4 −3 −2 −1 0 1 2 3 4
n
0.8
0.6
x[−n]
0.4
0.2
0
−4 −3 −2 −1 0 1 2 3 4
n
% define n
n = -4 : 4
% define x1[n] as unit step
x1 = ones(1,9); % set all 9 array elements to 1
x1(1:4) = 0; % set the first 4 elements to 0
Note that x1 is a 1 x 9 array with elements equal to x1(1), x1(2) , x1(3), ... x1(9) since in MAT-
LAB array indices start from 1.
but MATLAB will report an error concerning the existence of negative array indices:
We can now assign the elements of the MATLAB variable x2 in the following manner:
% a brute-force approach
% not particularly elegant, but it accomplishes the task
x2(1) = x1(9)
x2(2) = x1(8)
x2(3) = x1(7)
x2(4) = x1(6)
x2(5) = x1(5)
x2(6) = x1(4)
x2(7) = x1(3)
x2(8) = x1(2)
x2(9) = x1(1)
It should be noted that the above 9 assignment statements may be expressed more succinctly using
a for loop as in:
for index = 1 : 9
x2(index) = x1(10 -index);
end
Here the complete script file which also decomposes the original signal into its even and odd com-
ponents :
% T. Obuchowicz
% determines if a signal x[n] = function(n)
% is even or odd
% Tue Apr 24 14:14:13 EDT 2012
% define x1[n]
x1 = ones(1,9)
x1(1:4) = 0
10
for index = 1 : 9
x2(index) = x1(10 -index);
end
subplot(4,1,1)
stem(n,x1)
ylabel(‘ x[n] ‘ )
xlabel(‘ n ‘ )
subplot(4,1,2)
stem(n,x2)
ylabel(‘ x[-n] ‘)
xlabel(‘ n ‘ )
if ( x2 == x1 )
disp(‘EVEN’)
elseif ( x2 == (-x1) )
disp(‘ODD’)
else
disp(‘NEITHER EVEN NOR ODD’)
end
11
even_comp = (1/2) * ( x1 + x2 )
odd_comp = (1/2) * ( x1 - x2 )
subplot(4,1,3)
stem(n, even_comp)
xlabel(‘ n ‘ )
ylabel(‘ Even component of x[n] ‘)
subplot(4,1,4)
stem(n, odd_comp)
xlabel(‘ n ‘ )
ylabel(‘ Odd component of x[n] ‘)
1
x[n]
0.5
0
−4 −3 −2 −1 0 1 2 3 4
n
1
x[−n]
0.5
0
−4 −3 −2 −1 0 1 2 3 4
n
Even component
0.5
0
−4 −3 −2 −1 0 1 2 3 4
n
Odd component
0.5
−0.5
−4 −3 −2 −1 0 1 2 3 4
n
MATLAB allows the results of a plot window to be saved to a file. To save the results of the cur-
rent plot window to a Postscript file , use the print command in MATLAB as in:
Self-study: Use help print to learn what the various options ( -dpsc ) to the print com-
mand mean.
Questions
Create script files for each of the following questions. Include your name and ID as comments in
each script. For each question, submit as part of the written lab report the script file and any
required plots.
Question 1:
(a) Plot the input signal x[n] = n and the output signal y[n] = x2[n] over the interval n = 0,
1,2,3,4,5,6,7,8,9. Compute the total energy in the signal x[n] and y[n] (Hint: the total energy in a
signal is equal to the sum of the squares of all the values contained in the signal). Use the disp
command to display the two energies 3.
(b) Repeat part (a) using the input signal x[n] = sin( (2pi)/10 * n) , n = 0, 1, 2 ...9 . Use the MAT-
LAB function sin to compute the values of the input signal over the specified interval. Use the
help sin facility to learn how to use the sin function.
Question 2:
(a) Determine whether the discrete time system which has an output y[n] = 2 * x[n] over the
interval 0 <= n <= 10 is linear or not by determining the response y1[n] to the input signal x1[n] =
sin( (2*pi /10 ) * n ) and the response y2[n] to the input signal x2[n] = cos( (2*pi /10 ) * n ).
Determine the response y3[n] to the input signal x3[n] = x1[n] + x2[n] and compare it with y4[n] =
y1[n] + y2[n] . Plot (using stem) in one graph all the input signals and their corresponding out-
put signals. Use the disp command to output whether the system has ‘outputs consistent with a
linear system ’ or ‘not linear’.
(ii) y [ n ] = 2x [ n ] + 5δ [ n ]
14
are linear and time-invariant. Use the input data x[n] = [0,1]. Next, using a larger set of values,
for your choice of the input data, repeat the experiment and analyze and interpret the results
obtained with this new data set. Do the new results validate or invalidate the original results
obtained with x[n] = [0,1] as choice of input data? Explain how a choice of data used may impact
the results obtained.
Question 3:
(a) Plot the following signal x[n], it’s mirror image x[-n], and it’s even and odd components:
(c) Compare and contrast the two methods used to generate the two MATLAB arrays x1 and x2
in the following MATLAB code:
% T. Obuchowicz
%Fri Apr 27 16:03:27 EDT 2012
clear
n = [1 : 20 ]
x1 = sin((2*pi/40) * n) .* cos((2*pi/40) * n)
for index = 1 : 20
end
subplot(2,1,1)
stem(n, x1)
title(‘Elegant method making full use of MATLABs array capabilities’)
xlabel(‘n’)
ylabel(‘x[n]’)
subplot(2,1,2)
stem(n, x2)
title(‘Gets the job done, but it is a lot of work and we are not in the MATLAB
mindset’)
xlabel(‘n’)
ylabel(‘x[n]’)
15
PART II
Introduction:
1
H ( z ) = -------------------
1 –1
1 – --- z
4
From Table 13.1 of the textbook (p.572 Mandel and Asif) it can be determined that discrete-time
representation of H(n) is:
1n
H ( n ) = --- U [ n ]
4
The following difference equation can also be used to represent the system:
x[n] y[n]
+
−1
Z
1
4
Questions
16
Question 1:
For this system, compute the response y[n] over the interval 0 <= n <= 9 using the input signal
x[n] given in Figure 4 .
0.9
0.8
0.7
0.6
x[n]
0.5
0.4
0.3
0.2
0.1
0
0 1 2 3 4 5 6 7 8 9
n
Question 2:
MATLAB contains a built-in function called conv which performs the convolution of two vec-
tors:
Compute the system response (using the input signal x[n] given in Question (1)) by convolving
H[n] and x[n] using the MATLAB conv function. Plot the response using stem. Compare this
response with that obtained in Question 1. Explain any differences between the two outputs.
Question 3:
Sometimes a system that we are interested in is a black box, i.e., we can see what goes into the
system and what comes out of the system but we cannot see whats inside the system.
In such a case we can try to find out some things about the system by choosing particular inputs to
put in and then observing what comes out.
We could also use this same process of putting inputs in and observing the output to determine the
system entirely. When we do this it is called system identification.
y = Sys1(x)
Inputs to the function is the input vector x. The vector y is a vector which is the corresponding
output signal. Note that y may not be the same length as x.
i. Design an experiment to test linearity. Discuss why you chose particular inputs to put into the
system and what you are expecting to learn by observing the outputs. This answer should be 0.5 to
1 page long.
ii. Describe what you observed when you put your inputs into the system.
iii. What did you conclude from your experiments? Was the system linear or not? Can you state
this conclusion with certainty?
i. Design an experiment to test time invariance. Discuss why you chose particular inputs to put
into the system and what you are expecting to learn by observing the outputs. This answer should
be 0.5 to 1 page long.
ii. Describe what you observed when you put your inputs into the system.
iii. What did you conclude from your experiments? Was the system time invariant or not? Can you
state this conclusion with certainty?
18
References
1. Continuous and Discrete Time Signals and Systems, M. Mandal and A. Asif, Camebridge Uni-
versity Press, ISBN 0-521-85455-5, 2007, p.73.
2. Continuous and Discrete Time Signals and Systems, M. Mandal and A. Asif, Camebridge Uni-
versity Press, ISBN 0-521-85455-5, 2007, p.21.
3. ELEC 264, Winter 2011, Lab assignment 1, Dr. A. Amer and Dr. D. Davis.
4. Signals and Systems, 2nd ed., A.Oppenheim, A.S. Willsky, S. Nawad, Prentice-Hall, ISBN 0-
13-814757-4, 1997, p.784.
5. Signals and Systems, 2nd ed., A.Oppenheim, A.S. Willsky, S. Nawad, Prentice-Hall, ISBN 0-
13-814757-4, 1997, p. 784.
6. Signals and Systems, 2nd ed., A.Oppenheim, A.S. Willsky, S. Nawad, Prentice-Hall, ISBN 0-
13-814757-4, 1997, p. 785.
T. Obuchowicz
May 2, 2014