Fall Detection Algorithm - liuyiming091/falldetector GitHub Wiki
Fall Detection Algorithm
private void AddData(final double ax2,final double ay2,final double az2){
TimerTask task = new TimerTask(){
public void run(){
a_norm= Math.sqrt(ax2*ax2+ay2*ay2+az2*az2);
for(i=0;i<=500-2;i++){
win[i]=win[i+1];
}
win[500-1]=a_norm;//execute the task
}
};
Timer timer = new Timer();
timer.schedule(task,100);
}
//fall detection algorithm
private void Fall(double[] window2) {
sumText.setText("Working");
int tmax = 1;
int tmin = 1;
int i = 1;
diff[1] = window2[0];
for (i = 2; i < 500; i++) {
diff[i] = window2[i - 1] - window2[i - 2];
}
double max = diff[1];
double min = diff[1];
for (i = 1; i < 500; i++) {
if (max < diff[i])
tmax = i;
if (min > diff[i])
tmin = i;
}
double cha = window2[tmax - 1] - window2[tmin - 1];
if ((cha < 2 * 9.8) || tmax < tmin) {
tmin = 0;
tmax = 0;
} else if (cha > 2 * 9.8) {
sumText.setText("FALL!");
sensorManager.unregisterListener(threeParamListener);
this.finish();
Intent it=new Intent(AccelActivity.this,AlertActivity.class);
startActivity(it);
}
}
Above is the core part of the fall detection algorithm.
-
We use the accelerometer on the smart phone and get the value on 3 axis, X, Y and Z, the value shows the accelerator of each axis.
-
The module of acceleration (Acc=√ ((Ax^2)+(Ay^2)+(Az^2))) is calculated and saved in the array win[] with the SIZE of 500, the window moves as the value of the sensor changes.
-
Then we will continuously search for the Maximum and Minimum value in the win[] array and store it when it keeps changing. If the difference between the max and min value is higher than 2g, we would say that a fall happens.
-
Once the fall is detected, the application would generate the alert to ask if the user is fine. If the user doesn't respond for more than 10 seconds, the application would send the emergency message to the contact.