RealFFTUtils_2D { public double unpack(final int r, final int c, final double[] packed, final int pos) { final int index = getIndex(r, c); if (index >= 0) { return packed[pos + index]; } else if (index > Integer.MIN_VALUE) { return -packed[pos - index]; } else { return ZERO; } } RealFFTUtils_2D(final int rows, final int columns); int getIndex(final int r, final int c); void pack(final double val, final int r, final int c,
            final double[] packed, final int pos); void pack(final double val, final int r, final int c,
            final double[][] packed); void pack(final float val, final int r, final int c,
            final float[] packed, final int pos); void pack(final float val, final int r, final int c,
            final float[][] packed); double unpack(final int r, final int c, final double[] packed,
            final int pos); double unpack(final int r, final int c, final double[][] packed); float unpack(final int r, final int c, final float[] packed,
            final int pos); float unpack(final int r, final int c, final float[][] packed);  }
@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[rows * columns]; final float[][] expected = new float[rows][2 * columns]; for (int r = 0; r < rows; r++) { for (int c = 0; c < columns; c++) { final float rnd = random.nextFloat(); actual[r * columns + c] = rnd; expected[r][2 * c] = rnd; expected[r][2 * c + 1] = 0f; } } fft2f.complexForward(expected); fft2f.realForward(actual); float exp, act; for (int r = 0; r < rows; r++) { for (int c = 0; c < 2 * columns; c++) { final String msg = String.format("[%d][%d]", r, c); exp = expected[r][c]; act = unpacker.unpack(r, c, actual, 0); checker.assertEquals(msg, exp, act); } } }
@Test public void testUnpack2dInput() { 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[rows][columns]; final double[][] expected = new double[rows][2 * columns]; for (int r = 0; r < rows; r++) { for (int c = 0; c < columns; c++) { final double rnd = random.nextDouble(); actual[r][c] = rnd; expected[r][2 * c] = rnd; expected[r][2 * c + 1] = 0d; } } fft2d.complexForward(expected); fft2d.realForward(actual); double exp, act; for (int r = 0; r < rows; r++) { for (int c = 0; c < 2 * columns; c++) { final String msg = String.format("[%d][%d]", r, c); exp = expected[r][c]; act = unpacker.unpack(r, c, actual); checker.assertEquals(msg, exp, act); exp = expected[r][c]; act = unpacker.unpack(r, c, actual); } } }
@Test public void testUnpack2fInput() { 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[rows][columns]; final float[][] expected = new float[rows][2 * columns]; for (int r = 0; r < rows; r++) { for (int c = 0; c < columns; c++) { final float rnd = random.nextFloat(); actual[r][c] = rnd; expected[r][2 * c] = rnd; expected[r][2 * c + 1] = 0f; } } fft2f.complexForward(expected); fft2f.realForward(actual); float exp, act; for (int r = 0; r < rows; r++) { for (int c = 0; c < 2 * columns; c++) { final String msg = String.format("[%d][%d]", r, c); exp = expected[r][c]; act = unpacker.unpack(r, c, actual); checker.assertEquals(msg, exp, act); exp = expected[r][c]; act = unpacker.unpack(r, c, actual); } } }
@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[rows * columns]; final double[][] expected = new double[rows][2 * columns]; for (int r = 0; r < rows; r++) { for (int c = 0; c < columns; c++) { final double rnd = random.nextDouble(); actual[r * columns + c] = rnd; expected[r][2 * c] = rnd; expected[r][2 * c + 1] = 0d; } } fft2d.complexForward(expected); fft2d.realForward(actual); double exp, act; for (int r = 0; r < rows; r++) { for (int c = 0; c < 2 * columns; c++) { final String msg = String.format("[%d][%d]", r, c); exp = expected[r][c]; act = unpacker.unpack(r, c, actual, 0); checker.assertEquals(msg, exp, act); } } }