DHAKA UNIVERSITY OF ENGINEERING & TECHNOLOGY, GAZIPUR
Department of Electrical and Electronic Engineering
Course Title: Digital Signal Processing Sessional
Course Code: EEE 4204
D M Y D M Y
Date of Allocation: Date of Submission:
10 11 2024 01 12 2024
Experiment No: 02
Name of the Experiment: Exploring convolution and correlation
techniques in digital signal processing: foundations, applications, and
advanced methods.
Submitted By
Md. Al-Amin Islam Sajib
Student ID: 202087
Dept: EEE, Year/Sem: 4/1
Session: 2023-24
Section: B
Allocated Marks Obtained Marks Course Teacher’s Signature
EXPERIMENT NO. 2
EXPERIMENT NAME: Exploring convolution and correlation techniques in digital signal
processing: foundations, applications, and advanced methods.
OBJECTIVES:
I. To learn the convolution and correlation techniques in digital signal processing.
II. To perform different practical applications of convolution and correlation in
MATLAB simulation.
III. To verify the properties of convolution techniques in digital signal processing.
LEARNING OUTCOMES:
i. Students will be able to find the correlation and convolution of different signals using
MATLAB.
ii. They will learn to find the output response of the LTI System.
ii. They’ll learn to verify the build functions of MATLAB for convolution.
iii. Moreover, they’ll know how to verify the properties of convolution.
1. Autocorrelation
Q1. Find the auto-correlation between the x(n)={1,2,3,4}.
Ans: Matlab Code,
x = [1, 2, 3, 4];
[R_auto, lags] = xcorr (x, 'unbiased'); figure;
stem(lags, R_auto, 'filled');
title('Auto-Correlation of x(n) = {1, 2, 3, 4}'); xlabel('Lags');
ylabel('Auto-Correlation Coefficient'); grid on;
2. Cross correlation
Q2. Find the cross-correlation between the x(n)={1,2,3,4} and y(n)={1,0,-1}.
Ans: Matlab Code,
% Define the signals x = [1, 2,
3, 4, 5]; % Signal x(n)
y = [1, 0, -1]; % Signal y(n)
% Compute the cross-correlation
[R_cross, lags] = xcorr(x, y,); % Cross-correlation of x and y
% Plot the cross-correlation
figure;
stem(lags, R_cross, 'filled');
title('Cross-Correlation between x(n) and y(n)');
xlabel('Lags');
ylabel('Cross-Correlation Coefficient');
grid on;
3. CONVOLUTION
Q. 1. Write a MATLAB program to find the linear convolution between two discrete-time
sequences 𝑥(𝑛) = {1,2,3,4} and ℎ(𝑛) = {1,0,−1}. Verify your result with the inbuilt command
𝒄𝒐𝒏𝒗(𝒙,𝒉)
Ans: Matlab Code,
clc; clear all;
close all; h =
[1 0 -1]; h1 =
length(h); h2 =
h;
subplot(4,1,1);
stem(h2,
'filled');
title('h sequence');
% Reversing h for
i = 1:h1 h(i) =
h2(h1 + 1 - i); end
% Plot the reversed h
subplot(4,1,2);
stem(h, 'filled');
title('Inverse of h(n)');
% Define the input sequence x and its
length x = [1 2 3 4]; % Discrete sequence
x1 = length(x); n = x1 + h1 - 1;
% Append zeros to the input sequence x and filter h to match the
length f = [zeros(1, n - x1), x]; % Appending zeros in the beginning
g = [h, zeros(1, n - h1)]; % Appending zeros at the end
% Initialize the output sequence y
y = zeros(1, n);
% Transpose g for circular shift operations
a = g'; % Complementing f1 for performing circular shift
% Perform the convolution
for i = 1:n
y(i) = f * a;
a = circshift(g', 1); % Perform circular shift on g
g = a'; % Transpose back to match dimensions end
% Plot the result of the
convolution subplot(4,1,3);
stem(y, 'filled');
title('Linear Convolution');
Build in:
x=[1,2,3,4] h=[1,0,-
1] y=conv(x,h)
stem(y)
Buit in Program Results: Convolution of h(n)
Q2. Verify the commutative property of Linear Convolution by swapping the places of
𝑥(𝑛) and ℎ(𝑛) in your program. Also observe the output of the command 𝒄𝒐𝒏𝒗(𝒉,𝒙).
Ans: Matlab Code,
clc; % Clear command window
clear all; % Clear all variables close all;
% Close all figure windows % Define the
input sequence x and its length x = [1 2 3
4]; % Discrete sequence x1 = length(x);
% Length of x % Define the filter h and its
length
h = [1 0 -1];
h1 = length(h);
% Store x in x2 for later use
x2 = x;
% Plot the original input sequence
x subplot(4,1,1); stem(x2);
title('x(n) sequence');
% Reverse the input sequence x
for i = 1:x1
x(i) = x2(x1 + 1 - i);
end
% Plot the reversed input sequence
subplot(4,1,2);
stem(x);
title('Inverse of x(n)');
% Compute the length of the output sequence for linear convolution
n = x1 + h1 - 1;
% Append zeros to h and x for convolution f = [zeros(1, n - h1),
h]; % Appending zeros to h (filter) g = [x, zeros(1, n - x1)]; %
Appending zeros to x (reversed input)
% Initialize output sequence y
y = zeros(1, n);
% Complement the filter for performing circular shift operations
a = g'; % Transpose of g
% Perform convolution using circular shifts
for i = 1:n
y(i) = f * a; % Dot product of f and a (convolution at each
step) a = circshift(g', 1); % Circularly shift g by one position g
= a'; % Transpose back to maintain row vector end
% Plot the result of the convolution using manual circular shift
method subplot(4,1,3); stem(y);
title('Linear Convolution using Circular Shift');
% Perform convolution using MATLAB's built-in conv() function
y_builtin = conv(h, x2); % Convolution using conv(h, x)
% Plot the result of convolution using built-in conv()
function subplot(4,1,4); stem(y_builtin);
title('Convolution using conv(x, h)');
Results:
Q3. Consider two systems with individual impulse responses given by
ℎ1(𝑛)=sin(𝑛𝜋);0≤ 𝑛≤3 and ℎ2(𝑛)=cos(𝑛𝜋);0≤𝑛≤3. The two systems are arranged in
parallel such that the outputs from these systems are summed using an adder
(𝑦(𝑛)=𝑦1(𝑛)+𝑦2(𝑛)). An input x(𝑛) = {0,1,2,3} is applied to both the systems. Find the
overall response y(n) at the output of the adder.
Ans: Matlab Code,
% Define the input sequence x and the index range
n x = [0 1 2 3]; % Input sequence n = 0:3;
% Index range for input sequence
% Define the two filter sequences h1 and h2
h1 = sin(n * pi); % First filter (sin function)
h2 = cos(n * pi); % Second filter (cos
function)
%% Convolution of x and h1 to get y1
% Step 1: Reverse h1[n] a = length(x); %
Length of input sequence x
b = length(h1); % Length of filter sequence h1
k = h1;
% Store h1 for reversing
for i = 1:b
h1(i) = k(b + 1 - i); % Reverse h1
end
% Step 2: Perform convolution of x and reversed h1 n = a + b - 1; %
Length of output sequence after convolution f = [zeros(1, n - a), x]; %
Zero-padding x to match output length g = [h1, zeros(1, n - b)]; % Zero-
padding reversed h1 to match output length y1 = zeros(1, n); %
Initialize output for y1 a = g'; % Transpose of g for performing circular
shift % Perform the convolution using circular shift for i = 1:n
y1(i) = f * a;
% Dot product of f and a
a = circshift(g', 1); % Circular shift of g
g = a';
% Transpose back to row vector
end
%% Convolution of x and h2 to get y2
% Step 1: Reverse h2[n] a = length(x); %
Length of input sequence x b = length(h2);
% Length of filter sequence h2 k = h2;
% Store h2 for reversing
for i = 1:b
h2(i) = k(b + 1 - i); % Reverse h2
end
% Step 2: Perform convolution of x and reversed h2 n = a + b - 1; %
Length of output sequence after convolution f = [zeros(1, n - a), x]; %
Zero-padding x to match output length g = [h2, zeros(1, n - b)]; % Zero-
padding reversed h2 to match output length y2 = zeros(1, n); %
Initialize output for y2
a = g'; % Transpose of g for performing circular
shift % Perform the convolution using circular shift
for i = 1:n
y2(i) = f * a;
% Dot product of f and a
a = circshift(g', 1); % Circular shift of g
g = a';
% Transpose back to row vector
end
%% Combine the two results
y = y1 + y2; % Add the results of both convolutions
% Plot the system response
stem(y, 'filled'); title('System
Response'); xlabel('n'); % Label
x-axis ylabel('Amplitude'); %
Label y-axis
Q4. Verify the distributive property of linear convolution i.e., whether 𝑥(𝑛)∗[ℎ1(𝑛)+
ℎ2(𝑛)]=𝑥(𝑛)∗ℎ1(𝑛)+𝑥(𝑛)∗ ℎ2(𝑛).
Ans: Matlab Code,
clc; % Clear the command
window clear all; % Clear all
variables close all; % Close all
figure windows
x = [0 1 2 3]; % Input sequence
n = 0:3; % Index range for the input
sequence % Define the two filter sequences h1
and h2 h1 = sin(n * pi); % First filter (sin
function) h2 = cos(n * pi); % Second filter (cos
function) y1 = conv(x, h1); % Convolution of x
and h1 y2 = conv(x, h2); % Convolution of x
and h2
y_sum = y1 + y2; % Adding the results of the individual convolutions
subplot(2, 1, 1);
stem(y_sum, 'filled'); % Plot the sum of the individual convolutions
title('Individual Convolution Addition');
% Compute the convolution of x with the sum of h1 and h2
h3 = h1 + h2; % Summing h1 and h2
y_combined = conv(x, h3); % Convolution of x with h3 (h1 + h2)
subplot(2, 1, 2);
stem(y_combined, 'filled'); % Plot the result of convolution with h3
title('Convolution of x with h3(n)');
Q5. Circular convolution a. Write a MATLAB program to find the circular
convolution between two discrete time sequences 𝑥(𝑛)={1,2,3,4} and ℎ(𝑛)={1,0,−0}.
Verify the result with the build in command 𝒄𝒄𝒐𝒏𝒗.
Ans: Matlab Code,
x = [1 2 3 4]; % Input sequence h = [1 0 -1]; %
Filter sequence h = [h zeros(1, length(x) -
length(h))]; l = length(x);
y = zeros(1, l);
for i = 1:l
for j = 1:l
k = mod(i-j, l) + 1; % Modulo operation for circular
indexing y(i) = y(i) + x(j) * h(k); end end subplot(2, 1,
1); stem(y, 'filled');
title('Circular Convolution using
loop'); xlabel('n');
ylabel('Amplitude');
y_cconv = cconv(x, h, l); % Circular convolution using MATLAB's
cconv subplot(2, 1, 2); stem(y_cconv, 'filled');
title('Circular Convolution using
cconv()'); xlabel ('n'); ylabel
('Amplitude');
Q6. Perform the correlation between 𝑠𝑖𝑛(𝑛 ∗ 𝑝𝑖/30) and 𝑠𝑖𝑛(2 ∗ 𝑛 ∗ 𝑝𝑖/30) over 6
samples.
Ans: Matlab Code,
clc; clear all; close all;
% Define the sequence for n = 0 to 5 (6 samples)
n = 0:5;
% Compute sin(n * pi / 3) and sin(2 * n * pi /
3) x1 = sin(n * pi / 3); x2 = sin(2 * n * pi /
3); % Perform the cross-correlation
correlation_result = xcorr(x1, x2);
% Plot the result
subplot(2,1,1);
stem(n, x1, 'filled');
title('sin(n * pi / 3)');
xlabel('n');
ylabel('x1');
subplot(2,1,2);
stem(-5:5, correlation_result, 'filled'); % the result spans -5 to +5 lags
title('Correlation between sin(n * pi / 3) and sin(2n * pi / 3)');
xlabel('Lag'); ylabel('Correlation Value');
Discussion:
In this experiment, we have explored and applied fundamental concepts of convolution and
correlation in digital signal processing. Each method has been analyzed to understand its
theoretical foundations, practical applications, and inherent properties: Autocorrelation,
Cross-correlation, Convolution, Linear Convolution, Circular Convolution, Properties of
Convolution.
Through this lab, the relationships between convolution and correlation techniques have been
systematically analyzed. Autocorrelation and cross-correlation have provided insights into
signal similarities, while convolution methods have modeled system behavior. The findings
have emphasized the importance of understanding these techniques for practical applications
in signal processing, such as filtering, system identification, and communication systems. The
study has laid a strong foundation for further exploration of advanced methods and real-world
implementations in digital signal processing.