You are on page 1of 5

EE304 LAB

Experiment: 3 and 4
Mihir Dalal 2019eeb1171
Mihir Kumar Srivastava 2019eeb1172

Matlab Codes
1) MATLAB code for Part 1

clear all
close all
clc
% x=u[n]-u[n-6],this is equivalent to x=[1,1,1,1,1,1] (taking 6 samples)
x=[1,1,1,1,1,1]
h=x;
n_x=length(x);
n_h=length(h);

%FOR LINEAR CONVOLUTION


N = n_x+n_h-1; % linear convolution of two signals with n and m samples results in n+m-1
samples
naxis=0:N-1; %discrete time corresponding to N samples
x1=[x zeros(1,N-n_x)];
h1=[h zeros(1,N-n_h)];
%zero padding done to make the no of samples equal in both x1 and h1 as
%y(n) requires both x(n) and h(n)
n_axis=0:N-1;% discrete time values at which we may get non-zero convoluted samples.
linear_cov_output=zeros(1,N);%Initializing the output samples array with 0 and size N(no of
samples in convoluted signal)

% linear convolution formula is y(n)= Σ(over k) (x(k) * h(n-k))


% But in matlab we have 1 based indexing so the modified formula for
% calculating linear convolution becomes y(n)= (over k) (x(k) * h(n-k+1))
for n=1:N
for k=1:n % since for n-k>=0,we have h(n-k+1) term contributing
linear_cov_output(n)=linear_cov_output(n)+x1(k)*h1(n-k+1);

end
end
fprintf("The linear convolution without using the inbuilt library: ");
linear_cov_output %just for printing it
fprintf("The linear convolution using the inbuilt library: ");
desired_linear_output = conv(x,h) %calculating linear convolution using the inbuilt library

%% Our linear convolution of two signals coming with and without using the inbuilt library are
similar.

2) MATLAB code for Part 2

clear all
close all
clc
% x=u[n]-u[n-6],this is equivalent to x=[1,1,1,1,1,1] (taking 6 samples)
x=[1,1,1,1,1,1,0,0,0,0,0];
h=x;
N=length(x);
n_axis=0:N-1; %discrete time corresponding to N samples

2
circular_cov_output=zeros(1,N); %since circular convolution of the 2 given signals will lead to
N samples in the output
for n=1:N
for m=1:N
j=mod(n-m,N);
j=j+1;
circular_cov_output(n)=circular_cov_output(n)+x(m)*h(j);
end
end
fprintf("The circular convolution without using the inbuilt library: ");
circular_cov_output %just for printing it
fprintf("The circular convolution using the inbuilt library: ");
desired_output = cconv(x,h,N) %calculating circular convolution using the inbuilt library

% our output and the desired output are similar.

3) Part 3

We can see that the circular convolution and linear convolution comes out to be
equal in this case.

4) MATLAB code for Part 4

For Linear Convolution:


clear all
close all
clc
x=[-1,1,-1,1]
h= [-1,0,2,3,4];

3
n_x=length(x);
n_h=length(h);
N = n_x+n_h-1;
naxis=0:N-1;
x1=[x zeros(1,N-n_x)];
h1=[h zeros(1,N-n_h)];
n_axis=0:N-1;
linear_cov_output=zeros(1,N);
for n=1:N
for k=1:n
linear_cov_output(n)=linear_cov_output(n)+x1(k)*h1(n-k+1);

end
end
fprintf("The linear convolution: ");
linear_cov_output

For Circular Convolution:


clear all
close all
clc
x=[-1,1,-1,1,0];
h= [-1,0,2,3,4];
N=length(x);
n_axis=0:N-1;
circular_cov_output=zeros(1,N);
for n=1:N
for m=1:N
j=mod(n-m,N);
j=j+1;
circular_cov_output(n)=circular_cov_output(n)+x(m)*h(j);
end
end
fprintf("The circular convolution: ");
circular_cov_output

4
**************************

You might also like