Skip to content

Commit 8afef2c

Browse files
committed
Update fractional transform
1 parent 1dc90b3 commit 8afef2c

File tree

3 files changed

+205
-86
lines changed

3 files changed

+205
-86
lines changed
Lines changed: 146 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
### A Pluto.jl notebook ###
2-
# v0.19.9
2+
# v0.19.13
33

44
using Markdown
55
using InteractiveUtils
@@ -21,40 +21,92 @@ begin
2121
using Revise
2222
end
2323

24-
# ╔═╡ cc0f07d5-9192-48cd-9a90-50eebb06783f
25-
Pkg.add("IndexFunArrays")
26-
2724
# ╔═╡ 459649ed-ca70-426e-8273-97b146b5bcd5
28-
using FourierTools, FFTW, NDTools, TestImages, Colors, ImageShow, PlutoUI, Napari, IndexFunArrays, Plots
25+
using FourierTools, FFTW, NDTools, TestImages, Colors, ImageShow, PlutoUI, IndexFunArrays, Plots
2926

3027
# ╔═╡ 55894157-a2d1-4567-99a8-a052d5335dd1
3128
begin
29+
3230
"""
33-
complex_show(arr)
34-
Displays a complex array. Color encodes phase, brightness encodes magnitude.
31+
simshow(arr; set_one=false, set_zero=false,
32+
f=nothing, γ=1)
33+
Displays a real valued array . Brightness encodes magnitude.
3534
Works within Jupyter and Pluto.
35+
# Keyword args
36+
The transforms are applied in that order.
37+
* `set_zero=false` subtracts the minimum to set minimum to 1
38+
* `set_one=false` divides by the maximum to set maximum to 1
39+
* `f` applies an arbitrary function to the abs array
40+
* `γ` applies a gamma correction to the abs
41+
* `cmap=:gray` applies a colormap provided by ColorSchemes.jl. If `cmap=:gray` simply `Colors.Gray` is used
42+
and with different colormaps the result is an `Colors.RGB` element type
3643
"""
37-
function complex_show(cpx::AbstractArray{T, N}) where {T<:Complex, N}
38-
Tr = real(T)
39-
ac = abs.(cpx)
40-
HSV.(angle.(cpx)./Tr(2pi)*256, ones(Tr,size(cpx)), ac./maximum(ac))
44+
function simshow(arr::AbstractArray{T};
45+
set_one=true, set_zero=false,
46+
f = nothing,
47+
γ = one(T),
48+
cmap=:gray) where {T<:Real}
49+
arr = set_zero ? arr .- minimum(arr) : arr
50+
51+
if set_one
52+
m = maximum(arr)
53+
if !iszero(m)
54+
arr = arr ./ maximum(arr)
55+
end
56+
end
57+
58+
arr = isnothing(f) ? arr : f(arr)
59+
60+
if !isone(γ)
61+
arr = arr .^ γ
62+
end
63+
64+
if cmap == :gray
65+
Gray.(arr)
66+
else
67+
get(colorschemes[cmap], arr)
68+
end
4169
end
4270

4371

4472
"""
45-
gray_show(arr; set_one=false, set_zero=false)
46-
Displays a real gray color array. Brightness encodes magnitude.
73+
simshow(arr)
74+
Displays a complex array. Color encodes phase, brightness encodes magnitude.
4775
Works within Jupyter and Pluto.
48-
## Keyword args
49-
* `set_one=false` divides by the maximum to set maximum to 1
50-
* `set_zero=false` subtracts the minimum to set minimum to 1
76+
# Keyword args
77+
The transforms are applied in that order.
78+
* `f` applies a function `f` to the array.
79+
* `absf` applies a function `absf` to the absolute of the array
80+
* `absγ` applies a gamma correction to the abs
5181
"""
52-
function gray_show(arr; set_one=true, set_zero=false)
53-
arr = set_zero ? arr .- minimum(arr) : arr
54-
arr = set_one ? arr ./ maximum(arr) : arr
55-
Gray.(arr)
82+
function simshow(arr::AbstractArray{T};
83+
f=nothing,
84+
absγ=one(T),
85+
absf=nothing) where (T<:Complex)
86+
87+
if !isnothing(f)
88+
arr = f(arr)
89+
end
90+
91+
Tr = real(T)
92+
# scale abs to 1
93+
absarr = abs.(arr)
94+
absarr ./= maximum(absarr)
95+
96+
if !isnothing(absf)
97+
absarr .= absf(absarr)
98+
end
99+
100+
if !isone(absγ)
101+
absarr .= absarr .^ absγ
102+
end
103+
104+
angarr = angle.(arr) ./ Tr(2pi) * Tr(360)
105+
106+
HSV.(angarr, one(Tr), absarr)
56107
end
57108

