0% found this document useful (0 votes)
51 views

Digital Signal Processing: Name: Roll No: Aim

The document summarizes Charmil Gandhi's digital signal processing lab aimed at understanding concepts of convolutions and their applications. The lab includes: 1. Computing the linear convolution of sequences using a custom function and built-in commands. 2. Finding the cross-correlation between sequences using a custom function and built-in commands. 3. Computing the auto-correlation of finite sequences using custom and built-in functions.

Uploaded by

Charmil Gandhi
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)
51 views

Digital Signal Processing: Name: Roll No: Aim

The document summarizes Charmil Gandhi's digital signal processing lab aimed at understanding concepts of convolutions and their applications. The lab includes: 1. Computing the linear convolution of sequences using a custom function and built-in commands. 2. Finding the cross-correlation between sequences using a custom function and built-in commands. 3. Computing the auto-correlation of finite sequences using custom and built-in functions.

Uploaded by

Charmil Gandhi
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/ 13

Digital Signal Processing

Lab - 2
Name :​ Charmil Gandhi
Roll No : ​1741059

Aim: ​To understand different concepts of convolutions along with its applications.
-------------------------------------------------------------------------------------------------------------------------------
1. Find the linear convolution of following finite length sequences using available command
and also without command by developing your own function.
a. x(n) = {1,2,2,1}
h(n) = {1,-1,2}

b. x(n) = {-2,0,1,-1,3}
h(n) = {1,2,0,-1,}

c. x(n) = {1,2,3,1}
h(n) = {1,2,1,-1}

d. x(n) = {9,1,5,4}
h(n) = {0,2,2}
Approach: ​First I solved all the above questions on paper using matrix method for linear
convolution.

Code: convolve function


function​ ​[conv1,n_size]​ = ​convolve​(x1,ii1,x2,ii2)
lower_lim1 = ​0​-ii1+​1​;
upper_lim1 = ​length​(x1) - ii1;
n1 = lower_lim1:​1​:upper_lim1; ​% index array for sequence 1

lower_lim2 = ​0​-ii2+​1​;
higher_lim2 = ​length​(x2) - ii2;
n2 = lower_lim2:​1​:higher_lim2; ​% index array for sequence 2

n = lower_lim1+lower_lim2:upper_lim1+higher_lim2;
len1 = ​length​(x1); ​% length of sequence 1
len2 = ​length​(x2); ​% length of sequence 2
​size​ = len1+len2​-1​; ​% size of convoluted signal

​if​ max(len1,len2)==len1
k=x1;
l=x2;
​else
l=x1;
k=x2;
​end
conv=​zeros​(​1​,​size​); ​% adding zeroes
minimum = min(len1,len2);

% performing convolution
​for​ ​i​=​1​:​size​-minimum+​1
z=​zeros​(​1​,​size​);
z(​i​:​i​+minimum​-1​) = k(​i​)*l;
conv = conv+z;
​end
conv1 = conv;
n_size = n;
end

The above code is of user made convolution function ​convolve w ​ hich takes 2 signals ​x1 ​and
th​
x2,​ and also the 0​ index of both the signals (​ii1 ​and ​ii2​). First, I am finding a
lower​(lower_lim1)​ and upper​(upper_lim1)​ index of signal ​x1,​ thus getting the index range ​n1.​
Similarly, I have found the lower​(lower_lim2)​ and upper​(upper_lim2)​ index of signal ​x2, t​ hus
getting its index range ​n2.​ Now, the length of the output sequence after performing linear
convolution is ​size=len1 + len2 - 1 w​ here len1 is length of sequence 1 and length 2 is the
length of the sequence 2. The range of index of the resultant signal as
n=lower_lim1+lower_lim2:upper_lim1+higher_lim2.​ ​The next step is to perform
convolution on these 2 signals.​ DOUBT here….

Code: Main File


clc;
close all;
clear all;

x1 = input(​'Enter sequence x: '​); ​% taking first sequence as input


ii1 = input(​'Enter index of origin for sequence x: '​); ​% taking index of origin

x2 = input(​'Enter sequence h: '​); ​% taking first sequence as input


ii2 = input(​'Enter index of origin for sequence h: '​); ​% taking index of origin
% Finding the limits of sequence #1
lower_lim1 = ​0​-ii1+​1​;
upper_lim1 = ​length​(x1) - ii1;
n1 = lower_lim1:​1​:upper_lim1;
% Finding the limits of sequence #2
lower_lim2 = ​0​-ii2+​1​;
upper_lim2 = ​length​(x2) - ii2;
n2 = lower_lim2:​1​:upper_lim2;

