Example usage for org.apache.commons.math3.analysis.function Gaussian Gaussian

List of usage examples for org.apache.commons.math3.analysis.function Gaussian Gaussian

Introduction

In this page you can find the example usage for org.apache.commons.math3.analysis.function Gaussian Gaussian.

Prototype

public Gaussian(double norm, double mean, double sigma) throws NotStrictlyPositiveException 

Source Link

Document

Gaussian with given normalization factor, mean and standard deviation.

Usage

From source file:net.sf.mzmine.chartbasics.HistogramChartFactory.java

/**
 * Adds a Gaussian curve to the plot//from  w ww  . java2 s. c  o m
 * 
 * @param plot
 * @param fit double[] {normFactor, mean, sigma}
 * @param drawStart start of curve
 * @param drawEnd end of curve
 * @param gMin lower bound of Gaussian fit
 * @param gMax upper bound of Gaussian fit
 * @param sigDigits number of significant digits
 * @return
 */
public static double[] addGaussianFit(XYPlot plot, double[] fit, double drawStart, double drawEnd, double gMin,
        double gMax, int sigDigits, boolean annotations) {
    double gWidth = gMax - gMin;

    Gaussian g = new Gaussian(fit[0], fit[1], fit[2]);

    // create xy series for gaussian
    String mean = Precision.toString(fit[1], sigDigits, 7);
    String sigma = Precision.toString(fit[2], sigDigits, 7);
    String norm = Precision.toString(fit[0], sigDigits, 7);
    XYSeries gs = new XYSeries(
            "Gaussian: " + mean + " \u00B1 " + sigma + " [" + norm + "] (mean \u00B1 sigma [normalisation])");
    // add lower dp number out of gaussian fit range
    int steps = 100;
    if (gMin > drawStart) {
        for (int i = 0; i <= steps; i++) {
            double x = drawStart + ((gMin - drawStart) / steps) * i;
            double y = g.value(x);
            gs.add(x, y);
        }
    }
    // add high resolution in gaussian fit area
    steps = 1000;
    for (int i = 0; i <= steps; i++) {
        double x = gMin + (gWidth / steps) * i;
        double y = g.value(x);
        gs.add(x, y);
    }
    // add lower dp number out of gaussian fit range
    steps = 100;
    if (gMax < drawEnd) {
        for (int i = 0; i <= steps; i++) {
            double x = gMax + ((drawEnd - gMax) / steps) * i;
            double y = g.value(x);
            gs.add(x, y);
        }
    }
    // add gaussian
    XYSeriesCollection gsdata = new XYSeriesCollection(gs);
    int index = plot.getDatasetCount();
    plot.setDataset(index, gsdata);
    plot.setRenderer(index, new XYLineAndShapeRenderer(true, false));

    if (annotations)
        addGaussianFitAnnotations(plot, fit);

    return fit;
}