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.complex.*;
21 import org.apache.commons.math.MathException;
22 import junit.framework.TestCase;
23
24
25
26
27
28
29
30
31
32 public final class FastFourierTransformerTest extends TestCase {
33
34
35
36
37 public void testAdHocData() throws MathException {
38 FastFourierTransformer transformer = new FastFourierTransformer();
39 Complex result[]; double tolerance = 1E-12;
40
41 double x[] = {1.3, 2.4, 1.7, 4.1, 2.9, 1.7, 5.1, 2.7};
42 Complex y[] = {
43 new Complex(21.9, 0.0),
44 new Complex(-2.09497474683058, 1.91507575950825),
45 new Complex(-2.6, 2.7),
46 new Complex(-1.10502525316942, -4.88492424049175),
47 new Complex(0.1, 0.0),
48 new Complex(-1.10502525316942, 4.88492424049175),
49 new Complex(-2.6, -2.7),
50 new Complex(-2.09497474683058, -1.91507575950825)};
51
52 result = transformer.transform(x);
53 for (int i = 0; i < result.length; i++) {
54 assertEquals(y[i].getReal(), result[i].getReal(), tolerance);
55 assertEquals(y[i].getImaginary(), result[i].getImaginary(), tolerance);
56 }
57
58 result = transformer.inversetransform(y);
59 for (int i = 0; i < result.length; i++) {
60 assertEquals(x[i], result[i].getReal(), tolerance);
61 assertEquals(0.0, result[i].getImaginary(), tolerance);
62 }
63
64 double x2[] = {10.4, 21.6, 40.8, 13.6, 23.2, 32.8, 13.6, 19.2};
65 FastFourierTransformer.scaleArray(x2, 1.0 / Math.sqrt(x2.length));
66 Complex y2[] = y;
67
68 result = transformer.transform2(y2);
69 for (int i = 0; i < result.length; i++) {
70 assertEquals(x2[i], result[i].getReal(), tolerance);
71 assertEquals(0.0, result[i].getImaginary(), tolerance);
72 }
73
74 result = transformer.inversetransform2(x2);
75 for (int i = 0; i < result.length; i++) {
76 assertEquals(y2[i].getReal(), result[i].getReal(), tolerance);
77 assertEquals(y2[i].getImaginary(), result[i].getImaginary(), tolerance);
78 }
79 }
80
81
82
83
84 public void testSinFunction() throws MathException {
85 UnivariateRealFunction f = new SinFunction();
86 FastFourierTransformer transformer = new FastFourierTransformer();
87 Complex result[]; int N = 1 << 8;
88 double min, max, tolerance = 1E-12;
89
90 min = 0.0; max = 2.0 * Math.PI;
91 result = transformer.transform(f, min, max, N);
92 assertEquals(0.0, result[1].getReal(), tolerance);
93 assertEquals(-(N >> 1), result[1].getImaginary(), tolerance);
94 assertEquals(0.0, result[N-1].getReal(), tolerance);
95 assertEquals(N >> 1, result[N-1].getImaginary(), tolerance);
96 for (int i = 0; i < N-1; i += (i == 0 ? 2 : 1)) {
97 assertEquals(0.0, result[i].getReal(), tolerance);
98 assertEquals(0.0, result[i].getImaginary(), tolerance);
99 }
100
101 min = -Math.PI; max = Math.PI;
102 result = transformer.inversetransform(f, min, max, N);
103 assertEquals(0.0, result[1].getReal(), tolerance);
104 assertEquals(-0.5, result[1].getImaginary(), tolerance);
105 assertEquals(0.0, result[N-1].getReal(), tolerance);
106 assertEquals(0.5, result[N-1].getImaginary(), tolerance);
107 for (int i = 0; i < N-1; i += (i == 0 ? 2 : 1)) {
108 assertEquals(0.0, result[i].getReal(), tolerance);
109 assertEquals(0.0, result[i].getImaginary(), tolerance);
110 }
111 }
112
113
114
115
116 public void testParameters() throws Exception {
117 UnivariateRealFunction f = new SinFunction();
118 FastFourierTransformer transformer = new FastFourierTransformer();
119
120 try {
121
122 transformer.transform(f, 1, -1, 64);
123 fail("Expecting IllegalArgumentException - bad interval");
124 } catch (IllegalArgumentException ex) {
125
126 }
127 try {
128
129 transformer.transform(f, -1, 1, 0);
130 fail("Expecting IllegalArgumentException - bad samples number");
131 } catch (IllegalArgumentException ex) {
132
133 }
134 try {
135
136 transformer.transform(f, -1, 1, 100);
137 fail("Expecting IllegalArgumentException - bad samples number");
138 } catch (IllegalArgumentException ex) {
139
140 }
141 }
142 }