-
Notifications
You must be signed in to change notification settings - Fork 11
1D Fermat Number Transform deconvolution #14
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
351d128
37c58b6
a066162
a9ffe6c
b76315f
8ebb03e
a6ec3e1
43c47b1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -17,5 +17,6 @@ using FFTW | |
|
|
||
| include("wiener.jl") | ||
| include("lucy.jl") | ||
| include("fermat.jl") | ||
|
|
||
| end # module | ||
| 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 | ||
| 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 | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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 | ||
|
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Circular convolution again, it uses the same fft approach as above 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.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
xwill be exactly the same thing asx0, this means that chaningx0will have the same effect onxand vice versa. Is this what you want?There was a problem hiding this comment.
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.