Java tutorial
//package com.java2s; public class Main { public static double[] calculateKcal(double[][] data, int samplesPerWindow) { double res[] = new double[2]; // holds V and H result double history[] = new double[3]; double d[] = new double[3]; double p[] = new double[3]; // this is historical average of the past samples for (int i = 0; i < 3; i++) { for (int j = 0; j < samplesPerWindow; j++) { history[i] += data[i][j]; } history[i] /= samplesPerWindow; } for (int j = 0; j < samplesPerWindow; j++) { for (int i = 0; i < 3; i++) { d[i] = history[i] - data[i][j]; } double num = 0; double den = 0; double value = 0; for (int i = 0; i < 3; i++) { num = (d[0] * history[0] + d[1] * history[1] + d[2] * history[2]); den = (history[0] * history[0] + history[1] * history[1] + history[2] * history[2]); if (den == 0) den = 0.01; value = (num / den) * history[i]; p[i] = value; } double pMagn = p[0] * p[0] + p[1] * p[1] + p[2] * p[2]; res[0] += Math.sqrt(pMagn); res[1] += Math.sqrt( (d[0] - p[0]) * (d[0] - p[0]) + (d[1] - p[1]) * (d[1] - p[1]) + (d[2] - p[2]) * (d[2] - p[2])); } return res; } }