Permalink
Browse files

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...
1 parent 1bc0d64 commit e15ee6546370609adcda4fe3fc8aee7bb9640b12 Marije Baalman committed Feb 16, 2012
@@ -75,7 +75,7 @@ public int getSize() {
}
}
for ( int axis = 0; axis < 3; axis++ ){
- means[axis] = means[axis] / this.mSize;
+ means[axis] = means[axis] / (float) this.mSize;
}
// standard deviation
@@ -89,7 +89,7 @@ public int getSize() {
for ( int axis = 0; axis < 3; axis++ ){
stats[0][axis] = (float) means[axis];
- stats[1][axis] = (float) Math.sqrt( stds[axis] / this.mSize );
+ stats[1][axis] = (float) Math.sqrt( stds[axis] / (float) this.mSize );
}
/*
@@ -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;
+ }
+
+}
@@ -32,6 +32,14 @@
import android.view.WindowManager;
+import android.app.AlarmManager;
+import android.app.PendingIntent;
+import android.content.BroadcastReceiver;
+import android.os.PowerManager;
+
+import java.util.Calendar;
+
+
/*
import java.util.Timer;
import java.util.TimerTask;
@@ -726,7 +734,17 @@ public void send_estimate_settings(){
if (cb.isChecked() ){
signForward = -1;
}
+
+ ed = (EditText) findViewById(R.id.editUpdateLog );
+ int logUpdateTime = 500;
+ try {
+ logUpdateTime = Integer.parseInt(ed.getText().toString());
+ } catch(NumberFormatException nfe) {
+ System.out.println("Could not parse " + nfe);
+ }
+ cb = (CheckBox) findViewById(R.id.localLog);
+
if (mVelService != null) {
Bundle b = new Bundle();
Message msg = Message.obtain(null, VelocityEstimator.MSG_ESTIMATE_SETTINGS );
@@ -745,6 +763,8 @@ public void send_estimate_settings(){
b.putFloat("stilltime", stilltime );
b.putFloat("offsetma", offsetma );
b.putInt("signForward", signForward );
+ b.putBoolean( "makeLocalLog", cb.isChecked() );
+ b.putInt("updateLogTime", logUpdateTime );
msg.setData(b);
try {
@@ -758,9 +778,9 @@ public void send_estimate_settings(){
public void send_server_settings(){
EditText ed = (EditText) findViewById(R.id.editUpdateServer );
- int updateServerTime = 30000;
+ int updateServerTime = 30;
try {
- updateServerTime = Integer.parseInt(ed.getText().toString()) * 1000;
+ updateServerTime = Integer.parseInt(ed.getText().toString());
} catch(NumberFormatException nfe) {
System.out.println("Could not parse " + nfe);
}
@@ -810,7 +830,7 @@ public void send_server_settings(){
b.putInt("port", port );
b.putInt("client", client );
b.putInt("bufferSize", bufferSize );
- b.putInt("updateServerTime", updateServerTime );
+ b.putInt("updateServerTime", updateServerTime * 1000 );
b.putBoolean( "makeLocalLog", cb.isChecked() );
b.putInt("updateLogTime", logUpdateTime );
msg.setData(b);
@@ -853,8 +873,8 @@ public void onServiceConnected(ComponentName class_name, IBinder service) {
set_update_server();
*/
- send_estimate_settings();
send_server_settings();
+ send_estimate_settings();
/*
@@ -1021,7 +1041,7 @@ public void readPreferences(){
ed.setText( Integer.toString( updateTime ) );
ed = (EditText) findViewById(R.id.editUpdateServer );
- ed.setText( Float.toString( updateServerTime ) );
+ ed.setText( Integer.toString( updateServerTime ) );
ed = (EditText) findViewById(R.id.editHost);
ed.setText( host );
ed = (EditText) findViewById(R.id.editPort );
@@ -1185,9 +1205,9 @@ public void storePreferences(){
}
ed = (EditText) findViewById(R.id.editUpdateServer );
- int updateServerTime = 30000;
+ int updateServerTime = 30;
try {
- updateServerTime = Integer.parseInt(ed.getText().toString()) * 1000;
+ updateServerTime = Integer.parseInt(ed.getText().toString());
} catch(NumberFormatException nfe) {
System.out.println("Could not parse " + nfe);
}
@@ -1237,7 +1257,7 @@ public void storePreferences(){
mPrefsEdit.putInt("port", port );
mPrefsEdit.putInt("client", client );
mPrefsEdit.putInt("bufferSize", bufferSize );
- mPrefsEdit.putInt("updateServerTime", updateServerTime / 1000 );
+ mPrefsEdit.putInt("updateServerTime", updateServerTime );
mPrefsEdit.putInt("updateLogTime", logUpdateTime );
mPrefsEdit.putInt("sensor", sensorid );
@@ -1432,5 +1452,61 @@ public void closeLocalLog(){
}
*/
+ /*
+ void setupDayRhythm(){
+ AlarmManager am = (AlarmManager) GuesstimateVelocityBetter.this.getSystemService(Context.ALARM_SERVICE);
+ Calendar calendar = Calendar.getInstance();
+ Calendar calendar2 = Calendar.getInstance();
+
+ // 9:00 on
+ calendar.set(Calendar.HOUR_OF_DAY, 9);
+ calendar.set(Calendar.MINUTE, 0);
+ calendar.set(Calendar.SECOND, 0);
+ PendingIntent pi1 = PendingIntent.getService(GuesstimateVelocityBetter.this, 0, new Intent(GuesstimateVelocityBetter.this, VelocityEstimator.class), PendingIntent.FLAG_UPDATE_CURRENT);
+ am.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pi1);
+
+ // 21:00 off
+ calendar.set(Calendar.HOUR_OF_DAY, 21);
+ calendar.set(Calendar.MINUTE, 0);
+ calendar.set(Calendar.SECOND, 0);
+ PendingIntent pi2 = PendingIntent.getService(GuesstimateVelocityBetter.this, 1, new Intent(GuesstimateVelocityBetter.this, VelocityEstimator.class), PendingIntent.FLAG_UPDATE_CURRENT);
+ am.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pi2);
+
+ }
+ */
}
+/*
+public class Alarm extends BroadcastReceiver
+{
+ @Override
+ public void onReceive(Context context, Intent intent)
+ {
+ PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
+ PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "YOUR TAG");
+ wl.acquire();
+
+ // Put here YOUR code.
+ Toast.makeText(context, "WAKE UP, the TRAM starts!", Toast.LENGTH_LONG).show(); // For example
+
+ wl.release();
+ }
+
+ public void SetAlarm(Context context)
+ {
+ AlarmManager am=(AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
+ Intent i = new Intent(context, Alarm.class);
+ PendingIntent pi = PendingIntent.getBroadcast(context, 0, i, 0);
+ am.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), 1000 * 60 * 10, pi); // Millisec * Second * Minute
+ }
+
+ public void CancelAlarm(Context context)
+ {
+ Intent intent = new Intent(context, Alarm.class);
+ PendingIntent sender = PendingIntent.getBroadcast(context, 0, intent, 0);
+ AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
+ alarmManager.cancel(sender);
+ }
+
+}
+ */
Oops, something went wrong.

0 comments on commit e15ee65

Please sign in to comment.