9/28/23, 2:47 PM Untitled4.
ipynb - Colaboratory
EXPERIMENT NO : 2
1D and 2D Convolution
Aim:
To perform 1D and 2D convolution:
1. Linear Convolution (1D) • Using built-in function • Using Toeplitz matrix • Using the convolution sum equation
2. 2D Convolution
3. Circular Convolution
Theory:
Convolution is an important operation as far as Linear Shift Invariant systems are concerned. For any such systems, if h(m,n) is the system
response, then the output of the system y(m,n) to any input x(m,n) is given by:
y(m,n) = x(m,n)∗ h(m,n) ,where ∗ stands for convolution.
Another important property of convolution is its frequency domain representation. In the fre- quency domain, the convolution operation
becomes multiplication and is therefore easier to calculate.
Circular convolution, also known as cyclic convolution, is a special case of periodic convolution. This is the convolution of two periodic
functions that have the same period.
Apart from built-in functions and direct equations, linear convolution can be performed by mul- tiplying the input with a Toeplitz matrix. The
equivalent operation for circular convolution is the
multiplication with a Circulant matrix.
Scipy Familiarization
import numpy as np
from scipy.linalg import det
# Define your matrix A here (replace this with your actual matrix)
A = np.array([[1, 2, 3],
[4, 2, 6],
[7, 4, 9]])
# Calculate the determinant of A
detA = det(A)
# Print the determinant
print(f"Determinant of A: {detA}")
Determinant of A: 12.0
1. Linear Convolution
(a). Using built-in function
import numpy as np
from scipy.signal import convolve
h = [1, 2, 3, 3, 2, 1] # impulse response
x = [1, 2, 3, 4, 5] # input signal
y2 = convolve(x, h, mode='full') # Linear convolution using the scipy built-in function
print("Linear convolution using scipy built-in function, output response:")
print(f"y = {y2}")
Linear convolution using scipy built-in function, output response:
y = [ 1 4 10 19 30 36 35 26 14 5]
https://colab.research.google.com/drive/1neBmg3luxaZYBiuSd2YQ9d6yaM8rBLwL?authuser=0#scrollTo=aqbyV6zeEyWz&printMode=true 1/3
9/28/23, 2:47 PM Untitled4.ipynb - Colaboratory
(b). Convolution using equation
import numpy as np
h = np.array([1, 2, 3, 3, 2, 1]) # impulse response
x = np.array([1, 2, 3, 4, 5]) # input signal
N1, N2 = len(x), len(h)
N = N1 + N2 - 1
m = N - N1
n = N - N2
# Padding with zeros
x = np.pad(x, (0, m), 'constant')
h = np.pad(h, (0, n), 'constant')
y = np.zeros(N) # Initialize the output array
for n in range(N):
for k in range(N):
if n >= k:
y[n] = y[n] + x[n - k] * h[k]
print("Output response:")
print(y)
Output response:
[ 1. 4. 10. 19. 30. 36. 35. 26. 14. 5.]
(b). Convolution using Toeplitz matrix
import numpy as np
h = [1, 2, 3, 3, 2, 1]
x = [1, 2, 3, 4, 5]
# Create an empty Toeplitz matrix
N = len(x)
M = len(h)
toepH = np.zeros((N, N + M - 1))
# Fill the Toeplitz matrix
for i in range(N):
toepH[i, i:i + M] = h
# Perform the convolution using matrix-vector multiplication
#y = np.dot(toepH, x)
print("Toeplitz matrix:")
print(toepH)
print("\nConvolution result:")
print(y)
Toeplitz matrix:
[[1. 2. 3. 3. 2. 1. 0. 0. 0. 0.]
[0. 1. 2. 3. 3. 2. 1. 0. 0. 0.]
[0. 0. 1. 2. 3. 3. 2. 1. 0. 0.]
[0. 0. 0. 1. 2. 3. 3. 2. 1. 0.]
[0. 0. 0. 0. 1. 2. 3. 3. 2. 1.]]
Convolution result:
[ 1. 4. 10. 19. 30. 36. 35. 26. 14. 5.]
2. 2D Convolution
from scipy import signal
X = [[2, 1],[5, 4],[3, 1]]
H = [[1, 1],[-1, 1]]
Y = signal.convolve(X, H, 'full')
print(Y) # result is in matrix form
[[ 2 3 1]
[ 3 10 5]
[-2 5 5]
[-3 2 1]]
https://colab.research.google.com/drive/1neBmg3luxaZYBiuSd2YQ9d6yaM8rBLwL?authuser=0#scrollTo=aqbyV6zeEyWz&printMode=true 2/3
9/28/23, 2:47 PM Untitled4.ipynb - Colaboratory
3. Circular Convolution
from scipy.linalg import circulant
x = [1, 2, 3]
c = circulant(x)
print(f"Circulant matrix\n {c}")
Circulant matrix
[[1 3 2]
[2 1 3]
[3 2 1]]
x = np.array([[1],[1],[-1]])
# convolution result
y = c @ x # circular convolution is obtained by matrix multiplication of circulant matrix and input
print(f"Circular convolution result\n {y}")
Circular convolution result
[[2]
[0]
[4]]
https://colab.research.google.com/drive/1neBmg3luxaZYBiuSd2YQ9d6yaM8rBLwL?authuser=0#scrollTo=aqbyV6zeEyWz&printMode=true 3/3