You are on page 1of 11

S.

SIVASANKAR
3122 21 3002 100

CONVOLUTION & CORRELATION


EX.NO : 2
DATE : 09/03/2023

Aim:
To write MATLAB programs to demonstrate the convolution and correlation operations of
the Discrete Time sequence for the given inputs.

Software Used:
MATLAB INTERPRETER – R2022 b VERSION

Description:
Convolution is defined as the mathematical way of combining two signals to form the third signal.
Mathematically the Discrete Time Convolution is defined as:

y(n) = x(n)*h(n) = ∑ x ( k ) h (n−k )
k=−∞

Let N1 be the length of the first sequence and N2 be the length of the second sequence. The no.of
samples in the output sequence is :
N = N1+N2-1
In matrix notation : Y = H x X where H is the impulse sequence matrix of order N*N and X is the
input sequence matrix of order N*1.

Circular Convolution is similar to linear convolution but here One of the given sequences is
repeated via circular shift of one sample at a time to form a N X N matrix.

Mathematically , circular convolution is defined as:

Here this operation is done using the mod operator. The length of the resultant sequence is
the length of the longer input sequence,

Correlation is defined as the mathematical way of finding the relation between two signals. It
is similar to convolution but one of the sequences is the functional inverse.
S.SIVASANKAR
3122 21 3002 100

Programs:

1.The first input x[n]= {your register number} and h[n]=δ[n]. Also check your
program with h[n]=δ[n-2]. Display and plot the output.

Theory:
By the properties of convolution , x(n)*δ(n)=x(n) . Hence, we get the same input sequence as the
resultant sequence

MATLAB Program:
%Main Program
clc;
close all;
clear;

%Getting the sequence as inputs

x=input('Enter the input sequence=');


h=input('Enter the impulse sequence=');

%Code to plot the input sequence


subplot(3,1,1);

a=length(x); %Length of the input sequence

n=0:a-1; %Time index of the input sequence

stem(n,x);

xlabel('time');
ylabel('amplitude');
title('Input sequence');

%Code to plot the impulse sequence


subplot(3,1,2);

b=length(h); %Length of the impulse sequence

m=0:b-1; %Time index of the input sequence

stem(m,h);

xlabel('time');
ylabel('amplitude');
title('Impulse sequence');

%Length of the output sequence


c=a+b-1;

%Time index of the input sequence


S.SIVASANKAR
3122 21 3002 100

n=0:c-1;

Y=findlc(x,h);

subplot(3,1,3);
stem(n,Y);
title('Linear Convolution of the two sequences');
xlabel('Time');

ylabel('Amplitude');
disp('The Linear Convolution output is:Y=')
disp(Y);

%Function program
function[Y] = findlc(X,H)

m=length(X);
n=length(H);
z=n+m-1;
X=[X,zeros(1,z-m)];
H=[H,zeros(1,z-n)];

for i=1:z
Y(i)=0;
for j=1:m
if(i-j+1>0)
Y(i)=Y(i)+X(j)*H(i-j+1);
end
end
end
end

Output:
S.SIVASANKAR
3122 21 3002 100

Output when h(n) = δ(n-2):


x(n) * δ(n-k) = x(n-k) . Here k=2. Hence the result is x(n) shifted by 2 samples.
S.SIVASANKAR
3122 21 3002 100

2.Convolve two rectangular pulses of any arbitrary width . Plot the output. Also
plot the convolution of six rectangular pulses and write your inference.

MATLAB Program:
function y=linear_conv(h,x) %% Function definition
N1=length(h);
N2=length(x);
N=N1+N2-1;
h1=[h zeros(1,N-N1)]; %% Function body
x1=[x zeros(1,N-N2)];
H=zeros(N);
for m=1:N
for n=1:m
if m>n %% Looping and iterative statements
H(m,n)=h1(1,m-n+1);
else m==n
H(m,n)=h1(1,1);
end
end
end
y=H*x1'; %%Return variable
P=0:length(y)-1; %%Plotting the points in the graph
stem(P,y,'r');
ylabel('Amplitude')
xlabel('Time')
%% END OF FUNCTION
function y5=rect_pulse() %% Generating rectangular pulses
n=-10:10;
x1=input('input width of pulse');
y2=(n>=-x1);
S.SIVASANKAR
3122 21 3002 100

