コード例 #1
0
ファイル: test_mymath.py プロジェクト: brunohcastro/python
    def test_2d(self):
        a = array([
            [1.0, 2], 
            [3, 8]
        ])
        za = array([
            [1,   1, 0, 1], 
            [1.5, 2, 0, 2],
            [0,   0, 0, 0],
            [1.5, 2, 0, 2]
        ])
        self.assertArraysClose(za, mm.zpadf(a, 2))
        self.assertArraysClose(za, mm.zpadf(a, (2, 2)))

        za = array([
            [1,   2], 
            [1.5, 4],
            [0,   0],
            [1.5, 4]
        ])
        self.assertArraysClose(za, mm.zpadf(a, (2, 0)))

        za = array([
            [1,   1, 0, 1], 
            [3,   4, 0, 4],
        ])
        self.assertArraysClose(za, mm.zpadf(a, (0, 2)))
コード例 #2
0
    def test_2d(self):
        a = array([[1.0, 2], [3, 8]])
        za = array([[1, 1, 0, 1], [1.5, 2, 0, 2], [0, 0, 0, 0], [1.5, 2, 0,
                                                                 2]])
        self.assertArraysClose(za, mm.zpadf(a, 2))
        self.assertArraysClose(za, mm.zpadf(a, (2, 2)))

        za = array([[1, 2], [1.5, 4], [0, 0], [1.5, 4]])
        self.assertArraysClose(za, mm.zpadf(a, (2, 0)))

        za = array([
            [1, 1, 0, 1],
            [3, 4, 0, 4],
        ])
        self.assertArraysClose(za, mm.zpadf(a, (0, 2)))
コード例 #3
0
ファイル: test_mymath.py プロジェクト: brunohcastro/python
    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)
コード例 #4
0
    def test_1d(self):
        a = array([1.0, 1.0])
        za = array([1.0, 0.5, 0, 0.5])
        self.assertArraysClose(za, mm.zpadf(a, 2))
        self.assertArraysClose(za, mm.zpadf(a, (2, )))

        a = array([1.0, 2, 3])
        za = array([1, 2, 0, 0, 3])
        self.assertArraysClose(za, mm.zpadf(a, 2))

        a = array([1.0, 2, 3, 4])
        za = array([1, 2, 1.5, 0, 1.5, 4])
        self.assertArraysClose(za, mm.zpadf(a, 2))

        a = array([1.0, 2, 3, 4])
        za = array([1, 2, 1.5, 0, 0, 1.5, 4])
        self.assertArraysClose(za, mm.zpadf(a, 3))
コード例 #5
0
ファイル: test_mymath.py プロジェクト: brunohcastro/python
    def test_1d(self):
        a = array([1.0, 1.0])
        za = array([1.0, 0.5, 0, 0.5])
        self.assertArraysClose(za, mm.zpadf(a, 2))
        self.assertArraysClose(za, mm.zpadf(a, (2,)))


        a = array([1.0, 2, 3])
        za = array([1, 2, 0, 0, 3])
        self.assertArraysClose(za, mm.zpadf(a, 2))

        a = array([1.0, 2, 3, 4])
        za = array([1, 2, 1.5, 0, 1.5, 4])
        self.assertArraysClose(za, mm.zpadf(a, 2))

        a = array([1.0, 2, 3, 4])
        za = array([1, 2, 1.5, 0, 0, 1.5, 4])
        self.assertArraysClose(za, mm.zpadf(a, 3))
コード例 #6
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)
コード例 #7
0
ファイル: test_mymath.py プロジェクト: brunohcastro/python
    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)
コード例 #8
0
    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)