Top Description Inners Constructors Methods
org.apache.commons.math3.fitting

public Class HarmonicFitter

extends CurveFitter<HarmonicOscillator.Parametric>
Class Inheritance
Annotations
@Deprecated
Imports
org.apache.commons.math3.optim.nonlinear.vector.MultivariateVectorOptimizer, org.apache.commons.math3.analysis.function.HarmonicOscillator, org.apache.commons.math3.exception.ZeroException, .NumberIsTooSmallException, .MathIllegalStateException, org.apache.commons.math3.exception.util.LocalizedFormats, org.apache.commons.math3.util.FastMath

Deprecated

As of 3.3. Please use HarmonicCurveFitter and WeightedObservedPoints instead.

Class that 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

Nested and Inner Type Summary

Modifier and TypeClass and Description
public static class
HarmonicFitter.ParameterGuesser

This class guesses harmonic coefficients from a sample.

Constructor Summary

AccessConstructor and Description
public
HarmonicFitter(final MultivariateVectorOptimizer
Optimizer to use for the fitting.
optimizer
)
Deprecated as a consequence of MultivariateVectorOptimizer being deprecated.

Simple constructor.

Method Summary

Modifier and TypeMethod and Description
public double[]

Returns:

the parameters of the harmonic function that best fits the observed points (in the same order as above).
fit
(double[]
First guess values in the following order:
  • Amplitude
  • Angular frequency
  • Phase
initialGuess
)

Fit an harmonic function to the observed points.

public double[]

Returns:

