DSP Lab
DSP Lab
SUBMITTED BY
NAME: ROSHNI GHAI
ROLL NO: 21BEC037
BRANCH: ECE (4 YEAR)
SECTION: A
SEMESTER: 5
21BEC037 ROSHNI GHAI
INDEX
S NO. EXPERIMENT DATE PAGE SIGNATURE
NO.
1. Introduction to Matlab 2.8.23 3-8
To plot basic signals on Matlab
2. 2.8.23 9-15
and observe their waveform.
3. Write a Matlab program to find 9.8.23 16-19
linear convolution of two
discrete signals.
Write a MATLAB program to
4. 23.8.23 20-25
find the correlation of two
signals.
Write a MATLAB program to
5. 6.9.23 26-28
find the circular convolution of
two signals.
2
21BEC037 ROSHNI GHAI
INTRODUCTION TO MATLAB
MATLAB (Matrix Laboratory) is a powerful and widely-used programming language
and interactive computing environment that is specifically designed for numerical
computing, data analysis, visualization, and algorithm development. It was created by
MathWorks and first released in 1984. Over the years, MATLAB has evolved into a
versatile tool used across various industries, including engineering, mathematics,
science, finance, and academia.
The name MATLAB stands for matrix laboratory. MATLAB was originally written
to provide easy access to matrix software developed by the LINPACK and EISPACK
projects. Today, MATLAB engines incorporate the LAPACK and BLAS libraries,
embedding the state of the art in software for matrix computation.
MATLAB has evolved over a period of years with input from many users. In
university environments, it is the standard instructional tool for introductory and
advanced courses in mathematics, engineering, and science. In industry, MATLAB is
the tool of choice for high-productivity research, development, and analysis.
4
21BEC037 ROSHNI GHAI
7. Basic Plotting:
5
21BEC037 ROSHNI GHAI
ADVANTAGES OF MATLAB
6
21BEC037 ROSHNI GHAI
7
21BEC037 ROSHNI GHAI
DISADVANTAGES OF MATLAB:
8
21BEC037 ROSHNI GHAI
EXPERIMENT 1
AIM: To plot basic signals on Matlab and observe their waveform.
1. Unit Step Signal: The unit step signal, also known as the Heaviside
step function, is a fundamental signal in both continuous-time and discrete-
time signal processing. It has a value of 0 for negative time indices and a
value of 1 for non-negative time indices. It is denoted as u(t) or u[n] in the
continuous-time and discrete-time domains respectively
Formulas for the unit step signal:
For continuous-time (analog) domain:
u(t) = {
0, t < 0
1, t >= 0
}
For discrete-time domain:
u[n] = {
0, n < 0
1, n >= 0
}
In these formulas, t represents time in the continuous-time domain, and n
represents time indices in the discrete-time domain.
2. Unit Impulse Signal: The unit impulse signal, also known as the
Dirac delta function, is a theoretical construct used in mathematics and
signal processing to describe an infinitely short and infinitely high pulse.
It's often represented as δ(t) in continuous-time and δ[n] in discrete-time
domains. The unit impulse has the unique property that its integral (or
sum in the discrete case) over the entire domain is equal to 1.
9
21BEC037 ROSHNI GHAI
δ(t) = {
∞, t = 0
0, t ≠ 0
}
Discrete-Time Domain:
δ[n] = {
1, n = 0
0, n ≠ 0
}
3. Ramp Signal
A ramp signal is a type of signal that increases linearly with time in the
continuous-time domain or with time indices in the discrete-time domain.
It's a simple and often used signal in various applications such as control
systems, signal processing, and more. The ramp signal is denoted by
"r(t)" in continuous-time and "r[n]" in discrete-time.
11
21BEC037 ROSHNI GHAI
PROCEDURE:
1. Open the MATLAB software on your computer.
2. Define a time vector that covers the desired time span of the signal.
3. Create the desired signal using mathematical functions
4. Use the stem(t, y) function to display the signal in discrete domain.Then
customize it using functions like xlabel, ylabel, title, and grid.
5. Repeat steps 2 to 4 for all the other types of signals .
6. Use the subplot(rows, columns, index) function to create a grid of
subplots and place each signal plot in a specific location within the grid.
7. Observe the waveforms obtained of different signals and analyse them.
12
21BEC037 ROSHNI GHAI
MATLAB CODE
%unit step signal
t = -5:0.1:5;
u_t = zeros(size(t));
for i = 1:length(t)
if t(i) >= 0
u_t(i) = 1;
else
u_t(i) =0;
end
end
subplot(3,2,1);
stem(t, u_t, 'b', 'LineWidth', 2);
xlabel('Time (t)');
ylabel('u(t)');
title('Unit Step Function');
%unit impulse signal
t = -5:0.1:5;
i_t = zeros(size(t));
for i = 1:length(t)
if t(i) == 0
i_t(i) = 1;
else
i_t(i) =0;
end
end
subplot(3,2,2);
stem(t, i_t, 'b', 'LineWidth', 2);
xlabel('Time (t)');
ylabel('d(t)');
title('Unit Impulse Function');
% unit ramp signal
t = -5:0.1:5;
r_t = zeros(size(t));
for i = 1:length(t)
if t(i) >= 0
r_t(i) = t(i);
end
end
subplot(3,2,3);
stem(t, r_t, 'b', 'LineWidth', 2);
xlabel('Time (t)');
13
21BEC037 ROSHNI GHAI
ylabel('r(t)');
title('Ramp Signal');
%exponential signals
t = -5:0.1:5;
a = 2;
x_t = zeros(size(t));
for i = 1:length(t)
if t(i) >= 0
x_t(i) = exp(a * t(i));
end
end
subplot(3,2,4);
stem(t, x_t, 'b', 'LineWidth', 2);
xlabel('Time (t)');
ylabel('e(t)');
title('Exponential Signal');
%cosine signal
A=1;
F= 2;
dt=0.001;
t=0:dt:1;
F_s= 8*F;
T_s= 1/F_s;
n=F_s;
n_1 =0:T_s:1;
x_s = A*cos(2*pi*F*n_1);
subplot(3,2,5)
stem(n_1,x_s);
xlabel('Samples(n)');
ylabel('Amplitude');
title('Discrete Cosine Signal');
%sine signal
A=1;
F= 2;
dt=0.001;
t=0:dt:1;
F_s= 8*F; T_s= 1/F_s;
n=F_s;
n_1 =0:T_s:1;
x_s = A*sin(2*pi*F*n_1);
14
21BEC037 ROSHNI GHAI
subplot(3,2,6)
stem(n_1,x_s);
xlabel("Samples (n)");
ylabel("Amplitude");
title("Discrete Sine Signal",'LineWidth',5);
OUTPUT WAVEFORM
15
21BEC037 ROSHNI GHAI
EXPERIMENT 2
AIM: Write a MATLAB program to find the linear convolution of two discrete
signals.
THEORY:
For two functions F(n) and G(n), the convolution of the two is expressed and
given by the following mathematical expression.
In continuous time, the convolution of two continuous signals x(t) and h(t) is
defined as:
This operation involves sliding one signal (h(t)) over the other signal (x(t)) and
calculating the integral of their product at each point of overlap.
For two discrete signals x[n] and h[n], the convolution ]y[n] is defined as:
This operation involves sliding one signal (h[n]) over the other signal (x[n]) in
reverse order and calculating the sum of element-wise products at each step.
Linear Convolution
Linear Convolution is a means by which one may relate the output and input of an
LTI system given the system’s impulse response. It is required to convolve the input
signal with the impulse response of the system. the equation pf linear convolution is
as follows:
The reason why the expression is summed an infinite number of times is just to
ensure that the probability of the two functions overlapping is 1. The impulse
response is time-shifted endlessly so that during some duration of time, the two
functions will certainly overlap .
16
21BEC037 ROSHNI GHAI
PROCEDURE:
1. Open the MATLAB software on your computer.
2. Define the two input signals, x and h, that you want to convolve. These can
be represented as arrays in MATLAB.
3. Use the conv function to perform convolution on the input signals.
4. Then subplot the signals and observe it’s waveform.
5. Implement the logic of convolution function via for loops and if else
statements.
6. Again subplot the signals and observe their waveform.
MATLAB CODE:
Using ‘conv’ function
clc;
clear all;
close all;
x=[1,2,4,8,10,13];
h=[1,2,4,8,10,12,16,18,20];
subplot(3,1,1);
stem(x);
xlabel('n values ');
ylabel('Amplitude');
title("Input Signal x(n)");
disp('Input Signal x:');
disp(x);
subplot(3,1,2);
stem(h);
xlabel('n values ');
ylabel('Amplitude');
title("Input Signal h(n)");
disp('Input Signal h:');
disp(h);
subplot(3,1,3);
y= conv (x,h);
stem(y);
xlabel('n values ');
ylabel('Amplitude');
title("Convolution Output");
disp('Convolution Result y:');
disp(y);
17
21BEC037 ROSHNI GHAI
OUTPUT WAVEFORM
subplot(3,1,1);
stem(x);
xlabel('n values ');
ylabel('Amplitude');
title(" Input Signal x(n)");
disp('Input Signal x:');
disp(x);
subplot(3,1,2);
stem(h);
xlabel('n values ');
ylabel('Amplitude');
title("Input Signal h(n)");
disp('Input Signal h:');
18
21BEC037 ROSHNI GHAI
disp(h);
% Lengths of the input signals
M = length(x);
N = length(h);
% Initialize the result array
y = zeros(1, M + N - 1);
% Perform convolution
for n = 1:M + N - 1
y(n) = 0;
for k = max(1, n - N + 1):min(M, n)
y(n) = y(n) + x(k) * h(n - k + 1);
end
end
subplot(3,1,3);
stem(y);
xlabel('n values ');
ylabel('Amplitude');
title("Convolution Result");
disp('Convolution Result y:');
disp(y);
OUTPUT WAVEFORM
19
21BEC037 ROSHNI GHAI
EXPERIMENT 3
AIM: Write a MATLAB program to find the correlation of two signals.
Rxy[m]=∑nx[n]⋅y[n−m]
Here, Rxy[m] represents the correlation between x[n] and y[n] at lag m, and the
sum is taken over all possible time indices n.
Types of Correlation:
1. Auto-correlation: Auto-correlation measures how similar a signal is to
itself at different time lags. In this case, both x[n] and y[n] are the same
signal. The auto-correlation function is denoted as Rxx[m] or Ryy[m], and
it helps in analyzing the periodicity and repeating patterns within a signal.
The cross-correlation between two signals x[n] and y[n] at lag m is given
by:
Rxy[m]=∑nx[n]⋅y[n−m]
PROCEDURE:
1. Open the MATLAB software on your computer.
2. Define the two input signals, x and y, that you want to correlate. These
can be represented as random arrays in MATLAB.
20
21BEC037 ROSHNI GHAI
MATLAB CODE
AUTOCORRELATION USING ‘xcorr’ FUNCTION
%% Auto Correlation of two signals
clc;
close all;
clear all;
x= randn(1,50);
subplot(2,1,1);
stem(x);
title("Input Signal");
xlabel('Sample');
ylabel('Amplitude');
y= xcorr(x);
subplot(2,1,2);
stem(y);
title(" Auto Correlation ");
xlabel('Sample');
ylabel('Auto Correlation Values ');
OUTPUT WAVEFORM
21
21BEC037 ROSHNI GHAI
% input signal 1
x1= randn(1,30);
subplot(3,1,1);
stem(x1);
grid on;
title("Input Signal 1");
xlabel('Sample');
ylabel('Amplitude');
% input signal 2
x2=randn(1,40);
subplot(3,1,2);
stem(x2);
title("Input Signal 2");
xlabel('Sample');
ylabel('Amplitude');
grid on;
l1 = length(x1);
l2 = length(x2);
22
21BEC037 ROSHNI GHAI
if l1 > l2
x2 = [x2, zeros(1, l1 - l2)];
end
if l1 < l2
x1 = [x1, zeros(1, l2 - l1)];
end
l1 = length(x1);
l2 = length(x2);
% output signal
y= xcorr(x1,x2);
subplot(3,1,3);
stem(y);
title("Cross Correlation Output");
xlabel('Sample');
ylabel('Correlation Value');
grid on;
OUTPUT WAVEFORM
23
21BEC037 ROSHNI GHAI
% INPUT SIGNAL 1
x = randn(1,30);
subplot(3, 1, 1);
stem(x);
title('Input Sequence x');
xlabel('Sample');
ylabel('Amplitude');
grid on;
% INPUT SIGNAL 2
y = randn(1,40);
subplot(3, 1, 2);
stem(y);
title('Input Sequence y (Flipped)');
xlabel('Sample');
ylabel('Amplitude');
grid on;
y = fliplr(y);
l1 = length(x);
l2 = length(y);
if l1 > l2
y = [y, zeros(1, l1 - l2)];
end
if l1 < l2
x = [x, zeros(1, l2 - l1)];
end
l1 = length(x);
l2 = length(y);
r = zeros(1, l1 + l2 - 1);
for n = 1 : l1 + l2 - 1
for k = 1 : l2
24
21BEC037 ROSHNI GHAI
subplot(3, 1, 3);
stem(r);
title('Correlation Output r');
xlabel('Sample');
ylabel('Correlation Value');
grid on;
OUTPUT WAVEFORM
COMMAND WINDOW:
25
21BEC037 ROSHNI GHAI
EXPERIMENT 4
AIM: Write a MATLAB program to find the circular convolution of two
discrete signals.
26
21BEC037 ROSHNI GHAI
PROCEDURE:
Open the MATLAB software on your computer.
Define the two input signals, x and h, that you want to convolve. These
can be represented as random arrays in MATLAB.
Use the cconv function to perform convolution on the input signals.
Then subplot the signals and observe it’s waveform.
Implement the logic of correlation function via for loops and if else
statements.
Again subplot the signals and observe their waveform.
MATLAB CODE :
% Using in-built function
clc;
close all;
x = [1,2,-1,0,1];
h = [1,2,3];
lx = length(x);
lh = length(h);
if (lx<lh)
x = [x,zeros(1,lh-lx)];
27
21BEC037 ROSHNI GHAI
else
h = [h,zeros(1,lx-lh)];
end
y_inbuilt = cconv(x,h,length(x));
% stem(y_inbuilt);
subplot(221);
stem(x);
title('First Sequence');
xlabel('n');
ylabel('x(n)');
subplot(222);
stem(h);
title('Second Sequence');
xlabel('n');
ylabel('h(n)');
subplot(223);
stem(y_inbuilt);
title('Circular Convolution using in-built function');
xlabel('n');
ylabel('y(n)');
subplot(224);
stem(y);
title('Circular Convolution');
xlabel('n');
ylabel('y(n)');
OUTPUT WAVEFORM:
28
21BEC037 ROSHNI GHAI
29