org.apache.commons.math.optimization.fitting

## Class HarmonicFitter

- java.lang.Object
- - org.apache.commons.math.optimization.fitting.HarmonicFitter

    public class HarmonicFitter
    extends Object

This class implements a curve fitting specialized for sinusoids.

Harmonic fitting is a very simple case of curve fitting. The estimated coefficients are the amplitude a, the pulsation ω and the phase φ: `f (t) = a cos (ω t + φ)`. They are searched by a least square estimator initialized with a rough guess based on integrals.

Since:
  2.0
Version:
  $Revision: 1073158 $ $Date: 2011-02-21 22:46:52 +0100 (lun. 21 févr. 2011) $

### Nested Class Summary

Nested Classes
`private static class` `HarmonicFitter.ParametricHarmonicFunction` Parametric harmonic function.

### Field Summary

Fields
`private CurveFitter` `fitter` Fitter for the coefficients.
`private double[]` `parameters` Values for amplitude, pulsation ω and phase φ.

### Constructor Summary

Constructors
Constructor and Description
`HarmonicFitter(DifferentiableMultivariateVectorialOptimizer optimizer)` Simple constructor.
`HarmonicFitter(DifferentiableMultivariateVectorialOptimizer optimizer, double[] initialGuess)` Simple constructor.

### Method Summary

Methods
`void` `addObservedPoint(double weight, double x, double y)` Add an observed weighted (x,y) point to the sample.
`HarmonicFunction` `fit()` Fit an harmonic function to the observed points.

### Methods inherited from class java.lang.Object

`clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

### Field Detail

#### fitter

    private final CurveFitter fitter

Fitter for the coefficients.

#### parameters

    private double[] parameters

Values for amplitude, pulsation ω and phase φ.

### Constructor Detail

#### HarmonicFitter

    public HarmonicFitter(DifferentiableMultivariateVectorialOptimizer optimizer)

Simple constructor.

Parameters:
  `optimizer` - optimizer to use for the fitting

#### HarmonicFitter

    public HarmonicFitter(DifferentiableMultivariateVectorialOptimizer optimizer,
                  double[] initialGuess)

Simple constructor.

This constructor can be used when a first guess of the coefficients is already known.

Parameters:
  `optimizer` - optimizer to use for the fitting
  `initialGuess` - guessed values for amplitude (index 0), pulsation ω (index 1) and phase φ (index 2)

### Method Detail

#### addObservedPoint

    public void addObservedPoint(double weight,
                        double x,
                        double y)

Add an observed weighted (x,y) point to the sample.

Parameters:
  `weight` - weight of the observed point in the fit
  `x` - abscissa of the point
  `y` - observed value of the point at x, after fitting we should have P(x) as close as possible to this value

#### fit

    public HarmonicFunction fit()
                         throws OptimizationException

Fit an harmonic function to the observed points.

Returns:
  harmonic function best fitting the observed points
Throws:
  `OptimizationException` - if the sample is too short or if the first guess cannot be computed