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

DSP Lab2 D

The document outlines a laboratory experiment focused on generating continuous and discrete time signals using MATLAB. It includes objectives, required equipment, background knowledge on signal types, and detailed MATLAB code for various signal generation tasks. The conclusion emphasizes the successful exploration of signal visualization and generation techniques, enhancing students' understanding of the concepts.

Uploaded by

Yehia Maged
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)
17 views15 pages

DSP Lab2 D

The document outlines a laboratory experiment focused on generating continuous and discrete time signals using MATLAB. It includes objectives, required equipment, background knowledge on signal types, and detailed MATLAB code for various signal generation tasks. The conclusion emphasizes the successful exploration of signal visualization and generation techniques, enhancing students' understanding of the concepts.

Uploaded by

Yehia Maged
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 2

Lab Title: CONTINUOUS AND DISCRETE TIME SIGNALS IN MATLAB


Student Name: Yehia Maged Reg. No: 222692 _

Objective: To gain hands-on familiarity with generating 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: _
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.

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.

2|Page
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:
1, 𝑓𝑜𝑟 𝑛 ≥ 0
𝑢[𝑛] = {
0, 𝑓𝑜𝑟 𝑛 < 0

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:
1, 𝑓𝑜𝑟 𝑛 = 0
𝛿[𝑛] = {
0, 𝑜𝑡ℎ𝑒𝑟𝑤𝑖𝑠𝑒

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.
Exponential Sequences:
Another basic discrete-time sequence is the exponential sequence. The signal given below is an
example of exponential sequence:

𝑥[𝑛] = 𝑒𝑥𝑝−2𝑛 𝑓𝑜𝑟 0 ≤ 𝑛 ≤ 20

6|Page
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:
𝑥[𝑛] = 𝐴 ∗ cos ( 𝑤0 ∗ 𝑛 + ⏀)
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.

Using for loop:


clc

n=-10:10;
x1=[];
x2=[];

for i=1:length(n)
if (n(i)>=3)
x1(i)=1;
else
x1(i)=0;
end
end

for i=1:length(n)
if (n(i)>=-7)
x2(i)=1;
else
x2(i)=0;
end
end

x3=x1+x2;

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

subplot (3,1,2)
stem(n,x2,'c','filled','linewidth',2)
xlabel('n')
ylabel('x[n+7]')
axis([min(n)-1 max(n)+1 0 1])

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

9|Page
Using a direct method:
clc

n=-10:10;
u1=(n>=3);
u2=(n>=-7);
x=u1+u2;

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

subplot (3,1,2)
stem(n,u2,'c','filled','linewidth',2)
xlabel('n')
ylabel('x[n+7]')
axis([min(n)-1 max(n)+1 0 1])

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

same result for both method:

10 | P a g e
2. Generate the following signal in MATLAB:
𝑥[𝑛] = 𝑒 −𝑛 . 𝑢[𝑛]

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

Code:
clc

n=-10:10;
u1=(n>=0);
x=exp(-1*n).*u1;

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

11 | P a g e
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

File Code:
clc

n1 = input('Enter the time range of signal 1: ');


x1 = input('Enter the amplitude of signal 1: ');
n2 = input('Enter the time range of signal 2: ');
x2 = input('Enter the amplitude of signal 2: ');

[y, n] = sigadd(x1, n1, x2, n2);

subplot(3,1,1)
stem(n1, x1, 'r','filled', 'LineWidth', 2)
xlabel('n1')
ylabel('X1[n]')
title('Signal 1')
axis([min(n1)-1 max(n1)+1 min(x1)-1 max(x1)+1])

subplot(3,1,2)
stem(n2, x2, 'g', 'filled','LineWidth', 2)
xlabel('n2')
ylabel('X2[n]')
title('Signal 2')
axis([min(n2)-1 max(n2)+1 min(x2)-1 max(x2)+1])

subplot(3,1,3)
stem(n, y, 'b', 'filled','LineWidth', 2)
xlabel('n')
ylabel('Y[n]')
title('Sum of Signals')
axis([min(n)-1 max(n)+1 min(y)-1 max(y)+1])

function code :
function [y,n] = sigadd(x1,n1,x2,n2)

n = min(min(n1), min(n2)) : max(max(n1), max(n2));

y1 = zeros(1, length(n));
y2 = zeros(1, length(n));

for i = 1:length(n)
if any(n(i) == n1)
y1(i) = x1(n1 == n(i));
end
end

12 | P a g e
for i = 1:length(n)
if any(n(i) == n2)
y2(i) = x2(n2 == n(i));
end
end

y = y1 + y2;
end

Results:

13 | P a g e
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.
File Code:
clc

t=input('Enter the time range: ');


stepplot(t)

function code:
function stepplot(t)

u = (t >= 0);
delta = (t == 0);

subplot(2,1,1)
plot(t, u, 'r', 'LineWidth', 2)
xlabel('t')
ylabel('u(t)')
title('continuous time unit Step')
axis([min(t)-0.5 max(t)+0.5 -0.5 1.5])

subplot(2,1,2)
plot(t, delta, 'b', 'LineWidth', 2)
xlabel('t')
ylabel('δ(t)')
title('continuous time unit Impulse')
axis([min(t)-0.5 max(t)+0.5 -0.5 1.5])
end

Results with (Step Size= 1):

14 | P a g e
Results with (Step Size= 0.01):

Conclusion:
In this lab, we successfully explored the generation and visualization of continuous and discrete-
time signals using MATLAB. We implemented various signal types, including unit step, unit
impulse, and exponential signals. Additionally, we learned alternative methods to generate unit
step and unit impulse signals without using built-in functions like heaviside for unit step and dirac
for unit impulse, reinforcing our conceptual understanding of signal generation in both continuous
and discrete time.

15 | P a g e

You might also like