Lab 1 Ahmad Sataish Irfa
Lab 1 Ahmad Sataish Irfa
The purpose of this lab is to review the fundamentals of signals and systems using MATLAB, with a focus
on signal transformations, even and odd signal decomposition, and convolution—a key property of Linear
Time Invariant (LTI) systems. The lab explores various MATLAB functions and manual coding techniques
to reinforce theoretical concepts while emphasizing practical implementation. Through interactive tasks
and coding challenges, students gain hands-on experience in analyzing and manipulating signals, as well
as developing efficient algorithms to solve problems in signal processing.
OBJECTIVE
The purpose of this lab is to review the fundamentals of signals and systems with MATLAB,
particularly:
SOFTWARE
• MATLAB
1 Lab Procedure
1.1 Matrices/Vectors in MATLAB
a) Make sure that you understand the colon notation. Explain in words what the following MATLAB code
will produce:
jkl = 0:6;
% This will create a vector array from 0 to 6 with a default spacing of 1. jkl
= 4:4:17;
% This will create a vector array from 4 to 17 with a spacing of 4.
jkl = 99:-1:88;
% This will create a vector array from 99 to 88 with a spacing of -1.
ttt = 2:(1/9):4;
% This will create a vector array from 2 to 4 with a spacing of 1/9. tpi
= pi * (0:0.1:2);
% This will create a vector array from 0 to 2 with a spacing of 0.1, ...
% and then multiply each element with pi.
b) Extracting and/or inserting numbers into a vector is very easy to do. Consider the following definition
of xx:
xx = [zeros(1,3), linspace(0,1,5), ones(1,5)];
[s1 s2] = size(xx) s3
= length(xx)
Explain the results echoed from the last four lines of the above code.
The size() statement returns [row column] as the output which are assigned to the variables s1 and s2.
As xx is a 1 x 13 row vector; s1 = 1 and s2 = 13. The length() statement returns the maximum of the
size() statement, and hence, s3 = 13 as well.
What’s the difference between a length and a size statement for a matrix? To test this define a matrix X
with arbitrary inputs, having multiple rows and columns and test the output of length() and size()
function on it.
Answer: The size() statement returns the [row column] dimensions of the matrix, whereas length()
returns the maximum of the two elements of size() statement.
c) Assigning selective values in a matrix differently. Comment on the result of the following assignments:
yy = xx; yy(4:6)
= pi*(1:3);
Answer: Assignment of yy(4:6) will update the elements 4, 5, and 6 with multiples of 𝜋.
To test this, define a matrix X with arbitrary inputs, having multiple rows and columns and test the
output of length() and size() function on it.
Assigning selective values in a matrix differently. Comment on the result of the following
assignments:
yy = xx;
yy(4:6) = pi*(1:3);
Figure 3: working on xx matrix
Only 4th to 6th elements of matrix are being multiplied with the vector.
Go to File > New > M–file. MATLAB editor will open up. Enter the following code in the editor and then
save the file as Namelab1.m
tt = -1 : 0.01 : 1;
xx = cos( 5*pi*tt );
zz = 1.4*exp(j*pi/2)*exp(j*5*pi*tt);
plot( tt, xx, ’b-’, tt, real(zz), ’r--’ ), grid on
%<--- plot a sinusoid
title(’TEST PLOT of a SINUSOID’)
xlabel(’TIME (sec)’)
Write a simple MATLAB code (in the form of a function) that allows you to decompose a signal into its
even and odd parts.
Note: The function takes two inputs n, the timing index and x the values of the signal at the designated time
instants. The function outputs include the two sub-functions, x_e and x_o along with the timing index.
Test your function on the following signal x[n] and compute its even and odd parts.
2 𝑛=0
5 𝑛=1
−1 𝑛=2
𝑥[𝑛] =
4 𝑛=3
−5 𝑛=4
{0 elsewhere
Consider the first order system defined by the difference equation as follows (we’ll review the
discussion on how determination of order for a difference equation later):
y[n] = a. y[n-1] + x[n]
Write a function y = diffeqn (a, x, y[-1]) which computes the output y[n] of the system determined by
the given equation. The vectors x[n] contains the signal as defined in the upper part and y[n] = 0 for n
< 1.
c) Convolution of signals
Recalling that one of the most convenient ways to represent an LTI system is through its impulse
response h[n]. Once the impulse response of a system is known, the output (response) of the system to
any given input can be computed using the convolution operator as:
yn = xk hn − k
k = −
The convolution essentially involves two operations: flipping either the input signal or the impulse
response (as in above equation) and then sliding the flipped signal.
LAB TASKS
Write your own convolution function, myconv.m that computes the convolution between the two
signals (or the output of passing an input signal through a system). Designate all the necessary inputs
for your function, considering that the input signal and the impulse response may start at some ‘n’
that is negative. The function output is obviously the system output along with the timing index for
the output n1, which must be set manually. Your function should work on any general signal and the
impulse response (of finite length).
Code
(m-file)
function [y, n1] = myconv(x, nx, h, nh) fprintf('Convolution result in
% Calculate the index range for the discrete-time form:\n');
convolution result for i = 1:length(y)
n1_start = nx(1) + nh(1); fprintf('y[%d] = %.2f\n',
n1_end = nx(end) + nh(end); n1(i), y(i));
n1 = n1_start:n1_end; end
end
% Perform the convolution
y = conv(x, h);
Code
randomNumber = randi([1, 50]);
if userGuess == randomNumber
disp('Congratulations! You guessed the correct number.');
else
disp(['Better Luck Next Time! The correct number was ',
num2str(randomNumber), '.']);
end
Output:
h[n] x[n]
5 5
4 4
3 3
2 2
1 1
0 0
-1 0 1 2 3 4 5 -1 0 1 2 3 4 5 6
n n
Code
x = [1, 2, 1,2];
nx = 0:4;
h = [1, -1];
nh = 0:4;
[y, n1] = myconv(x, nx, h, nh);
disp('Convolution result:');
disp(y);
disp('Output indices:');
disp(n1);
Output:
Code
x = [1, 2, 4, 1, 1];
nx = 1:6;
h = [1, -1];
nh = 1:6;
[y, n1] = lab1(x, nx, h, nh);
disp('Convolution result:');
disp(y);
disp('Output indices:');
disp(n1);
The manual results are the same as the MATLAB results.
MATLAB has a built-in function ‘conv’ that performs the same operation. Compare the results of
part (ii) with the conv function of MATLAB.
Code
userGuess = input('Guess the hidden number pattern (as a vector, e.g., [0 1 0 0 1]):
');
if isequal(userGuess, hiddenNumber)
disp('Congratulations! You guessed the correct hidden number pattern.');
else
disp(['Better luck next time! The correct pattern was ', mat2str(hiddenNumber),
'.']);
end
Output:
Write a MATLAB function which tests whether the input matrix is symmetric or not. The function
should display “The entered matrix is symmetric” in case the matrix input from user is symmetric.
The function should be called by typing m = matsymm(A) in the editor file and the matrix A should
be taken as an input from user.
Note: An nxn matrix is called symmetric matrix when A(i,j)=A(j,i) with i≠j
Code
function m = matsymm(A)
% Function to check if a matrix is symmetric
% Input: A - matrix entered by the user
% Output: m - 1 if symmetric, 0 otherwise
Output
Case 1: When symmetric matrix is given
Code
x = 1000;
tolerance = 0.1;
y = 0;
N = 0;
if N > 1e6
disp('N is too large, exiting loop');
break;
end
end
Output:
Consider now that x[n] starts from n = -1 and h[n] starts from -2. What will be the result of the
convolution then? Plot the corresponding output signal using the stem command and proper timing
axis.
Code
x = [1,1,1,1];
nx = -1:2;
h = [1, -1];
nh = -2:1;
[y, n1] = lab1(x, nx, h, nh);
disp('Convolution result:');
disp(y);
disp('Output indices:');
disp(n1);
CONCLUSION
In this lab, we have reviewed the fundamental concepts of signals and systems and explored their
implementation using MATLAB. Through the course of the lab, we have covered various aspects of signals
and systems, including signal transformations, the distinction between even and odd parts of a signal, and
the Convolution operator as a basic property of Linear Time Invariant (LTI) Systems. By performing various
operations and analyses on signals and systems, we have been able to observe the behavior of signals and
systems and understand how they are affected by different operations and transformations.
In conclusion, this lab has provided us with a comprehensive overview of the fundamental concepts of
signals and systems and has equipped us with the skills to apply these concepts using MATLAB. The
knowledge and skills gained through this lab will be valuable for future studies and applications in the
field of signals and systems.