def test_known2x2(self): # expected to fail because upsampled_idft2 doesn't handle the nyquist # component properly A = array([[1, 4], [0, 2]]) AA = fft2(A) known_out = array([[4, 2.5], [3, 1.75]]) out = mm.upsampled_idft2(AA, 2, 2, 2, 0, 2) self.assertArraysClose(known_out, out) # code comparing the ideal fourier matrix approach with proper # zero-padding, to the idft_upsampled AAA = zpadf(AA, (2, 2)) F = array([[1, 1, 1, 1],[1, 1j, -1, -1j],[1,-1,1,-1],[1,-1j,-1,1j]])/4.0 out2 = mm.dot(F, AAA, F)*4.0 out = mm.upsampled_idft2(AA, 2, 4, 4, 0, 0)
def test_known2x2(self): # expected to fail because upsampled_idft2 doesn't handle the nyquist # component properly A = array([[1, 4], [0, 2]]) AA = fft2(A) known_out = array([[4, 2.5], [3, 1.75]]) out = mm.upsampled_idft2(AA, 2, 2, 2, 0, 2) self.assertArraysClose(known_out, out) # code comparing the ideal fourier matrix approach with proper # zero-padding, to the idft_upsampled AAA = zpadf(AA, (2, 2)) F = array([[1, 1, 1, 1], [1, 1j, -1, -1j], [1, -1, 1, -1], [1, -1j, -1, 1j]]) / 4.0 out2 = mm.dot(F, AAA, F) * 4.0 out = mm.upsampled_idft2(AA, 2, 4, 4, 0, 0)
def test_full_array(self): nx = self.nx ny = self.ny upsample = self.upsample aa = rand(ny, nx) aa[:-5, :-5] += 1 aa[:-2, :] += 2 a = fft2(aa) aa, a = self.remove_nyquist(aa, a) ## calculate the slow way extra_zeros = ((upsample - 1)*ny, (upsample - 1)*nx) padded = mm.zpadf(a, extra_zeros) b_slow_big = ifft2(padded) # calculate the fast way b_fast_big = mm.upsampled_idft2(a, upsample, upsample*ny, upsample*nx, 0, 0) b_slow_big = b_slow_big/mean(abs(b_slow_big)) b_fast_big = b_fast_big/mean(abs(b_fast_big)) if PLOTTING: subplot(131) imshow(abs(b_fast_big - b_slow_big)) colorbar() subplot(132) imshow(angle(b_fast_big)) colorbar() subplot(133) imshow(angle(b_slow_big)) colorbar() show() self.assertArraysClose(abs(b_fast_big), abs(b_slow_big), rtol=1e-2)
def test_full_array(self): nx = self.nx ny = self.ny upsample = self.upsample aa = rand(ny, nx) aa[:-5, :-5] += 1 aa[:-2, :] += 2 a = fft2(aa) aa, a = self.remove_nyquist(aa, a) ## calculate the slow way extra_zeros = ((upsample - 1) * ny, (upsample - 1) * nx) padded = mm.zpadf(a, extra_zeros) b_slow_big = ifft2(padded) # calculate the fast way b_fast_big = mm.upsampled_idft2(a, upsample, upsample * ny, upsample * nx, 0, 0) b_slow_big = b_slow_big / mean(abs(b_slow_big)) b_fast_big = b_fast_big / mean(abs(b_fast_big)) if PLOTTING: subplot(131) imshow(abs(b_fast_big - b_slow_big)) colorbar() subplot(132) imshow(angle(b_fast_big)) colorbar() subplot(133) imshow(angle(b_slow_big)) colorbar() show() self.assertArraysClose(abs(b_fast_big), abs(b_slow_big), rtol=1e-2)
def test_normalization(self): """The function should be properly normalized.""" a = ones([10, 10]) aa = fft2(a) b = mm.upsampled_idft2(aa, 3, 30, 30, 0, 0) self.assertAlmostEqual(amax(a), amax(abs(b)))
def test_equivalence(self): """ The dft_upsample function should be equivalent to: 1. Embedding the array "a" in an array that is "upsample" times larger in each dimension. ifftshift to bring the center of the image to (0, 0). 2. Take the IFFT of the larger array 3. Extract an [height, width] region of the result. Starting with the [top, left] element. """ aa = self.aa a = self.a nx = self.nx ny = self.ny height = self.height width = self.width top = self.top left = self.left upsample = self.upsample ## calculate the slow way extra_zeros = ((upsample - 1)*ny, (upsample - 1)*nx) padded = mm.zpadf(a, extra_zeros) b_slow_big = ifft2(padded) b_slow = b_slow_big[ top:top + height, left:left + width, ] # calculate the fast way b_fast = mm.upsampled_idft2(a, upsample, height, width, top, left) b_fast_big = mm.upsampled_idft2(a, upsample, upsample*ny, upsample*nx, 0, 0) if PLOTTING: subplot(411) imshow(abs(b_fast_big)) subplot(412) imshow(abs(b_slow_big)) subplot(413) imshow(abs(b_fast)) subplot(414) imshow(abs(b_slow)) title('{}x{} starting at {}x{}'.format(height, width, top, left)) figure() imshow(aa) show() if PLOTTING: subplot(221) imshow(abs(b_slow)) title('slow abs') subplot(222) imshow(abs(b_fast)) title('fast abs') subplot(223) imshow(unwrap(angle(b_slow))) title('slow angle') subplot(224) imshow(unwrap(angle(b_fast))) title('fast angle') show() # are they the same (within a multiplier) b_slow = b_slow/mean(abs(b_slow)) b_fast = b_fast/mean(abs(b_fast)) self.assertArraysClose(b_slow, b_fast, rtol=1e-2)
def test_equivalence(self): """ The dft_upsample function should be equivalent to: 1. Embedding the array "a" in an array that is "upsample" times larger in each dimension. ifftshift to bring the center of the image to (0, 0). 2. Take the IFFT of the larger array 3. Extract an [height, width] region of the result. Starting with the [top, left] element. """ aa = self.aa a = self.a nx = self.nx ny = self.ny height = self.height width = self.width top = self.top left = self.left upsample = self.upsample ## calculate the slow way extra_zeros = ((upsample - 1) * ny, (upsample - 1) * nx) padded = mm.zpadf(a, extra_zeros) b_slow_big = ifft2(padded) b_slow = b_slow_big[top:top + height, left:left + width, ] # calculate the fast way b_fast = mm.upsampled_idft2(a, upsample, height, width, top, left) b_fast_big = mm.upsampled_idft2(a, upsample, upsample * ny, upsample * nx, 0, 0) if PLOTTING: subplot(411) imshow(abs(b_fast_big)) subplot(412) imshow(abs(b_slow_big)) subplot(413) imshow(abs(b_fast)) subplot(414) imshow(abs(b_slow)) title('{}x{} starting at {}x{}'.format(height, width, top, left)) figure() imshow(aa) show() if PLOTTING: subplot(221) imshow(abs(b_slow)) title('slow abs') subplot(222) imshow(abs(b_fast)) title('fast abs') subplot(223) imshow(unwrap(angle(b_slow))) title('slow angle') subplot(224) imshow(unwrap(angle(b_fast))) title('fast angle') show() # are they the same (within a multiplier) b_slow = b_slow / mean(abs(b_slow)) b_fast = b_fast / mean(abs(b_fast)) self.assertArraysClose(b_slow, b_fast, rtol=1e-2)