the parameters of the harmonic function that best fits the observed points (see the other fit method.
fit
()

Fit an harmonic function to the observed points.

Inherited from org.apache.commons.math3.fitting.CurveFitter:
addObservedPointaddObservedPointaddObservedPointclearObservationsfitfitgetObservations

Constructor Detail

HarmonicFitterback to summary
public HarmonicFitter(final MultivariateVectorOptimizer optimizer)

Deprecated

as a consequence of MultivariateVectorOptimizer being deprecated. See corresponding docs for further information.

Simple constructor.

Parameters
optimizer:MultivariateVectorOptimizer

Optimizer to use for the fitting.

Method Detail

fitback to summary
public double[] fit(double[] initialGuess)

Fit an harmonic function to the observed points.

Parameters
initialGuess:double[]

First guess values in the following order:

  • Amplitude
  • Angular frequency
  • Phase
Returns:double[]

the parameters of the harmonic function that best fits the observed points (in the same order as above).

fitback to summary
public double[] fit()

Fit an harmonic function to the observed points. An initial guess will be automatically computed.

Returns:double[]

the parameters of the harmonic function that best fits the observed points (see the other fit method.

Exceptions
NumberIsTooSmallException:
if the sample is too short for the the first guess to be computed.
ZeroException:
if the first guess cannot be computed because the abscissa range is zero.
org.apache.commons.math3.fitting back to summary

public Class HarmonicFitter.ParameterGuesser

extends Object
Class Inheritance
  • java.lang.Object
  • org.apache.commons.math3.fitting.HarmonicFitter.ParameterGuesser

This class guesses harmonic coefficients from a sample.

The algorithm used to guess the coefficients is as follows:

We know f (t) at some sampling points ti and want to find a, ω and φ such that f (t) = a cos (ω t + φ).

From the analytical expression, we can compute two primitives :

    If2  (t) = ∫ f2  = a2 × [t + S (t)] / 2
    If'2 (t) = ∫ f'2 = a2 ω2 × [t - S (t)] / 2
    where S (t) = sin (2 (ω t + φ)) / (2 ω)

We can remove S between these expressions :

    If'2 (t) = a2 ω2 t - ω2 If2 (t)

The preceding expression shows that If'2 (t) is a linear combination of both t and If2 (t): If'2 (t) = A × t + B × If2 (t)

From the primitive, we can deduce the same form for definite integrals between t1 and ti for each ti :

  If2 (ti) - If2 (t1) = A × (ti - t1) + B × (If2 (ti) - If2 (t1))

We can find the coefficients A and B that best fit the sample to this linear expression by computing the definite integrals for each sample points.

For a bilinear expression z (xi, yi) = A × xi + B × yi, the coefficients A and B that minimize a least square criterion ∑ (zi - z (xi, yi))2 are given by these expressions:


        ∑yiyi ∑xizi - ∑xiyi ∑yizi
    A = ------------------------
        ∑xixi ∑yiyi - ∑xiyi ∑xiyi

        ∑xixi ∑yizi - ∑xiyi ∑xizi
    B = ------------------------
        ∑xixi ∑yiyi - ∑xiyi ∑xiyi

In fact, we can assume both a and ω are positive and compute them directly, knowing that A = a2 ω2 and that B = - ω2. The complete algorithm is therefore:


for each ti from t1 to tn-1, compute:
  f  (ti)
  f' (ti) = (f (ti+1) - f(ti-1)) / (ti+1 - ti-1)
  xi = ti - t1
  yi = ∫ f2 from t1 to ti
  zi = ∫ f'2 from t1 to ti
  update the sums ∑xixi, ∑yiyi, ∑xiyi, ∑xizi and ∑yizi
end for

           |--------------------------
        \  | ∑yiyi ∑xizi - ∑xiyi ∑yizi
a     =  \ | ------------------------
          \| ∑xiyi ∑xizi - ∑xixi ∑yizi


           |--------------------------
        \  | ∑xiyi ∑xizi - ∑xixi ∑yizi
ω     =  \ | ------------------------
          \| ∑xixi ∑yiyi - ∑xiyi ∑xiyi

Once we know ω, we can compute:

   fc = ω f (t) cos (ω t) - f' (t) sin (ω t)
   fs = ω f (t) sin (ω t) + f' (t) cos (ω t)

It appears that fc = a ω cos (φ) and fs = -a ω sin (φ), so we can use these expressions to compute φ. The best estimate over the sample is given by averaging these expressions.

Since integrals and means are involved in the preceding estimations, these operations run in O(n) time, where n is the number of measurements.

Field Summary

Modifier and TypeField and Description
private final double
a

Amplitude.

private final double
omega

Angular frequency.

private final double
phi

Phase.

Constructor Summary

AccessConstructor and Description
public
ParameterGuesser(WeightedObservedPoint[]
Sampled observations.
observations
)

Simple constructor.

Method Summary

Modifier and TypeMethod and Description
public double[]

Returns:

the guessed parameters, in the following order:
  • Amplitude
  • Angular frequency
  • Phase
guess
()

Gets an estimation of the parameters.

private double[]

Returns:

the guessed amplitude (at index 0) and circular frequency (at index 1).
guessAOmega
(WeightedObservedPoint[]
Observations, sorted w.r.t. abscissa.
observations
)

Estimate a first guess of the amplitude and angular frequency.

private double

Returns:

the guessed phase.
guessPhi
(WeightedObservedPoint[]
Observations, sorted w.r.t. abscissa.
observations
)

Estimate a first guess of the phase.

private WeightedObservedPoint[]

Returns:

the input observations, sorted.
sortObservations
(WeightedObservedPoint[]
Input observations.
unsorted
)

Sort the observations with respect to the abscissa.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

aback to summary
private final double a

Amplitude.

omegaback to summary
private final double omega

Angular frequency.

phiback to summary
private final double phi

Phase.

Constructor Detail

ParameterGuesserback to summary
public ParameterGuesser(WeightedObservedPoint[] observations)

Simple constructor.

Parameters
observations:WeightedObservedPoint[]

Sampled observations.

Exceptions
NumberIsTooSmallException:
if the sample is too short.
ZeroException:
if the abscissa range is zero.
MathIllegalStateException:
when the guessing procedure cannot produce sensible results.

Method Detail

guessback to summary
public double[] guess()

Gets an estimation of the parameters.

Returns:double[]

the guessed parameters, in the following order:

  • Amplitude
  • Angular frequency
  • Phase
guessAOmegaback to summary
private double[] guessAOmega(WeightedObservedPoint[] observations)

Estimate a first guess of the amplitude and angular frequency. This method assumes that the sortObservations(WeightedObservedPoint[]) method has been called previously.

Parameters
observations:WeightedObservedPoint[]

Observations, sorted w.r.t. abscissa.

Returns:double[]

the guessed amplitude (at index 0) and circular frequency (at index 1).

Exceptions
ZeroException:
if the abscissa range is zero.
MathIllegalStateException:
when the guessing procedure cannot produce sensible results.
guessPhiback to summary
private double guessPhi(WeightedObservedPoint[] observations)

Estimate a first guess of the phase.

Parameters
observations:WeightedObservedPoint[]

Observations, sorted w.r.t. abscissa.

Returns:double

the guessed phase.

sortObservationsback to summary
private WeightedObservedPoint[] sortObservations(WeightedObservedPoint[] unsorted)

Sort the observations with respect to the abscissa.

Parameters
unsorted:WeightedObservedPoint[]

Input observations.

Returns:WeightedObservedPoint[]

the input observations, sorted.