Skip to content

Commit d5780e3

Browse files
committed
Real dispatched DFT, changes to typing
1 parent f00bfea commit d5780e3

File tree

1 file changed

+64
-17
lines changed

1 file changed

+64
-17
lines changed

src/algos.jl

Lines changed: 64 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -162,9 +162,8 @@ function fft!(out::AbstractVector{T}, in::AbstractVector{T}, ::Val{FFT_FORWARD},
162162
N1 = left.sz
163163
N2 = right.sz
164164

165-
inc = 2*π/N
166-
w1 = T(cos(inc), -sin(inc))
167-
wj1 = T(1, 0)
165+
w1 = convert(T, cispi(-2/N))
166+
wj1 = one(T)
168167
tmp = g.workspace[idx]
169168
for j1 in 1:N1
170169
wk2 = wj1;
@@ -188,9 +187,8 @@ function fft!(out::AbstractVector{T}, in::AbstractVector{T}, ::Val{FFT_BACKWARD}
188187
N1 = left.sz
189188
N2 = right.sz
190189

191-
inc = 2*π/N
192-
w1 = T(cos(inc), sin(inc))
193-
wj1 = T(1, 0)
190+
w1 = convert(T, cispi(2/N))
191+
wj1 = one(T)
194192
tmp = g.workspace[idx]
195193
for j1 in 2:N1
196194
Complex<F,L> wk2 = wj1;
@@ -228,9 +226,8 @@ function fft_pow2!(out::AbstractVector{T}, in::AbstractVector{T}, ::Val{FFT_FORW
228226
fft_pow2!(@view(out[1:(end÷2)]), @view(in[1:2:end]), Val(FFT_FORWARD))
229227
fft_pow2!(@view(out[(end÷2+1):end]), @view(in[2:2:end]), Val(FFT_FORWARD))
230228

231-
inc = 2*π/N
232-
w1 = T(cos(inc), -sin(inc));
233-
wj = T(1,0)
229+
w1 = convert(T, cispi(-2/N))
230+
wj = one(T)
234231
m = N ÷ 2
235232
for j in 1:m
236233
out_j = out[j]
@@ -253,9 +250,8 @@ function fft_pow2!(out::AbstractVector{T}, in::AbstractVector{T}, ::Val{FFT_BACK
253250
fft_pow2!(@view(out[1:(end÷2)]), @view(in[1:2:end]), Val(FFT_BACKWARD))
254251
fft_pow2!(@view(out[(end÷2+1):end]), @view(in[2:2:end]), Val(FFT_BACKWARD))
255252

256-
inc = 2*π/N
257-
w1 = T(cos(inc), sin(inc));
258-
wj = T(1,0)
253+
w1 = convert(T, cispi(2/N))
254+
wj = one(T)
259255
m = N ÷ 2
260256
for j in 1:m
261257
out_j = out[j]
@@ -268,8 +264,8 @@ end
268264
function fft_dft!(out::AbstractVector{T}, in::AbstractVector{T}, ::Val{FFT_BACKWARD}) where {T<:Complex}
269265
N = length(out)
270266
inc = 2*π/N
271-
wn² = wn = w = T(cos(inc), sin(inc))
272-
wn_1 = T(1., 0.)
267+
wn² = wn = w = convert(T, cispi(2/N))
268+
wn_1 = one(T)
273269

274270
tmp = in[1]
275271
out .= tmp
@@ -291,11 +287,36 @@ function fft_dft!(out::AbstractVector{T}, in::AbstractVector{T}, ::Val{FFT_BACKW
291287
end
292288
end
293289

290+
function fft_dft!(out::AbstractVector{T}, in::AbstractVector{T}, ::Val{FFT_BACKWARD}) where {T<:Real}
291+
N = length(out)
292+
halfN = N÷2
293+
wn² = wn = w = convert(T, cispi(2/N))
294+
wn_1 = one(T)
295+
296+
out .= in[1]
297+
out[1] = sum(in)
298+
iseven(N) && out[halfN+1] = foldr(-,in)
299+
300+
wk = wn²;
301+
for d in 2:halfN
302+
out[d] = in[d]*wk + out[d]
303+
for k in (d+1):halfN
304+
wk *= wn
305+
out[d] = in[k]*wk + out[d]
306+
out[k] = in[d]*wk + out[k]
307+
end
308+
wn_1 = wn
309+
wn *= w
310+
wn² *= (wn*wn_1)
311+
wk = wn²
312+
end
313+
out[(N-halfN+2):end] .= conj.(out[halfN:-1:2])
314+
end
315+
294316
function fft_dft!(out::AbstractVector{T}, in::AbstractVector{T}, ::Val{FFT_FORWARD}) where {T<:Complex}
295317
N = length(out)
296-
inc = 2*π/N
297-
wn² = wn = w = T(cos(inc), -sin(inc));
298-
wn_1 = T(1., 0.);
318+
wn² = wn = w = convert(T, cispi(-2/N))
319+
wn_1 = one(T)
299320

300321
tmp = in[1];
301322
out .= tmp;
@@ -317,6 +338,32 @@ function fft_dft!(out::AbstractVector{T}, in::AbstractVector{T}, ::Val{FFT_FORWA
317338
end
318339
end
319340

341+
function fft_dft!(out::AbstractVector{T}, in::AbstractVector{T}, ::Val{FFT_FORWARD}) where {T<:Real}
342+
N = length(out)
343+
halfN = N÷2
344+
wn² = wn = w = convert(T, cispi(-2/N))
345+
wn_1 = one(T)
346+
347+
out .= in[1]
348+
out[1] = sum(in)
349+
iseven(N) && out[halfN+1] = foldr(-,in)
350+
351+
wk = wn²;
352+
for d in 2:halfN
353+
out[d] = in[d]*wk + out[d]
354+
for k in (d+1):halfN
355+
wk *= wn
356+
out[d] = in[k]*wk + out[d]
357+
out[k] = in[d]*wk + out[k]
358+
end
359+
wn_1 = wn
360+
wn *= w
361+
wn² *= (wn*wn_1)
362+
wk = wn²
363+
end
364+
out[(N-halfN+2):end] .= conj.(out[halfN:-1:2])
365+
end
366+
320367

321368
function fft!(out::AbstractVector{T}, in::AbstractVector{T}, ::Val{FFT_FORWARD}, ::DFT, ::CallGraph{T}, ::Int) where {T<:Complex}
322369
fft_dft!(out, in, Val(FFT_FORWARD))

0 commit comments

Comments
 (0)