Professional Documents
Culture Documents
Y (T) X (T) H (T) : Convolution and Correlation
Y (T) X (T) H (T) : Convolution and Correlation
Y (T) X (T) H (T) : Convolution and Correlation
Date: 30.3.23
Ex No: 2
Aim: To perform convolution (linear and circular) and correlation of two given sequences
using MATLAB.
y(t)= ∫x1(τ)x2(t- τ) dτ
−∞
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
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
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
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
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.