Created
December 27, 2014 07:54
-
-
Save rageandqq/754b5d7471856c4a4a51 to your computer and use it in GitHub Desktop.
Pebble - Average AccelData for an array of Accelerometer Inputs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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