From 2495e2479fb00674a8ad78ea79e10ac2c952f2a7 Mon Sep 17 00:00:00 2001 From: Benoit Steiner Date: Thu, 22 Oct 2015 16:52:55 -0700 Subject: Added tests for the fft code --- unsupported/test/cxx11_tensor_ifft.cpp | 154 +++++++++++++++++++++++++++++++++ 1 file changed, 154 insertions(+) create mode 100644 unsupported/test/cxx11_tensor_ifft.cpp (limited to 'unsupported/test/cxx11_tensor_ifft.cpp') diff --git a/unsupported/test/cxx11_tensor_ifft.cpp b/unsupported/test/cxx11_tensor_ifft.cpp new file mode 100644 index 000000000..5fd88fa6c --- /dev/null +++ b/unsupported/test/cxx11_tensor_ifft.cpp @@ -0,0 +1,154 @@ +// This file is part of Eigen, a lightweight C++ template library +// for linear algebra. +// +// Copyright (C) 2014 Jianwei Cui +// +// This Source Code Form is subject to the terms of the Mozilla +// Public License v. 2.0. If a copy of the MPL was not distributed +// with this file, You can obtain one at http://mozilla.org/MPL/2.0/. + +#include "main.h" +#include +#include +#include + +using Eigen::Tensor; + +template +static void test_1D_fft_ifft_invariant(int sequence_length) { + Tensor tensor(sequence_length); + tensor.setRandom(); + + array fft; + fft[0] = 0; + + Tensor, 1, DataLayout> tensor_after_fft; + Tensor, 1, DataLayout> tensor_after_fft_ifft; + + tensor_after_fft = tensor.template fft(fft); + tensor_after_fft_ifft = tensor_after_fft.template fft(fft); + + VERIFY_IS_EQUAL(tensor_after_fft.dimension(0), sequence_length); + VERIFY_IS_EQUAL(tensor_after_fft_ifft.dimension(0), sequence_length); + + for (int i = 0; i < sequence_length; ++i) { + VERIFY_IS_APPROX(static_cast(tensor(i)), static_cast(std::real(tensor_after_fft_ifft(i)))); + } +} + +template +static void test_2D_fft_ifft_invariant(int dim0, int dim1) { + Tensor tensor(dim0, dim1); + tensor.setRandom(); + + array fft; + fft[0] = 0; + fft[1] = 1; + + Tensor, 2, DataLayout> tensor_after_fft; + Tensor, 2, DataLayout> tensor_after_fft_ifft; + + tensor_after_fft = tensor.template fft(fft); + tensor_after_fft_ifft = tensor_after_fft.template fft(fft); + + VERIFY_IS_EQUAL(tensor_after_fft.dimension(0), dim0); + VERIFY_IS_EQUAL(tensor_after_fft.dimension(1), dim1); + VERIFY_IS_EQUAL(tensor_after_fft_ifft.dimension(0), dim0); + VERIFY_IS_EQUAL(tensor_after_fft_ifft.dimension(1), dim1); + + for (int i = 0; i < dim0; ++i) { + for (int j = 0; j < dim1; ++j) { + //std::cout << "[" << i << "][" << j << "]" << " Original data: " << tensor(i,j) << " Transformed data:" << tensor_after_fft_ifft(i,j) << std::endl; + VERIFY_IS_APPROX(static_cast(tensor(i,j)), static_cast(std::real(tensor_after_fft_ifft(i,j)))); + } + } +} + +template +static void test_3D_fft_ifft_invariant(int dim0, int dim1, int dim2) { + Tensor tensor(dim0, dim1, dim2); + tensor.setRandom(); + + array fft; + fft[0] = 0; + fft[1] = 1; + fft[2] = 2; + + Tensor, 3, DataLayout> tensor_after_fft; + Tensor, 3, DataLayout> tensor_after_fft_ifft; + + tensor_after_fft = tensor.template fft(fft); + tensor_after_fft_ifft = tensor_after_fft.template fft(fft); + + VERIFY_IS_EQUAL(tensor_after_fft.dimension(0), dim0); + VERIFY_IS_EQUAL(tensor_after_fft.dimension(1), dim1); + VERIFY_IS_EQUAL(tensor_after_fft.dimension(2), dim2); + VERIFY_IS_EQUAL(tensor_after_fft_ifft.dimension(0), dim0); + VERIFY_IS_EQUAL(tensor_after_fft_ifft.dimension(1), dim1); + VERIFY_IS_EQUAL(tensor_after_fft_ifft.dimension(2), dim2); + + for (int i = 0; i < dim0; ++i) { + for (int j = 0; j < dim1; ++j) { + for (int k = 0; k < dim2; ++k) { + VERIFY_IS_APPROX(static_cast(tensor(i,j,k)), static_cast(std::real(tensor_after_fft_ifft(i,j,k)))); + } + } + } +} + +template +static void test_sub_fft_ifft_invariant(int dim0, int dim1, int dim2, int dim3) { + Tensor tensor(dim0, dim1, dim2, dim3); + tensor.setRandom(); + + array fft; + fft[0] = 2; + fft[1] = 0; + + Tensor, 4, DataLayout> tensor_after_fft; + Tensor tensor_after_fft_ifft; + + tensor_after_fft = tensor.template fft(fft); + tensor_after_fft_ifft = tensor_after_fft.template fft(fft); + + VERIFY_IS_EQUAL(tensor_after_fft.dimension(0), dim0); + VERIFY_IS_EQUAL(tensor_after_fft.dimension(1), dim1); + VERIFY_IS_EQUAL(tensor_after_fft.dimension(2), dim2); + VERIFY_IS_EQUAL(tensor_after_fft.dimension(3), dim3); + VERIFY_IS_EQUAL(tensor_after_fft_ifft.dimension(0), dim0); + VERIFY_IS_EQUAL(tensor_after_fft_ifft.dimension(1), dim1); + VERIFY_IS_EQUAL(tensor_after_fft_ifft.dimension(2), dim2); + VERIFY_IS_EQUAL(tensor_after_fft_ifft.dimension(3), dim3); + + for (int i = 0; i < dim0; ++i) { + for (int j = 0; j < dim1; ++j) { + for (int k = 0; k < dim2; ++k) { + for (int l = 0; l < dim3; ++l) { + VERIFY_IS_APPROX(static_cast(tensor(i,j,k,l)), static_cast(tensor_after_fft_ifft(i,j,k,l))); + } + } + } + } +} + +void test_cxx11_tensor_ifft() { + CALL_SUBTEST(test_1D_fft_ifft_invariant(4)); + CALL_SUBTEST(test_1D_fft_ifft_invariant(16)); + CALL_SUBTEST(test_1D_fft_ifft_invariant(32)); + CALL_SUBTEST(test_1D_fft_ifft_invariant(1024*1024)); + + CALL_SUBTEST(test_2D_fft_ifft_invariant(4,4)); + CALL_SUBTEST(test_2D_fft_ifft_invariant(8,16)); + CALL_SUBTEST(test_2D_fft_ifft_invariant(16,32)); + CALL_SUBTEST(test_2D_fft_ifft_invariant(1024,1024)); + + CALL_SUBTEST(test_3D_fft_ifft_invariant(4,4,4)); + CALL_SUBTEST(test_3D_fft_ifft_invariant(8,16,32)); + CALL_SUBTEST(test_3D_fft_ifft_invariant(16,4,8)); + CALL_SUBTEST(test_3D_fft_ifft_invariant(256,256,256)); + + CALL_SUBTEST(test_sub_fft_ifft_invariant(4,4,4,4)); + CALL_SUBTEST(test_sub_fft_ifft_invariant(8,16,32,64)); + CALL_SUBTEST(test_sub_fft_ifft_invariant(16,4,8,12)); + CALL_SUBTEST(test_sub_fft_ifft_invariant(64,64,64,64)); +} -- cgit v1.2.3