# Help writing simple convolution algorithm

**URL:** <https://discourse.julialang.org/t/help-writing-simple-convolution-algorithm/15407>\
**Category:** General Usage\
**Tags:** array\
**Created:** [September 24, 2018, 1:49am UTC](https://discourse.julialang.org/t/help-writing-simple-convolution-algorithm/15407 "2018-09-24T01:49:41Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Nakul\_Tiruviluamala](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nakul_tiruviluamala/32/11228_2.png) [@Nakul\_Tiruviluamala](https://discourse.julialang.org/u/Nakul_Tiruviluamala)\
**Post date:** [September 24, 2018, 1:49am UTC](https://discourse.julialang.org/t/help-writing-simple-convolution-algorithm/15407/1 "2018-09-24T01:49:41Z")

</div>

Hello,  
I am trying to write a convolution algorithm for learning purposes. I haven’t gotten to the superposition stage yet. I am stuck on prepending 0 to each iteration of the array which I am attempting to do with unshift!. It occurred to me that I might have to declare the size of the array dimensions beforehand so that each row is sized the same. I’m guessing that my code is also extremely inefficient so insight into that arena would be helpful as well! I’ve attached a screenshot of the arrays I am trying to generate to eventually superimpose (right side of image)

 ![38%20PM](https://global.discourse-cdn.com/julialang/original/3X/b/b/bbc98add37f545b24fb5fb186d95ef514ed57e48.jpeg)

Thanks!  
Nakul

```julia
x = [1, 2, -1, 3]
ir = [2, 1, -1]
xir = []
for i in x
        xir = push!(xir,i*ir)
        unshift!(xir,0)
end

```

---

<div class="post-metadata">

**Author:** ![y4lu](https://avatars.discourse-cdn.com/v4/letter/y/47e85d/32.png) [@y4lu](https://discourse.julialang.org/u/y4lu)\
**Post date:** [September 24, 2018, 2:12am UTC](https://discourse.julialang.org/t/help-writing-simple-convolution-algorithm/15407/2 "2018-09-24T02:12:51Z")

</div>

This should be fine, but check the output dimensions are correct

```julia
xir = x' .* ir; ##vector broadcast
xir = hcat(zeros(4), xir);
```

---

<div class="post-metadata">

**Author:** ![dpsanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpsanders/32/3573_2.png) [@dpsanders](https://discourse.julialang.org/u/dpsanders)\
**Post date:** [September 24, 2018, 2:31am UTC](https://discourse.julialang.org/t/help-writing-simple-convolution-algorithm/15407/3 "2018-09-24T02:31:31Z")

</div>

Note that `unshift!` has been renamed to the much more intuitive `pushfirst!` in Julia 0.7 / 1.0.

---

<div class="post-metadata">

**Author:** ![Nakul\_Tiruviluamala](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nakul_tiruviluamala/32/11228_2.png) [@Nakul\_Tiruviluamala](https://discourse.julialang.org/u/Nakul_Tiruviluamala)\
**Post date:** [September 24, 2018, 4:09am UTC](https://discourse.julialang.org/t/help-writing-simple-convolution-algorithm/15407/5 "2018-09-24T04:09:08Z")

</div>

Thank you. Currently, I am trying to circularly shift my 2D array on a row by row basis. I used circshift but it is does not shift on a row by row basis. This is my array:

2.0 1.0 -1.0 0.0 0.0 0.0  
4.0 2.0 -2.0 0.0 0.0 0.0  
-2.0 -1.0 1.0 0.0 0.0 0.0  
6.0 3.0 -3.0 0.0 0.0 0.0

I would like it look like this, where each row is shifted one more than the previous row:  
2.0 1.0 -1.0 0.0 0.0 0.0  
0.0 4.0 2.0 -2.0 0.0 0.0  
0.0 0.0 -2.0 -1.0 1.0 0.0  
0.0 0.0 0.0 6.0 3.0 -3.0

Thank you!

---

<div class="post-metadata">

**Author:** ![y4lu](https://avatars.discourse-cdn.com/v4/letter/y/47e85d/32.png) [@y4lu](https://discourse.julialang.org/u/y4lu)\
**Post date:** [September 24, 2018, 6:29am UTC](https://discourse.julialang.org/t/help-writing-simple-convolution-algorithm/15407/6 "2018-09-24T06:29:32Z")

</div>

Ah, a loop will help here

```julia
for iter = 2:size(xir,1)
  xir[iter, :] = circshift(xir[iter, :], iter-1);
  end;
```

---

<div class="post-metadata">

**Author:** ![Nakul\_Tiruviluamala](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nakul_tiruviluamala/32/11228_2.png) [@Nakul\_Tiruviluamala](https://discourse.julialang.org/u/Nakul_Tiruviluamala)\
**Post date:** [September 25, 2018, 1:12am UTC](https://discourse.julialang.org/t/help-writing-simple-convolution-algorithm/15407/7 "2018-09-25T01:12:29Z")

</div>

Thank you! I was unfamiliar with the : notation. It works really well.
