Professional Documents
Culture Documents
DSP 2
DSP 2
Ex.No -2
Linear and Circular Convolution
Date:
AIM:
To write a MATLAB program to obtain the linear and circular convolution of two sequences.
APPARATUS REQUIRED:
MATLAB, PC
THEORY:
For a linear time invariant system if the input sequence x(n) and
impulse response h(n) is given ,we can find the output y(n) by using the
equation(n)=∑x(k) h(n-k),where k varies from -∞ to +∞ which is known
as convolution sum and can be represented as y(n)=x(n)*h(n),where *
denotes the convolution operation. This is an extremely powerful result
that allows us to compute the system output for any input excitation.
ALGORITHM:
1. Choose an initial value of n, the starting time for evaluating the output sequences y(n).If x(n)
starts at n=n1 and hence h(n) starts at n=n2 then n=n1+n2 is good choice
2. E expresses both sequences in terms of the index k
3. Fold h (K) about k=0 to obtain h (-K) and shift by n to the right if h is positive and left if n is
negative to obtain h (n-k).
4. Multiply the two sequences x (k) and h (n-k) element by element and the sum of products to
get y (n).
5. Increment the index n, shift the sequence h (n-k) to right by one sample and do step 4.
6. Repeat step 5 will until the sum of products is zero for all remaining values of n.
7
LOYOLA - ICAM
COLLEGE OF ENGINEERING AND TECHNOLOGY (LICET)
PROGRAM:
1. LINEAR CONVOLUTION FUNCTION
clc
y = conv(x,h);
subplot(3,1,1);
t = length(x);
stem(0:t-1,x);
xlabel('Time');
ylabel('Amp');
subplot(3,1,2);
t = length(h);
stem(0:t-1,h);
title('Input H')
xlabel('Time');
ylabel('Amp');
disp(y);
subplot(3,1,3);
t = length(y);
8
LOYOLA - ICAM
COLLEGE OF ENGINEERING AND TECHNOLOGY (LICET)
stem(0:t-1,y);
title('Result Y');
xlabel('Time');
ylabel('Amp');
INPUT:
OUTPUT:
9
LOYOLA - ICAM
COLLEGE OF ENGINEERING AND TECHNOLOGY (LICET)
clc
x = input("Enter the values of x");
n1 = length(x);
h = input("Enter the values of h");
n2 = length(h);
n = n2+n1-1;
x = [x,zeros(1,n-n1)];
h = [h,zeros(1,n-n2)];
y = zeros(1,n);
%y = conv(x,h);
for c1 = 1:n
temp = 0;
for c2 = 1:c1
temp = temp + x(c2)*h(c1+1-c2);
end
y(c1) = temp;
end
subplot(3,1,1);
t = length(x);
stem(0:t-1,x);
title(' Input X')
xlabel('Time');
ylabel('Amp');
subplot(3,1,2);
t = length(h);
stem(0:t-1,h);
title('Input H')
xlabel('Time');
ylabel('Amp');
subplot(3,1,3);
t = length(y);
stem(0:t-1,y);
10
LOYOLA - ICAM
COLLEGE OF ENGINEERING AND TECHNOLOGY (LICET)
title('Result Y');
xlabel('Time');
ylabel('Amp');
INPUT:
Enter the values of x
[ 1 2 3]
Enter the values of h
[4 5 6]
OUTPUT:
The result of convolution is
4 13 28 27 18
11
LOYOLA - ICAM
COLLEGE OF ENGINEERING AND TECHNOLOGY (LICET)
n = n2+n1-1;
x = [x,zeros(1,n-n1)];
h = [h,zeros(1,n-n2)];
y = cconv(x,h,length(x));
subplot(3,1,1);
t = length(x);
stem(0:t-1,x);
title(' Input X')
xlabel('Time');
ylabel('Amp');
subplot(3,1,2);
t = length(h);
stem(0:t-1,h);
title('Input H')
xlabel('Time');
ylabel('Amp');
12
LOYOLA - ICAM
COLLEGE OF ENGINEERING AND TECHNOLOGY (LICET)
INPUT:
Enter the values of x
[1 2 3]
Enter the values of h
[4 5 6]
OUTPUT:
The result of convolution is
31 31 28
GRAPH:
13
LOYOLA - ICAM
COLLEGE OF ENGINEERING AND TECHNOLOGY (LICET)
n = max(n1,n2);
if n2 > n1
x = [x,zeros(1,n2-n1)];
elseif n1>n2
h = [h,zeros(1,n1-n2)];
end
mat = zeros(n,n);
for c1 = 1:n
for c2 = 1:n
mat(c1,c2) = x(c2);
end
temp = x(n);
for c3 = 1:n-1
x(n+1-c3) = x(n-c3);
end
x(1) = temp;
end
disp(mat);
subplot(3,1,1);
t = length(x);
stem(0:t-1,x);
title(' Input X')
xlabel('Time');
ylabel('Amp');
subplot(3,1,2);
t = length(h);
stem(0:t-1,h);
title('Input H')
xlabel('Time');
ylabel('Amp');
14
LOYOLA - ICAM
COLLEGE OF ENGINEERING AND TECHNOLOGY (LICET)
INPUT:
Enter the values of x
[1 2 3]
Enter the values of h
[4 5 6]
OUTPUT:
The result of convolution is
31 31 28
15
LOYOLA - ICAM
COLLEGE OF ENGINEERING AND TECHNOLOGY (LICET)
clc
x = input("Enter the values of x");
n1 = length(x);
h = input("Enter the values of h");
n2 = length(h);
n = n2+n1-1;
x = [x,zeros(1,n-n1)];
h = [h,zeros(1,n-n2)];
y = cconv(x,h,length(x));
subplot(3,1,1);
t = length(x);
stem(0:t-1,x);
title(' Input X')
xlabel('Time');
ylabel('Amp');
subplot(3,1,2);
t = length(h);
stem(0:t-1,h);
title('Input H')
xlabel('Time');
ylabel('Amp');
INPUT:
Enter the values of x
[1 2 3]
Enter the values of h
[4 5 6]
OUTPUT:
16
LOYOLA - ICAM
COLLEGE OF ENGINEERING AND TECHNOLOGY (LICET)
GRAPH:
Preparation 5
Conduction &
Observation 5
Result Execution
Viva-Voce &
5
Inference
Record 5
Total 20
RESULT:
Thus a MATLAB program is written to obtain the linear and Circular convolution of two
sequences.
17