Skip to content

Instantly share code, notes, and snippets.

@rageandqq
Created December 27, 2014 07:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rageandqq/754b5d7471856c4a4a51 to your computer and use it in GitHub Desktop.
Save rageandqq/754b5d7471856c4a4a51 to your computer and use it in GitHub Desktop.
Pebble - Average AccelData for an array of Accelerometer Inputs
// Returns the mathematical mean (average) of a number of samples of accelerometer data.
// Sets the x, y, and z values as a mean.
// Sets 'did_vibrate' to true if the pebble vibrated during any of the samples.
// Time stamp is set to the time stamp of the first sample.
AccelData accel_data_mean(AccelData *data, uint32_t num_samples) {
// Declare the struct
AccelData average_data;
// Time taken is set to the first sample's time
average_data.timestamp = data[0].timestamp;
// Initialize x, y, and z values
average_data.x = average_data.y = average_data.z = 0;
// Calculate the sum of the x, y, and z values.
for (uint32_t i = 0; i < num_samples; i++) {
average_data.x += data[i].x;
average_data.y += data[i].y;
average_data.z += data[i].z;
// If any sample was taken during a vibration, the value of the mean is set to vibrate
if (data[i].did_vibrate) {
average_data.did_vibrate = true;
}
}
// Divide by number of samples to get the average
average_data.x /= num_samples;
average_data.y /= num_samples;
average_data.z /= num_samples;
return average_data;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment