KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > JSci > tests > FourierTest


1 package JSci.tests;
2
3 import java.util.Map JavaDoc;
4 import java.util.HashMap JavaDoc;
5 import junit.framework.*;
6 import JSci.maths.*;
7
8 /**
9 * Testcase for Fourier methods.
10 * @author Mark Hale
11 */

12 public class FourierTest extends TestCase {
13     private static final int N=128;
14     private double[] signal;
15
16     public static void main(String JavaDoc arg[]) {
17                 junit.textui.TestRunner.run(suite());
18         }
19     public static Test suite() {
20         double[][] signals = new double[][] {
21             gaussian(N, 1.0, 8.0),
22             topHat(N, 1.0),
23             constant(N, 1.0),
24             square(N, 1.0),
25             triangle(N, 1.0),
26             sine(N, 1.0, 16)
27         };
28         TestSuite suite = new TestSuite(FourierTest.class.toString());
29         for(int i=0; i<signals.length; i++) {
30             Map JavaDoc properties = new HashMap JavaDoc();
31             properties.put("test.signal", signals[i]);
32             suite.addTest(new TestProperties(new TestSuite(FourierTest.class), properties));
33         }
34         return suite;
35     }
36         public FourierTest(String JavaDoc name) {
37                 super(name);
38         }
39         protected void setUp() throws Exception JavaDoc {
40                 JSci.GlobalSettings.ZERO_TOL=1.0e-10;
41         signal = (double[]) TestProperties.getProperties().get("test.signal");
42         super.setUp();
43         }
44     /** Tests invFFT(f) = (FFT(f*))* */
45     public void testInverseTransform() {
46         Complex[] inverse = FourierMath.inverseTransform(signal);
47         Complex[] transform = FourierMath.transform(signal);
48         for(int i=0; i<N; i++) {
49             Complex result = transform[i].conjugate().divide(N);
50             assertEquals("Real", inverse[i].real(), result.real(), JSci.GlobalSettings.ZERO_TOL);
51             assertEquals("Imag", inverse[i].imag(), result.imag(), JSci.GlobalSettings.ZERO_TOL);
52         }
53     }
54     public void testTransform() {
55         Complex[] transform = FourierMath.transform(signal);
56         Complex[] result = FourierMath.inverseTransform(transform);
57         for(int i=0; i<N; i++) {
58             assertEquals("Real", signal[i], result[i].real(), JSci.GlobalSettings.ZERO_TOL);
59             assertEquals("Imag", 0.0, result[i].imag(), JSci.GlobalSettings.ZERO_TOL);
60         }
61     }
62     public void testParsevalTheorem() {
63         Complex[] transform = FourierMath.transform(signal);
64         assertEquals(N*ArrayMath.sumSquares(signal), ArrayMath.sumModSqrs(transform), JSci.GlobalSettings.ZERO_TOL);
65         }
66
67 // A selection of test signals
68

69         /**
70         * Under transform should give something like exp(-x^2).
71         * Real spectrum.
72         */

73         public static double[] gaussian(int n, double amplitude, double k) {
74                 double data[]=new double[n];
75                 double x;
76                 for(int i=0;i<n;i++) {
77                         x=(i-n/2)/k;
78                         data[i]=amplitude*Math.exp(-x*x);
79                 }
80                 return data;
81         }
82         /**
83         * Under transform should give something like cos(x)/x.
84         * Real spectrum.
85         */

86         public static double[] topHat(int n, double amplitude) {
87                 double data[]=new double[n];
88                 int i=0;
89                 for(;i<n/4;i++)
90                         data[i]=0.0;
91                 for(;i<3*n/4;i++)
92                         data[i]=amplitude;
93                 for(;i<n;i++)
94                         data[i]=0.0;
95                 return data;
96         }
97         /**
98         * Under transform should give a delta-function at origin.
99         * Real spectrum.
100         */

101         public static double[] constant(int n, double amplitude) {
102                 double data[]=new double[n];
103                 for(int i=0;i<n;i++)
104                         data[i]=amplitude;
105                 return data;
106         }
107         /**
108         * Under transform should give something like i*sin(x)/x.
109         * Complex spectrum.
110         */

111         public static double[] square(int n, double amplitude) {
112                 double data[]=new double[n];
113                 int i=0;
114                 for(;i<n/2;i++)
115                         data[i]=-amplitude;
116                 for(;i<n;i++)
117                         data[i]=amplitude;
118                 return data;
119         }
120         /**
121         * Under transform should give something like i*sin(x)/x^2.
122         * Complex spectrum.
123         */

124         public static double[] triangle(int n, double amplitude) {
125                 double data[]=new double[n];
126                 double gradient=amplitude*4.0/n;
127                 int i=0;
128                 for(;i<n/4;i++)
129                         data[i]=-gradient*i;
130                 for(;i<3*n/4;i++)
131                         data[i]=-2.0*amplitude+gradient*i;
132                 for(;i<n;i++)
133                         data[i]=4.0*amplitude-gradient*i;
134                 return data;
135         }
136         /**
137         * Under transform should give two delta-functions at +/- frequency.
138         * Complex spectrum.
139         */

140         public static double[] sine(int n, double amplitude, int cycles) {
141                 double data[]=new double[n];
142                 double w=NumericalConstants.TWO_PI*cycles/n;
143                 for(int i=0;i<n;i++)
144                         data[i]=amplitude*Math.sin((i-n/2)*w);
145                 return data;
146         }
147 }
148
Popular Tags