% CONVOLUTION
[conv1,n] = convolve(x1,ii1,x2,ii2);
w = conv(x1,x2); ​% using in-built function for convolution
x1 ​% displaying sequence x1
x2 ​% displaying sequence x2
conv1 ​% displaying convoluted signal
% Plotting the graph of x(n)
subplot(​2​,​2​,​1​);
stem(n1,x1);
title(​'x(n)'​);

% Plotting the graph of h(n)


subplot(​2​,​2​,​2​);
stem(n2,x2);
title(​'h(n)'​);

% Plotting the graph of convolution(without in-built function)


subplot(​2​,​2​,​3​);
stem(n,conv1);
title(​'Convolved Sequence'​);

% Plotting the graph of convolution(in-built function)


subplot(​2​,​2​,​4​);
stem(n,w);
title(​'Convolved Sequence(conv function)'​);

In the above code, I am first taking signal ​x1, x2,​ and their range of index(​ii1 and ii2​) from the
user. Then, I have called the user made convolution function​(convolve)​ and the inbuilt function
of convolution ​(conv) ​which takes both the signal ​x1 ​and ​x2​ as input parameters and then
displays both the signals and their convoluted signal. I have used ​subplot​ command to plot
multiple signals in a single figure. This is the common code for all the 4 subquestions.
The above figures are of subquestion 1 and subquestion 2.

The above figures are of sub-question 3 and sub-question 4

2. Find cross-correlation between two sequences using available command and also
without command by developing your own function.
x(n) = {1,2,2,1}
h(n) = {1,-1,2}
Code: crossCorrelate Function
function​ ​[conv,n]​ = ​crossCorrelate​(x1,ii1,x2,ii2)
lower_lim1 = ​0​-ii1+​1​; ​% lower limit of first sequence
upper_lim1 = ​length​(x1) - ii1; ​% upper limit of first sequence
n1 = lower_lim1:​1​:upper_lim1; ​% total length

lower_lim2 = ​0​-ii2+​1​; ​% upper length of second sequence


upper_lim2 = ​length​(x2) - ii2; ​% lower limit of second sequence
n2 = lower_lim2:​1​:upper_lim2; ​ total length
%
x3 = ​fliplr​(x2); ​ flipping the sequence 2
%
n3 = ​fliplr​(​-1​*n2);

n = lower_lim1+n3(​1​):upper_lim1+n3(​length​(n2));
l1 = ​length​(x1); ​% length of sequence 1
l2 = ​length​(x2); ​% length of sequence 2
​size​ = ​length​(n); ​% length of resultant signal

​if​ max(l1,l2)==l1
k=x1;
l=x3;
​else
k=x3;
l=x1;
​end

conv=​zeros​(​1​,​size​); ​% adding zeroes


minimum = min(l1,l2);

​for​ ​i​=​1​:​size​-minimum+​1
z=​zeros​(​1​,​size​);
z(​i​:​i​+minimum​-1​) = k(​i​)*l;
conv = conv+z;
​end
end

The above code of the user made function to perform the cross-correlation ​(crossCorrelate)
which takes 2 signals ​x1 a ​ nd ​x2​, and also the 0​th​ index of both the signals (​ii1 a
​ nd ​ii2)​ . First, I
am finding a lower​(lower_lim1)​ and upper​(upper_lim1)​ index of signal ​x1,​ thus getting the
index range ​n1.​ Similarly, I have found the lower​(lower_lim2)​ and upper​(upper_lim2)​ index of
signal ​x2, ​thus getting its index range ​n2.​ In cross correlation, we flip the one signal. So, I have
flipped the signal 2 ​x2 ​using the ​fliplr f​ unction and storing the signal in ​x3. A
​ s the signal is
flipped, the index should also be taken care of. So, I have also flipped the index accordingly and
stored ​n2​ into ​n3.​ The range of index of the resultant signal can be given as
n=lower_lim1+n3(1):upper_lim1+n3(length(n2)).​ ​The length of the resultant signal
would be the same as the length of index range ​n​. The next step is to perform correlation.
Code: Main File
clc;
close all;
clear all;

jj = [​-2​:​1​:​4​]
x1 = input(​'Enter sequence x: '​); ​% taking sequence x from user
ii1 = input(​'Enter index of origin in Sequence x: '​); ​% taking index of origin from
user

x2 = input(​'Enter sequence h: '​); ​% taking sequence h from user


