Data reduction on edge devices
Guide specification | |
---|---|
Guide type: | Studio code |
Requirements: | Android device with accelerometer |
Recommended reading: | Basic data reduction |
Introduction
In this guide we look at how to reduce data from an edge device. We will use an Android phone for this example.
The steps here are applicable for all types of edge devices. If you do not have an Android phone you can simply connect another edge device with sensors and replace the use of the accelerometer in the examples with any sensor supported by SA Engine.
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();
To run this code block you must be logged in and your studio instance must be started.
When we have verified that our Android edge client is connected we can use signal_stream('accelerometer')
to display the accelerometer stream and verify that our connection is working.
//peer: android-edge
//plot: Line plot
signal_stream('accelerometer');
To run this code block you must be logged in and your studio instance must be started.
On-device time-based sampling
Now that we have connected our Android device and verified that it can send data from the accelerometer, we can create a function that samples the accelerometer and takes the sampling rate as input.
//peer: android-edge
create function sample_accelerometer(Number samplingrate) -> Vector
as select [timestamp(tsv), value(tsv)[1]]
from Timeval of Vector tsv, stream of timeval s,
stream of vector sov
where sov = signal_stream("accelerometer")
and s = streamof(ts(sov))
and tsv in twinagg(s, samplingrate, samplingrate);
To run this code block you must be logged in and your studio instance must be started.
The function returns a vector with the timestamp and the sampled reading of the three accelerometer values. Try the sampling function with a sampling rate of 1.0 seconds.
//peer: android-edge
sample_accelerometer(1.0);
To run this code block you must be logged in and your studio instance must be started.
You can see on the timestamps that the stream is sampled every second. To plot the sampled values you can create a wrapper function that simply extracts the readings and drops the timestamps.
//peer: android-edge
create function sample_accelerometer_values(Number samplingrate) -> Vector
as select sample_accelerometer(samplingrate)[2];
To run this code block you must be logged in and your studio instance must be started.
Try the value-sampling function and see how the accelerometer value plot is updated every 0.5 seconds.
//peer: android-edge
//plot: Line plot
sample_accelerometer_values(0.5);
To run this code block you must be logged in and your studio instance must be started.
On-device statistics over streams
Now we take the concepts used for vector-valued streams in the Basic data reduction guide and create a function that does some statistics on the accelerometer readings.
//peer: android-edge
create function acc_mean_stream(Number winsize)
-> Stream of Vector of number
as select [mean(t[1]), mean(t[2]), mean(t[3])]
from Matrix m, Matrix t
where m in winagg(signal_stream("accelerometer"),winsize,1)
and t = transpose(m);
To run this code block you must be logged in and your studio instance must be started.
Now you can get smoothed accelerometer readings by calling acc_mean_stream()
with some window size.
//peer: android-edge
//plot: Line plot
acc_mean_stream(20);
To run this code block you must be logged in and your studio instance must be started.
Conclusion
In this guide we took the concepts from the Basic data reduction guide and tired them to a real live edge device.