首页 > 解决方案 > 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();
    }
}

标签: javaandroid

解决方案


推荐阅读