In [1]:
import numpy as np
from scipy import signal
%matplotlib inline
import matplotlib.pyplot as plt
In [81]:
b = np.random.random(4)
a = np.random.random(10)
np.convolve(a, b)
Out[81]:
In [82]:
def convolve(a, b):
if a.shape[0] < b.shape[0]:
a, b = b, a
return np.array([
# important to remember the [::-1]
np.matmul(a[i:i+b.shape[0]], b[::-1]) # \equiv dot().sum()
for i in range(a.shape[0] - b.shape[0] + 1)
])
plt.plot(convolve(a, b))
plt.plot(signal.convolve(a, b, mode="valid"))
plt.show()
# print(convolve(a, b), signal.convolve(a, b, mode="valid"))
In [150]:
a = np.random.random((3, 6))
b = np.random.random((2, 2))
In [151]:
# 2D convolution
def convolve2d(a, b):
#a_f = a.flatten().reshape((a.size, 1))
#b_f = b.flatten().reshape((1, b.size))
return np.array(
[
[
(a[i:i+b.shape[0], j:j+b.shape[1]]* b[::-1,::-1]).sum()
for j in range(a.shape[1] - b.shape[1] + 1)
]
for i in range(a.shape[0] - b.shape[0] + 1)
])
print(convolve2d(a,b) - signal.convolve2d(a,b,mode='valid'))
plt.figure(figsize=(12,5))
plt.subplot(131)
plt.imshow(a, interpolation="none")
plt.subplot(132)
plt.imshow(convolve2d(a, b), interpolation="none")
plt.subplot(133)
plt.imshow(convolve2d(a, b)-signal.convolve2d(a, b, mode='valid'), interpolation="none")
plt.show()
results in the difference are from floating point imprecision.
In [155]:
a = np.random.random((3, 6, 4))
b = np.random.random((2, 2, 3))
In [154]:
# 2D convolution
def convolve3d(a, b):
#a_f = a.flatten().reshape((a.size, 1))
#b_f = b.flatten().reshape((1, b.size))
return np.array(
[
[
[
(a[i:i+b.shape[0], j:j+b.shape[1], k:k+b.shape[2]]* b[::-1, ::-1, ::-1]).sum()
for k in range(a.shape[2] - b.shape[2] + 1)
]
for j in range(a.shape[1] - b.shape[1] + 1)
]
for i in range(a.shape[0] - b.shape[0] + 1)
])
In [ ]: