Y (T) X (T) H (T) : Convolution and Correlation

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 9

Name: CH Vikram aditya

Reg No: 3122 21 3002 307

Date: 30.3.23
Ex No: 2

CONVOLUTION AND CORRELATION

Aim: To perform convolution (linear and circular) and correlation of two given sequences
using MATLAB.

Software used: MATLAB (R2022-b)


Description: Convolution is a mathematical operation used to express the relation between the
input and output of an LTI system. It relates the input, output, and impulse response of an LTI
system as
y(t)=x(t)∗h(t)
Where y (t) = output of LTI
x (t) = input of LTI
h (t) = impulse response of LTI
Correlation is a measure of similarity between two signals. The general formula for correlation
is

y(t)= ∫x1(τ)x2(t- τ) dτ
−∞

Where y (t) = output of LTI


x1(t) and x2(t)= input of LTI
1.The first input, 𝑥[𝑛] = {𝑦𝑜𝑢𝑟 𝑟𝑒𝑔𝑖𝑠𝑡𝑒𝑟 𝑛𝑢𝑚𝑏𝑒𝑟} and ℎ[𝑛] = 𝜕(𝑛). Also, check your program
with, ℎ[𝑛] = 𝜕(𝑛 − 2). Display and plot the output.
Code
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);

1
Name: CH Vikram aditya
Reg No: 3122 21 3002 307

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 y1= impulse_shift(x1) %%Generates Imopulse responses


h = dirac(0);
idx = h == Inf; % find Inf
h(idx) = 1; % set Inf to finite value
shift=input("enter n to be shifted");
s=zeros(1,2*x1);
disp(h);
h1=[h zeros(1,length(s)-1)];
if shift>0
s(shift+1:end)=h1(1:length(h1)-shift);
y1=s;
elseif shift==0
y1=h1;

end

%%Driver code
clear
clc
close all
x=input("Enter sequence 1");
x1 = length(x);
h=impulse_shift(x1);
y=linear_conv(h,x);
disp('Output of Linear Convolution');
disp(y')

Output
Enter sequence 1 [3 1 2 2 2 1 3 0 0 2 3 0 7]
enter n to be shifted 0

2
Name: CH Vikram aditya
Reg No: 3122 21 3002 307

Graph

FIG 1.1 Output when ℎ[𝑛] = 𝜕(𝑛) FIG 1.2 Output when ℎ[𝑛] = 𝜕(𝑛-2)

2. Convolve two rectangular pulses of any arbitrary width. Plot the output. Also, Plot the
convolution of six rectangular pulses and write your inference.
Code
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);
y4=(n<=x1);
y5=y2&y4;

3
Name: CH Vikram aditya
Reg No: 3122 21 3002 307

%%Driver code
clear
clc
close all
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 3
input width of pulse 2
input width of pulse 2
input width of pulse 3
[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]

Graph

FIG 2.1 Convolution between 2 rectangular pulses FIG 2.2 Convolution between 6 rectangular pulses

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.

4
Name: CH Vikram aditya
Reg No: 3122 21 3002 307

3. Perform circular convolution between the sequences,𝑥1 [𝑛] = {𝑦𝑜𝑢𝑟 𝑟𝑒𝑔𝑖𝑠𝑡𝑒𝑟 𝑛𝑢𝑚𝑏𝑒𝑟}
and 𝑥2 [𝑛] = {𝑦𝑜𝑢𝑟 𝑑𝑎𝑡𝑒 𝑜𝑓 𝑏𝑖𝑟𝑡ℎ}. Display and plot the output.
Code
function y=circular_conv(h,x) %% Function definition
N2=length(h);
N1=length(x);
if N1>N2
N=N1;
h1=[h zeros(1,N1-N2)]; %% Function body
x1=x;
else
N=N2;
h1=h;
x1=[x zeros(1,N2-N1)];
end
H=zeros(N);
for m=1:N %% Looping and iterative statements
for n=1:m
if m>n
H(m,n)=h1(1,m-n+1);
elseif m==n
H(m,n)=h1(1,1);
elseif m<n
H(m,n)=h1(mod(m-n,N)+1);
end
end
end
y=H*x'; %%Return variable
P=0:length(y)-1; %%Plotting the points in the graph
stem(P,y);

%%Driver code
clear
clc
close all
x=input("Enter 1st sequence");
h=input("Enter 2nd sequence");
y=circular_conv(h,x);
disp('Output of Circular Convolution');
disp(y)

Output
Enter 1st sequence [3 1 2 2 2 1 3 0 0 2 3 0 7]
Enter 2nd sequence [2 4 0 4 2 0 0 3]
Graph

5
Name: CH Vikram aditya
Reg No: 3122 21 3002 307

Output of Circular Convolution


6
14
8
24
24
22
12
29
23
18
28

6
Name: CH Vikram aditya
Reg No: 3122 21 3002 307

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.
Code
function y=correlations(h,x) %% Function definition
L=1000;
N2=length(h);
N1=length(x);
N=N1+N2-1; %% Function body
h2=fliplr(h);
h1=[h2 zeros(1,N-N2)];
x1=[x zeros(1,N-N1)]
H=zeros(N);
for m=1:N %% Looping and iterative statements
for n=1:m
if m>n
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
disp(H);
%% END OF FUNCTION
%% END OF FUNCTION

%%Driver code

L=1000; %Sample length for the random signal


mu=0;
sigma=1;
X=sigma*randn(L,1)+mu;
y=correlations(X',X');
P=(-L+1):1:(L-1); %%Plotting the points in the graph
stem(P,y,'g');
ylabel('Amplitude')
xlabel('Time')

Graph

7
Name: CH Vikram aditya
Reg No: 3122 21 3002 307

8
Name: CH Vikram aditya
Reg No: 3122 21 3002 307

Result

Convolution and correlation for the given two sequences have been performed using
MATLAB and the output is observed by plotting it in the graph.

You might also like