RealFFTUtils_3D { public double unpack(final int s, final int r, final int c, final double[] packed, final int pos) { final int i = getIndex(s, r, c); if (i >= 0) { return packed[pos + i]; } else if (i > Integer.MIN_VALUE) { return -packed[pos - i]; } else { return ZERO; } } RealFFTUtils_3D(final int slices, final int rows, final int columns); int getIndex(final int s, final int r, final int c); void pack(final double val, final int s, final int r, final int c,
            final double[] packed, final int pos); void pack(final double val, final int s, final int r, final int c,
            final double[][][] packed); void pack(final float val, final int s, final int r, final int c,
            final float[] packed, final int pos); void pack(final float val, final int s, final int r, final int c,
            final float[][][] packed); double unpack(final int s, final int r, final int c,
            final double[] packed, final int pos); double unpack(final int s, final int r, final int c,
            final double[][][] packed); float unpack(final int s, final int r, final int c,
            final float[] packed, final int pos); float unpack(final int s, final int r, final int c,
            final float[][][] packed);  }
@Test public void testUnpack1dInput() { final double rel = 1E-8; final double x0 = 5E-3; final double abs = rel * x0; final FloatingPointEqualityChecker checker = createEqualityChecker(rel, abs); final double[] actual = new double[slices * rows * columns]; final double[][][] expected = new double[slices][rows][2 * columns]; for (int s = 0; s < slices; s++) { for (int r = 0; r < rows; r++) { for (int c = 0; c < columns; c++) { final double rnd = random.nextDouble(); actual[(s * rows + r) * columns + c] = rnd; expected[s][r][2 * c] = rnd; expected[s][r][2 * c + 1] = 0d; } } } fft3d.complexForward(expected); fft3d.realForward(actual); double exp, act; for (int s = 0; s < slices; s++) { for (int r = 0; r < rows; r++) { for (int c = 0; c < 2 * columns; c++) { final String msg = String.format("[%d][%d][%d]", s, r, c); exp = expected[s][r][c]; act = unpacker.unpack(s, r, c, actual, 0); checker.assertEquals(msg, exp, act); } } } }
@Test public void testUnpack1fInput() { final float rel = 1E-1f; final float x0 = 5E-3f; final float abs = rel * x0; final FloatingPointEqualityChecker checker = createEqualityChecker(rel, abs); final float[] actual = new float[slices * rows * columns]; final float[][][] expected = new float[slices][rows][2 * columns]; for (int s = 0; s < slices; s++) { for (int r = 0; r < rows; r++) { for (int c = 0; c < columns; c++) { final float rnd = random.nextFloat(); actual[(s * rows + r) * columns + c] = rnd; expected[s][r][2 * c] = rnd; expected[s][r][2 * c + 1] = 0f; } } } fft3f.complexForward(expected); fft3f.realForward(actual); float exp, act; for (int s = 0; s < slices; s++) { for (int r = 0; r < rows; r++) { for (int c = 0; c < 2 * columns; c++) { final String msg = String.format("[%d][%d][%d]", s, r, c); exp = expected[s][r][c]; act = unpacker.unpack(s, r, c, actual, 0); checker.assertEquals(msg, exp, act); } } } }
@Test public void testUnpack3dInput() { final double rel = 1E-8; final double x0 = 5E-3; final double abs = rel * x0; final FloatingPointEqualityChecker checker = createEqualityChecker(rel, abs); final double[][][] actual = new double[slices][rows][columns]; final double[][][] expected = new double[slices][rows][2 * columns]; for (int s = 0; s < slices; s++) { for (int r = 0; r < rows; r++) { for (int c = 0; c < columns; c++) { final double rnd = random.nextDouble(); actual[s][r][c] = rnd; expected[s][r][2 * c] = rnd; expected[s][r][2 * c + 1] = 0d; } } } fft3d.complexForward(expected); fft3d.realForward(actual); double exp, act; for (int s = 0; s < slices; s++) { for (int r = 0; r < rows; r++) { for (int c = 0; c < 2 * columns; c++) { final String msg = String.format("[%d][%d][%d]", s, r, c); exp = expected[s][r][c]; act = unpacker.unpack(s, r, c, actual); checker.assertEquals(msg, exp, act); } } } }
@Test public void testUnpack3fInput() { final float rel = 1E-1f; final float x0 = 5E-3f; final float abs = rel * x0; final FloatingPointEqualityChecker checker = createEqualityChecker(rel, abs); final float[][][] actual = new float[slices][rows][columns]; final float[][][] expected = new float[slices][rows][2 * columns]; for (int s = 0; s < slices; s++) { for (int r = 0; r < rows; r++) { for (int c = 0; c < columns; c++) { final float rnd = random.nextFloat(); actual[s][r][c] = rnd; expected[s][r][2 * c] = rnd; expected[s][r][2 * c + 1] = 0f; } } } fft3f.complexForward(expected); fft3f.realForward(actual); float exp, act; for (int s = 0; s < slices; s++) { for (int r = 0; r < rows; r++) { for (int c = 0; c < 2 * columns; c++) { final String msg = String.format("[%d][%d][%d]", s, r, c); exp = expected[s][r][c]; act = unpacker.unpack(s, r, c, actual); checker.assertEquals(msg, exp, act); } } } }