diff --git a/contents/convolutions/1d/1d.md b/contents/convolutions/1d/1d.md index 3a3ca0cf6..3e663938e 100644 --- a/contents/convolutions/1d/1d.md +++ b/contents/convolutions/1d/1d.md @@ -53,7 +53,7 @@ With this in mind, we can almost directly transcribe the discrete equation into {% method %} {% sample lang="jl" %} -[import:29-48, lang:"julia"](../code/julia/1d_convolution.jl) +[import:27-46, lang:"julia"](../code/julia/1d_convolution.jl) {% endmethod %} The easiest way to reason about this code is to read it as you might read a textbook. @@ -184,7 +184,7 @@ Here it is again for clarity: {% method %} {% sample lang="jl" %} -[import:29-48, lang:"julia"](../code/julia/1d_convolution.jl) +[import:27-46, lang:"julia"](../code/julia/1d_convolution.jl) {% endmethod %} Here, the main difference between the bounded and unbounded versions is that the output array size is smaller in the bounded case. @@ -192,14 +192,14 @@ For an unbounded convolution, the function would be called with a the output arr {% method %} {% sample lang="jl" %} -[import:60-61, lang:"julia"](../code/julia/1d_convolution.jl) +[import:58-59, lang:"julia"](../code/julia/1d_convolution.jl) {% endmethod %} On the other hand, the bounded call would set the output array size to simply be the length of the signal {% method %} {% sample lang="jl" %} -[import:63-64, lang:"julia"](../code/julia/1d_convolution.jl) +[import:61-62, lang:"julia"](../code/julia/1d_convolution.jl) {% endmethod %} Finally, as we mentioned before, it is possible to center bounded convolutions by changing the location where we calculate the each point along the filter. @@ -207,7 +207,7 @@ This can be done by modifying the following line: {% method %} {% sample lang="jl" %} -[import:37-37, lang:"julia"](../code/julia/1d_convolution.jl) +[import:35-35, lang:"julia"](../code/julia/1d_convolution.jl) {% endmethod %} Here, `j` counts from `i-length(filter)` to `i`. @@ -239,7 +239,7 @@ In code, this typically amounts to using some form of modulus operation, as show {% method %} {% sample lang="jl" %} -[import:4-27, lang:"julia"](../code/julia/1d_convolution.jl) +[import:4-25, lang:"julia"](../code/julia/1d_convolution.jl) {% endmethod %} This is essentially the same as before, except for the modulus operations, which allow us to work on a periodic domain. diff --git a/contents/convolutions/code/julia/1d_convolution.jl b/contents/convolutions/code/julia/1d_convolution.jl index 78dafc0ed..0019c2496 100644 --- a/contents/convolutions/code/julia/1d_convolution.jl +++ b/contents/convolutions/code/julia/1d_convolution.jl @@ -13,9 +13,7 @@ function convolve_cyclic(signal::Array{T, 1}, for i = 1:output_size for j = 1:output_size - if (mod1(i-j, output_size) <= length(filter)) - sum += signal[mod1(j, output_size)] * filter[mod1(i-j, output_size)] - end + sum += get(signal, mod1(j, output_size), 0) * get(filter, mod1(i-j, output_size), 0) end out[i] = sum @@ -57,8 +55,8 @@ function main() normalize!(x) normalize!(y) - # full convolution, output will be the size of x + y - full_linear_output = convolve_linear(x, y, length(x) + length(y)) + # full convolution, output will be the size of x + y - 1 + full_linear_output = convolve_linear(x, y, length(x) + length(y) - 1) # simple boundaries simple_linear_output = convolve_linear(x, y, length(x))