content: /*  ContourPolynomialFitterTest.java
  
      Copyright (c) 2009-2011 Andrew Rosenberg
  
      This file is part of the AuToBI prosodic analysis package.
  
      AuToBI is free software: you can redistribute it and/or modify
      it under the terms of the GNU General Public License as published by
      the Free Software Foundation, either version 3 of the License, or
      (at your option) any later version.
  
      AuToBI is distributed in the hope that it will be useful,
      but WITHOUT ANY WARRANTY; without even the implied warranty of
      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      GNU General Public License for more details.
  
      You should have received a copy of the GNU General Public License
      along with AuToBI.  If not, see <http://www.gnu.org/licenses/>.
   */
  
  package edu.cuny.qc.speech.AuToBI.featureextractor.shapemodeling;
  
  import edu.cuny.qc.speech.AuToBI.core.Contour;
  import org.junit.Test;
  
  import static junit.framework.Assert.assertEquals;
  
  /**
   * Test class for ContourPolynomialFitter.
   *
   * @see ContourPolynomialFitter
   */
  public class ContourPolynomialFitterTest {
  
    @Test
    public void testFitterFitsALine() {
      Contour c = new Contour(0, 1, 4);
      c.set(0, 0);
      c.set(1, 1);
      c.set(2, 2);
      c.set(3, 3);
  
      ContourPolynomialFitter fitter = new ContourPolynomialFitter(1);
      double[] results = fitter.fitContour(c);
  
      assertEquals(2, results.length);
      assertEquals(0.0, results[0], 0.00001);
      assertEquals(1.0, results[1], 0.00001);
    }
  
    @Test
    public void testFitterFitsAParabola() {
      Contour c = new Contour(1, 1, 4);
  
      // generated by 1 - 2x + 3x^2
      c.set(0, 2);
      c.set(1, 9);
      c.set(2, 22);
      c.set(3, 41);
  
      ContourPolynomialFitter fitter = new ContourPolynomialFitter(2);
      double[] results = fitter.fitContour(c);
  
      assertEquals(3, results.length);
      assertEquals(1.0, results[0], 0.00001);
      assertEquals(-2.0, results[1], 0.00001);
      assertEquals(3.0, results[2], 0.00001);
    }
  
    @Test
    public void testFitterFitsAParabolaWithEmptyEntries() {
      Contour c = new Contour(1, 1, 4);
  
      // generated by 1 - 2x + 3x^2
      c.set(0, 2);
      c.set(1, 9);
      c.set(2, 999999);  // shouldn't matter, since index 2 is going to be empty
      c.setEmpty(2);
      c.set(3, 41);
  
      ContourPolynomialFitter fitter = new ContourPolynomialFitter(2);
      double[] results = fitter.fitContour(c);
  
      assertEquals(3, results.length);
      assertEquals(1.0, results[0], 0.00001);
      assertEquals(-2.0, results[1], 0.00001);
      assertEquals(3.0, results[2], 0.00001);
    }
  
    @Test
    public void testFitterGetsMSE() {
      Contour c = new Contour(1, 1, 5);
  
      c.set(0, 1);
      c.set(1, 2);
      c.set(2, 2);
      c.set(3, 3);
      c.set(4, 3);
  
      ContourPolynomialFitter fitter = new ContourPolynomialFitter(2);
      double[] w = fitter.fitContour(c);
      double mse = fitter.getMSE(c, w);
  
      assertEquals(0.22857142857142837, mse, 0.00001);
    }
  
    @Test
    public void testFitterGetsMSEWithEmptyEntries() {
  
      Contour c = new Contour(1, 1, 6);
  
      c.set(0, 1);
      c.set(1, 2);
      c.set(2, 2);
      c.set(3, 3);
      c.set(4, 3);
      c.set(5, 1000000);
      c.setEmpty(5);
  
      ContourPolynomialFitter fitter = new ContourPolynomialFitter(2);
      double[] w = fitter.fitContour(c);
      double mse = fitter.getMSE(c, w);
  
      assertEquals(0.22857142857142837, mse, 0.00001);
    }
  }
  