Skip to content

Commit

Permalink
Det virker?
Browse files Browse the repository at this point in the history
  • Loading branch information
andersahp committed Nov 23, 2017
1 parent 4c51293 commit 8f03df7
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 49 deletions.
2 changes: 2 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.example.ulrich.contextapp.datawindow;

/**
* Created by Anders on 23-11-2017.
*/

public class DataWindow {
private float min, max, stDevMag;

public DataWindow(float min, float max, float stDevMag)
{
this.min = min;
this.max = max;
this.stDevMag = stDevMag;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,90 +3,108 @@
/**
* Created by anders on 11/21/17.
*/
import android.content.Context;

import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;


import java.util.*;
import java.util.Collections;


import weka.filters.*;
import weka.classifiers.*;
import weka.core.*;

import android.util.Log;
import com.example.ulrich.contextapp.datawindow.DataWindow;
import java.util.ArrayList;
import java.lang.Math;

public class AccelerometerWidget implements SensorEventListener{

private SensorManager senSensorManager;
private Sensor senAccelerometer;
private long lastUpdate = 0;
private float last_x, last_y, last_z;
private static final int SHAKE_THRESHOLD = 600;
private ArrayList <Double> dataList = new ArrayList<Double>();
private ArrayList<DataWindow> dataWindows= new ArrayList<>();
private float[] xReadings = new float[128];
private float[] yReadings = new float[128];
private float[] zReadings = new float[128];
private int currentIndex = 0;
private final int SAMPLE_FREQUENCY = 40;

public AccelerometerWidget(SensorManager manager){
senSensorManager = manager;
senAccelerometer = senSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
senSensorManager.registerListener(this, senAccelerometer , SensorManager.SENSOR_DELAY_NORMAL);
}

@Override
public void onAccuracyChanged(Sensor sensor, int i) {
}
@Override
public void onSensorChanged(SensorEvent sensorEvent) {
Sensor mySensor = sensorEvent.sensor;

if (mySensor.getType() == Sensor.TYPE_ACCELEROMETER) {
float x = sensorEvent.values[0];
float y = sensorEvent.values[1];
float z = sensorEvent.values[2];

float r = x*x + y*y + z*z;
double k = Math.sqrt(r);

long curTime = System.currentTimeMillis();

if ((curTime - lastUpdate) > 200) {
long diffTime = (curTime - lastUpdate);
// Only add data point for each SAMPLE_FREQUENCY ms
if ((curTime - lastUpdate) > SAMPLE_FREQUENCY){
lastUpdate = curTime;
float speed = Math.abs(x + y + z - last_x - last_y - last_z)/ diffTime * 10000;
//TextView hej =(TextView)findViewById(R.id.hej);
//hej.setText(k + "");
dataList.add(k);
if (speed > SHAKE_THRESHOLD) {

float x = sensorEvent.values[0];
float y = sensorEvent.values[1];
float z = sensorEvent.values[2];

if(currentIndex > 127)
currentIndex = 0;

xReadings[currentIndex] = x;
yReadings[currentIndex] = y;
zReadings[currentIndex] = z;

// When adding a datapoint to the middle or the end of the x,y,z reading arrays -
// Make a new DataWindow object (the last check is to avoid making a DataWindow for-
// the first 64 readings.)
Log.d("hey","currentIndex: "+ currentIndex + " : " + (xReadings[127] != 0L));
if((currentIndex == 63 || currentIndex == 127) && xReadings[127] != 0L)
{
makeDataWindow();
}

last_x = x;
last_y = y;
last_z = z;
currentIndex++;
}
}
}

public void getMax(){
double i = Collections.max(dataList);
//TextView max =(TextView)findViewById(R.id.max);
//max.setText(i + "");
}

public void getMin(){
double i = Collections.min(dataList);
//TextView min =(TextView)findViewById(R.id.min);
//min.setText(i + "");
}
private void makeDataWindow()
{

public void getStdDevOfMagnitude(){}
// Calculate min, max
int N = 128; //window size
float min = Float.MAX_VALUE;
float max = Float.MIN_VALUE;
float sum = 0;
for (int i = 0; i < N; i++) {
float val = sample(xReadings[i],yReadings[i],zReadings[i]);
if(val < min) { min = val; }
if(val > max) { max = val; }
sum += val;
}

// Calculate stdDev
float mean = sum/N;
float summedDifference = 0;
for (int i = 0; i < N; i++) {
summedDifference += Math.pow(sample(xReadings[i],yReadings[i],zReadings[i]) - mean,2);
}
float stdDev = (float)Math.sqrt(summedDifference / (N-1)); //standard deviation

Log.d("newWindow", "Making new datawindow: " + min + ":" + max + ":" +stdDev );
// Add new DataWindow
DataWindow newWindow = new DataWindow(min, max,stdDev);
dataWindows.add(newWindow);
}

private float sample(float x, float y, float z){
double x_squared = Math.pow(x,2);
double y_squared = Math.pow(y,2);
double z_squared = Math.pow(z,2);
return (float)Math.sqrt(x_squared + y_squared + z_squared);
}

@Override
public void onAccuracyChanged(Sensor sensor, int i) {}

}

0 comments on commit 8f03df7

Please sign in to comment.