-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
now calculate stats as sensor data comes in, rather than at time inte…
…rvals. and fix calculation of stats. some stuff for alarm managament
- Loading branch information
Marije Baalman
committed
Feb 16, 2012
1 parent
1bc0d64
commit e15ee65
Showing
6 changed files
with
330 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
151 changes: 151 additions & 0 deletions
151
app/src/com/steim/nescivi/android/gvb/CircularFloatArrayBuffer2.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,151 @@ | ||
package com.steim.nescivi.android.gvb; | ||
|
||
import java.lang.Math; | ||
|
||
public class CircularFloatArrayBuffer2 { | ||
private int mSize, mNewestElement; | ||
private double mBuffer[][]; | ||
private int mDim; | ||
|
||
public CircularFloatArrayBuffer2(int dim, int size) { | ||
if (size < 1) { | ||
throw new IllegalArgumentException(); | ||
} | ||
if (dim < 1) { | ||
throw new IllegalArgumentException(); | ||
} | ||
|
||
// Create buffer | ||
mBuffer = new double[dim][size]; | ||
|
||
// Initialize pointers | ||
mNewestElement = size - 1; | ||
mSize = 0; | ||
mDim = dim; | ||
} | ||
|
||
public void add(float elem[]) { | ||
int victim = (mNewestElement + 1) % mBuffer.length; | ||
|
||
for ( int i=0; i<mDim; i++ ){ | ||
mBuffer[i][victim] = elem[i]; | ||
} | ||
|
||
mNewestElement = victim; | ||
|
||
if (mSize < mBuffer.length) { | ||
mSize++; | ||
} | ||
|
||
} | ||
|
||
public int getSize() { | ||
return mSize; | ||
} | ||
|
||
public int getDim() { | ||
return mDim; | ||
} | ||
|
||
public float[][] getContents() { | ||
float result[][] = new float[mDim][mSize]; | ||
|
||
if (mSize == mBuffer.length) { | ||
int oldestElement = (mNewestElement + 1) % mBuffer.length; | ||
|
||
for ( int j=0; j<mDim; j++ ){ | ||
for (int i = 0; i < result.length; i++) { | ||
result[j][i] = (float) mBuffer[j][oldestElement]; | ||
|
||
if (++oldestElement == mBuffer.length) | ||
oldestElement = 0; | ||
} | ||
} | ||
} | ||
else { | ||
for ( int j=0; j<mDim; j++ ){ | ||
// special case here: buffer is not filled yet (so just dump the buffer) | ||
for (int i = 0; i < result.length; i++) { | ||
result[j][i] = (float) mBuffer[j][i]; | ||
} | ||
} | ||
} | ||
|
||
return result; | ||
} | ||
|
||
public static double sum(double[] a) { | ||
double sum = 0.0; | ||
for (int i = 0; i < a.length; i++) { | ||
sum += a[i]; | ||
} | ||
return sum; | ||
} | ||
|
||
float[][] getStats(){ | ||
double vart; | ||
double[] stds = {0.0, 0.0, 0,0}; | ||
double[] means = {0.0, 0.0, 0,0}; | ||
float[][] stats = { {0.0f, 0.0f, 0.0f}, {0.0f, 0.0f, 0.0f} }; | ||
/* | ||
for ( int axis = 0; axis < 3; axis++ ){ | ||
stats[0][axis] = (float) 0.0; | ||
} | ||
*/ | ||
|
||
// mean | ||
for ( int axis = 0; axis < 3; axis++ ){ | ||
means[axis] = sum( this.mBuffer[axis] ) / this.mSize; | ||
} | ||
/* | ||
for (int i = 0; i < this.mSize; i++){ | ||
means[axis] += this.mBuffer[i][axis]; | ||
} | ||
} | ||
for ( int axis = 0; axis < 3; axis++ ){ | ||
means[axis] = means[axis] / (float) this.mSize; | ||
} | ||
*/ | ||
|
||
// standard deviation | ||
// std = sqrt(mean( abs(x - x.mean())**2) ) | ||
|
||
if ( this.mSize > 1 ){ | ||
for ( int axis = 0; axis < 3; axis++ ){ | ||
for (int i = 0; i < this.mSize; i++) { | ||
vart = this.mBuffer[axis][i] - means[axis]; | ||
stds[axis] += vart * vart; | ||
} | ||
stds[axis] = stds[axis] / (this.mSize-1); | ||
} | ||
} | ||
|
||
for ( int axis = 0; axis < 3; axis++ ){ | ||
stats[0][axis] = (float) means[axis]; | ||
stats[1][axis] = (float) Math.sqrt( stds[axis] ); | ||
} | ||
|
||
/* | ||
//float delta; | ||
//int n = 0; | ||
//float[] M2 = { (float) 0.0,(float) 0.0, (float) 0.0 }; | ||
for (int i = 0; i < this.mSize; i++){ | ||
n = n + 1; | ||
for ( int axis = 0; axis < 3; axis++ ){ | ||
delta = this.mBuffer[i][axis] - stats[0][axis]; | ||
stats[0][axis] = stats[0][axis] + delta/n; | ||
if ( n > 1 ){ | ||
M2[axis] = M2[axis] + delta*(this.mBuffer[i][axis] - stats[0][axis]); | ||
} | ||
} | ||
} | ||
for ( int axis = 0; axis < 3; axis++ ){ | ||
stats[1][axis] = (float) Math.sqrt( M2[axis]/(n - 1) ); | ||
} | ||
*/ | ||
return stats; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.