lab2
lab2
EXPERIMENT NO 02
Lab Title: CONTINUOUS AND DISCRETE TIME SIGNALS IN MATLAB
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)
Data presentation
Experimental results
Conclusion
Date: Signature:
1 | Page
LABORATORY
EXPERIMENT NO. 2
• Familiarizing students with the generation of continuous and discrete time signals in
MATLAB
Equipment required:
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.
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.
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.
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 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:
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:
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:
FUNCTION CODE:
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