Translating a 1d convolution from Python to Julia

The following produces the same result:

using DSP
x = [0 0 1 2 3 5 0 0; 0 0 4 5 6 5 0 0 ;;; 0 0 2 3 4 5 0 0 ; 0 0 5 6 7 5 0 0]
z = [5,6,7,10]

n, m = length(z)÷2, size(x,2)
r0 = conv.(eachslice(x,dims=1), (z,))
r = [view(u,n+1:n+m,:) for u in r0]

julia> r[1]
8×2 Matrix{Int64}:
  5  10
 16  27
 34  52
 67  90
 71  88
 65  75
 50  50
  0   0

julia> r[2]
8×2 Matrix{Int64}:
  20   25
  49   60
  88  106
 136  159
 122  139
  95  105
  50   50
   0    0

The result of the DSP convolution must be cut off at the beginning and end, so that its output has the same length as the input.

1 Like