y4=(n<=x1);
y5=y2&y4;
N=input("how many inputs");
x=rect_pulse();
h=rect_pulse();
y=linear_conv(h,x);
for m=1:N-2
x=rect_pulse();
h1=y';
y=linear_conv(h1,x);
end
disp('Output of Linear Convolution');
disp(y')

Output
how many inputs : 6
input width of pulse : 3
input width of pulse : 2
%%Six pulse width
input width of pulse : 3
Output of linear convolution :
[0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 3 6 10 15 19 22 23 22 19 15 10 6 3 1 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0]
S.SIVASANKAR
3122 21 3002 100

Graph:

INFERENCE:
Convolution between 2 rectangular pulses results in a trapezoidal waveform but if the pulses
are of equal width then the output would have the shape of an isosceles triangle. By convolving
more pulses, the curve smoothens resembling a gaussian curve.
S.SIVASANKAR
3122 21 3002 100

3.Perform circular convolution between the sequences x1[n]={your register


number} and x2[n]={date of birth}. Display and plot the output.

MATLAB Program:
%Main program
clc;
close all;
clear;
x=input('Enter the input sequence=');
h=input('Enter the impulse sequence=');
subplot(3,1,1);
a=length(x);
n=0:a-1;
stem(n,x);
xlabel('Time');
ylabel('Amplitude');
title('Input sequence');
subplot(3,1,2);
b=length(h);
m=0:b-1;
stem(m,h);
xlabel('Time');
ylabel('Amplitude');
title('Impulse sequence');
c=max(a,b);
n=0:c-1;
Y=findcc(x,h);
subplot(3,1,3);
stem(n,Y);
title('Circular Convolution of the two sequences');
xlabel('Time');
ylabel('Amplitude');
disp('The Circular Convolution output is:Y=')
disp(Y);

function[Y] = findcc(X,H)
m=length(X);
n=length(H);
N=max(m,n);
X=[X zeros(1,N-m)];

H=[H zeros(1,N-n)];
Y=zeros(1,N);
for n=1:N
for m=1:N
j=mod(n-m,N)
j=j+1
Y(n)=Y(n)+X(m)*H(j)
end
end
end
S.SIVASANKAR
3122 21 3002 100

Output:
S.SIVASANKAR
3122 21 3002 100

4. Generate white Gaussian noise with zero mean and unit variance using the
randn function of MATLAB. Determine the autocorrelation of the white
Gaussian noise and plot the output.

MATLAB Program:
%Main Program
clc;
close all;
clear;
L=1000;
mu=0;
sigma=1;
V=sigma*randn(1,L)+mu;
subplot(2,1,1);
stem(V);
title('White Gaussian Noise');
subplot(2,1,2);
Y=findac(V,V);
n=(-L+1):1:(L-1)
stem(n,Y);
title('Linear Convolution of the two sequences');
xlabel('Time');
ylabel('Amplitude');

%Function program
function Y = findac(x,h)
N1=length(x);
N2=length(h);
H2=fliplr(h);
N=N1+N2-1; %Length of the output sequence.
X1=[x,zeros(1,N-N1)]; %Zero padding to the input sequence
H1=[H2,zeros(1,N-N2)]; %Zero padding to the impulse sequence

for i=1:N %1st for loop


Y(i)=0;
for j=1:i %2nd for loop
if(i-j+1>0)
Y(i)=Y(i)+X1(j)*H1(i-j+1); %Performing the convolution operation.
end
end
end
end
S.SIVASANKAR
3122 21 3002 100

Output:

Result:
Thus, the programs to demonstrate Discrete Time Convolution and Correlation is executed for the
given inputs and the output is verified.

You might also like