org.apache.commons.math3.fitting.PolynomialCurveFitter Java Examples

The following examples show how to use org.apache.commons.math3.fitting.PolynomialCurveFitter. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: Filters.java    From ECG-Viewer with GNU General Public License v2.0 6 votes vote down vote up
public static void detrend(List<double[]> set, int detrendPolynomial) {
	PolynomialCurveFitter p = PolynomialCurveFitter.create(detrendPolynomial);
	WeightedObservedPoints wop = new WeightedObservedPoints();
	for(int i = 0; i < set.size(); i++) {
		wop.add(set.get(i)[0], set.get(i)[1]);
	}
	double[] coeff = p.fit(wop.toList());
	for(int h = 0; h < set.size(); h++) {
		double val = set.get(h)[0];
		double off = 0;
		for(int i = detrendPolynomial; i >= 0; i--) {
			off += coeff[i] * Math.pow(val, i);
		}
		set.set(h, new double[]{set.get(h)[0], set.get(h)[1]-off});
	}
}
 
Example #2
Source File: ExtremumComputer.java    From RipplePower with Apache License 2.0 6 votes vote down vote up
private boolean isLinearCoeffDiff(double[] input, int centerIndex) {
	double xStep = 2.0 / (input.length + 1);

	WeightedObservedPoints firstHalf = new WeightedObservedPoints();
	WeightedObservedPoints secondHalf = new WeightedObservedPoints();

	for (int i = 0; i < input.length; i++) {
		if (i <= centerIndex) {
			firstHalf.add(i * xStep, input[i]);
		}

		if (i >= centerIndex) {
			secondHalf.add((i - centerIndex) * xStep, input[i]);
		}
	}

	PolynomialCurveFitter fitter = PolynomialCurveFitter.create(1);

	double[] firstHalfCoeffs = fitter.fit(firstHalf.toList());
	double[] secondHalfCoeffs = fitter.fit(secondHalf.toList());

	double firstHalfCoeff = firstHalfCoeffs[1];
	double secondHalfCoeff = secondHalfCoeffs[1];

	return firstHalfCoeff * secondHalfCoeff < 0;
}