Help writing simple convolution algorithm

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)

Thanks!
Nakul

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

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

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

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

3 Likes

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!

Ah, a loop will help here

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

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