Skip to content

Commit 9372553

Browse files
authored
Merge pull request #40 from mcabbott/shift
Use tuples in fftshift, and add copy method
2 parents eb90bc6 + 71d939c commit 9372553

File tree

2 files changed

+9
-14
lines changed

2 files changed

+9
-14
lines changed

src/definitions.jl

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -346,8 +346,6 @@ plan_irfft
346346

347347
##############################################################################
348348

349-
fftshift(x) = circshift(x, div.([size(x)...],2))
350-
351349
"""
352350
fftshift(x, [dim])
353351
@@ -363,16 +361,11 @@ If `dim` is not given then the signal is shifted along each dimension.
363361
"""
364362
fftshift
365363

366-
function fftshift(x,dim)
367-
s = zeros(Int,ndims(x))
368-
for i in dim
369-
s[i] = div(size(x,i),2)
370-
end
364+
function fftshift(x, dim = 1:ndims(x))
365+
s = ntuple(d -> d in dim ? div(size(x,d),2) : 0, ndims(x))
371366
circshift(x, s)
372367
end
373368

374-
ifftshift(x) = circshift(x, div.([size(x)...],-2))
375-
376369
"""
377370
ifftshift(x, [dim])
378371
@@ -388,11 +381,8 @@ If `dim` is not given then the signal is shifted along each dimension.
388381
"""
389382
ifftshift
390383

391-
function ifftshift(x,dim)
392-
s = zeros(Int,ndims(x))
393-
for i in dim
394-
s[i] = -div(size(x,i),2)
395-
end
384+
function ifftshift(x, dim = 1:ndims(x))
385+
s = ntuple(d -> d in dim ? -div(size(x,d),2) : 0, ndims(x))
396386
circshift(x, s)
397387
end
398388

@@ -423,6 +413,8 @@ end
423413
Base.size(x::Frequencies) = (x.n,)
424414
Base.step(x::Frequencies) = x.multiplier
425415

416+
Base.copy(x::Frequencies) = x
417+
426418
"""
427419
fftfreq(n, fs=1)
428420
Return the discrete Fourier transform (DFT) sample frequencies for a DFT of length `n`. The returned

test/runtests.jl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,9 @@ end
8282
end
8383

8484
@testset "FFT Frequencies" begin
85+
@test fftfreq(8) isa Frequencies
86+
@test copy(fftfreq(8)) isa Frequencies
87+
8588
# N even
8689
@test fftfreq(8) == [0.0, 0.125, 0.25, 0.375, -0.5, -0.375, -0.25, -0.125]
8790
@test rfftfreq(8) == [0.0, 0.125, 0.25, 0.375, 0.5]

0 commit comments

Comments
 (0)