Skip to content

Commit 6434327

Browse files
authored
Merge pull request #66 from Gregstrq/prealloc_fftshift
`fftshift!` and `ifftshift!`
2 parents 4f630d6 + bdcf670 commit 6434327

File tree

3 files changed

+43
-8
lines changed

3 files changed

+43
-8
lines changed

src/AbstractFFTs.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import ChainRulesCore
55
export fft, ifft, bfft, fft!, ifft!, bfft!,
66
plan_fft, plan_ifft, plan_bfft, plan_fft!, plan_ifft!, plan_bfft!,
77
rfft, irfft, brfft, plan_rfft, plan_irfft, plan_brfft,
8-
fftshift, ifftshift, Frequencies, fftfreq, rfftfreq
8+
fftshift, ifftshift, fftshift!, ifftshift!, Frequencies, fftfreq, rfftfreq
99

1010
include("definitions.jl")
1111
include("chainrules.jl")

src/definitions.jl

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -344,6 +344,16 @@ plan_irfft
344344

345345
##############################################################################
346346

347+
"""
348+
fftshift!(dest, src, [dim])
349+
350+
Nonallocating version of [`fftshift`](@ref). Stores the result of the shift of the `src` array into the `dest` array.
351+
"""
352+
function fftshift!(dest, src, dim = 1:ndims(src))
353+
s = ntuple(d -> d in dim ? div(size(dest,d),2) : 0, Val(ndims(dest)))
354+
circshift!(dest, src, s)
355+
end
356+
347357
"""
348358
fftshift(x, [dim])
349359
@@ -356,12 +366,21 @@ swapping the first and second halves, so `fftshift` and [`ifftshift`](@ref) are
356366
the same.
357367
358368
If `dim` is not given then the signal is shifted along each dimension.
369+
370+
The output of `fftshift` is allocated. If one desires to store the output in a preallocated array, use [`fftshift!`](@ref) instead.
359371
"""
360372
fftshift
361373

362-
function fftshift(x, dim = 1:ndims(x))
363-
s = ntuple(d -> d in dim ? div(size(x,d),2) : 0, Val(ndims(x)))
364-
circshift(x, s)
374+
fftshift(x, dim = 1:ndims(x)) = fftshift!(similar(x), x, dim)
375+
376+
"""
377+
ifftshift!(dest, src, [dim])
378+
379+
Nonallocating version of [`ifftshift`](@ref). Stores the result of the shift of the `src` array into the `dest` array.
380+
"""
381+
function ifftshift!(dest, src, dim = 1:ndims(src))
382+
s = ntuple(d -> d in dim ? -div(size(src,d),2) : 0, Val(ndims(src)))
383+
circshift!(dest, src, s)
365384
end
366385

367386
"""
@@ -376,13 +395,12 @@ swapping the first and second halves, so [`fftshift`](@ref) and `ifftshift` are
376395
the same.
377396
378397
If `dim` is not given then the signal is shifted along each dimension.
398+
399+
The output of `ifftshift` is allocated. If one desires to store the output in a preallocated array, use [`ifftshift!`](@ref) instead.
379400
"""
380401
ifftshift
381402

382-
function ifftshift(x, dim = 1:ndims(x))
383-
s = ntuple(d -> d in dim ? -div(size(x,d),2) : 0, Val(ndims(x)))
384-
circshift(x, s)
385-
end
403+
ifftshift(x, dim = 1:ndims(x)) = ifftshift!(similar(x), x, dim)
386404

387405
##############################################################################
388406

test/runtests.jl

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,20 +103,37 @@ end
103103
@test @inferred(AbstractFFTs.fftshift([1 2 3])) == [3 1 2]
104104
@test @inferred(AbstractFFTs.fftshift([1, 2, 3])) == [3, 1, 2]
105105
@test @inferred(AbstractFFTs.fftshift([1 2 3; 4 5 6])) == [6 4 5; 3 1 2]
106+
a = [0 0 0]
107+
b = [0, 0, 0]
108+
c = [0 0 0; 0 0 0]
109+
@test (AbstractFFTs.fftshift!(a, [1 2 3]); a == [3 1 2])
110+
@test (AbstractFFTs.fftshift!(b, [1, 2, 3]); b == [3, 1, 2])
111+
@test (AbstractFFTs.fftshift!(c, [1 2 3; 4 5 6]); c == [6 4 5; 3 1 2])
106112

107113
@test @inferred(AbstractFFTs.fftshift([1 2 3; 4 5 6], 1)) == [4 5 6; 1 2 3]
108114
@test @inferred(AbstractFFTs.fftshift([1 2 3; 4 5 6], ())) == [1 2 3; 4 5 6]
109115
@test @inferred(AbstractFFTs.fftshift([1 2 3; 4 5 6], (1,2))) == [6 4 5; 3 1 2]
110116
@test @inferred(AbstractFFTs.fftshift([1 2 3; 4 5 6], 1:2)) == [6 4 5; 3 1 2]
117+
@test (AbstractFFTs.fftshift!(c, [1 2 3; 4 5 6], 1); c == [4 5 6; 1 2 3])
118+
@test (AbstractFFTs.fftshift!(c, [1 2 3; 4 5 6], ()); c == [1 2 3; 4 5 6])
119+
@test (AbstractFFTs.fftshift!(c, [1 2 3; 4 5 6], (1,2)); c == [6 4 5; 3 1 2])
120+
@test (AbstractFFTs.fftshift!(c, [1 2 3; 4 5 6], 1:2); c == [6 4 5; 3 1 2])
111121

112122
@test @inferred(AbstractFFTs.ifftshift([1 2 3])) == [2 3 1]
113123
@test @inferred(AbstractFFTs.ifftshift([1, 2, 3])) == [2, 3, 1]
114124
@test @inferred(AbstractFFTs.ifftshift([1 2 3; 4 5 6])) == [5 6 4; 2 3 1]
125+
@test (AbstractFFTs.ifftshift!(a, [1 2 3]); a == [2 3 1])
126+
@test (AbstractFFTs.ifftshift!(b, [1, 2, 3]); b == [2, 3, 1])
127+
@test (AbstractFFTs.ifftshift!(c, [1 2 3; 4 5 6]); c == [5 6 4; 2 3 1])
115128

116129
@test @inferred(AbstractFFTs.ifftshift([1 2 3; 4 5 6], 1)) == [4 5 6; 1 2 3]
117130
@test @inferred(AbstractFFTs.ifftshift([1 2 3; 4 5 6], ())) == [1 2 3; 4 5 6]
118131
@test @inferred(AbstractFFTs.ifftshift([1 2 3; 4 5 6], (1,2))) == [5 6 4; 2 3 1]
119132
@test @inferred(AbstractFFTs.ifftshift([1 2 3; 4 5 6], 1:2)) == [5 6 4; 2 3 1]
133+
@test (AbstractFFTs.ifftshift!(c, [1 2 3; 4 5 6], 1); c == [4 5 6; 1 2 3])
134+
@test (AbstractFFTs.ifftshift!(c, [1 2 3; 4 5 6], ()); c == [1 2 3; 4 5 6])
135+
@test (AbstractFFTs.ifftshift!(c, [1 2 3; 4 5 6], (1,2)); c == [5 6 4; 2 3 1])
136+
@test (AbstractFFTs.ifftshift!(c, [1 2 3; 4 5 6], 1:2); c == [5 6 4; 2 3 1])
120137
end
121138

122139
@testset "FFT Frequencies" begin

0 commit comments

Comments
 (0)