示例#1
0
    def test_forward_dft(self):
        # Manually build the DFT transform matrices
        # according to https://en.wikipedia.org/wiki/Discrete_Fourier_transform

        idx_freq_0 = np.arange(self.array_shape_oc[0]).reshape((-1, 1))
        idx_freq_1 = np.arange(self.array_shape_oc[1]).reshape((-1, 1))
        idx_space_0 = np.arange(self.array_shape_oc[0]).reshape((1, -1))
        idx_space_1 = np.arange(self.array_shape_oc[1]).reshape((1, -1))

        coeff_mtx_0 = -2 * np.pi * 1j * idx_freq_0.dot(
            idx_space_0 / self.array_shape_oc[0])
        dft_mtx_0 = np.sqrt(1 / self.array_shape_oc[0]) * np.exp(coeff_mtx_0)

        coeff_mtx_1 = -2 * np.pi * 1j * idx_freq_1.dot(
            idx_space_1 / self.array_shape_oc[1])
        dft_mtx_1 = np.sqrt(1 / self.array_shape_oc[1]) * np.exp(coeff_mtx_1)

        # Compute the reference DFT transform
        reference = dft_mtx_0.dot(
            np.pad(self.d2_array,
                   ((0, self.array_shape_oc[0] - self.array_shape[0]), (0, 0)),
                   'constant')).T
        reference = dft_mtx_1.dot(
            np.pad(reference,
                   ((0, self.array_shape_oc[1] - self.array_shape[1]), (0, 0)),
                   'constant')).T

        # Compute the DFT transform by the tested function
        dft_mtx = get_DFT(self.array_shape, self.array_shape_oc)
        d2_array_dft = vec2mat(dft_mtx.conj().T.dot(mat2vec(self.d2_array)),
                               self.array_shape_oc)

        # Is the result identical to the reference?
        self.assertTrue(np.allclose(reference, d2_array_dft))
示例#2
0
    def test_roundtrip_dft(self):
        # Test DFT
        dft_mtx = get_DFT(self.array_shape, self.array_shape_oc)
        d2_array_dft = dft_mtx.conj().T.dot(mat2vec(self.d2_array))
        d2_array_roundtrip = vec2mat(dft_mtx.dot(d2_array_dft),
                                     self.array_shape)

        # Does the DFT array have the expected shape?
        self.assertSequenceEqual(
            d2_array_dft.shape,
            (self.array_shape_oc[0] * self.array_shape_oc[1], 1))
        # Is the result identical to the original?
        self.assertTrue(np.allclose(self.d2_array, d2_array_roundtrip))
示例#3
0
    def test_forward_dct(self):
        # Manually build the DCT-II transform matrices
        # according to the documentation for scipy.fftpack.dct

        idx_freq_0 = np.arange(self.array_shape_oc[0]).reshape((-1, 1))
        idx_freq_1 = np.arange(self.array_shape_oc[1]).reshape((-1, 1))
        idx_space_0 = np.arange(self.array_shape_oc[0]).reshape((1, -1))
        idx_space_1 = np.arange(self.array_shape_oc[1]).reshape((1, -1))

        coeff_mtx_0 = np.pi * idx_freq_0.dot(
            (2 * idx_space_0 + 1) / (2 * self.array_shape_oc[0]))
        dct_mtx_0 = 2 * np.cos(coeff_mtx_0)
        # Due to norm='ortho'
        dct_mtx_0[0, :] = (np.sqrt(1 / (4 * self.array_shape_oc[0])) *
                           dct_mtx_0[0, :])
        dct_mtx_0[1:, :] = (np.sqrt(1 / (2 * self.array_shape_oc[0])) *
                            dct_mtx_0[1:, :])

        coeff_mtx_1 = np.pi * idx_freq_1.dot(
            (2 * idx_space_1 + 1) / (2 * self.array_shape_oc[1]))
        dct_mtx_1 = 2 * np.cos(coeff_mtx_1)
        # Due to norm='ortho'
        dct_mtx_1[0, :] = (np.sqrt(1 / (4 * self.array_shape_oc[1])) *
                           dct_mtx_1[0, :])
        dct_mtx_1[1:, :] = (np.sqrt(1 / (2 * self.array_shape_oc[1])) *
                            dct_mtx_1[1:, :])

        # Compute the reference DCT transform
        reference = dct_mtx_0.dot(
            np.pad(self.d2_array,
                   ((0, self.array_shape_oc[0] - self.array_shape[0]), (0, 0)),
                   'constant')).T
        reference = dct_mtx_1.dot(
            np.pad(reference,
                   ((0, self.array_shape_oc[1] - self.array_shape[1]), (0, 0)),
                   'constant')).T

        # Compute the DCT transform by the tested function
        dct_mtx = get_DCT(self.array_shape, self.array_shape_oc)
        d2_array_dct = vec2mat(dct_mtx.T.dot(mat2vec(self.d2_array)),
                               self.array_shape_oc)

        # Is the result identical to the reference?
        self.assertTrue(np.allclose(reference, d2_array_dct))