You are on page 1of 12

TE COMPS

EXPERIMENT 3

Aim: To perform convolution of two discrete signals using the graphical method
and tabular method.

Theory:
Convolution is a mathematical operation used to express the relation between
input and output of an LTI system. It relates 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

There are two types of convolutions:

● Continuous convolution
y(t)=x(t)∗h(t)
=∫∞−∞x(τ)h(t−τ)dτ

● Discrete convolution
y(n)=x(n)∗h(n)
=Σ∞k=−∞x(k)h(n−k)
Linear convolution

Code:

xn = list(map(int, input("Enter the values of x[n]:\n").split()))

xo = int(input("Enter the origin index:\n"))

hn = list(map(int, input("Enter the values of h[n]:\n").split()))

ho = int(input("Enter the origin index:\n"))

L = len(xn)

M = len(hn)

N = L+M-1
lx = - xo

lh = - ho

ly = lx + lh

hx = L - 1 - xo

hh = M - 1 - ho

hy = hx + hh

yn = [0]*N

multiplication =[]

print("Tabular form:")

print(" ")
print("\t\t",xn)

for value in hn:

for val in xn:

multiplication.append(value*val)

print("{}".format(value),"\t|","\t",multiplication)

multiplication=[]

for i in range(L):

for j in range(M):

yn[i+j]+=xn[i]*hn[j]

print("\n y[n] is :",yn)

Output:
Plot:

Circular convolution –Tabular format

Code:

#Circular table format

x_n = list(map(int, input("Enter x(n) : ").split(',')))

h_n = list(map(int, input("Enter h(n) : ").split(',')))

L = len(x_n)

M = len(h_n)

K = max(L, M)

for i in range(L,K):

x_n.append(0)
for i in range(M,K):

h_n.append(0)

# Making Circular Matrix

circular_mat = list()

rotate = 0

for i in range(K):

row = list()

for j in range(K):

row.append(x_n[(j - rotate) % K])

rotate += 1

circular_mat.append(row)

print("\nTable : ")

print("h(n)" + '\t' * int(K/2) + " x(n) ")

y_n = dict()

for i in range(K):

y_n[i] = 0

for i in range(K):

row = circular_mat[i]

for j in range(len(row)):
y_n[j] += row[j] * h_n[i]

row = '\t'.join(list(map(str, row)))

print(str(h_n[i]) + "\t" + row)

print(' ' * (K) + '-')

y_n = list(y_n.values())

print(" \t" + '\t'.join(list(map(str, y_n))))

Output:

Plot
Circular convolution – Matrix Multiplication

Code:

# Circular Convolution

MAX_SIZE = 10;

def convolution(x, h, n, m):

row_vec = [0] * MAX_SIZE;

col_vec = [0] * MAX_SIZE;

out = [0] * MAX_SIZE;

circular_shift_mat = [[0 for i in range(MAX_SIZE)]

for j in range(MAX_SIZE)] ;

if(n > m ):

maxSize = n;

else:

maxSize = m;

for i in range(maxSize):

if (i >= n):
row_vec[i] = 0;

else:

row_vec[i] = x[i];

for i in range(maxSize):

if (i >= m):

col_vec[i] = 0;

else:

col_vec[i] = h[i];

k = 0;

d = 0;

for i in range(maxSize):

curIndex = k - d;

for j in range(maxSize):

circular_shift_mat[j][i] = row_vec[curIndex % maxSize];

curIndex += 1;

k = maxSize;

d += 1;
for i in range(maxSize):

for j in range(maxSize):

out[i] += circular_shift_mat[i][j] * col_vec[j];

print(out[i], end = " ");

if name == ' main ':

x = [ 5, 7, 3, 2 ];

n = len(x);

h = [ 1, 5 ];

m = len(h);

convolution(x, h, n, m);

Output:

Plot:
Conclusion:

 I understood the implementation of linear and circular convolution


using python.
 I implemented it using python.

You might also like