Correlatio and Convolution Using Matlab
Correlatio and Convolution Using Matlab
ECE 2006
LAB TASK 2
Submitted to Prof.ASHISH P
Submitted by D.Sai theja
Reg:no 17BEC0323
Q1. Linear Convolution
Give a MATLAB program to find the linear convolution between two discrete-time sequences
x(n) = {1, 2, 3, 4} and h(n) = {1, 0, -1}. Verify your result with the inbuilt command
conv(x,h).
Code-
clc;
clear all;
close all;
h=[1 0 -1];
h1=length(h);
h2=h; % storing h in h2
subplot(4,1,1);
stem(h2)
title('hsequence');
%reversing h
for i=1:h1
h(i)=h2(h1+1-i);
end
subplot(4,1,2); stem(h)
title('Inverse of h(n)');
Output-
Code:
clear all;
close all;
x=[1 2 3 4]; % discrete sequence
x1=length(x);
h=[1 0 -1] ;
h1=length(h);
x2=x;%storing x in x2
subplot(4,1,1);
stem(x2)
title('x(n)sequence');
%reversing x
for i=1:x1
x(i)=x2(x1+1-i);
end
subplot(4,1,2);
stem(x)
title('Inverse of x(n)');
n=x1+h1-1;
f=[zeros(1,n-h1) h]; %appending zeros in the beginning
g=[x zeros(1,n-x1)]; % appending zeros at the end
y=[zeros(1,n)];
a=g'; %complementing f1 for performing circular shift
%performing convolution
for i=1:n y(i)=f*a;
a=circshift(g',1);
g=a';
end
subplot(4,1,3)
stem(y);
title('Linear Convolution');
subplot(4,1,4) y=conv(h,x2) stem(y);
title('Convolution using conv(x,h)')
Output-
Output of Convolution = [1 2 2 2 -3 -4]
b. Consider two systems with individual impulse responses given by ℎ1(𝑛) = 𝑠𝑖𝑛(nπ);0 ≤ n ≤ 3
and ℎ2(𝑛) = 𝑐𝑜𝑠(𝑛𝜋);0 ≤ 𝑛 ≤ 3. The two systems are arranged in parallel such that the outputs
from these systems are summed using an adder(y(n)=y1(n)+y2(n)). An input x(n) = {0, 1,
2, 3} is applied to both the systems. Find the overall response y(n) at the output of
the adder.
Code -
clc;
clear all;
close all;
% first convolve x and h1 to get y1
% Then convolve x and h2 to get y2.
% Both of these when added give y3 which is the answer.
x=[0 1 2 3];
n=0:3;
h1=sin(n*pi);
h2=cos(n*pi);
%convolving x and h1
a=length(x);
b=length(h1);
k=h1;
%reversing h1[n]
for i=1:b
h1(i)=k(b+1-i);
end
n=a+b-1;
f=[zeros(1,n-a) x];
g=[h1 zeros(1,n-b)];
y1=[zeros(1,n)];
a=g';
%performing convolution
for i=1:n
y1(i)=f*a;
a=circshift(g',1);
g=a';
end
%convolving x and h2
a=length(x);
b=length(h2);
k=h2;
%reversing H2[n]
for i=1:b
h2(i)=k(b+1-i);
end
n=a+b-1;
f=[zeros(1,n-a) x];
g=[h2 zeros(1,n-b)];
y2=[zeros(1,n)];
a=g';
%performing convolution
for i=1:n
y2(i)=f*a;
a=circshift(g',1);
g=a';
end
y=y1+y2;
stem(y)
title('Systemresponse');
Output-
c.Verify the distributive property of linear convolution i.e., whether
𝑥(𝑛) ∗ [ℎ1(𝑛) + ℎ2(𝑛)] = 𝑥(𝑛) ∗ ℎ1(𝑛) + 𝑥(𝑛) ∗ ℎ2(𝑛).
Code-
Clc;
Clear all;
Close all;
x=[0 1 2 3];
n=0:3;
h1=sin(n*pi);
h2=cos(n*pi);
y=conv(x,h1)+conv(x,h2);
subplot(2,1,1)
stem(y)
title('Individual Convolution addition');
h3=h1+h2;
y=conv(x,h3);
subplot(2,1,2)
stem(y)
title('Convolution of x with h3(n)’);
Output-
Q3. Circular Convolution
a. Give a MATLAB program to find the circular convolution between two discrete-
time sequences x(n) = {1, 2, 3, 4} and h(n) = {1, 0, -1}.
Verify your result with the inbuilt command cconv.
Code-
clc;
closeall;
clearall;
x=[1 2 3 4];
h=[1 0 -10];
l=length(x);
y=[zeros(l)];
for i=1:l
for j=1:l
y(j,i)=x(j);
end
a=circshift(x',1);
x=a';
end
y1=y*h';
h=[1 2 3 4];
x=[1 0 -1 0];
l=length(x);
y=[zeros(l)];
for i=1:l
for j=1:l
y(j,i)=x(j);
end
a=circshift(x',1);
x=a';
end
y2=y*h';
subplot(2,1,1);
stem(y1)
title('Circular Convolution');
subplot(2,1,2);
stem(y2)
title('Circular Convolution');
Output-
Q4. Correlation