1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
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   * Testcase for fast Fourier transformer.
26   * <p>
27   * FFT algorithm is exact, the small tolerance number is used only
28   * to account for round-off errors.
29   * 
30   * @version $Revision$ $Date$ 
31   */
32  public final class FastFourierTransformerTest extends TestCase {
33  
34      /**
35       * Test of transformer for the ad hoc data taken from Mathematica.
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       * Test of transformer for the sine function.
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      * Test of parameters for the transformer.
115      */
116     public void testParameters() throws Exception {
117         UnivariateRealFunction f = new SinFunction();
118         FastFourierTransformer transformer = new FastFourierTransformer();
119 
120         try {
121             // bad interval
122             transformer.transform(f, 1, -1, 64);
123             fail("Expecting IllegalArgumentException - bad interval");
124         } catch (IllegalArgumentException ex) {
125             // expected
126         }
127         try {
128             // bad samples number
129             transformer.transform(f, -1, 1, 0);
130             fail("Expecting IllegalArgumentException - bad samples number");
131         } catch (IllegalArgumentException ex) {
132             // expected
133         }
134         try {
135             // bad samples number
136             transformer.transform(f, -1, 1, 100);
137             fail("Expecting IllegalArgumentException - bad samples number");
138         } catch (IllegalArgumentException ex) {
139             // expected
140         }
141     }
142 }