diff --git a/base/dft.jl b/base/dft.jl index b676dee045b85..d611813f14156 100644 --- a/base/dft.jl +++ b/base/dft.jl @@ -365,14 +365,16 @@ fftshift(x) function fftshift(x,dim) s = zeros(Int,ndims(x)) - s[dim] = div(size(x,dim),2) + for i in dim + s[i] = div(size(x,i),2) + end circshift(x, s) end """ fftshift(x,dim) -Swap the first and second halves of the given dimension of array `x`. +Swap the first and second halves of the given dimension or iterable of dimensions of array `x`. """ fftshift(x,dim) @@ -387,7 +389,9 @@ ifftshift function ifftshift(x,dim) s = zeros(Int,ndims(x)) - s[dim] = -div(size(x,dim),2) + for i in dim + s[i] = -div(size(x,i),2) + end circshift(x, s) end diff --git a/test/dsp.jl b/test/dsp.jl index a40566edcf838..e1a9ee650c439 100644 --- a/test/dsp.jl +++ b/test/dsp.jl @@ -26,13 +26,25 @@ si = [0.9967207836936347,-1.4940914728163142,1.2841226760316475,-0.4524417279474 @test_throws ArgumentError filt!([1, 2], [1], [1], [1]) @test xcorr([1, 2], [3, 4]) == [4, 11, 6] +# Shift-Functions @test fftshift([1 2 3]) == [3 1 2] @test fftshift([1, 2, 3]) == [3, 1, 2] @test fftshift([1 2 3; 4 5 6]) == [6 4 5; 3 1 2] + +@test fftshift([1 2 3; 4 5 6], 1) == [4 5 6; 1 2 3] +@test fftshift([1 2 3; 4 5 6], ()) == [1 2 3; 4 5 6] +@test fftshift([1 2 3; 4 5 6], (1,2)) == [6 4 5; 3 1 2] +@test fftshift([1 2 3; 4 5 6], 1:2) == [6 4 5; 3 1 2] + @test ifftshift([1 2 3]) == [2 3 1] @test ifftshift([1, 2, 3]) == [2, 3, 1] @test ifftshift([1 2 3; 4 5 6]) == [5 6 4; 2 3 1] +@test ifftshift([1 2 3; 4 5 6], 1) == [4 5 6; 1 2 3] +@test ifftshift([1 2 3; 4 5 6], ()) == [1 2 3; 4 5 6] +@test ifftshift([1 2 3; 4 5 6], (1,2)) == [5 6 4; 2 3 1] +@test ifftshift([1 2 3; 4 5 6], 1:2) == [5 6 4; 2 3 1] + # Convolution a = [1., 2., 1., 2.] b = [1., 2., 3.]