ii2 = input(​'Enter index of origin in Sequence h: '​);​% taking index of origin from
user

% Finding the limits of sequence #1


lower_lim1 = ​0​-ii1+​1​;
upper_lim1 = ​length​(x1) - ii1;
n1 = lower_lim1:​1​:upper_lim1;

% Finding the limits of sequence #2


lower_lim2 = ​0​-ii2+​1​;
upper_lim2 = ​length​(x2) - ii2;
n2 = lower_lim2:​1​:upper_lim2;

% calculating cross correlation


[conv,n] = crossCorrelate(x1,ii1,x2,ii2); ​% function calling
w = xcorr(x1,x2);
cross_conv = conv;
inbuilt_conv_fxn = w;

x1 ​% displaying the input sequence x1


x2 ​ % displaying the input sequence x2(h)
conv ​ % displaying the result of cross correlation
w ​ % displaying the result using in-built function

% Plotting Graphs
subplot(​2​,​2​,​1​);
stem(n1,x1);
title(​'Sequence x(n)'​);

subplot(​2​,​2​,​2​);
stem(n2,x2);
title(​'Sequence h(n)'​);

%subplot(2,2,[3 4]);
subplot(​2​,​2​,​3​)
stem(n,conv);
title(​'Cross Correlated Sequence'​);

subplot(​2​,​2​,​4​)
stem(jj,w);
title(​'Cross Correlated Sequence(in-built function)'​);

In the above code, I am first taking signal ​x1, x2​, and their range of index(​ii1 and ii2​) from the
user. Then, I have called the user made cross-correlation function function​(crossCorrelate)​ and
the inbuilt function of cross-correlation ​(xcorr) ​which takes both the signal ​x1 a
​ nd ​x2​ as input
parameters and then displays both the signals and their cross-correlated signal.
3. Find Auto-correlation of finite length sequences using available command and also
without command by developing your own function.
Code:
function​ ​[conv,n]​ = ​autoCorrelate​(x1,ii1)
lower_lim1 = ​0​-ii1+​1​; ​% lower limit of sequence
upper_lim1 = ​length​(x1) - ii1; ​% upper limit of sequence
n1 = lower_lim1:​1​:upper_lim1; ​% array of sequence

x2 = ​fliplr​(x1); ​% flipping the sequence


​%n2 = fliplr(-1*n1);

len1 = ​length​(x1); ​% length of sequence x1


​size​ = ​2​*len1​-1​; ​ size of array of sequence
%
n = ​2​*lower_lim1:​2​*upper_lim1;

k = x1;
l = x2;

conv=​zeros​(​1​,​size​);
z=conv;

​for​ ​i​=​1​:​size​-len1+​1
z=​zeros​(​1​,​size​);
z(​i​:​i​+len1​-1​) = k(​i​)*l;
conv = conv+z;
​end
end

The above code is a user made function of autocorrelation ​(autoCorrelate)​ which takes signal
x1​ and its index of origin as input parameter. In autocorrelation, only one signal is needed. That
signal is reversed and then the convolution is performed on it. First, I am finding a
lower​(lower_lim1)​ and upper​(upper_lim1)​ index of signal ​x1,​ thus getting the index range ​n1.​
Then I am flipping the sequence ​x1 a ​ nd storing the flipped sequence in ​x2​. ​Now the length of
resultant signal is given by ​size = 2*len1 - 1 ​where len1 is the length of the sequence 2. The
output sequence is ​conv​ which is initialized as zeros. The next step is to perform convolution on
x1​ and ​flipped​ ​x1.
Code: Main File
clc;
close all;

x1 = input(​'Enter a sequence x: '​); ​% taking sequence as input


ii1 = input(​'Enter index of origin in Seq x: '​);​% taking index of origin as input

lower_liml1 = ​0​-ii1+​1​;
upper_lim1 = ​length​(x1) - ii1;
n1 = lower_liml1:​1​:upper_lim1;

% calculating auto-correlation
[conv,n] = autoCorrelate(x1,ii1);
w = xcorr(x1,x1);

x1 ​% displaying signal x1
%x2 % displaying signal x2

% plotting graphs
subplot(​2​,​2​,​1​);
stem(n1,x1);
title(​'Sequence 1'​);

subplot(​2​,​2​,​2​);
stem(n2,x2);
title(​'Sequence 2'​);
subplot(​2​,​2​,​3​);
stem(n,conv);
title(​'Auto Correlated Sequence'​);

subplot(​2​,​2​,​4​);
stem(n,w);
title(​'Auto Correlated Sequence(in-built function)'​);