109+
58110
end
59111

60112
# ╔═╡ 18b0700c-20fc-4e58-8950-ca09fe34ea19
@@ -74,10 +126,7 @@ $(@bind s Slider(-3:0.001:3, show_value=true))
74126
"
75127

76128
# ╔═╡ 7c445baa-d970-4954-a3dc-df828971bfd7
77-
[gray_show(log1p.(abs.(ft(img)))) gray_show(log1p.(abs.(sqrt(length(img)) .* frfft(img, s, shift=true))))]
78-
79-
# ╔═╡ 50a798b3-e3b6-47bc-98a5-fffe47053976
80-
[gray_show(log1p.(abs.(angle.(ft(img))))) gray_show(log1p.(abs.(angle.(sqrt(length(img)) .* frfft(img, s, shift=true)))))]
129+
[simshow(abs.(ft(img)), γ=0.2) simshow(sqrt(length(img)) .* abs.(frfft(img, s, shift=true)), γ=0.2)]
81130

82131
# ╔═╡ 1915c023-69cf-4d18-90cb-b47465dbef69
83132
begin
@@ -87,8 +136,14 @@ end
87136

88137
# ╔═╡ 3109fc21-50c6-46e6-850d-add6f54872d7
89138
begin
90-
plot(abs.(imag.(ft(img)[(end+begin)÷2+1,:] ./ sqrt(length(img)))) .|> log1p)
91-
plot!(abs.(imag.(frfft(img, 0.99999999999)[(end+begin)÷2+1,:])) .|> log1p)
139+
plot((imag.(ft(img)[(end+begin)÷2+1,:] ./ sqrt(length(img)))))
140+
plot!((imag.(frfft(img, 0.99999999999)[(end+begin)÷2+1,:])))
141+
end
142+
143+
# ╔═╡ 284cd6f2-1ee3-4923-afa6-ea57e93b28a7
144+
begin
145+
plot((angle.(ft(img)[(end+begin)÷2+1,:] ./ sqrt(length(img)))))
146+
plot!((angle.(frfft(img, 0.99999999999)[(end+begin)÷2+1,:])))
92147
end
93148

