Skip to content

Commit c05abee

Browse files
authored
Merge branch 'master' into dw/region
2 parents cd29d0d + 6434327 commit c05abee

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-
region, fftshift, ifftshift, Frequencies, fftfreq, rfftfreq
8+
region, 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
@@ -358,6 +358,16 @@ plan_irfft
358358

359359
##############################################################################
360360

361+
"""
362+
fftshift!(dest, src, [dim])
363+
364+
Nonallocating version of [`fftshift`](@ref). Stores the result of the shift of the `src` array into the `dest` array.
365+
"""
366+
function fftshift!(dest, src, dim = 1:ndims(src))
367+
s = ntuple(d -> d in dim ? div(size(dest,d),2) : 0, Val(ndims(dest)))
368+
circshift!(dest, src, s)
369+
end
370+
361371
"""
362372
fftshift(x, [dim])
363373
@@ -370,12 +380,21 @@ swapping the first and second halves, so `fftshift` and [`ifftshift`](@ref) are
370380
the same.
371381
372382
If `dim` is not given then the signal is shifted along each dimension.
383+
384+
The output of `fftshift` is allocated. If one desires to store the output in a preallocated array, use [`fftshift!`](@ref) instead.
373385
"""
374386
fftshift
375387

376-
function fftshift(x, dim = 1:ndims(x))
377-
s = ntuple(d -> d in dim ? div(size(x,d),2) : 0, Val(ndims(x)))
378-
circshift(x, s)
388+
fftshift(x, dim = 1:ndims(x)) = fftshift!(similar(x), x, dim)
389+
390+
"""
391+
ifftshift!(dest, src, [dim])
392+
393+
Nonallocating version of [`ifftshift`](@ref). Stores the result of the shift of the `src` array into the `dest` array.
394+
"""
395+
function ifftshift!(dest, src, dim = 1:ndims(src))
396+
s = ntuple(d -> d in dim ? -div(size(src,d),2) : 0, Val(ndims(src)))
397+
circshift!(dest, src, s)
379398
end
380399

381400
"""
@@ -390,13 +409,12 @@ swapping the first and second halves, so [`fftshift`](@ref) and `ifftshift` are
390409
the same.
391410
392411
If `dim` is not given then the signal is shifted along each dimension.
412+
413+
The output of `ifftshift` is allocated. If one desires to store the output in a preallocated array, use [`ifftshift!`](@ref) instead.
393414
"""
394415
ifftshift
395416

396-
function ifftshift(x, dim = 1:ndims(x))
397-
s = ntuple(d -> d in dim ? -div(size(x,d),2) : 0, Val(ndims(x)))
398-
circshift(x, s)
399-
end
417+
ifftshift(x, dim = 1:ndims(x)) = ifftshift!(similar(x), x, dim)
400418

401419
##############################################################################
402420

test/runtests.jl

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,20 +109,37 @@ end
109109
@test @inferred(AbstractFFTs.fftshift([1 2 3])) == [3 1 2]
110110
@test @inferred(AbstractFFTs.fftshift([1, 2, 3])) == [3, 1, 2]
111111
@test @inferred(AbstractFFTs.fftshift([1 2 3; 4 5 6])) == [6 4 5; 3 1 2]
112+
a = [0 0 0]
113+
b = [0, 0, 0]
114+
c = [0 0 0; 0 0 0]
115+
@test (AbstractFFTs.fftshift!(a, [1 2 3]); a == [3 1 2])
116+
@test (AbstractFFTs.fftshift!(b, [1, 2, 3]); b == [3, 1, 2])
117+
@test (AbstractFFTs.fftshift!(c, [1 2 3; 4 5 6]); c == [6 4 5; 3 1 2])
112118

113119
@test @inferred(AbstractFFTs.fftshift([1 2 3; 4 5 6], 1)) == [4 5 6; 1 2 3]
114120
@test @inferred(AbstractFFTs.fftshift([1 2 3; 4 5 6], ())) == [1 2 3; 4 5 6]
115121
@test @inferred(AbstractFFTs.fftshift([1 2 3; 4 5 6], (1,2))) == [6 4 5; 3 1 2]
116122
@test @inferred(AbstractFFTs.fftshift([1 2 3; 4 5 6], 1:2)) == [6 4 5; 3 1 2]
123+
@test (AbstractFFTs.fftshift!(c, [1 2 3; 4 5 6], 1); c == [4 5 6; 1 2 3])
124+
@test (AbstractFFTs.fftshift!(c, [1 2 3; 4 5 6], ()); c == [1 2 3; 4 5 6])
125+
@test (AbstractFFTs.fftshift!(c, [1 2 3; 4 5 6], (1,2)); c == [6 4 5; 3 1 2])
126+
@test (AbstractFFTs.fftshift!(c, [1 2 3; 4 5 6], 1:2); c == [6 4 5; 3 1 2])
117127

118128
@test @inferred(AbstractFFTs.ifftshift([1 2 3])) == [2 3 1]
119129
@test @inferred(AbstractFFTs.ifftshift([1, 2, 3])) == [2, 3, 1]
120130
@test @inferred(AbstractFFTs.ifftshift([1 2 3; 4 5 6])) == [5 6 4; 2 3 1]
131+
@test (AbstractFFTs.ifftshift!(a, [1 2 3]); a == [2 3 1])
132+
@test (AbstractFFTs.ifftshift!(b, [1, 2, 3]); b == [2, 3, 1])
133+
@test (AbstractFFTs.ifftshift!(c, [1 2 3; 4 5 6]); c == [5 6 4; 2 3 1])
121134

122135
@test @inferred(AbstractFFTs.ifftshift([1 2 3; 4 5 6], 1)) == [4 5 6; 1 2 3]
123136
@test @inferred(AbstractFFTs.ifftshift([1 2 3; 4 5 6], ())) == [1 2 3; 4 5 6]
124137
@test @inferred(AbstractFFTs.ifftshift([1 2 3; 4 5 6], (1,2))) == [5 6 4; 2 3 1]
125138
@test @inferred(AbstractFFTs.ifftshift([1 2 3; 4 5 6], 1:2)) == [5 6 4; 2 3 1]
139+
@test (AbstractFFTs.ifftshift!(c, [1 2 3; 4 5 6], 1); c == [4 5 6; 1 2 3])
140+
@test (AbstractFFTs.ifftshift!(c, [1 2 3; 4 5 6], ()); c == [1 2 3; 4 5 6])
141+
@test (AbstractFFTs.ifftshift!(c, [1 2 3; 4 5 6], (1,2)); c == [5 6 4; 2 3 1])
142+
@test (AbstractFFTs.ifftshift!(c, [1 2 3; 4 5 6], 1:2); c == [5 6 4; 2 3 1])
126143
end
127144

128145
@testset "FFT Frequencies" begin

0 commit comments

Comments
 (0)