Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ version = "0.3.0"

[deps]
FFTW = "7a1cc6ca-52ef-59f5-83cd-3a7055c09341"
NumberTheoreticTransforms = "8497c1d1-af72-4391-8d22-bdd566511a1c"

[compat]
FFTW = "0.0, 0.1, 0.2, 0.3, 1"
Expand All @@ -13,6 +14,7 @@ julia = "1"
[extras]
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
SpecialMatrices = "928aab9d-ef52-54ac-8ca1-acd7ca42c160"

[targets]
test = ["Test", "Random"]
test = ["Test", "Random", "SpecialMatrices"]
1 change: 1 addition & 0 deletions src/Deconvolution.jl
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,6 @@ using FFTW

include("wiener.jl")
include("lucy.jl")
include("fermat.jl")

end # module
39 changes: 39 additions & 0 deletions src/fermat.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@

using NumberTheoreticTransforms

export fermat

"""
fermat(convolved, h, g, q)

Calculates deconvolution with Number Theoretic Transform.
"""
function fermat(convolved::AbstractArray{T, 1}, h::AbstractArray{T, 1}, g::T, q::T) where {T<:Integer}
N = length(convolved)
bias = div(q-1, 2) + 1 #negative numbers are represeted by the upper half of [0, q) range
H = fnt(h, g, q)
H_inv = invmod.(H, q)
D = fft(h) |> prod |> real |> T #TODO: avoid floats
Dm = mod(D, q)
xm = ifnt(H_inv .* fnt(convolved, g, q), g, q)
x0 = mod.(Dm * xm, q)
x0[x0 .>= bias] .-= q
x = x0
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

x will be exactly the same thing as x0, this means that chaning x0 will have the same effect on x and vice versa. Is this what you want?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, this assignment is just to follow variable naming form the paper, x0 is not modified later, and new variable xj is introduced later int the loop.

y_prim = fft(h) .* fft(x0) |> ifft .|> real .|> round.|> T #TODO: avoid floats
@assert all(iszero, rem.(convolved * D - y_prim, q))
y = div.(convolved * D - y_prim, q)
ym = mod.(y, q)
j = 1
while !all(iszero, y)
xj = ifnt(H_inv .* fnt(ym, g, q), g, q)
xj[xj .>= bias] .-= q
x = x .+ (xj * q^j)
y_prim = fft(h) .* fft(xj) |> ifft .|> real .|> round .|> T #TODO: avoid floats
@assert all(iszero, rem.(y - y_prim, q))
y = div.(y - y_prim, q)
ym = mod.(y, q)
j = j + 1
end

return x // D
end
22 changes: 22 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,25 @@ end

@test sum(abs.(s .- estimated)) < sum(abs.(s .- blurred_s))
end

##### Fermat Number Transform deconvolution

@testset "Fermat Number Transform deconvolution tests" begin
using SpecialMatrices

h = [3, 2, 0, 0]
y = [3, 5, 3, 0]
x = fermat(y, h, 4, 17)
@test Circulant(h) * x == y
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Circular convolution again, it uses the same fft approach as above
https://github.com/JuliaMatrices/SpecialMatrices.jl/blob/master/src/toeplitz.jl#L54

I have feeling that we need some package with better api to perform convolution. I don't like the fact that floating point arithmetic is used here.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like you have a plan 🙂


h = [4, 3, 1, 1, 1, 0, 0, 0]
y = [4, 2, 5, 1, 4, 2, 1, 3]
x = fermat(y, h, 2, 17)
@test Circulant(h) * x == y

h = [3, 2, 0, 0]
y = [11, 8, 13, 18]
x = fermat(y, h, 4, 17)
@test Circulant(h) * x == y

end