Skip to content

Commit c562ae1

Browse files
committed
Rewrote allocating shift functions to use non-allocating ones. Added
backreferences to non-allocating functions.
1 parent 8c9d817 commit c562ae1

File tree

1 file changed

+23
-19
lines changed

1 file changed

+23
-19
lines changed

src/definitions.jl

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -344,6 +344,17 @@ 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+
@assert size(dest)==size(src)
354+
s = ntuple(d -> d in dim ? div(size(dest,d),2) : 0, Val(ndims(dest)))
355+
circshift!(dest, src, s)
356+
end
357+
347358
"""
348359
fftshift(x, [dim])
349360
@@ -356,22 +367,24 @@ swapping the first and second halves, so `fftshift` and [`ifftshift`](@ref) are
356367
the same.
357368
358369
If `dim` is not given then the signal is shifted along each dimension.
370+
371+
The output of `fftshift` is allocated. If one desires to store the output in a preallocated array, use [`fftshift!`](@ref) instead.
359372
"""
360373
fftshift
361374

362375
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)
376+
dest = similar(x)
377+
fftshift!(dest, x, dim)
365378
end
366379

367380
"""
368-
fftshift!(dest, src, [dim])
381+
ifftshift!(dest, src, [dim])
369382
370-
Nonallocating version of [`fftshift`](@ref). Stores the result of the shift of the `src` array into the `dest` array.
383+
Nonallocating version of [`ifftshift`](@ref). Stores the result of the shift of the `src` array into the `dest` array.
371384
"""
372-
function fftshift!(dest, src, dim = 1:ndims(src))
385+
function ifftshift!(dest, src, dim = 1:ndims(src))
373386
@assert size(dest)==size(src)
374-
s = ntuple(d -> d in dim ? div(size(dest,d),2) : 0, Val(ndims(dest)))
387+
s = ntuple(d -> d in dim ? -div(size(src,d),2) : 0, Val(ndims(src)))
375388
circshift!(dest, src, s)
376389
end
377390

@@ -387,23 +400,14 @@ swapping the first and second halves, so [`fftshift`](@ref) and `ifftshift` are
387400
the same.
388401
389402
If `dim` is not given then the signal is shifted along each dimension.
403+
404+
The output of `ifftshift` is allocated. If one desires to store the output in a preallocated array, use [`ifftshift!`](@ref) instead.
390405
"""
391406
ifftshift
392407

393408
function ifftshift(x, dim = 1:ndims(x))
394-
s = ntuple(d -> d in dim ? -div(size(x,d),2) : 0, Val(ndims(x)))
395-
circshift(x, s)
396-
end
397-
398-
"""
399-
ifftshift!(dest, src, [dim])
400-
401-
Nonallocating version of [`ifftshift`](@ref). Stores the result of the shift of the `src` array into the `dest array`.
402-
"""
403-
function ifftshift!(dest, src, dim = 1:ndims(src))
404-
@assert size(dest)==size(src)
405-
s = ntuple(d -> d in dim ? -div(size(src,d),2) : 0, Val(ndims(src)))
406-
circshift!(dest, src, s)
409+
dest = similar(x)
410+
ifftshift!(dest, x, dim)
407411
end
408412

409413
##############################################################################

0 commit comments

Comments
 (0)