Skip to content

Commit c249854

Browse files
jiegilletleios
andauthored
[#852] [#852] Fix for Julia 1D convolution (#854)
* [#852] [#853] Fix for Julia 1D convolution * fixing line numbers for jie's PR Co-authored-by: James Schloss <jrs.schloss@gmail.com>
1 parent 0930d27 commit c249854

File tree

2 files changed

+9
-11
lines changed

2 files changed

+9
-11
lines changed

contents/convolutions/1d/1d.md

+6-6
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ With this in mind, we can almost directly transcribe the discrete equation into
5353

5454
{% method %}
5555
{% sample lang="jl" %}
56-
[import:29-48, lang:"julia"](../code/julia/1d_convolution.jl)
56+
[import:27-46, lang:"julia"](../code/julia/1d_convolution.jl)
5757
{% endmethod %}
5858

5959
The easiest way to reason about this code is to read it as you might read a textbook.
@@ -184,30 +184,30 @@ Here it is again for clarity:
184184

185185
{% method %}
186186
{% sample lang="jl" %}
187-
[import:29-48, lang:"julia"](../code/julia/1d_convolution.jl)
187+
[import:27-46, lang:"julia"](../code/julia/1d_convolution.jl)
188188
{% endmethod %}
189189

190190
Here, the main difference between the bounded and unbounded versions is that the output array size is smaller in the bounded case.
191191
For an unbounded convolution, the function would be called with a the output array size specified to be the size of both signals put together:
192192

193193
{% method %}
194194
{% sample lang="jl" %}
195-
[import:60-61, lang:"julia"](../code/julia/1d_convolution.jl)
195+
[import:58-59, lang:"julia"](../code/julia/1d_convolution.jl)
196196
{% endmethod %}
197197

198198
On the other hand, the bounded call would set the output array size to simply be the length of the signal
199199

200200
{% method %}
201201
{% sample lang="jl" %}
202-
[import:63-64, lang:"julia"](../code/julia/1d_convolution.jl)
202+
[import:61-62, lang:"julia"](../code/julia/1d_convolution.jl)
203203
{% endmethod %}
204204

205205
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.
206206
This can be done by modifying the following line:
207207

208208
{% method %}
209209
{% sample lang="jl" %}
210-
[import:37-37, lang:"julia"](../code/julia/1d_convolution.jl)
210+
[import:35-35, lang:"julia"](../code/julia/1d_convolution.jl)
211211
{% endmethod %}
212212

213213
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
239239

240240
{% method %}
241241
{% sample lang="jl" %}
242-
[import:4-27, lang:"julia"](../code/julia/1d_convolution.jl)
242+
[import:4-25, lang:"julia"](../code/julia/1d_convolution.jl)
243243
{% endmethod %}
244244

245245
This is essentially the same as before, except for the modulus operations, which allow us to work on a periodic domain.

contents/convolutions/code/julia/1d_convolution.jl

+3-5
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,7 @@ function convolve_cyclic(signal::Array{T, 1},
1313

1414
for i = 1:output_size
1515
for j = 1:output_size
16-
if (mod1(i-j, output_size) <= length(filter))
17-
sum += signal[mod1(j, output_size)] * filter[mod1(i-j, output_size)]
18-
end
16+
sum += get(signal, mod1(j, output_size), 0) * get(filter, mod1(i-j, output_size), 0)
1917
end
2018

2119
out[i] = sum
@@ -57,8 +55,8 @@ function main()
5755
normalize!(x)
5856
normalize!(y)
5957

60-
# full convolution, output will be the size of x + y
61-
full_linear_output = convolve_linear(x, y, length(x) + length(y))
58+
# full convolution, output will be the size of x + y - 1
59+
full_linear_output = convolve_linear(x, y, length(x) + length(y) - 1)
6260

6361
# simple boundaries
6462
simple_linear_output = convolve_linear(x, y, length(x))

0 commit comments

Comments
 (0)