Skip to content

Instantly share code, notes, and snippets.

@kevinzhang96
Created November 7, 2015 23:52
Show Gist options
  • Save kevinzhang96/66f03ebe20a90cfbb8a2 to your computer and use it in GitHub Desktop.
Save kevinzhang96/66f03ebe20a90cfbb8a2 to your computer and use it in GitHub Desktop.
Approximate stop frequency of movement using strided averages
keep track of up to last 3 stops
each time we stop, take average of last 3 times of stops; invert and that’s the BPM
double x[NUMBER];
double y[NUMBER];
long times[NUMBER];
long last[3] = {0};
double last_dist = UDOUBLE_MAX;
int curr_last = 0;
for (int i = 0; i < NUMBER - 1; i++) {
double distance = Math.sqrt(x[i]*x[i] + y[i]*y[i]);
if (last_dist > 0.3 && distance <= 0.3) {
last[curr_last] = times[i];
curr_last = (curr_last + 1) % 3;
long sum = 0;
int total = 0;
for (int i = 0; i < 3; i++) {
if (last[i] != 0) {
sum += last[i];
total += 1;
}
}
printf(“New BPM: “ (total / sum * 1000 * 60)); // multiply by 1000 cuz milliseconds, 60 cuz minute
}
last_dist = distance;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment