Exemplo n.º 1
0
 def test_wrong_size_dct(self):
     # Check that the function does not allow specifying an
     # _under_-complete transform
     with self.assertRaises(ValueError):
         matrix = get_DCT(
             self.array_shape,
             (self.array_shape[0] - 1, self.array_shape[1] - 1))
Exemplo n.º 2
0
    def test_roundtrip_dct(self):
        # Test DCT
        dct_mtx = get_DCT(self.array_shape, self.array_shape_oc)
        d2_array_dct = dct_mtx.T.dot(mat2vec(self.d2_array))
        d2_array_roundtrip = vec2mat(dct_mtx.dot(d2_array_dct),
                                     self.array_shape)

        # Does the DCT array have the expected shape?
        self.assertSequenceEqual(
            d2_array_dct.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))
Exemplo n.º 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))