Example usage for org.apache.commons.math.linear RealMatrixImpl getDataRef

List of usage examples for org.apache.commons.math.linear RealMatrixImpl getDataRef

Introduction

In this page you can find the example usage for org.apache.commons.math.linear RealMatrixImpl getDataRef.

Prototype

public double[][] getDataRef() 

Source Link

Document

Returns a reference to the underlying data array.

Usage

From source file:com.lzy.heartset.sg.SGFilter.java

/**
 * Computes Savitzky-Golay coefficients for given parameters
 * /*from  w w w .  j a  v a  2  s.co  m*/
 * @param nl
 *            numer of past data points filter will use
 * @param nr
 *            number of future data points filter will use
 * @param degree
 *            order of smoothin polynomial
 * @return Savitzky-Golay coefficients
 * @throws IllegalArgumentException
 *             if {@code nl < 0} or {@code nr < 0} or {@code nl + nr <
 *             degree}
 */
public static double[] computeSGCoefficients(int nl, int nr, int degree) {
    if (nl < 0 || nr < 0 || nl + nr < degree)
        throw new IllegalArgumentException("Bad arguments");
    RealMatrixImpl matrix = new RealMatrixImpl(degree + 1, degree + 1);
    double[][] a = matrix.getDataRef();
    double sum;
    for (int i = 0; i <= degree; i++) {
        for (int j = 0; j <= degree; j++) {
            sum = (i == 0 && j == 0) ? 1 : 0;
            for (int k = 1; k <= nr; k++)
                sum += pow(k, i + j);
            for (int k = 1; k <= nl; k++)
                sum += pow(-k, i + j);
            a[i][j] = sum;
        }
    }
    double[] b = new double[degree + 1];
    b[0] = 1;
    b = matrix.solve(b);
    double[] coeffs = new double[nl + nr + 1];
    for (int n = -nl; n <= nr; n++) {
        sum = b[0];
        for (int m = 1; m <= degree; m++)
            sum += b[m] * pow(n, m);
        coeffs[n + nl] = sum;
    }
    return coeffs;
}