In [161]:
import numpy as np
In [162]:
xn = 6
yn = 6
original = np.arange(1,xn*yn + 1,dtype=np.float).reshape([yn,xn])
print original
In [163]:
print "THE IFFT SHIFT SHOULD GIVE:"
model_ifft_shift = np.fft.ifftshift(original)
print model_ifft_shift
In [164]:
print "THE FFT SHIFT SHOULD GIVE:"
model_fft_shift = np.fft.fftshift(original)
print model_fft_shift
In [165]:
def ifftshift(mat):
T = np.copy(mat)
half_x = T.shape[1] // 2
half_y = T.shape[0] // 2
odd_offset_x = 1 if T.shape[1] % 2 != 0 else 0
#rotate all the rows right
for iy in range(0,T.shape[0]):
swap_mid = T[iy,half_x]
for ix in range(0,half_x):
ix_reverse = half_x - 1 - ix
swap_x = T[iy,half_x + ix_reverse + odd_offset_x]
T[iy,half_x + ix_reverse + odd_offset_x] = T[iy,ix_reverse]
T[iy,ix_reverse+odd_offset_x] = swap_x
T[iy,0] = swap_mid #doesn't matter for the even case
odd_offset_y = 1 if T.shape[0] % 2 != 0 else 0
#rotate all the columns down
for ix in range(0,T.shape[1]):
swap_mid = T[half_y,ix]
for iy in range(0,half_y):
iy_reverse = half_y - 1 - iy
swap_y = T[half_y + iy_reverse + odd_offset_y,ix]
T[half_y + iy_reverse + odd_offset_y,ix] = T[iy_reverse,ix]
T[iy_reverse+odd_offset_y,ix] = swap_y
T[0,ix] = swap_mid #doesn't matter for the even case
return T
In [166]:
assert np.all(model_ifft_shift == ifftshift(original)), "Test case failed"
In [167]:
def fftshift(mat):
T = np.copy(mat)
half_x = T.shape[1] // 2
half_y = T.shape[0] // 2
odd_offset_x = 1 if T.shape[1] % 2 != 0 else 0
#rotate all the rows right
for iy in range(0,T.shape[0]):
swap_mid = T[iy,half_x]
for ix in range(0,half_x):
swap = T[iy,ix] #in case this dimension is even
T[iy,ix] = T[iy,half_x + ix + odd_offset_x]
T[iy,half_x + ix] = swap
if T.shape[1] % 2 != 0:
T[iy,T.shape[1]-1] = swap_mid
odd_offset_y = 1 if T.shape[0] % 2 != 0 else 0
#rotate all the columns down
for ix in range(0,T.shape[1]):
swap_mid = T[half_y,ix]
for iy in range(0,half_y):
swap = T[iy,ix] #in case this dimension is even
T[iy,ix] = T[half_y + iy + odd_offset_y,ix]
T[half_y + iy,ix] = swap
if T.shape[0] % 2 != 0:
T[T.shape[0]-1,ix] = swap_mid
return T
In [168]:
assert np.all(model_fft_shift == fftshift(original)), "Test case failed"
In [168]: