Java tutorial
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package javatranslation.matlab; import static java.lang.Math.log; import static java.lang.Math.log10; import java.util.Arrays; import org.apache.commons.lang3.ArrayUtils; import org.apache.commons.math3.stat.regression.*; import org.apache.commons.math3.fitting.PolynomialFitter; import org.apache.commons.math3.optim.nonlinear.vector.MultivariateVectorOptimizer; /** * * @author Marco Marson */ public class dfa { public static double mean(double[] m) { double sum = 0; for (int i = 0; i < m.length; i++) { sum = sum + (double) m[i]; } return sum / (double) m.length; } public static double sum(double[] array) { int i; double result = 0; for (i = 0; i < array.length; i++) { result = result + array[i]; } return result; } public static double[] cumsum(double[] array, double p) { int i; double[] result = new double[array.length]; for (i = 0; i < array.length; i++) { if (i == 0) result[i] = array[i] - p; else result[i] = result[i - 1] + array[i] - p; } return result; } public static double sum2(double[] array) { int i; double result = 0; for (i = 0; i < array.length; i++) { if (array[i] > 0) result = result + 1; } return result; } public static void dfafunction(double[] tau, double MinBox, double MaxBox, int DFAorder) { double[] incoef = null; double[] l = null; l = new double[50]; incoef = new double[50]; for (int i = 0; i < 50; i++) { l = logspace(MinBox, MaxBox); //System.out.println(l[i]); incoef[i] = Math.round(l[i]); } double xx = mean(tau); for (int i = 0; i < tau.length; i++) { tau[i] = tau[i] - xx; } double[] Y = cumsum(tau, dfa.mean(tau)); double maxnumseg = incoef.length; double[] winlen = null; winlen = new double[50]; for (int truta = 0; truta < 50; truta++) { winlen[truta] = incoef[truta]; } Arrays.sort(winlen); ArrayUtils.reverse(winlen); double Ylength = Y.length; double F[][] = new double[(int) maxnumseg][1]; for (int k = 0; k < maxnumseg; k++) { F[k][0] = 0; } double[] timevec = new double[50]; for (int kk = 0; kk < maxnumseg; kk++) { timevec[kk] = winlen[kk]; double numsegm = Math.floor(Ylength / winlen[kk]); double tempY[][] = new double[1][(int) numsegm]; for (int k = 0; k < numsegm; k++) { tempY[0][k] = 0; } for (int zz = 0; zz < numsegm; zz++) { double overflowtest = zz * winlen[kk]; if (overflowtest <= Ylength) { double[] tempvec = null; int ko = 0; for (double p = ((zz - 1) * winlen[kk] + 1); p <= (zz * winlen[kk]); p = ((zz - 1) * winlen[kk] + 1) + 1) { // there are some errors in this loop tempvec[ko] = Y[(int) p]; System.out.println(tempvec[(int) p]); ko++; } //double temppol = polyfit(timevec,tempvec,DFAorder); MultivariateVectorOptimizer optimizer; optimizer = null; PolynomialFitter x = new PolynomialFitter(optimizer); // error here too double[] temppol = x.fit(DFAorder, timevec); double[] temppol2 = x.fit(DFAorder, tempvec); double[] arrayOfCoefficients = new double[temppol2.length]; arrayOfCoefficients = temppol2; int len = arrayOfCoefficients.length; double retAnswer = 0; double ret = 0; for (int i = 0; i < len; i++) { retAnswer = retAnswer + Math.pow(arrayOfCoefficients[i], i); } for (int i = 0; i < tempvec.length; i++) { ret = tempvec[i] - (Math.pow(retAnswer, 2)); } tempY[0][zz] = ((ret) / winlen[kk]); } } int k; double[] kopp = new double[(int) maxnumseg]; for (k = 0; k < maxnumseg; k++) { kopp[k] = tempY[0][k]; } double nonzerotempY = dfa.sum2(kopp); F[kk][0] = Math.sqrt(dfa.sum(kopp) / nonzerotempY); } double[] LF; LF = new double[(int) maxnumseg]; double[] LN; LN = new double[(int) maxnumseg]; for (int i = 0; i < maxnumseg; i++) { LF[i] = Math.log10(F[i][0]); LN[i] = Math.log10(winlen[i]); } double[][] XLN = new double[LN.length][LN.length]; for (int i = 0; i < LN.length; i++) { XLN[i][0] = 1; XLN[i][1] = LN[i]; } SimpleRegression x; x = new SimpleRegression(); x.addObservations(XLN, LF); // and probably error in this one too RegressionResults b = x.regress(); // System.out.println(b.getParameterEstimates()); //double LF=Math.log10(F); % log fluctuations //double LN=Math.log10(winlen); } public static double[] logspace(double d3, double d4) { int n = 50; double base = 10; double d1 = Math.log10(d3); double d2 = Math.log10(d4); double[] y = new double[n]; double[] p = linspace(d1, d2, n); for (int i = 0; i < y.length - 1; i++) { y[i] = Math.pow(base, p[i]); } y[y.length - 1] = Math.pow(base, d2); return y; } public static double[] linspace(double d1, double d2, int n) { double[] y = new double[n]; double dy = (d2 - d1) / (n - 1); for (int i = 0; i < n; i++) { y[i] = d1 + (dy * i); } return y; } }