0% found this document useful (0 votes)
15 views15 pages

lab2

The document outlines a laboratory experiment focused on generating continuous and discrete time signals using MATLAB, aimed at familiarizing students with signal types and their properties. It includes objectives, required equipment, background knowledge on signal classifications, and detailed tasks for generating various signals such as unit step and unit impulse sequences. The conclusion emphasizes the understanding gained regarding signal behavior and manipulation through practical implementation in MATLAB.

Uploaded by

Muhammad Hamza
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)
15 views15 pages

lab2

The document outlines a laboratory experiment focused on generating continuous and discrete time signals using MATLAB, aimed at familiarizing students with signal types and their properties. It includes objectives, required equipment, background knowledge on signal classifications, and detailed tasks for generating various signals such as unit step and unit impulse sequences. The conclusion emphasizes the understanding gained regarding signal behavior and manipulation through practical implementation in MATLAB.

Uploaded by

Muhammad Hamza
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/ 15

AIR UNIVERSITY

DEPARTMENT OF ELECTRICAL AND COMPUTER ENGINEERING

EXPERIMENT NO 02
Lab Title: CONTINUOUS AND DISCRETE TIME SIGNALS IN MATLAB

Student Name: Maryam Razzaque , Muhammad Hamza

Reg. No: 220644, 220666

Objective : Familiarizing students with the generation of continuous and discrete time
signals in MATLAB

LAB ASSESSMENT:
Excellent Good Average Satisfactory Unsatisfactory
Attributes
(5) (4) (3) (2) (1)

Ability to conduct experiment

Ability to assimilate the results

Effective use of lab equipment


and follows the lab safety rules

Total Marks: Obtained Marks:


LAB REPORT ASSESSMENT:
Excellent Good Average Satisfactory Unsatisfactory
Attributes
(5) (4) (3) (2) (1)

Data presentation

Experimental results

Conclusion

Total Marks: Obtained Marks

Date: Signature:

1 | Page
LABORATORY
EXPERIMENT NO. 2

CONTINUOUS AND DISCRETE TIME SIGNALS IN MATLAB


Objectives:

• Familiarizing students with the generation of continuous and discrete time signals in
MATLAB

Equipment required:

 MATLAB installed on PCs

Background Knowledge:

Signals are classified based on two parameters which are time and amplitude. Depending on these
parameters, signals are generally into following four categories:
Continuous Time - Continuous Amplitude Signal:

These signals vary continuously with time and amplitude and can take any value at a particular
instant of time. These signals are also known as analog signals.

Discrete Time - Continuous Amplitude Signal:

These signals vary continuously with amplitude; however, these changes occur only at discrete time
instant. The amplitude values are continuous, and the signal assumes any value at discrete time index.

2 | Page
Continuous Time - Discrete Amplitude Signal:

These signals vary continuously with time; however, they can take a certain number of amplitude
values. The discrete amplitude indices are, sometimes, marked to indicate that these type of signals
can only take these set of values at any time instant.

Discrete Time - Discrete Amplitude Signal:

A signal in which both time and amplitude are discrete entities is known as a discrete time – discrete
amplitude signal. These signals can change amplitude values only at certain instants and the
amplitude values are also discrete. These signals are also known as digital signals.

Continuous Time Signals:

A continuous-time signal is a function of continuous argument; each instant in time has a definite
amplitude. Continuous-time signals are defined for all real numbers within a given interval, whereas
digital computers work with finite precision and cannot represent an infinite number of values.
Therefore, when working with continuous-time signals in MATLAB, it is needed to approximate them
using a finite number of samples. This leads to the discretization of signals w.r.t time and amplitude.
Discrete Time Signals:

A discrete-time signal is not a function of continuous argument; it is a series consisting of sequence


of quantities. In a discrete-time signal, the number of elements in the set is finite and countable. The
aim of this section is to familiarize students with commands to generate various types of discrete
time signals and plot these signals using MATLAB. The discrete time signals will be stored in the form
of vectors and will be finite and causal for the most part.
Unit Step Sequence:

A unit step signal is the basic example of discrete time signal. Discrete time unit step signal is defined
as:

3 | Page
Unit step signal can be generated in MATLAB using the Heaviside step function. Heaviside(x)
evaluates the Heaviside step function (also known as the unit step function) at x. The Heaviside
function is a discontinuous function that returns 0 for x < 0, 1/2 for x = 0, and 1 for x > 0.
For example, the code given below generates a unit step sequence for a finite time duration:

To avoid the discontinuity provided in Heaviside function, students can create their own unit step
signals using if-else conditions, such as:

This code can be saved in a function file and can be called in the script file or command window, when
required. The code given above generates the following result:

4 | Page
The axis command is used to change the axes of graphic window. The axis command has four
parameters, the first two are the minimum and maximum values of x-axis and the last two are the
minimum and maximum values of y-axis.
Unit Impulse Sequence:

A unit impulse signal is another basic example of discrete time signal. Discrete time unit impulse
signal is denoted by δ[ n] and is defined as :

A unit impulse signal of length N can be generated using the following MATLAB code:

5 | Page
This code generates a unit impulse sequence for a finite time duration.

The axis command is used to change the axes of graphic window. The axis command has four
parameters, the first two are the minimum and maximum values of x-axis and the last two are the
minimum and maximum values of y-axis.

6 | Page
Exponential Sequences:

Another basic discrete-time sequence is the exponential sequence. The signal given below is an
example of exponential sequence:
To generate this signal in MATLAB, use the given code:

Sinusoidal Sequences:

Another very useful class of discrete-time signals is the real sinusoidal sequence. The real sinusoidal
sequence with constant amplitude is of the form:

Where A, ω0, and are real numbers. The parameters A, ω0, and are called amplitude, angular
frequency, and initial phase of the sinusoidal sequence x[n] respectively.
Discrete Time Signals:

