DSP Lab2 D
DSP Lab2 D
EXPERIMENT NO 2
Objective: To gain hands-on familiarity with generating continuous and discrete time signals in
MATLAB.
LAB ASSESSMENT:
Data presentation
Experimental results
Conclusion
Date: Signature: _
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.
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.
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:
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.
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])
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
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)
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
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
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