-
-
Notifications
You must be signed in to change notification settings - Fork 360
/
Copy path1d_convolution.jl
72 lines (52 loc) · 1.77 KB
/
1d_convolution.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
using DelimitedFiles
using LinearAlgebra
function convolve_cyclic(signal::Array{T, 1},
filter::Array{T, 1}) where {T <: Number}
# output size will be the size of sign
output_size = max(length(signal), length(filter))
# convolutional output
out = Array{Float64,1}(undef,output_size)
sum = 0
for i = 1:output_size
for j = 1:output_size
sum += get(signal, mod1(j, output_size), 0) * get(filter, mod1(i-j, output_size), 0)
end
out[i] = sum
sum = 0
end
return out
end
function convolve_linear(signal::Array{T, 1}, filter::Array{T, 1},
output_size) where {T <: Number}
# convolutional output
out = Array{Float64,1}(undef, output_size)
sum = 0
for i = 1:output_size
for j = max(1, i-length(filter)):i
if j <= length(signal) && i-j+1 <= length(filter)
sum += signal[j] * filter[i-j+1]
end
end
out[i] = sum
sum = 0
end
return out
end
function main()
# sawtooth functions for x and y
x = [float(i)/200 for i = 1:200]
y = [float(i)/200 for i = 1:200]
# Normalization is not strictly necessary, but good practice
normalize!(x)
normalize!(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))
# cyclic convolution
cyclic_output = convolve_cyclic(x, y)
# outputting convolutions to different files for plotting in external code
writedlm("full_linear.dat", full_linear_output)
writedlm("simple_linear.dat", simple_linear_output)
writedlm("cyclic.dat", cyclic_output)
end