To generate discrete sequences of desired values, use the following set of commands:

where, n is the discrete time index and x is the amplitude at each time index. The desired signal is:

7 | Page
8 | Page
Lab Tasks:

1. Generate the following signal in MATLAB:


x[n] = u[n-3] + u[n+7]
where, -10 ≤ n ≤ 10. Plot results with proper axis label and figure title.
CODE:
n=-10:10
u1=[]
for i=1:length(n)
if (n(i)<3)
u1(i)=0
else
u1(i)=1
end
end
u2=[]
for j=1:length(n)
if (n(j)<-7)
u2(j)=0
else
u2(j)=1
end
end

x=u1+u2;

subplot(3,1,1)
stem(n,u1,'g','filled')
title('unit step shifted by 3 units')
xlabel('n')
ylabel('u[n-3]')
axis([(( min(n))-1) ((max(n))+1) 0 2 ])

subplot(3,1,2)
stem(n,u2,'r','filled')
title('unit step shifted by 7 units')
xlabel('n')
ylabel('u[n+7]')
axis([(( min(n))-1) ((max(n))+1) 0 2 ])

subplot(3,1,3)
stem(n,x,'b','filled')
title('x[n]')
xlabel('n')
ylabel('x[n]')
axis([(( min(n))-1) ((max(n))+1) 0 2 ])

9 | Page
OUTPUT:

2. Generate the following signal in MATLAB:


x[n] = e^−n . u[n]
where, -10 ≤ n ≤ 10. Plot u[n] and x[n] with proper axis label and figure title

CODE:
n=-10:10;
u=[];
for i=1:length(n)
if (n(i)>=0)
u(i)=1
else (n(i)<0)
u(i)=0
end
end
e=exp(-n)
x = e.*u;

10 | Page
subplot(3,1,1)
stem(n,u,'r','filled')
title('unit step signal')
xlabel('n')
ylabel('u[n]')
axis([(( min(n))-1) ((max(n))+1) 0 1 ])
grid on

subplot(3,1,2)
stem(n,e,'p','filled')
title('e^-n')
xlabel('n')
ylabel('e^-n')
axis([(( min(n))-1) ((max(n))+1) 0 3 ])
grid on

subplot(3,1,3)
stem(n,x,'g','filled')
title('x[n]')
xlabel('n')
ylabel('x[n]')
grid on

OUTPUT:

11 | Page
3. Create a function in MATLAB which adds two discrete time signals and outputs the resultant
signal. The input and output arguments of the function should be:
function [y,n] = sigadd(x1,n1,x2,n2)
where, x1 and x2 are amplitude values of the input discrete time
signals n1 and n2 are time ranges for the input discrete time signals y is
the amplitude values of the output discrete time signal n is the time
range of the output discrete time signal

SCRIPTED CODE:

x1=input('Enter the value of x1')


x2=input('Enter the value of x2')
n1=input('Enter the value of n1')
n2=input('ENter the value of n2')
[x,n]=sigadd(x1 ,n1 ,x2 ,n2 );

FUNCTION CODE:

function [x,n]=sigadd(x1 ,n1 ,x2 ,n2 )


nmin=min(min(n1),min(n2));
nmax=max(max(n1),max(n2));
n=nmin:nmax;
A=zeros(1,length(n))
A(ismember(n,n1))=x1
B=zeros(1,length(n))
B(ismember(n,n2))=x2

x=A+B;

subplot(3,1,1)
stem(n,A,'r','filled')
title('y1[n]')
xlabel('n1')
ylabel('x1[n]')
axis([((min(n))-1) ((max(n))+1) 0 6 ])
grid on
subplot(3,1,2)
stem(n,B,'p','filled')
title('y2[n]')
xlabel('n2')
ylabel('x2[n]')
axis([((min(n))-1) ((max(n))+1) 0 6 ])
grid on
subplot(3,1,3)
stem(n,x,'g','filled')
title('y[n]')
xlabel('n')
ylabel('x[n]')
axis([((min(n))-1) ((max(n))+1) 0 6 ])
grid on

12 | Page
USER INPUTS:

OUTPUT:

13 | Page
4. Create a function which plots continuous time unit step and unit impulse signals. Call
this function in the script file by passing the time vector ‘t’ in its arguments.

Note: Create the unit step and unit impulse signal without using heaviside and dirac
command in MATLAB.

SCRIPTED CODE:

t=-20:0.001:20;
u=continuous(t)

FUNCTION CODE:

function u=continuous(t)
for i=1:length(t)
if (t(i)<0)
u(i)=0;
else
u(i)=1;
end
end
for i=1:length(t)
if (t(i)==0)
u1(i)=1;
else
u1(i)=0;
end
end
subplot(2,2,1)
plot(t,u,'r','lineWidth',2)
xlabel('t')
ylabel('uS')
title('Unit Step')
axis([-20 20 0 1.5])
subplot(2,2,2)
plot(t,u1,'B','lineWidth',2)
xlabel('t')
ylabel('uI')
title('Unit Impulse')
axis([-20 20 0 1.5])

14 | Page
OUTPUT:

Conclusion:

In this lab we get to know about the four categories of signals and implement basic signals like unit
step, unit impulse and exponential signals. Instead of using built in functions like heaviside and dirac,
we create these signals using loops and conditions, which helped us understand how signals are
formed step by step. This helps us understand the difference between continuous and discrete signal,
and their behavior in terms of time and amplitude .We also perform addition and multiplication of
discrete time signals, which show us how signals can be combined and modified. By plotting these
signals, we could see how they change over time and how mathematical operations affect them. This
gave us a better understanding of signal types, their properties, and how to work with them in Matlab.

15 | Page

You might also like