Discrete Convolution and Correlation: Part-1: Aim
Discrete Convolution and Correlation: Part-1: Aim
Part-1 :
Aim : The aim of this experiment is to study mathematical operation such as Linear convolution,
Circular convolution, Linear convolution using circular convolution.
Objective
1. Develop a function to find Linear Convolution and Circular Convolution
2. Calculate Linear Convolution, Circular Convolution, Linear Convolution using Circular
Convolution and verify the results using mathematical formulation.
3. Conclude on aliasing effect in Circular convolution
Problem Definition :
1. Find Linear Convolution and Circular Convolution of L point sequence x[n] and M point
sequence h[n].
2. Find Linear Convolution of L point sequence x[n] and M point sequence h[n] using Circular
convolution.
3. Give your conclusion about number of values in Linearly Convolved Signal, and Aliasing
effect in Circular Convolution.
Code:
%TO FIND CONVOLUTION OF THE GIVEN SIGNAL
clc;
clear all;
close all;
X = [4,-5,-6,-7];
figure
stem(X,'LineWidth',2);
xlabel('No. of Samples');
ylabel('Amplitude');
set(gca,'Xlim',[0,len+1],'Ylim',
[min(X),max(X)],'FontSize',14);
set(gcf,'Color','white')
title('Input Response')
H = [1,-1];
figure
stem(H,'LineWidth',2);
xlabel('No. of Samples');
ylabel('Amplitude');
set(gca,'Xlim',[0,len+1],'Ylim',
[min(H),max(H)],'FontSize',14);
set(gcf,'Color','white')
title('Impulse Response')
%Linear convolution
table = H'*X
len = length(H)+length(X)-1;
Y = zeros(1,len)
for i=1:length(H)
for j=1:length(X)
Y(i+j-1) = Y(i+j-1) + table(i,j);
end
end
figure
stem(Y,'LineWidth',2);
xlabel('No. of Samples');
ylabel('Amplitude');
set(gca,'Xlim',[0,len+1],'Ylim',
[min(Y),max(Y)],'FontSize',14);
set(gcf,'Color','white')
title('Linear Convolution')
%Circular Convolution
len_c = 0;
if length(X)>length(H)
len_c = length(X);
else
len_c = length(H);
end
B = [H,zeros(1,length(X)-length(H))]
B=B'
c=B;
B=circshift(B,1);
c=cat(2,c,B);
B=circshift(B,1);
c=cat(2,c,B);
B=circshift(B,1);
c=cat(2,c,B);
y=c*X';
figure
stem(y,'LineWidth',2);
xlabel('No. of Samples');
ylabel('Amplitude');
set(gca,'Xlim',[0,len+1],'Ylim',
[min(y),max(y)],'FontSize',14);
set(gcf,'Color','white')
title('Circular Convolution')
X=[X,zeros(1,length(H)-1)];
H=[H,zeros(1,length(X)-1)];
H=H';
c=H;
h=H
h=circshift(h,1);
c=cat(2,c,h);
h=circshift(h,1);
c=cat(2,c,h);
h=circshift(h,1);
c=cat(2,c,h);
h=circshift(h,1);
c=cat(2,c,h);
z=c*X';
n=1:length(X);
figure
stem(z(1:5),'LineWidth',2);
xlabel('No. of Samples');
ylabel('Amplitude');
set(gca,'Xlim',[0,len+1],'Ylim',
[min(z),max(z)],'FontSize',12);
set(gcf,'Color','white')
title('Linear Convolution using Circular')
Conclusion:
1) Linear convolution is basically used to calculate output of any LTI system. In linear
convolution, The length of output response (N) is given by formula
N = L+M-1
2) Circular convolution is same as linear convolution, but both the input signal and
impulse response signal are periodic and of same length. Also the length of output
response is same as both of them. In circular convolution, while finding the
multiplication with the shifted signal, the shifted sequence is replaced by either
previous or next sequence. This replacement is also known as aliasing.
Part-2 :
Objective:
1. Write a function to find correlation operation.
2. Calculate correlation of a DT signals and verify the results using mathematical
formulation.
3. Measure the degree of similarity using Carl’s Correlation Coefficient formula in time
domain.
Input Specifications :
1. Length of first Signal L and Signal values
x(n)= {4,-5,-6,-7} L= 4
2. Length of second Signal M and Signal values
y(n)= {1, -1} M= 2
Problem Definition:
1. Find auto-correlation of input signal. What is the significance of value of output signal
value at n=0?.
2. Find auto-correlation of delayed input signal.
3. Find cross-correlation of input signal and delayed input signal.
4. Find cross-correlation of input signal and scaled delayed input signal.
5. Compare the resultant signals. Give your conclusion.
Code:
%Title: Correlation
clc;
clear all;
close all;
%Input
x=[4,-5,-6,-7];
h=[1,-1];
if length(h)>length(x)
pad=length(h)-length(x);
x=[x zeros(1,pad)];
else if length(h)<length(x)
pad=length(x)-length(h);
h=[h zeros(1,pad)];
end
end
%AUTO CORRELATION
out_len = length(h);
out = zeros(1,out_len);
temp=x;
%Left Shift
for n=1:out_len
out(n)=x*temp';
temp=[0 temp(1:end-1)];
end
%new_len=2*length(x)-1;
%out=[zeros(1,out_len-1) out];
temp=x;
out1=zeros(1,out_len-1);
tmp=zeros(1,out_len-1);
for n=1:(out_len-1)
temp=[temp(2:end) 0];
out1(n)=x*temp';
end
for n=1:(out_len-1)
tmp(n)=out1(out_len-n);
end
out2 =[tmp out]
corr = xcorr(x,x)
stem([-3 -2 -1 0 1 2 3],out2,'LineWidth',2);
title('AutoCorrelation');
xlabel('SAMPLES');
ylabel('AMPLITUDE');
set(gca,'Xlim',[-4,4],'Ylim',[-50,150],'FontSize',15);
set(gcf,'Color','white');
figure
stem(x_delayed,'LineWidth',2);
title('Delayed Input');
xlabel('SAMPLES');
ylabel('AMPLITUDE');
set(gca,'Xlim',[0,6],'Ylim',[5,-10],
'FontSize',15);
set(gcf,'Color','white');
out_len = length(h);
out = zeros(1,out_len);
temp=x_delayed;
%Left Shift
for n=1:out_len
out(n)=x_delayed*temp';
temp=[0 temp(1:end-1)];
end
temp=x_delayed;
out1=zeros(1,out_len-1);
tmp=zeros(1,out_len-1);
for n=1:(out_len-1)
temp=[temp(2:end) 0];
out1(n)=x_delayed*temp';
end
for n=1:(out_len-1)
tmp(n)=out1(out_len-n);
end
out2 =[tmp out]
figure
stem([-4 -3 -2 -1 0 1 2 3
4],out2,'LineWidth',2);
title('AutoCorrelation of Delayed
Input');
xlabel('SAMPLES');
ylabel('AMPLITUDE');
set(gca,'Xlim',[-6,6],'Ylim',[-50,150],'FontSize',15);
set(gcf,'Color','white');
%CROSS CORRELATION
out_len = length(h);
out = zeros(1,out_len);
temp=h;
%Left Shift
for n=1:out_len
out(n)=x*temp';
temp=[0 temp(1:end-1)];
end
temp=h;
out1=zeros(1,out_len-1);
tmp=zeros(1,out_len-1);
for n=1:(out_len-1)
temp=[temp(2:end) 0];
out1(n)=x*temp';
end
for n=1:(out_len-1)
tmp(n)=out1(out_len-n);
end
out2 =[tmp out]
corr = xcorr(x,h)
stem([-3 -2 -1 0 1 2
3],out2,'LineWidth',2);
title('CrossCorrelation');
xlabel('SAMPLES');
ylabel('AMPLITUDE');
set(gca,'Xlim',[-4,4],'Ylim',[-10,10],'FontSize',15);
set(gcf,'Color','white');
figure
stem([-4 -3 -2 -1 0 1 2 3 4],out2,'LineWidth',2);
title('CrossCorrelation of Input with Delayed Input');
xlabel('SAMPLES');
ylabel('AMPLITUDE');
set(gca,'Xlim',[-6,6],'Ylim',[-50,150],'FontSize',15);
set(gcf,'Color','white');
x_scaled= 3*[4,-5,-6,-7];
len = length(x_scaled)
Figure
x_scaled = 3*x[n]
stem(x_scaled,'LineWidth',2);
xlabel('No. of Samples');
ylabel('Amplitude');
set(gca,'Xlim',[0,len+1],'Ylim',
[min(x_scaled),max(x_scaled)],
'FontSize',14);
set(gcf,'Color','white')
title('Scaled Input')
figure
stem([-3 -2 -1 0 1 2
3],out2,'LineWidth',2);
title('CrossCorrelation of Input
with scaled Input');
xlabel('SAMPLES');
ylabel('AMPLITUDE');
set(gca,'Xlim',[-6,6],'Ylim',
[-100,400],'FontSize',15);
set(gcf,'Color','white');
Conclusion:
1) The analysis of autocorrelation is a mathematical tool for identifying the missing
fundamental frequency in a signal. In an autocorrelation, there will always be a
peak at a lag of zero, and its size will be the signal energy.
2) Cross-correlations are useful for determining the time delay between two signals.
After calculating the cross-correlation between the two signals, the maximum (or
minimum if the signals are negatively correlated) of the cross-correlation function
indicates the point in time where the signals are best aligned.