forked from JuliaMath/AbstractFFTs.jl
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtestplans.jl
More file actions
256 lines (220 loc) · 8.81 KB
/
testplans.jl
File metadata and controls
256 lines (220 loc) · 8.81 KB
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
mutable struct TestPlan{T,N,G} <: Plan{T}
region::G
sz::NTuple{N,Int}
pinv::Plan{T}
function TestPlan{T}(region::G, sz::NTuple{N,Int}) where {T,N,G}
return new{T,N,G}(region, sz)
end
end
mutable struct InverseTestPlan{T,N,G} <: Plan{T}
region::G
sz::NTuple{N,Int}
pinv::Plan{T}
function InverseTestPlan{T}(region::G, sz::NTuple{N,Int}) where {T,N,G}
return new{T,N,G}(region, sz)
end
end
Base.size(p::TestPlan) = p.sz
Base.ndims(::TestPlan{T,N}) where {T,N} = N
Base.size(p::InverseTestPlan) = p.sz
Base.ndims(::InverseTestPlan{T,N}) where {T,N} = N
AbstractFFTs.AdjointStyle(::TestPlan) = AbstractFFTs.FFTAdjointStyle()
AbstractFFTs.AdjointStyle(::InverseTestPlan) = AbstractFFTs.FFTAdjointStyle()
function AbstractFFTs.plan_fft(x::AbstractArray{T}, region; kwargs...) where {T}
return TestPlan{T}(region, size(x))
end
function AbstractFFTs.plan_bfft(x::AbstractArray{T}, region; kwargs...) where {T}
return InverseTestPlan{T}(region, size(x))
end
function AbstractFFTs.plan_inv(p::TestPlan{T}) where {T}
unscaled_pinv = InverseTestPlan{T}(p.region, p.sz)
N = AbstractFFTs.normalization(T, p.sz, p.region)
unscaled_pinv.pinv = AbstractFFTs.ScaledPlan(p, N)
pinv = AbstractFFTs.ScaledPlan(unscaled_pinv, N)
return pinv
end
function AbstractFFTs.plan_inv(pinv::InverseTestPlan{T}) where {T}
unscaled_p = TestPlan{T}(pinv.region, pinv.sz)
N = AbstractFFTs.normalization(T, pinv.sz, pinv.region)
unscaled_p.pinv = AbstractFFTs.ScaledPlan(pinv, N)
p = AbstractFFTs.ScaledPlan(unscaled_p, N)
return p
end
# Just a helper function since forward and backward are nearly identical
# The function does not check if the size of `y` and `x` are compatible, this
# is done in the function where `dft!` is called since the check differs for FFTs
# with complex and real-valued signals
function dft!(
y::AbstractArray{<:Complex,N},
x::AbstractArray{<:Union{Complex,Real},N},
dims,
sign::Int
) where {N}
# check that dimensions that are transformed are unique
allunique(dims) || error("dimensions have to be unique")
T = eltype(y)
# we use `size(x, d)` since for real-valued signals
# `size(y, first(dims)) = size(x, first(dims)) ÷ 2 + 1`
cs = map(d -> T(sign * 2π / size(x, d)), dims)
fill!(y, zero(T))
for yidx in CartesianIndices(y)
# set of indices of `x` on which `y[yidx]` depends
xindices = CartesianIndices(
ntuple(i -> i in dims ? axes(x, i) : yidx[i]:yidx[i], Val(N))
)
for xidx in xindices
y[yidx] += x[xidx] * cis(sum(c * (yidx[d] - 1) * (xidx[d] - 1) for (c, d) in zip(cs, dims)))
end
end
return y
end
function mul!(
y::AbstractArray{<:Complex,N}, p::TestPlan, x::AbstractArray{<:Union{Complex,Real},N}
) where {N}
size(y) == size(p) == size(x) || throw(DimensionMismatch())
dft!(y, x, p.region, -1)
end
function mul!(
y::AbstractArray{<:Complex,N}, p::InverseTestPlan, x::AbstractArray{<:Union{Complex,Real},N}
) where {N}
size(y) == size(p) == size(x) || throw(DimensionMismatch())
dft!(y, x, p.region, 1)
end
Base.:*(p::TestPlan, x::AbstractArray) = mul!(similar(x, complex(float(eltype(x)))), p, x)
Base.:*(p::InverseTestPlan, x::AbstractArray) = mul!(similar(x, complex(float(eltype(x)))), p, x)
mutable struct TestRPlan{T,N,G} <: Plan{T}
region::G
sz::NTuple{N,Int}
pinv::Plan{Complex{T}}
TestRPlan{T}(region::G, sz::NTuple{N,Int}) where {T,N,G} = new{T,N,G}(region, sz)
end
mutable struct InverseTestRPlan{T,N,G} <: Plan{Complex{T}}
d::Int
region::G
sz::NTuple{N,Int}
pinv::Plan{T}
function InverseTestRPlan{T}(d::Int, region::G, sz::NTuple{N,Int}) where {T,N,G}
sz[first(region)::Int] == d ÷ 2 + 1 || error("incompatible dimensions")
return new{T,N,G}(d, region, sz)
end
end
AbstractFFTs.AdjointStyle(::TestRPlan) = AbstractFFTs.RFFTAdjointStyle()
AbstractFFTs.AdjointStyle(p::InverseTestRPlan) = AbstractFFTs.IRFFTAdjointStyle(p.d)
function AbstractFFTs.plan_rfft(x::AbstractArray{T}, region; kwargs...) where {T<:Real}
return TestRPlan{T}(region, size(x))
end
function AbstractFFTs.plan_brfft(x::AbstractArray{Complex{T}}, d, region; kwargs...) where {T}
return InverseTestRPlan{T}(d, region, size(x))
end
function AbstractFFTs.plan_inv(p::TestRPlan{T,N}) where {T,N}
firstdim = first(p.region)::Int
d = p.sz[firstdim]
sz = ntuple(i -> i == firstdim ? d ÷ 2 + 1 : p.sz[i], Val(N))
_N = AbstractFFTs.normalization(T, p.sz, p.region)
unscaled_pinv = InverseTestRPlan{T}(d, p.region, sz)
unscaled_pinv.pinv = AbstractFFTs.ScaledPlan(p, _N)
pinv = AbstractFFTs.ScaledPlan(unscaled_pinv, _N)
return pinv
end
function AbstractFFTs.plan_inv(pinv::InverseTestRPlan{T,N}) where {T,N}
firstdim = first(pinv.region)::Int
sz = ntuple(i -> i == firstdim ? pinv.d : pinv.sz[i], Val(N))
_N = AbstractFFTs.normalization(T, sz, pinv.region)
unscaled_p = TestRPlan{T}(pinv.region, sz)
unscaled_p.pinv = AbstractFFTs.ScaledPlan(pinv, _N)
p = AbstractFFTs.ScaledPlan(unscaled_p, _N)
return p
end
Base.size(p::TestRPlan) = p.sz
Base.ndims(::TestRPlan{T,N}) where {T,N} = N
Base.size(p::InverseTestRPlan) = p.sz
Base.ndims(::InverseTestRPlan{T,N}) where {T,N} = N
function real_invdft!(
y::AbstractArray{<:Real,N},
x::AbstractArray{<:Union{Complex,Real},N},
dims,
) where {N}
# check that dimensions that are transformed are unique
allunique(dims) || error("dimensions have to be unique")
firstdim = first(dims)
size_x_firstdim = size(x, firstdim)
iseven_firstdim = iseven(size(y, firstdim))
# we do not check that the input corresponds to a real-valued signal
# (i.e., that the first and, if `iseven_firstdim`, the last value in dimension
# `haldim` of `x` are real values) due to numerical inaccuracies
# instead we just use the real part of these entries
T = eltype(y)
# we use `size(y, d)` since `size(x, first(dims)) = size(y, first(dims)) ÷ 2 + 1`
cs = map(d -> T(2π / size(y, d)), dims)
fill!(y, zero(T))
for yidx in CartesianIndices(y)
# set of indices of `x` on which `y[yidx]` depends
xindices = CartesianIndices(
ntuple(i -> i in dims ? axes(x, i) : yidx[i]:yidx[i], Val(N))
)
for xidx in xindices
coeffimag, coeffreal = sincos(
sum(c * (yidx[d] - 1) * (xidx[d] - 1) for (c, d) in zip(cs, dims))
)
# the first and, if `iseven_firstdim`, the last term of the DFT are scaled
# with 1 instead of 2 and only the real part is used (see note above)
xidx_firstdim = xidx[firstdim]
if xidx_firstdim == 1 || (iseven_firstdim && xidx_firstdim == size_x_firstdim)
y[yidx] += coeffreal * real(x[xidx])
else
xreal, ximag = reim(x[xidx])
y[yidx] += 2 * (coeffreal * xreal - coeffimag * ximag)
end
end
end
return y
end
to_real!(x::AbstractArray) = map!(real, x, x)
function Base.:*(p::TestRPlan, x::AbstractArray)
size(p) == size(x) || error("array and plan are not consistent")
# create output array
firstdim = first(p.region)::Int
d = size(x, firstdim)
firstdim_size = d ÷ 2 + 1
T = complex(float(eltype(x)))
sz = ntuple(i -> i == firstdim ? firstdim_size : size(x, i), Val(ndims(x)))
y = similar(x, T, sz)
# compute DFT
dft!(y, x, p.region, -1)
# we clean the output a bit to make sure that we return real values
# whenever the output is mathematically guaranteed to be a real number
to_real!(selectdim(y, firstdim, 1))
if iseven(d)
to_real!(selectdim(y, firstdim, firstdim_size))
end
return y
end
function Base.:*(p::InverseTestRPlan, x::AbstractArray)
size(p) == size(x) || error("array and plan are not consistent")
# create output array
firstdim = first(p.region)::Int
d = p.d
sz = ntuple(i -> i == firstdim ? d : size(x, i), Val(ndims(x)))
y = similar(x, real(float(eltype(x))), sz)
# compute DFT
real_invdft!(y, x, p.region)
return y
end
# In-place plans
# (simple wrapper of out-of-place plans that does not support inverses)
struct InplaceTestPlan{T,P<:Plan{T}} <: Plan{T}
plan::P
end
Base.size(p::InplaceTestPlan) = size(p.plan)
Base.ndims(p::InplaceTestPlan) = ndims(p.plan)
AbstractFFTs.AdjointStyle(p::InplaceTestPlan) = AbstractFFTs.AdjointStyle(p.plan)
function AbstractFFTs.plan_fft!(x::AbstractArray, region; kwargs...)
return InplaceTestPlan(plan_fft(x, region; kwargs...))
end
function AbstractFFTs.plan_bfft!(x::AbstractArray, region; kwargs...)
return InplaceTestPlan(plan_bfft(x, region; kwargs...))
end
function LinearAlgebra.mul!(y::AbstractArray, p::InplaceTestPlan, x::AbstractArray)
return mul!(y, p.plan, x)
end
Base.:*(p::InplaceTestPlan, x::AbstractArray) = copyto!(x, p.plan * x)