Skip to content

Instantly share code, notes, and snippets.

@eduardo4jesus
Last active May 14, 2020 19:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eduardo4jesus/0dc157d505152f41c3651fe5e639ca8a to your computer and use it in GitHub Desktop.
Save eduardo4jesus/0dc157d505152f41c3651fe5e639ca8a to your computer and use it in GitHub Desktop.
Difference in Spatial Convolution: Numpy vs Matlab
a = [ 0 3 1 -4]
b = [-1 -1 -1 3]
c = cconv(a, b, 4) % [12 4 -16 0]
l = conv(a, b, 'same') % [ -4 0 12 7]
f = conv(a, b) % [0 -3 -4 0 12 7 -12]
import numpy as np
import scipy as sp
import scipy.signal
def spatial_full(img, kernel):
return sp.signal.convolve(img, kernel, mode='full', method="direct")
def spatial_linear(img, kernel):
return sp.signal.convolve(img, kernel, mode='same', method="direct")
def spatial_circ(img, kernel):
return sp.ndimage.convolve1d(img, kernel, mode='wrap')
a = np.array([ 0, 3, 1, -4])
b = np.array([-1, -1, -1, 3])
c = spatial_circ(a, b) # array([-16, 0, 12, 4])
l = spatial_linear(a, b) # array([-3, -4, 0, 12])
f = spatial_full(a, b) # array([ 0, -3, -4, 0, 12, 7, -12])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment