1   // Licensed to the Apache Software Foundation (ASF) under one
2   // or more contributor license agreements.  See the NOTICE file
3   // distributed with this work for additional information
4   // regarding copyright ownership.  The ASF licenses this file
5   // to you under the Apache License, Version 2.0 (the
6   // "License"); you may not use this file except in compliance
7   // with 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,
12  // software distributed under the License is distributed on an
13  // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14  // KIND, either express or implied.  See the License for the
15  // specific language governing permissions and limitations
16  // under the License.
17  
18  package org.apache.commons.math.optimization.fitting;
19  
20  import static org.junit.Assert.assertEquals;
21  import static org.junit.Assert.assertTrue;
22  
23  import java.util.Random;
24  
25  import org.apache.commons.math.optimization.OptimizationException;
26  import org.apache.commons.math.optimization.general.LevenbergMarquardtOptimizer;
27  import org.apache.commons.math.util.MathUtils;
28  import org.junit.Test;
29  
30  public class HarmonicFitterTest {
31  
32      @Test
33      public void testNoError() throws OptimizationException {
34          HarmonicFunction f = new HarmonicFunction(0.2, 3.4, 4.1);
35  
36          HarmonicFitter fitter =
37              new HarmonicFitter(new LevenbergMarquardtOptimizer());
38          for (double x = 0.0; x < 1.3; x += 0.01) {
39              fitter.addObservedPoint(1.0, x, f.value(x));
40          }
41  
42          HarmonicFunction fitted = fitter.fit();
43          assertEquals(f.getAmplitude(), fitted.getAmplitude(), 1.0e-13);
44          assertEquals(f.getPulsation(), fitted.getPulsation(), 1.0e-13);
45          assertEquals(f.getPhase(),     MathUtils.normalizeAngle(fitted.getPhase(), f.getPhase()), 1.0e-13);
46  
47          for (double x = -1.0; x < 1.0; x += 0.01) {
48              assertTrue(Math.abs(f.value(x) - fitted.value(x)) < 1.0e-13);
49          }
50  
51      }
52  
53      @Test
54      public void test1PercentError() throws OptimizationException {
55          Random randomizer = new Random(64925784252l);
56          HarmonicFunction f = new HarmonicFunction(0.2, 3.4, 4.1);
57  
58          HarmonicFitter fitter =
59              new HarmonicFitter(new LevenbergMarquardtOptimizer());
60          for (double x = 0.0; x < 10.0; x += 0.1) {
61              fitter.addObservedPoint(1.0, x,
62                                     f.value(x) + 0.01 * randomizer.nextGaussian());
63          }
64  
65          HarmonicFunction fitted = fitter.fit();
66          assertEquals(f.getAmplitude(), fitted.getAmplitude(), 7.6e-4);
67          assertEquals(f.getPulsation(), fitted.getPulsation(), 2.7e-3);
68          assertEquals(f.getPhase(),     MathUtils.normalizeAngle(fitted.getPhase(), f.getPhase()), 1.3e-2);
69  
70      }
71  
72      @Test
73      public void testInitialGuess() throws OptimizationException {
74          Random randomizer = new Random(45314242l);
75          HarmonicFunction f = new HarmonicFunction(0.2, 3.4, 4.1);
76  
77          HarmonicFitter fitter =
78              new HarmonicFitter(new LevenbergMarquardtOptimizer(), new double[] { 0.15, 3.6, 4.5 });
79          for (double x = 0.0; x < 10.0; x += 0.1) {
80              fitter.addObservedPoint(1.0, x,
81                                     f.value(x) + 0.01 * randomizer.nextGaussian());
82          }
83  
84          HarmonicFunction fitted = fitter.fit();
85          assertEquals(f.getAmplitude(), fitted.getAmplitude(), 1.2e-3);
86          assertEquals(f.getPulsation(), fitted.getPulsation(), 3.3e-3);
87          assertEquals(f.getPhase(),     MathUtils.normalizeAngle(fitted.getPhase(), f.getPhase()), 1.7e-2);
88  
89      }
90  
91      @Test
92      public void testUnsorted() throws OptimizationException {
93          Random randomizer = new Random(64925784252l);
94          HarmonicFunction f = new HarmonicFunction(0.2, 3.4, 4.1);
95  
96          HarmonicFitter fitter =
97              new HarmonicFitter(new LevenbergMarquardtOptimizer());
98  
99          // build a regularly spaced array of measurements
100         int size = 100;
101         double[] xTab = new double[size];
102         double[] yTab = new double[size];
103         for (int i = 0; i < size; ++i) {
104             xTab[i] = 0.1 * i;
105             yTab[i] = f.value(xTab[i]) + 0.01 * randomizer.nextGaussian();
106         }
107 
108         // shake it
109         for (int i = 0; i < size; ++i) {
110             int i1 = randomizer.nextInt(size);
111             int i2 = randomizer.nextInt(size);
112             double xTmp = xTab[i1];
113             double yTmp = yTab[i1];
114             xTab[i1] = xTab[i2];
115             yTab[i1] = yTab[i2];
116             xTab[i2] = xTmp;
117             yTab[i2] = yTmp;
118         }
119 
120         // pass it to the fitter
121         for (int i = 0; i < size; ++i) {
122             fitter.addObservedPoint(1.0, xTab[i], yTab[i]);
123         }
124 
125         HarmonicFunction fitted = fitter.fit();
126         assertEquals(f.getAmplitude(), fitted.getAmplitude(), 7.6e-4);
127         assertEquals(f.getPulsation(), fitted.getPulsation(), 3.5e-3);
128         assertEquals(f.getPhase(),     MathUtils.normalizeAngle(fitted.getPhase(), f.getPhase()), 1.5e-2);
129 
130     }
131 
132 }