コード例 #1
0
  /**
   * Fit an harmonic function to the observed points.
   *
   * @return harmonic function best fitting the observed points
   * @throws OptimizationException if the sample is too short or if the first guess cannot be
   *     computed
   */
  public HarmonicFunction fit() throws OptimizationException {

    // shall we compute the first guess of the parameters ourselves ?
    if (parameters == null) {
      final WeightedObservedPoint[] observations = fitter.getObservations();
      if (observations.length < 4) {
        throw new OptimizationException(
            LocalizedFormats.INSUFFICIENT_OBSERVED_POINTS_IN_SAMPLE, observations.length, 4);
      }

      HarmonicCoefficientsGuesser guesser = new HarmonicCoefficientsGuesser(observations);
      guesser.guess();
      parameters =
          new double[] {
            guesser.getGuessedAmplitude(), guesser.getGuessedPulsation(), guesser.getGuessedPhase()
          };
    }

    try {
      double[] fitted = fitter.fit(new ParametricHarmonicFunction(), parameters);
      return new HarmonicFunction(fitted[0], fitted[1], fitted[2]);
    } catch (FunctionEvaluationException fee) {
      // should never happen
      throw new RuntimeException(fee);
    }
  }