0% found this document useful (0 votes)
23 views16 pages

Lab 1 Ahmad Sataish Irfa

Dsp

Uploaded by

nb7kb2jjtp
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views16 pages

Lab 1 Ahmad Sataish Irfa

Dsp

Uploaded by

nb7kb2jjtp
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

Department of Electrical Engineering

Faculty Member: Tauseef ur Rehman Dated: 27th January, 2025

Course/Section: BEE-14D Semester:6th (Spring 2024)

EE-330 Digital Signal Processing

Lab1: MATLAB REVIEW-Signals & Systems Fundamentals

PLO4-CLO4 PLO5- PLO8- PLO9-


CLO5 CLO6 CLO7

Name Reg. No Viva / Quiz / Analysis Modern Ethics and Individual


Lab of data in Tool Safety and Team
Performance Lab Usage Work
Report

5 Marks 5 Marks 5 Marks 5 Marks 5 Marks

Ahmad Nasir 409959

Sataish Elahi 421623

Irfa Farooq 412564


Lab1: MATLAB REVIEW-Signals & Systems Fundamentals
INTRODUCTION

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:

• Signal transformations (shifting, inversion, scaling)


• Even and Odd parts of a signal
• Convolution operator-the basic property of Linear Time Invariant (LTI) Systems

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.

Figure 1: Using symmetric matrix

Figure 2: Using asymmetric matrix

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.

1.2 Creating a M-file

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)’)

1.3 Functions-Key to Efficient Coding


It is often convenient to define functions so that they may use at multiple instances and with different
inputs. Functions are a special type of M-file that can accept inputs (matrices and vectors) and may
return outputs. The keyword function must appear as the first word in the M-file that defines the
function, and the first line of the M-file defines how the function will pass input and output
arguments. The file extension must be lower case “m” as in my func.m. The following function has a
few mistakes. Before looking at the correct one below, try to find these mistakes (there are at least
three):

Matlab mfile [xx,tt] = badcos(ff,dur)


%BADCOS Function to generate a cosine wave
% xx = badcos(ff,dur)
% ff = desired frequency in Hz
% dur = duration of the waveform in seconds
tt = 0:1/(100*ff):dur; %-- gives 100 samples per period
badcos = cos(2*pi*freeq*tt);

The corrected function should look something like:


function [xx,tt] = goodcos(ff,dur)
tt = 0:1/(100*ff):dur; %-- gives 100 samples per period
xx = cos(2*pi*ff*tt);
Following are the few mistakes in the given piece of code
1. The function name should be start with keyword “function”. The corrected definition is
function [xx,tt] = badcos(ff,dur)

2. The variable freeq is used without any definition

3. Function name cannot be used as the variable name.

1.4 Review of Basic Signals and Systems


a) Even and odd parts of a signal:
Any signal x[n] can be decomposed into its even part and odd parts as:
1
𝑥𝑒 (𝑛) = [𝑥(𝑛) + 𝑥(−𝑛)]
2
1
𝑥0 (𝑛) = [𝑥(𝑛) − 𝑥(−𝑛)]
2

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

b) First order Difference equation:


Recall that one way of defining the LTI systems is through the difference equations that relate the input
x[n] to the output y[n].

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:

yn  =  xk hn − 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);

% Display the convolution output x = [1, 2, 3];


and index range nx = -1:1;
fprintf('The convolution output
y[n] is:\n'); h = [1, -1];
disp(y); nh = 0:1;
fprintf('The index range n1
is:\n'); [y, n1] = myconv(x, nx, h, nh);
disp(n1); disp('Convolution result:');
disp(y);
% Display the result in discrete disp('Output indices:');
form disp(n1);
Output:
Use MATLAB to write a code for “Guess My Number” game. Use randi command to generate a
number between 1 to 50. Ask the user to input a random number; if the two numbers match, display
“Congratulations”. Otherwise, display a message of “Better Luck Next Time”.

Code
randomNumber = randi([1, 50]);

userGuess = input('Guess a number between 1 and 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:

Figure 4: Output of the task 2


Test your function on the signal and the impulse response provided in the figures below and verify
the correctness of your function through a comparison of manual computation of the convolution for
the given signal and a plot of your function’s 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

sequence = [1, 2, 3, 4, 5];


disp('The sequence to guess is: [1, 2, 3, 4, 5]');

hiddenNumber = [0, 1, 0, 0, 1];

output = conv(sequence, hiddenNumber);

disp('The result of the convolution (hidden pattern combined with sequence):');


disp(output);

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

% Check if the matrix is symmetric


if isequal(A, A.') % Compare the matrix with its transpose
disp('The entered matrix is symmetric');
m = 1; % Return 1 for symmetric
else
disp('The entered matrix is not symmetric');
m = 0; % Return 0 for not symmetric
end
end

A = input('Enter a matrix: '); % Prompt the user to enter a matrix


m = matsymm(A); % Call the function

Output
Case 1: When symmetric matrix is given

Figure 5: when symmetric matrix is given by user

Case 2: When asymmetric matrix is given by user


Write a program in MATLAB which finds the upper bound for the following summation such that,
x-y < 0.1, where x = 1000 and y is calculated as

Code
x = 1000;
tolerance = 0.1;
y = 0;
N = 0;

while (x - y) > tolerance


N = N + 1;
y = y + 1 / (N^2);

if N > 1e6
disp('N is too large, exiting loop');
break;
end
end

fprintf('The required upper bound N is: %d\n', N);


fprintf('Final summation value y: %.6f\n', y);
fprintf('Difference (x - y): %.6f\n',x-y)

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.

You might also like