# Translating a 1d convolution from Python to Julia

**URL:** <https://discourse.julialang.org/t/translating-a-1d-convolution-from-python-to-julia/85080>\
**Category:** General Usage\
**Tags:** question, python, dsp\
**Created:** [July 31, 2022, 10:20pm UTC](https://discourse.julialang.org/t/translating-a-1d-convolution-from-python-to-julia/85080 "2022-07-31T22:20:23Z")\
**Posts on this page:** 1\
**Showing post:** 4

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [July 31, 2022, 11:31pm UTC](https://discourse.julialang.org/t/translating-a-1d-convolution-from-python-to-julia/85080/4 "2022-07-31T23:31:33Z")

</div>

The following produces the same result:

```julia
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.

---

_[View the full topic](https://discourse.julialang.org/t/translating-a-1d-convolution-from-python-to-julia/85080)._
