|
Java example source code file (HarmonicFitter.java)
The HarmonicFitter.java Java example source code/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.math3.fitting;
import org.apache.commons.math3.optim.nonlinear.vector.MultivariateVectorOptimizer;
import org.apache.commons.math3.analysis.function.HarmonicOscillator;
import org.apache.commons.math3.exception.ZeroException;
import org.apache.commons.math3.exception.NumberIsTooSmallException;
import org.apache.commons.math3.exception.MathIllegalStateException;
import org.apache.commons.math3.exception.util.LocalizedFormats;
import org.apache.commons.math3.util.FastMath;
/**
* 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 ?: <code>f (t) = a cos (? t + ?). They are
* searched by a least square estimator initialized with a rough guess
* based on integrals.
*
* @since 2.0
* @deprecated As of 3.3. Please use {@link HarmonicCurveFitter} and
* {@link WeightedObservedPoints} instead.
*/
@Deprecated
public class HarmonicFitter extends CurveFitter<HarmonicOscillator.Parametric> {
/**
* Simple constructor.
* @param optimizer Optimizer to use for the fitting.
*/
public HarmonicFitter(final MultivariateVectorOptimizer optimizer) {
super(optimizer);
}
/**
* Fit an harmonic function to the observed points.
*
* @param initialGuess First guess values in the following order:
* <ul>
* <li>Amplitude
* <li>Angular frequency
* <li>Phase
* </ul>
* @return the parameters of the harmonic function that best fits the
* observed points (in the same order as above).
*/
public double[] fit(double[] initialGuess) {
return fit(new HarmonicOscillator.Parametric(), initialGuess);
}
/**
* Fit an harmonic function to the observed points.
* An initial guess will be automatically computed.
*
* @return the parameters of the harmonic function that best fits the
* observed points (see the other {@link #fit(double[]) fit} method.
* @throws NumberIsTooSmallException if the sample is too short for the
* the first guess to be computed.
* @throws ZeroException if the first guess cannot be computed because
* the abscissa range is zero.
*/
public double[] fit() {
return fit((new ParameterGuesser(getObservations())).guess());
}
/**
* This class guesses harmonic coefficients from a sample.
* <p>The algorithm used to guess the coefficients is as follows:
*
* <p>We know f (t) at some sampling points ti and want to find a,
* ? and ? such that f (t) = a cos (? t + ?).
* </p>
*
* <p>From the analytical expression, we can compute two primitives :
* <pre>
* If2 (t) = ? f<sup>2 = a2 × [t + S (t)] / 2
* If'2 (t) = ? f'<sup>2 = a2 ?2 × [t - S (t)] / 2
* where S (t) = sin (2 (? t + ?)) / (2 ?)
* </pre>
* </p>
*
* <p>We can remove S between these expressions :
* <pre>
* If'2 (t) = a<sup>2 ?2 t - ?2 If2 (t)
* </pre>
* </p>
*
* <p>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)
* </p>
*
* <p>From the primitive, we can deduce the same form for definite
* integrals between t<sub>1 and ti for each ti :
* <pre>
* If2 (t<sub>i) - If2 (t1) = A × (ti - t1) + B × (If2 (ti) - If2 (t1))
* </pre>
* </p>
*
* <p>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.
* </p>
*
* <p>For a bilinear expression z (xi, yi) = A × xi + B × yi, the
* coefficients A and B that minimize a least square criterion
* ? (z<sub>i - z (xi, yi))2 are given by these expressions:
* <pre>
*
* ?y<sub>iyi ?xizi - ?xiyi ?yizi
* A = ------------------------
* ?x<sub>ixi ?yiyi - ?xiyi ?xiyi
*
* ?x<sub>ixi ?yizi - ?xiyi ?xizi
* B = ------------------------
* ?x<sub>ixi ?yiyi - ?xiyi ?xiyi
* </pre>
* </p>
*
*
* <p>In fact, we can assume both a and ? are positive and
* compute them directly, knowing that A = a<sup>2 ?2 and that
* B = - ?<sup>2. The complete algorithm is therefore:
* <pre>
*
* for each t<sub>i from t1 to tn-1, compute:
* f (t<sub>i)
* f' (t<sub>i) = (f (ti+1) - f(ti-1)) / (ti+1 - ti-1)
* x<sub>i = ti - t1
* y<sub>i = ? f2 from t1 to ti
* z<sub>i = ? f'2 from t1 to ti
* update the sums ?x<sub>ixi, ?yiyi, ?xiyi, ?xizi and ?yizi
* end for
*
* |--------------------------
* \ | ?y<sub>iyi ?xizi - ?xiyi ?yizi
* a = \ | ------------------------
* \| ?x<sub>iyi ?xizi - ?xixi ?yizi
*
*
* |--------------------------
* \ | ?x<sub>iyi ?xizi - ?xixi ?yizi
* ? = \ | ------------------------
* \| ?x<sub>ixi ?yiyi - ?xiyi ?xiyi
*
* </pre>
* </p>
*
* <p>Once we know ?, we can compute:
* <pre>
* fc = ? f (t) cos (? t) - f' (t) sin (? t)
* fs = ? f (t) sin (? t) + f' (t) cos (? t)
* </pre>
* </p>
*
* <p>It appears that
Other Java examples (source code examples)Here is a short list of links related to this Java HarmonicFitter.java source code file: |
... this post is sponsored by my books ... | |
#1 New Release! |
FP Best Seller |
Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.
A percentage of advertising revenue from
pages under the /java/jwarehouse
URI on this website is
paid back to open source projects.