Beispiel #1
0
 def test_wrong_size_dft(self):
     # Check that the function does not allow specifying an
     # _under_-complete transform
     with self.assertRaises(ValueError):
         matrix = get_DFT(
             self.array_shape,
             (self.array_shape[0] - 1, self.array_shape[1] - 1))
Beispiel #2
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))
Beispiel #3
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))