java - Is there any way to create a fixed accelerometer sensor delay on my Android app? I want to achieve, exactly, 20 Hz sampling rate
问题描述
I want to simulate a fixed accelerometer sensor delay with sampling rate 20Hz. (600 samples/ 30sec)
This is for a Human Activity Recognition Android mobile app, that uses TensorFlow to predict the activity of a user. I experimented with fixed SENSOR_DELAY
(NORMAL, UI, FASTEST) and samplingPeriodUS. I noticed that with the custom value on samplingPeriodUS
you cannot get a consistent delay, as the sampling rate alters constantly. I ended up, using the SENSOR_DELAY_NORMAL
as it gives me a consistent sampling rate, nearly 46Hz.
With this code I collect a 600 accelerometer samples (x,y,z) and then add it to an Arraylist that sends it to Tensorflow to return the probabilities.
With this set I expect to get 600 samples per 13-14 seconds. I want to achieve a 30 seconds delay(20Hz).
Is there any way achieving it with Thread.Sleep
, ScheduledExecutorService
or maybe a Linear Interpolation?
The source code's taken from curiousily and has been slightly modified: Github Link
protected void onResume() {
super.onResume();
getSensorManager().registerListener(this, getSensorManager().getDefaultSensor(Sensor.TYPE_ACCELEROMETER), SensorManager.SENSOR_DELAY_NORMAL);
}
@Override
public void onSensorChanged(SensorEvent event) {
specificActivityPrediction(activity);
x.add(event.values[0]);
y.add(event.values[1]);
z.add(event.values[2]);
}
private void specificActivityPrediction(String activity) {
if (x.size() == N_SAMPLES && y.size() == N_SAMPLES && z.size() == N_SAMPLES) {
data = new ArrayList<>();
data.addAll(x);
data.addAll(y);
data.addAll(z);
results = classifier.predictProbabilities(toFloatArray(data));
downstairsTextView.setText(Float.toString(round(results[0], 2)));
joggingTextView.setText(Float.toString(round(results[1], 2)));
sittingTextView.setText(Float.toString(round(results[2], 2)));
standingTextView.setText(Float.toString(round(results[3], 2)));
upstairsTextView.setText(Float.toString(round(results[4], 2)));
walkingTextView.setText(Float.toString(round(results[5], 2)));
sendProbabilities(activity);
x.clear();
y.clear();
z.clear();
}
}
解决方案
推荐阅读
- javascript - 拖放的角度问题
- node-red - 创建从节点 red 到 watson 对话的上下文变量
- spring-boot - Angular 6服务工作,未加载在春季战争中
- azure-data-factory - ADF - Web 活动 - “字符串”与字段不匹配:“正文”
- node.js - 等待事务完成以在 Node.js/Solidity 中执行
- c++ - 在 C++ 中编辑类私有
- visual-studio - 如何在 Visual Studio 2017 中通过 cmake 启用 Visual Leak Detector?
- android - MPAndroidChart 绘制多个使用共享 X 轴垂直排列的图表
- c - sscanf 的返回值
- python - 子流程“echo $ENVIRONMENT_VAR”输出“$ENVIRONMENT_VAR”