FastSineTransformer implements RealTransformer, Serializable { public double[] transform(final double[] f, final TransformType type) { if (normalization == DstNormalization.ORTHOGONAL_DST_I) { final double s = FastMath.sqrt(2.0 / f.length); return TransformUtils.scaleArray(fst(f), s); } if (type == TransformType.FORWARD) { return fst(f); } final double s = 2.0 / f.length; return TransformUtils.scaleArray(fst(f), s); } FastSineTransformer(final DstNormalization normalization); double[] transform(final double[] f, final TransformType type); double[] transform(final UnivariateFunction f,
        final double min, final double max, final int n,
        final TransformType type);  }
@Test public void testTransformRealFirstElementNotZero() { final TransformType[] type = TransformType.values(); final double[] data = new double[] { 1.0, 1.0, 1.0, 1.0 }; final RealTransformer transformer = createRealTransformer(); for (int j = 0; j < type.length; j++) { try { transformer.transform(data, type[j]); Assert.fail(type[j].toString()); } catch (MathIllegalArgumentException e) { } } }
@Test public void testAdHocData() { FastSineTransformer transformer; transformer = new FastSineTransformer(DstNormalization.STANDARD_DST_I); double result[], tolerance = 1E-12; double x[] = { 0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0 }; double y[] = { 0.0, 20.1093579685034, -9.65685424949238, 5.98642305066196, -4.0, 2.67271455167720, -1.65685424949238, 0.795649469518633 }; result = transformer.transform(x, TransformType.FORWARD); for (int i = 0; i < result.length; i++) { Assert.assertEquals(y[i], result[i], tolerance); } result = transformer.transform(y, TransformType.INVERSE); for (int i = 0; i < result.length; i++) { Assert.assertEquals(x[i], result[i], tolerance); } TransformUtils.scaleArray(x, FastMath.sqrt(x.length / 2.0)); transformer = new FastSineTransformer(DstNormalization.ORTHOGONAL_DST_I); result = transformer.transform(y, TransformType.FORWARD); for (int i = 0; i < result.length; i++) { Assert.assertEquals(x[i], result[i], tolerance); } result = transformer.transform(x, TransformType.INVERSE); for (int i = 0; i < result.length; i++) { Assert.assertEquals(y[i], result[i], tolerance); } }
@Test public void testSinFunction() { UnivariateFunction f = new Sin(); FastSineTransformer transformer; transformer = new FastSineTransformer(DstNormalization.STANDARD_DST_I); double min, max, result[], tolerance = 1E-12; int N = 1 << 8; min = 0.0; max = 2.0 * FastMath.PI; result = transformer.transform(f, min, max, N, TransformType.FORWARD); Assert.assertEquals(N >> 1, result[2], tolerance); for (int i = 0; i < N; i += (i == 1 ? 2 : 1)) { Assert.assertEquals(0.0, result[i], tolerance); } min = -FastMath.PI; max = FastMath.PI; result = transformer.transform(f, min, max, N, TransformType.FORWARD); Assert.assertEquals(-(N >> 1), result[2], tolerance); for (int i = 0; i < N; i += (i == 1 ? 2 : 1)) { Assert.assertEquals(0.0, result[i], tolerance); } }
@Test public void testParameters() throws Exception { UnivariateFunction f = new Sin(); FastSineTransformer transformer; transformer = new FastSineTransformer(DstNormalization.STANDARD_DST_I); try { transformer.transform(f, 1, -1, 64, TransformType.FORWARD); Assert.fail("Expecting IllegalArgumentException - bad interval"); } catch (IllegalArgumentException ex) { } try { transformer.transform(f, -1, 1, 0, TransformType.FORWARD); Assert.fail("Expecting IllegalArgumentException - bad samples number"); } catch (IllegalArgumentException ex) { } try { transformer.transform(f, -1, 1, 100, TransformType.FORWARD); Assert.fail("Expecting IllegalArgumentException - bad samples number"); } catch (IllegalArgumentException ex) { } }