Example usage for org.apache.commons.math3.fitting PolynomialFitter fit

List of usage examples for org.apache.commons.math3.fitting PolynomialFitter fit

Introduction

In this page you can find the example usage for org.apache.commons.math3.fitting PolynomialFitter fit.

Prototype

public double[] fit(double[] guess) 

Source Link

Document

Get the coefficients of the polynomial fitting the weighted data points.

Usage

From source file:edu.uchc.octane.CalibrationDialogAstigmatism.java

double[] parabolicFit(double[] sigma) {

    PolynomialFitter fitter = new PolynomialFitter(new LevenbergMarquardtOptimizer());

    for (int i = 0; i < sigma.length; i++) {

        fitter.addObservedPoint((i - sigma.length / 2) * sliceSpacing_, sigma[i]);

    }//  w  w w.j  a  va 2  s . c o  m

    double[] guess = new double[3];
    guess[0] = sigma[(int) (sigma.length / 2)];
    guess[2] = (sigma[0] - guess[0]) / (sliceSpacing_ * sliceSpacing_ * sigma.length * sigma.length / 4);

    double[] results = fitter.fit(guess);

    return results;
}

From source file:uk.ac.diamond.scisoft.analysis.optimize.ApachePolynomial.java

/**
 * Fit a polynomial to given x and y data
 * @param x/*www.  j  a  v  a  2  s .  c  om*/
 * @param y
 * @param guess initial values for coefficients (given in increasing degree of the terms)
 * @return coefficients of fitted polynomial
 */
public static double[] polynomialFit(Dataset x, Dataset y, double... guess) {
    PolynomialFitter fitter = new PolynomialFitter(new LevenbergMarquardtOptimizer());

    for (int i = 0; i < y.getSize(); i++) {
        fitter.addObservedPoint(1, x.getDouble(i), y.getDouble(i));
    }

    return fitter.fit(guess);
}

From source file:uk.ac.diamond.scisoft.analysis.optimize.ApachePolynomial.java

/**
 * Calculate a polynomial filtered dataset using a specified window size and polynomial order.
 * /* w  w  w.j ava 2 s .  co  m*/
 * @param x The abscissa (generally energy/wavelength etc)
 * @param y The ordinate to be smoothed
 * @param windowSize The size of window used in the smoothing
 * @param polyOrder The order of the polynomial fitted to the window
 * @return result The smoothed data set
 * @throws Exception 
 */
public static Dataset getPolynomialSmoothed(final Dataset x, final Dataset y, int windowSize, int polyOrder)
        throws Exception {
    // Could probably do with more sanity check on relative size of
    // window vs polynomial but doesn't seem to trip up
    // So we'll see how it goes...

    // integer divide window size so window is symmetric around point
    int window = windowSize / 2;

    PolynomialFitter fitter = new PolynomialFitter(new LevenbergMarquardtOptimizer());
    double dx = x.getDouble(1) - x.getDouble(0); // change in x for edge extrapolation
    int xs = x.getSize();
    Dataset result = DatasetFactory.zeros(y);
    double[] guess = new double[polyOrder + 1];

    for (int idx = 0; idx < xs; idx++) {
        fitter.clearObservations();

        // Deal with edge cases:
        // In both cases extend x edge by dx required for window size
        // Pad y with first or last value
        for (int idw = -window; idw < window + 1; idw++) {
            if (idx + idw < 0) {
                fitter.addObservedPoint(1, x.getDouble(0) + (dx * (idx + idw)), y.getDouble(0));
            } else if ((idx + idw) > (xs - 1)) {
                fitter.addObservedPoint(1, x.getDouble(xs - 1) + dx * (idx + idw - (xs - 1)),
                        y.getDouble(xs - 1));
            } else {
                fitter.addObservedPoint(1, x.getDouble(idx + idw), y.getDouble(idx + idw));
            }
        }

        PolynomialFunction fitted = new PolynomialFunction(fitter.fit(guess));
        result.set(fitted.value(x.getDouble(idx)), idx);
    }

    return result;
}