Mismatch in Sensor Readings Using Edge Impulse APIs through Android Studio for Smartphone App

I am experiencing inconsistencies in sensor readings taken on my smartphone app using Edge Impulse APIs. Specifically, a 30-second reading appears to be shortened randomly on Edge Impulse, which affects the reliability of the data collected. I suspect this issue might be related to sampling rates.

Our project involves using smartphone sensors to collect and process data for human activity recognition. We rely on consistent data uploads to Edge Impulse for machine learning model training and inference.

Steps Taken:

  1. Implemented sensor data collection in the Android app using SensorManager.

  2. Set the sensor sampling rate with mSensorManager.registerListener(sensorEventListener, mAccelerometer, SensorManager.SENSOR_DELAY_NORMAL).

  3. Uploaded the sensor readings to Edge Impulse using the provided APIs:
    private void sendToEdgeImpulse(List<float[]> accelerometerData) {
    JSONObject jsonData = formatRealTimeData(accelerometerData);
    OkHttpClient client = new OkHttpClient();

     ByteArrayOutputStream bos = new ByteArrayOutputStream();
     try {
         bos.write(jsonData.toString().getBytes("UTF-8"));
     } catch (IOException e) {
         throw new RuntimeException(e);
     }
     RequestBody body = RequestBody.create(bos.toByteArray(), MediaType.parse("application/json"));
    
     if (body != null) {
         Log.d("EdgeImpulse", "RequestBody created successfully");
     } else {
         Log.e("EdgeImpulse", "Failed to create RequestBody");
     }
    
     Request request = new Request.Builder()
             .url("https://ingestion.edgeimpulse.com/api/training/data")
             .addHeader("x-api-key", "ei_62163debcc80ef9f0b41205469a830a7fa8bc305eb2409da")
             .addHeader("x-label", activityType) // Send the chosen activity as label
             .addHeader("Content-Type", "application/json")
             .addHeader("X-File-Name", "real_time_data.json")
             .post(body)
             .build();
    
     client.newCall(request).enqueue(new Callback() {
         @Override
         public void onFailure(Call call, IOException e) {
             runOnUiThread(() -> txtProgress.setText("Upload failed"));
             Log.d("EdgeImpulse", "Upload failed: " + e.getMessage());
         }
    
         @Override
         public void onResponse(Call call, Response response) throws IOException {
             if (!response.isSuccessful()) {
                 runOnUiThread(() -> txtProgress.setText("Upload failed"));
                 Log.d("EdgeImpulse", "Upload failed with response code: " + response.code());
                 throw new IOException("Unexpected code " + response);
             }
             runOnUiThread(() -> {
                 txtProgress.setText("Data uploaded successfully");
             });
         }
     });
    

    }

Expected Outcome:

I expected the sensor readings taken over a longer period to be accurately reflected on Edge Impulse without random truncation or loss of data.

Actual Outcome:

The sensor readings appear to be randomly shortened on Edge Impulse, leading to data inconsistencies and challenges in analysis.

Reproducibility:

  • Always

I am also seeking guidance on the sampling rates for the sensors used and how to ensure that they align with Edge Impulse’s requirements. Any relevant documentation or support regarding sending data through APIs instead of QR code scanning would also be appreciated.

1 Like

Hi @iraj

Thank you for sharing this, my suggestion is to start by adjusting: SENSOR_DELAY_NORMAL.

Your sensors will need to be consistently sampled, you can batch process them and normalise them before uploading them in a given format.

My preference is to test this with a CSV export first with our CSV wizard, as it will be the easiest to use for validation.

Again here is the starting point to the API examples | Edge Impulse Documentation and if you continue with difficulty please speak with one of our solutions engineers for more guidance on your implementation. If you don’t have a trial or engagement ongoing set one up from here Contact Sales

Best

Eoin

1 Like

Hi @Eoin ,

Thank you for your response and suggestions.

I agree that the inconsistencies we’re experiencing likely stem from a mismatch between the Edge Impulse sampling rate and our sensor sampling rate. Here’s some information about the different sensor delays we’ve explored:

SENSOR_DELAY_NORMAL

  • Delay: 200,000 microseconds (200 milliseconds).
  • Sampling rate: 5 Hz (5 samples per second).

SENSOR_DELAY_UI

  • Delay: 60,000 microseconds (60 milliseconds).
  • Sampling rate: ~16.67 Hz (about 16-17 samples per second).

SENSOR_DELAY_GAME

  • Delay: 20,000 microseconds (20 milliseconds).
  • Sampling rate: 50 Hz (50 samples per second).

SENSOR_DELAY_FASTEST

  • Delay: 0 microseconds.
  • Sampling rate: Hardware-dependent, typically up to 100+ Hz.

Through experimentation, we’ve determined that Edge Impulse operates at a sampling rate of 62.5 Hz. However, there doesn’t appear to be an option to configure our sensors to match this exact rate.

Given that this is our first major project utilizing Edge Impulse, we would greatly appreciate any further guidance you or any reference to solutions engineers that may also provide guidance. Specifically, we are looking for advice on how best to align the sampling rates or any alternative approaches to ensure consistent data collection and upload.

Thank you for your assistance,
Iraj