1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.math.transform;
18
19 import org.apache.commons.math.analysis.*;
20 import org.apache.commons.math.MathException;
21 import junit.framework.TestCase;
22
23
24
25
26
27
28
29
30
31 public final class FastSineTransformerTest extends TestCase {
32
33
34
35
36 public void testAdHocData() {
37 FastSineTransformer transformer = new FastSineTransformer();
38 double result[], tolerance = 1E-12;
39
40 double x[] = { 0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0 };
41 double y[] = { 0.0, 20.1093579685034, -9.65685424949238,
42 5.98642305066196, -4.0, 2.67271455167720,
43 -1.65685424949238, 0.795649469518633 };
44
45 result = transformer.transform(x);
46 for (int i = 0; i < result.length; i++) {
47 assertEquals(y[i], result[i], tolerance);
48 }
49
50 result = transformer.inversetransform(y);
51 for (int i = 0; i < result.length; i++) {
52 assertEquals(x[i], result[i], tolerance);
53 }
54
55 FastFourierTransformer.scaleArray(x, Math.sqrt(x.length / 2.0));
56
57 result = transformer.transform2(y);
58 for (int i = 0; i < result.length; i++) {
59 assertEquals(x[i], result[i], tolerance);
60 }
61
62 result = transformer.inversetransform2(x);
63 for (int i = 0; i < result.length; i++) {
64 assertEquals(y[i], result[i], tolerance);
65 }
66 }
67
68
69
70
71 public void testSinFunction() throws MathException {
72 UnivariateRealFunction f = new SinFunction();
73 FastSineTransformer transformer = new FastSineTransformer();
74 double min, max, result[], tolerance = 1E-12; int N = 1 << 8;
75
76 min = 0.0; max = 2.0 * Math.PI;
77 result = transformer.transform(f, min, max, N);
78 assertEquals(N >> 1, result[2], tolerance);
79 for (int i = 0; i < N; i += (i == 1 ? 2 : 1)) {
80 assertEquals(0.0, result[i], tolerance);
81 }
82
83 min = -Math.PI; max = Math.PI;
84 result = transformer.transform(f, min, max, N);
85 assertEquals(-(N >> 1), result[2], tolerance);
86 for (int i = 0; i < N; i += (i == 1 ? 2 : 1)) {
87 assertEquals(0.0, result[i], tolerance);
88 }
89 }
90
91
92
93
94 public void testParameters() throws Exception {
95 UnivariateRealFunction f = new SinFunction();
96 FastSineTransformer transformer = new FastSineTransformer();
97
98 try {
99
100 transformer.transform(f, 1, -1, 64);
101 fail("Expecting IllegalArgumentException - bad interval");
102 } catch (IllegalArgumentException ex) {
103
104 }
105 try {
106
107 transformer.transform(f, -1, 1, 0);
108 fail("Expecting IllegalArgumentException - bad samples number");
109 } catch (IllegalArgumentException ex) {
110
111 }
112 try {
113
114 transformer.transform(f, -1, 1, 100);
115 fail("Expecting IllegalArgumentException - bad samples number");
116 } catch (IllegalArgumentException ex) {
117
118 }
119 }
120 }