Skip to main content

Detect shake

Guide specification
Guide type:Studio code
Requirements:Android device with an accelerometer
Recommended reading:None

Introduction

In this guide we develop a model for detecting shake movement in an Android phone. Developing a detect shake model has almost become the "Hello world" of edge analytics. So naturally we had to include a detect shake guide in the SA Engine documentation. Shake detection models are not only for illustrative purposes though. They can be quite useful to detect fall impacts or vibrations in machinery.

Shake detection models can be used to detect impacts from falls.

Connect your edge device

Start by connecting your Android device to the local federation. How to download, install and connect an Android device is described in Connecting edge devices in the SA Studio manual. In this guide we use the name "ANDROID-EDGE" for our Android edge client.

Verify connection

To verify that the edge device is connected we use listening_edges() function. Your edge device should be listed in the result.

listening_edges();
Not connected

To run this code block you must be logged in and your studio instance must be started.

Examine the sensors

Now that we have verified that our Android edge client is connected we can query the Android directly to examine the available sensors on the Android. Note that we are using //peer: android-edge to tell SA Studio to run the code on the Android device.

//peer: android-edge
signals();
Not connected

To run this code block you must be logged in and your studio instance must be started.

The query returns a set of the names of all the signals on the device and should include some of the following.

"accelerometer"
"magnetometer"
"gyroscope"
"light"
"step_detector"
"step_counter"

We can use signal_stream('accelerometer') to display the accelerometer stream.

//peer: android-edge
//plot: Line plot
signal_stream('accelerometer');
Not connected

To run this code block you must be logged in and your studio instance must be started.

Try shaking the phone a bit it and see how the readings change. See if you can determine which movement direction corresponds to which line in the plot.

Develop the model

Now that we have examined the accelerometer it is time to develop the detect shake model. Our model will use the accelerometer to signal if the phone is shaking. In its final form the model will signal 1 if the phone starts shaking, and 0 if it stops.

First we define a stream filter function that combines the three gravitation vectors in the accelerometer stream:

//peer: android-edge
create function gravity_acceleration() -> Stream of Number
as select Stream of sqrt(sum(g .^ 2))
from Vector of Number g
where g in signal_stream("accelerometer");
Not connected

To run this code block you must be logged in and your studio instance must be started.

We can test the helping function by running the following query:

//peer: android-edge
//plot: Line plot
stdev(winagg(gravity_acceleration(), 50, 5));
Not connected

To run this code block you must be logged in and your studio instance must be started.

The query passes "a window" over the stream and computes the standard deviation on each window. The standard deviation will be zero when the phone is not moving and take on positive values when the accelerator readings change. You can shake the phone and see how the plot changes.

Now we use the stddev() expression to create a function that signals if the shaking is above some threshold:

//peer: android-edge
create function shake_state(Number threshold) -> Stream of Number
as select Stream of shakes
from Number shakes, Number elem
where elem in stdev(winagg(gravity_acceleration(), 50, 5))
and shakes = case when elem > threshold then 1
else 0 end;
Not connected

To run this code block you must be logged in and your studio instance must be started.

Test the shake state function by running the function and shake the phone:

//peer: android-edge
//plot: Line plot
shake_state(5);
Not connected

To run this code block you must be logged in and your studio instance must be started.

The output plot should show a step function with value 1 if the phone is shaking and 0 when the phone is still.

The model is almost done, but we only want to emit values when the shake state changes. We accomplish this by using the function changed() with the result from shake_state() as input:

//peer: android-edge
create function shakes(Number threshold) -> Stream of Number
as changed(shake_state(threshold));
Not connected

To run this code block you must be logged in and your studio instance must be started.

Now the model is finished. Try the model by calling shakes() and alternate between shaking the phone and holding it still:

//peer: android-edge
shakes(5);
Not connected

To run this code block you must be logged in and your studio instance must be started.

It should emit the value 1 when the phone starts shaking, and the value 0 when it stops. Just what we wanted!

0
1
0
1
0
1
0
1
0

Conclusion

In this guide we implemented and deployed a detect shake model on an Android phone.