Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def shift_fft(f):
nx = f.shape[0]
ny = f.shape[1]
p0 = nx / 2
q0 = ny / 2
X, Y = np.meshgrid(np.arange(p0, p0 + nx) % nx,
np.arange(q0, q0 + ny) % ny,
indexing='ij')
fs = f[X, Y, ...]
return T2FFT.analyze(fs, axes=(0, 1))
# Shows that when the image rotates around center (p0, q0), the FT also rotates around (p0, q0) (which corresponds
# to frequency (0, 0).
f1 = np.zeros((nx, ny))
f1[p0 - 1, q0 - 1] = 1.
f1[p0, q0] = 1.
f1[p0 + 1, p0 + 1] = 1.
f1 = np.random.randn(nx, ny) + 1j * np.random.randn(nx, ny)
X, Y = np.meshgrid(np.arange(p0, p0 + f1.shape[0]) % f1.shape[0],
np.arange(q0, q0 + f1.shape[1]) % f1.shape[1],
indexing='ij')
f1shift = f1[X, Y]
f1h = T2FFT.analyze(f1)
f1sh = T2FFT.analyze(f1shift)
# Do a phase shift and check that it is equal to the FT of the shifted image
delta = -0.5 # we're shifting from [0, 1) to [-0.5, 0.5)
xi1 = np.arange(-np.floor(f1.shape[0] / 2.), np.ceil(f1.shape[0] / 2.))
xi2 = np.arange(-np.floor(f1.shape[1] / 2.), np.ceil(f1.shape[1] / 2.))
XI1, XI2 = np.meshgrid(xi1, xi2, indexing='ij')
phase = np.exp(-2 * np.pi * 1j * delta * (XI1 + XI2))
f1psh = f1h * phase
return f1, f1shift, f1h, f1sh, f1psh
# Shows that when the image rotates around center (p0, q0), the FT also rotates around (p0, q0) (which corresponds
# to frequency (0, 0).
f1 = np.zeros((nx, ny))
f1[p0 - 1, q0 - 1] = 1.
f1[p0, q0] = 1.
f1[p0 + 1, p0 + 1] = 1.
f1 = np.random.randn(nx, ny) + 1j * np.random.randn(nx, ny)
X, Y = np.meshgrid(np.arange(p0, p0 + f1.shape[0]) % f1.shape[0],
np.arange(q0, q0 + f1.shape[1]) % f1.shape[1],
indexing='ij')
f1shift = f1[X, Y]
f1h = T2FFT.analyze(f1)
f1sh = T2FFT.analyze(f1shift)
# Do a phase shift and check that it is equal to the FT of the shifted image
delta = -0.5 # we're shifting from [0, 1) to [-0.5, 0.5)
xi1 = np.arange(-np.floor(f1.shape[0] / 2.), np.ceil(f1.shape[0] / 2.))
xi2 = np.arange(-np.floor(f1.shape[1] / 2.), np.ceil(f1.shape[1] / 2.))
XI1, XI2 = np.meshgrid(xi1, xi2, indexing='ij')
phase = np.exp(-2 * np.pi * 1j * delta * (XI1 + XI2))
f1psh = f1h * phase
return f1, f1shift, f1h, f1sh, f1psh