1
1
"""
2
2
shiftedkernel = centered(kernel)
3
3
4
- Shift the origin-of-coordinates to the center of `kernel`. The
5
- center-element of `kernel` will be accessed by `shiftedkernel[0, 0,
6
- ...]`.
4
+ Shift the origin-of-coordinates to the center of `kernel`.
5
+ The center-element of `kernel` will be accessed by `shiftedkernel[0, 0, ...]`.
7
6
8
7
This function makes it easy to supply kernels using regular Arrays,
9
8
and provides compatibility with other languages that do not support
21
20
kernfft = freqkernel([T::Type], kern, sz=size(kern); rfft=false)
22
21
23
22
Return a frequency-space representation of `kern`.
24
- This embeds `kern` in an array of size `sz`, in a manner that implicitly imposes periodic boundary
25
- conditions, and then returns the fourier transform. This is sometimes called the optical transfer
26
- function, and known in some frameworks as `psf2otf`. If `rfft` is `true`, the fft for real-valued
27
- arrays (`rfft`) is returned instead and the first dimension size will be approximately half of `sz[1]`.
28
-
29
- `kern` should be zero-centered, i.e., `kern[0, 0]` should reference the center of your kernel.
30
- See [`centered`](@ref). Optionally specify the numeric type `T` (which must be one of the
31
- types supported by FFTW, either `Float32` or `Float64`).
23
+ This embeds `kern` in an array of size `sz`,
24
+ in a manner that implicitly imposes periodic boundary conditions,
25
+ and then returns the Fourier transform (frequency response).
26
+ This is sometimes called the optical transfer function,
27
+ and is known in some frameworks as `psf2otf`.
28
+ If `rfft` is `true`, the FFT for real-valued arrays (`rfft`) is returned instead
29
+ and the first dimension size will be approximately half of `sz[1]`.
30
+
31
+ `kern` should be zero-centered, i.e.,
32
+ `kern[0, 0]` should reference the center of your kernel,
33
+ and `sz` must be large enough to support `kern`.
34
+ See [`centered`](@ref).
35
+ Optionally specify the numeric type `T`
36
+ (which must be one of the types supported by FFTW,
37
+ either `Float32` or `Float64`).
32
38
33
39
The inverse of `freqkernel` is [`spacekernel`](@ref).
34
40
"""
35
41
function freqkernel (:: Type{T} , kern:: AbstractArray , sz:: Dims = size (kern); rfft= false ) where T<: Union{Float32,Float64}
36
- wrapindex (i, s) = i< 1 ? i+ s : i
37
- all (size (kern) .<= sz) || throw (DimensionMismatch (" kernel size $(size (kern)) is bigger than supplied size $sz " ))
42
+ wrapindex (i, s) = 1 + (i< 0 ? i+ s : i)
43
+ all (size (kern) .<= sz) ||
44
+ throw (DimensionMismatch (" kernel size $(size (kern)) exceeds supplied size $sz " ))
45
+ rhs = Tuple (last (CartesianIndices (kern)))
46
+ lhs = Tuple (first (CartesianIndices (kern)))
47
+ limit = collect ((sz .+ 1 ) .÷ 2 ) # handle odd and even sizes
48
+ all (rhs .< limit) ||
49
+ throw (DimensionMismatch (" kernel last index $rhs >= limit $limit " ))
50
+ all (- limit .<= lhs) ||
51
+ throw (DimensionMismatch (" kernel first index $lhs < limit $(- limit) " ))
38
52
kernw = zeros (T, sz... )
39
53
for I in CartesianIndices (kern)
40
54
J = CartesianIndex (map (wrapindex, Tuple (I), sz))
@@ -48,17 +62,22 @@ freqkernel(kern::AbstractArray{T}, args...; rfft=false) where T =
48
62
"""
49
63
kern = spacekernel(kernfft, axs; rfftsz=0)
50
64
51
- Return a real-space representation of `kernfft`, a frequency-space representation of a kernel.
52
- This performs an inverse fourier transform, implicitly imposes periodic boundary conditions,
53
- and then trims & truncates axes of the output to `axs`. By default `kernfft` is assumed to have
54
- been generated by `fft`; if it was instead generated by `rfft`, specify the original size
55
- of the first dimension. (If `kernfft` was generated by [`freqkernel`](@ref), this is just `sz[1]`.)
65
+ Return a real-space representation of `kernfft`,
66
+ the frequency-space representation of a kernel.
67
+ This performs an inverse Fourier transform,
68
+ implicitly imposes periodic boundary conditions,
69
+ and then trims & truncates axes of the output to `axs`.
70
+ By default `kernfft` is assumed to have been generated by `fft`;
71
+ if it was instead generated by `rfft`,
72
+ the specify the original size of the first dimension.
73
+ (If `kernfft` was generated by [`freqkernel`](@ref), this is just `sz[1]`.)
56
74
57
75
The inverse of `spacekernel` is [`freqkernel`](@ref).
58
76
"""
59
77
function spacekernel (kernfft:: AbstractArray , axs:: Indices ; rfftsz= 0 )
60
- wrapindex (i, s) = i < 1 ? i+ s : i
78
+ wrapindex (i, s) = 1 + (i < 0 ? i+ s : i)
61
79
kernw = rfftsz > 0 ? irfft (kernfft, rfftsz) : ifft (kernfft)
80
+ # there could be some checking of axs vs size(kernfft) here
62
81
kern = zeros (eltype (kernw), axs... )
63
82
sz = size (kernw)
64
83
for I in CartesianIndices (kern)
0 commit comments