94149
# ╔═╡ 227ae9a3-9387-4ac3-b391-e2a78ce40d49
@@ -103,18 +158,38 @@ Comparison with [FractionalTransforms.jl](https://github.com/SciFracX/Fractional
103158
"
104159

105160
# ╔═╡ bae3c5b7-8964-493b-9e7b-d343e092219c
106-
r = box(Float64, (101,), (50,)).+ 0.1 .* randn((101,))
161+
r = box(Float64, (301,), (201,))#.+ 0.4 .* randn((301,))
162+
163+
# ╔═╡ 5655dc10-f4e9-4765-9a89-ac9702864de1
164+
plot(abs.(ft(r)))
107165

108166
# ╔═╡ 07d2b3b6-3584-4c64-9c4a-138beb3d6b88
109-
@bind s2 Slider(-5:0.1:5, show_value=true)
167+
@bind s2 Slider(-5:0.001:5, show_value=true)
110168

111169
# ╔═╡ 1839f03e-6add-4c85-b6fd-9035656ed86c
112170
begin
113-
plot(imag.(frfft(r, s2, shift=true)))
114-
plot!(real.(frfft(r, s2, shift=true)))
171+
plot(real.(frfft(r, s2, shift=true)), label="FourierTools")
172+
#plot!(imag.(frfft(frfft(r, s2/2, shift=true), s2/2, shift=true)), label="FourierTools 2 Step")
173+
174+
plot!(real.(FractionalTransforms.frft(r, s2)), label="FractionalTransforms")
175+
#plot!(imag.(FractionalTransforms.frft(ft(r) ./ sqrt(length(r)), -1+s2)), label="FractionalTransforms")
176+
177+
#plot!(imag.(FractionalTransforms.frft(r, s2)))
178+
179+
#plot!(real.(FractionalTransforms.frft(r, s2)))
180+
end
181+
182+
# ╔═╡ f3cb2153-a7b3-46ed-adbb-038a812b6a81
183+
begin
115184

116-
plot!(imag.(FractionalTransforms.frft(r, s2)))
117-
plot!(real.(FractionalTransforms.frft(r, s2)))
185+
#plot(abs.(frfft(r, s2, shift=true, p_change=false)))
186+
plot(abs.(frfft(r, s2, shift=true, p_change=true)))
187+
188+
#plot!(imag.(FractionalTransforms.frft(r, s2)), label="FractionalTransforms")
189+
#plot!(imag.(FractionalTransforms.frft(ft(r) ./ sqrt(length(r)), -1+s2)), label="FractionalTransforms")
190+
plot!(abs.(FractionalTransforms.frft(r, s2)))
191+
192+
#plot!(real.(FractionalTransforms.frft(r, s2)))
118193
end
119194

120195
# ╔═╡ 37ebf4d8-28fa-4d0b-929c-5df4c9f418e0
@@ -124,54 +199,75 @@ md"## Gaussian Propagation"
124199
x = -256:256
125200

126201
# ╔═╡ 02708a88-14ce-45cc-8d40-71a74bc5a56d
127-
amp = exp.(-(x.^2 .+ x'.^2) ./ 100);
202+
amp = exp.(-(x.^2 .+ x'.^2) ./ 3000);
128203

129204
# ╔═╡ 77807bbe-a33a-4d65-8e06-446ad368784f
130-
phase_term = exp.(1im .* x .* 2π ./ 5)
205+
phase_term = exp.(1im .* x .* 2π ./ 5 .+ 1im .* x'.^2);
131206

132207
# ╔═╡ 696a77b2-a904-4cf8-805e-b66621dbbb8f
133208
field = amp .* phase_term;
134209

135-
# ╔═╡ e556fd79-00b1-468b-85eb-79a08edfc5bf
136-
gray_show(amp)
137-
138210
# ╔═╡ 4e53efc4-de25-4b97-8dc8-985d56b8bc67
139-
complex_show(field)
211+
simshow(field)
140212

141-
# ╔═╡ 14d21206-ba85-485c-b1c1-2fca106a7169
142-
complex_show(ft(field))
213+
# ╔═╡ 3fa82b96-e701-40d1-89c2-8f71038b6d05
214+
simshow(ft(field))
143215

144216
# ╔═╡ 4dcf3db5-6d37-4a09-a161-4af53ffc91ec
145-
@bind f2 Slider(-1:0.01:2, show_value=true)
217+
@bind f2 Slider(-4:0.001:4, show_value=true)
218+
219+
# ╔═╡ e4db42df-cfe9-4ae0-91f6-5672707d87d5
220+
simshow(frfft(field, f2))
221+
222+
# ╔═╡ 67e0e7dc-4692-451c-97d0-742ab5df3853
223+
rev2(x) = ifftshift(reverse(fftshift(x)))
224+
225+
# ╔═╡ a52deb8f-64e6-46ab-b77e-13b35a20f17c
226+
rev2([simshow(frfft(field, f2));;; simshow(frfft(ift(field), f2-1))][:, :, 2])
227+
228+
# ╔═╡ ce65aa2c-e558-434b-aa8a-2268c47f5684
229+
md"### Comparison with two step FRFT with half the order"
230+
231+
# ╔═╡ e0529213-f2c9-49e4-b2fe-96bbd16a77b7
232+
@bind f3 Slider(-4:0.001:4, show_value=true)
146233

147234
# ╔═╡ 1fe0d80f-664b-4b9f-9ff3-95f0d00e32d5
148-
complex_show(frfft(field, f2))
235+
simshow(frfft(field, f3, p_change=false))
236+
237+
# ╔═╡ e5f32874-5f98-4825-824a-780764e8ef91
238+
simshow(frfft(frfft(field, f3/2),f3/2))
149239

150240
# ╔═╡ Cell order:
151241
# ╠═a696290a-0122-11ed-01e5-a39256aed683
152242
# ╟─55894157-a2d1-4567-99a8-a052d5335dd1
153-
# ╠═cc0f07d5-9192-48cd-9a90-50eebb06783f
154243
# ╠═18b0700c-20fc-4e58-8950-ca09fe34ea19
155244
# ╠═459649ed-ca70-426e-8273-97b146b5bcd5
156245
# ╟─4371cfbf-a3b3-45dc-847b-019994fbb234
157246
# ╠═d90b7f67-4166-44fa-aab7-de2c4f38fc00
158-
# ╠═24901666-4cc4-497f-a6ff-68c3e7ead629
247+
# ╟─24901666-4cc4-497f-a6ff-68c3e7ead629
159248
# ╠═7c445baa-d970-4954-a3dc-df828971bfd7
160-
# ╠═50a798b3-e3b6-47bc-98a5-fffe47053976
161249
# ╠═1915c023-69cf-4d18-90cb-b47465dbef69
162250
# ╠═3109fc21-50c6-46e6-850d-add6f54872d7
251+
# ╠═284cd6f2-1ee3-4923-afa6-ea57e93b28a7
163252
# ╠═227ae9a3-9387-4ac3-b391-e2a78ce40d49
164253
# ╟─abff911a-e10d-4311-955a-7afc4e0d344c
165254
# ╠═bae3c5b7-8964-493b-9e7b-d343e092219c
166-
# ╠═07d2b3b6-3584-4c64-9c4a-138beb3d6b88
255+
# ╠═5655dc10-f4e9-4765-9a89-ac9702864de1
167256
# ╠═1839f03e-6add-4c85-b6fd-9035656ed86c
168-
# ╠═37ebf4d8-28fa-4d0b-929c-5df4c9f418e0
257+
# ╠═07d2b3b6-3584-4c64-9c4a-138beb3d6b88
258+
# ╠═f3cb2153-a7b3-46ed-adbb-038a812b6a81
259+
# ╟─37ebf4d8-28fa-4d0b-929c-5df4c9f418e0
169260
# ╠═fab2b38f-7a93-438e-a1f9-9e58709aec2e
170261
# ╠═02708a88-14ce-45cc-8d40-71a74bc5a56d
171262
# ╠═77807bbe-a33a-4d65-8e06-446ad368784f
172263
# ╠═696a77b2-a904-4cf8-805e-b66621dbbb8f
173-
# ╠═e556fd79-00b1-468b-85eb-79a08edfc5bf
174264
# ╠═4e53efc4-de25-4b97-8dc8-985d56b8bc67
175-
# ╠═14d21206-ba85-485c-b1c1-2fca106a7169
176-
# ╠═4dcf3db5-6d37-4a09-a161-4af53ffc91ec
265+
# ╠═3fa82b96-e701-40d1-89c2-8f71038b6d05
266+
# ╟─4dcf3db5-6d37-4a09-a161-4af53ffc91ec
267+
# ╠═e4db42df-cfe9-4ae0-91f6-5672707d87d5
268+
# ╠═67e0e7dc-4692-451c-97d0-742ab5df3853
269+
# ╠═a52deb8f-64e6-46ab-b77e-13b35a20f17c
270+
# ╟─ce65aa2c-e558-434b-aa8a-2268c47f5684
271+
# ╟─e0529213-f2c9-49e4-b2fe-96bbd16a77b7
177272
# ╠═1fe0d80f-664b-4b9f-9ff3-95f0d00e32d5
273+
# ╠═e5f32874-5f98-4825-824a-780764e8ef91

0 commit comments

Comments
 (0)