You are on page 1of 5

3) Convolution and Correlation (auto and cross correlation) of discrete sequences without using

built in functions for convolution and correlation operations.

(a) Linear Convolution

AIM:To write a program in MATLAB to perform linear Convolution between two discrete time sequences

APPARATUS REQUIRED : MATLAB Software

PROGRAM:

% MATLAB program for linear convolution

%linear convolution program

clc;

clear all;

close all;

disp('linear convolution program');

x=input('enter i/p x(n):');

m=length(x);

h=input('enter i/p h(n):');

n=length(h);

x=[x,zeros(1,n)];

subplot(2,2,1), stem(x);

title('i/p sequence x(n)is:');

xlabel('---->n');

ylabel('---->x(n)');grid;

h=[h,zeros(1,m)];

subplot(2,2,2), stem(h);

title('i/p sequence h(n)is:');

xlabel('---->n');

ylabel('---->h(n)');grid;

disp('convolution of x(n) & h(n) is y(n):');


y=zeros(1,m+n-1);

for i=1:m+n-1

y(i)=0;

for j=1:m+n-1

if(j<i+1)

y(i)=y(i)+x(j)*h(i-j+1);

end

end

end

subplot(2,2,[3,4]),stem(y);

title('convolution of x(n) & h(n) is :');

xlabel('---->n');

ylabel('---->y(n)');grid;

OUTPUT :

RESULT : The linear convolution of two sequences is verified by using MATLAB program.
b)Auto Correlation

AIM:To write a program in MATLAB to perform Auto Correlation of Discrète Sequences.

APPARATUS REQUIRED : MATLAB Software

PROGRAM :

clc;

clear all;

close all;

%% Inputs

% You can specify the inputs or can take them through command window

% h= input ('enter the sequence h');

% x= input ('enter the sequence x');

h=[1 1 2 3 4];

x=[1 3 5 7 9 2 4 6 8];

pp=h;qq=x;

% Plot the inputs

% subplot(3,1,1); stem(h,'m'); title ('h');ylabel('amplitude')

%%

% subplot(3,1,2); stem(x,'b'); title ('x');ylabel('amplitude')

%% calculate the cross correlation

l1= length (h); % calculate length of sequence h

l2= length (x); % calculate length of sequence x

l=abs (l1-l2); % calculate difference in lengths of sequence

if (l1 > l2)

x= [x zeros(1,l)];

else if(l2 > l)

h= [h zeros(1,l)];

end
end

h= [h zeros(1, max (l1, l2))];

for shift= 0:max(l1,l2);

new_x = [zeros(1, shift) x zeros(1, (max (l1 , l2))-shift)];

y(shift+1,:)= sum(h.* new_x);

end

y=y';

% Display the correlation sum

subplot(3,1,1); stem(pp,'m'); title ('h');ylabel('amplitude')

subplot(3,1,2); stem(qq,'b'); title ('x');ylabel('amplitude')

subplot(3,1,3); stem(y,'r'); title ('cross correlation'); xlabel('time');

OUPUT:

RESULT:

c)Cross Correlation

AIM:To write a program in MATLAB to perform Cross Correlation of Discrète Sequences.

APPARATUS REQUIRED : MATLAB Software

PROGRAM :

close all;
clear all;
clc;
x=input('enter the value of 1st sequence');
j=input('enter the value of 2nd sequence');
h=fliplr(j);
disp('the 1st sequence is-');
disp(x);
disp('the 2nd sequence is-');
disp(j);
lx=length(x);
lh=length(h);
n=lx+lh-1;
subplot(3,1,1);
stem(x);
title('1st sequence');
subplot(3,1,2);
stem(j);
title('2nd sequence');
hh=[h zeros(1,n-lh)];
xx=zeros(n);
xx(1:lx,1)=x;
for i=2:n
for j=2:n
xx(j,i)=xx(j-1,i-1);
end;
end;
yy=xx*hh';
subplot(3,1,3);
stem(yy);
disp('cross correlate o/p->');
disp(yy');
title('y=cross correlation of x & j');

OUPUT:

RESULT: