If you have an Android phone, new or used, even without a SIM card as long as you have wifi – you can follow this tutorial. Apart from the phone itself (version 10 or higher) you'll only need a computer with a web browser. For this project, we're using the SA Studio platform by Stream Analyze, which has a no-strings-attached free account perfect for evaluation and test purposes. Easy sign up at https://studio.streamanalyze.com [link] and start your SA Studio instance.
On the left hand side of your browser of the SA Studio webapp there is a menu.
Click on
Devices > Get Started > Android
and scan the QR code shown there. Should you need a code scanning app if you camera app doesn't provide this, then search for "QR code reader" on Google Play.
This will take you to a download on AWS for the SA Engine app for Android OS, a nifty run-time engine that you can query and send commands and machine learning models to. You'll connect it to the SA Studio is the next step, and then we can start doing some fun things!
Click on Connected Edge
You'll find it on the left hand side in SA Studio menu, and select a name for your android device. There is another QR code for connecting just below, for connecting your phone with its new name. Go to the Andriod app called sa.edge, and press Setup and scan the Connect QR code and... you're connected!
Click
Federation Info
to verify
(found also under Devices in your SA Studio window). If your device is on the list, you're up and running!
Interacting with the Android clientIn SA Studio, you can interact with any and all the nodes in the federation (meaning the server as well as all connected edge nodes, clients running on various devices), and this time we'll choose our Android edge device. Yes, you can have more than one phone at once too, in fact you can hook up as many devices as you like!
We'll be using OSQL, a high-level search and query language which is pretty easy to learn and understand. First some warm-ups, in the OSQL editor, type
this_peerid();
and press the Go button, triangle in circle as seen in the screen shot above, which returns the name of the selected target device. Just pressing Go runs everything you've typed, while highlighting something runs that part only.
Now, we'd like to start a heartbeat, a generated stream of data streaming from the phone, and it's invoked as heartbeat(frequency), e.g. heartbeat(0.5). Practically, it returns the time alapsed in seconds since you started it, at a selected interval.
heartbeat(0.5);
This is a continuous query, running until you stop it, or the battery runs out on the Android device. Experiment with adding a sine function, to get the hang of sending queries to the device:
select sin(10*x)
from Number x
where x in heartbeat(0.05)
This code generates a sine wave signal based on the heartbeat signal. The sin()
function is applied to the heartbeat signal, which is generated using the heartbeat()
function. The heartbeat()
function returns a stream of values that simulates a heartbeat. The x
variable is used as a multiplier for the sine function, with a value of 10 in this case. So the output of this code would be a stream of values that represents the sine wave generated from the heartbeat signal, with a period of 0.05 seconds.
A short-hand way of issuing the same query would be to write it as
sin(10*heartbeat(.05))
applying the sine function to the generated value times 10, while I prefer the more verbose version myself.
Built into SA Studio, there is an elaborate math library with many useful functions. For instance, there are elaborate functions for simulating streams of data, such as the simulated harmonic signal generator simsig(), which applies a series of sine wave functions on a heartbeat of data. This is what the simsig function looks like under the hood;
sin(1.1*x)+sin(x/97)*sin(91*x)+sin(203*x)+sin(301*x)
The high-level function simstream() applies simsig(), taking the heartbeat and applies it to the heartbeat at a given pace.
If you would like time stamps to go with that, there is a sibling function ts_simstream() to handle that, which you can try out.
ts_simstream(0.1);
Windowing in SA StudioUp till now, we've been applying functions globally, applying to all data points since pressing Go, while often we want to apply analysis to the latest period of values. This is known as windowing, and there are corresponding functions in OSQL to handle such cases.
We use the function winagg() which holds three parameters; the function which it operates on, the number of samples to take into account, and the stride forwards to a new set of values.
winagg(sin(heartbeat(0.2)*1),10,10)
We're operating on the sinus value of the heartbeat(), for ten values, striding forward ten steps at a time, which renders a moving bar graph:
Another related function worth looking into is the moving average function mean(), for aggregating data over a selected time period:
mean(winagg(simstream(0.01),10,5))
This renders a plot over the moving average of the data stream, like the following looking at the lastest ten value, with a five pace stride:
Out of the box, some device platforms have sensors exposed to the the SA Engine runtime (e.g. the Android app), and you can use the signals() command to list them:
"accelerometer"
"magnetometer"
"gyroscope"
"light"
"step_detector"
"step_counter"
You can query the accelerometer readings simply by using the signal_stream() function, with the sensor as parameter:
signal_stream("accelerometer");
If you would want to calculate the earth's gravitational force at your location, you can write a short script, that you can try out.
select Stream of sqrt(sum(g .^ 2))
from Vector of Number g
where g in signal_stream("accelerometer");
Where I live in Sweden, the gravitational acceleration is close to 9.82 m/s^2 when my phone is at rest, which seems to line up with readings using considerably more expensive equipment.
Shake detectionMany interactions with phones and other mobile devices, use movement as part of the interaction. Calculating the standard deviation of movement over time, is one way of approaching how to monitor movement patterns, here with a helper function taking the input stream of accelerometer data from the "accelerometer" signal stream, calculate the magnitude of the acceleration vector for each value in the stream, and return a stream of the resulting values.
// Helper function
// calculates the magnitude of the acceleration vector by taking the
// square root of the sum of the squares of the X, Y, and Z components
// of the vector, and returns a stream of numbers
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");
stdev(winagg(gravity_acceleration(),50,5))
You can also read more in the in-line Docs, search for "accelerometer" and you'll find more on creating a working library for shake detection. Hope you found interest in the first steps towards how to analyse data streams from SA Engine running on an Android phone, and you've become curious to learn more!
Before we sign out; if you want to learn more about any of these functions, just issue the query sourcecode(); or doc(), e.g. for rending a simulated signal stream doc("simsig"); in the OSQL editor or look in the documentation under "Docs" in the menu. By navigating into Docs > Tutorial > Streams, you'll find interactive versions of the examples in this article for you to experiment with.
Comments
Please log in or sign up to comment.