First I have taken signal ​x1 ​and the index of its origin ​ii1​ as user input. After that I have
calculated the lower index and upper index, thus finding the range of index. Then, I have called
the user made function of autocorrelation ​(autoCorrelate).​ There is no inbuilt function of
autocorrelation, but as the steps are very similar to that of cross-correlation, I have used the
xcorr​ function to perform auto-correlation whose both input parameters will be the signal ​x1.​ To
plot more than one graph in a single figure, I have used ​subplot ​command.
4. Find the linear convolution for following infinite length sequences and Plot required
outputs.
a. X(n) = u(n), h(n) = u(n)
Code:

clc;
close all;
clear all;

n1 = ​-5​:​0.1​:​5​;
x_n = (n1>=​0​); ​% for u(n),if n==0 it will take step
ii1 = n1;
h_n =(n1>=​0​); ​% for u(n),if n==0 it will take step
ii2 = n1;

minimum_x = min(ii1); ​% minimum index of sequence x


minimum_h = min(ii2); ​ minimum index of sequence h
%

limits_x = max(min(ii1),-min(ii1)); ​% taking the limits of x


limits_h = max(min(ii2),-min(ii2)); ​ taking the limits of h
%

maximum_x = max(ii1); ​% maximum index of sequence x


maximum_h = max(ii2); ​% maximum index of sequence h
x=​zeros​(​1​,​length​(x_n)+​length​(h_n)​-1​); ​% adding zeroes
h=​zeros​(​1​,​length​(x_n)+​length​(h_n)​-1​); ​% adding zeroes
x(​1​:​length​(x_n)) = x_n;
h(​1​:​length​(h_n)) = h_n;

% calculating linear convolution of infinite sequence


for​ ​i​=​1​:(​length​(x_n)+​length​(h_n)​-1​)
​ans​(​i​)=​0​;
​for​ ​j​ = ​1​:​i
​ans​(​i​)=​ans​(​i​)+x(​j​)*h(​i​-​j​+​1​);
​end
end
padding = limits_x + limits_h+​1​;

% plotting the graphs


hold on;
subplot(​2​,​2​,​1​)

jj = minimum_x:​0.1​:maximum_x
stem(jj,x_n);
title(​'x(n)'​)
subplot(​2​,​2​,​2​)

jj = minimum_h:​0.1​:maximum_h
stem(jj,h_n);
title(​'h(n)'​)

subplot(​2​, ​2​,[​3​,​4​])
​ 1​)
jj = (-(padding​-1​)):(​length​(x_n)+​length​(h_n)​-1​-(padding​-1​)-
stem(jj,​ans​);
title(​'Convoluted Signal'​)

First I have defined an array ​n1​ ranging from -5 to +5 with an increment of 0.1. ​x(n) ​and ​h(n)​,
both are unit step functions
b. X(n) = cos (2n u(n) , h(n) = u(n)
Code:
clc;
close all;
clear all;

t = ​-100​:​0.1​:​100​; ​% defining the array of time


X = ​cos​(​pi​*t); ​ defining the function
%
unitstep = t>=​0​; ​ generating unit step function
%

x=[X,zeros(​1​,length(t))]; ​% adding zeroes to remaining places


h=[unitstep,zeros(​1​,length(t))]; ​ adding zeroes to remaining places
%

% calculating the linear convolution of infinite sequence


for​ ii = ​1​:​2​*​length​(t)​-1
y(ii) = ​0​;
​for​ jj = ​1​:ii
y(ii) = y(ii) + x(jj)*h(ii-jj+​1​);
​end
end

w = conv(single(X),double(unitstep));
t_ = ​-200​:​0.1​:​200​; ​% defining new array for time

% plotting graphs
subplot(​2​,​2​,​1​)
plot(t,X)
title(​'Sequence1'​)
​ ​]);
ylim([​-1​ 1
xlim([​-5​ 5​ ​]);

subplot(​2​,​2​,​2​)
plot(t,unitstep)
title(​'Sequence2'​)
ylim([​0​ ​1.5​]);

subplot(​2​,​2​,​3​)
plot(t_,y)
title(​'Convoluted Sequence'​)
xlim([​-150​ ​200​]);

subplot(​2​,​2​,​4​)
plot(t_,w)
title(​'Convoluted Sequence(inbuilt function)'​)
xlim([​-150​ ​200​]);

Conclusion:
In this lab, I have learnt the basics of convolution, cross-correlation, auto correlation and also
performed the simulations of these on various finite and infinite length input signals.

You might also like