Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- typedef
- struct {
- double x,y,z;
- }
- vec_d3;
- #define RECENT_COUNT 10
- - (void) accelerometer: (UIAccelerometer *) A
- didAccelerate: (UIAcceleration *) a
- {
- static vec_d3 recent[ RECENT_COUNT ];
- static int recentPtr = 0;
- // get |smoothed-acc|
- static vec_d3 smooth = { DOUBLE_EMPTY, DOUBLE_EMPTY, DOUBLE_EMPTY };
- {
- if ( smooth.x == DOUBLE_EMPTY )
- {
- smooth.x = a.x;
- smooth.y = a.y;
- smooth.z = a.z;
- return;
- }
- #define SMOOTHING_FACTOR_XYZ 0.9
- smooth.x = SMOOTHING_FACTOR_XYZ * smooth.x + (1.-SMOOTHING_FACTOR_XYZ) * a.x;
- smooth.y = SMOOTHING_FACTOR_XYZ * smooth.y + (1.-SMOOTHING_FACTOR_XYZ) * a.y;
- smooth.z = SMOOTHING_FACTOR_XYZ * smooth.z + (1.-SMOOTHING_FACTOR_XYZ) * a.z;
- }
- static BOOL gotEnoughData = NO;
- recent[ recentPtr ] = smooth;
- recentPtr++;
- if ( recentPtr == RECENT_COUNT )
- {
- recentPtr = 0;
- if ( ! gotEnoughData )
- gotEnoughData = YES;
- }
- if ( ! gotEnoughData )
- return;
- vec_d3 min = smooth, max = smooth;
- for ( int i=0; i < RECENT_COUNT; i++ )
- {
- min.x = MIN( min.x, recent[ i ].x );
- min.y = MIN( min.y, recent[ i ].y );
- min.z = MIN( min.z, recent[ i ].z );
- max.x = MAX( max.x, recent[ i ].x );
- max.y = MAX( max.y, recent[ i ].y );
- max.z = MAX( max.z, recent[ i ].z );
- }
- vec_d3 variation =
- {
- .x = max.x - min.x,
- .y = max.y - min.y,
- .z = max.z - min.z
- };
- double mag_variation = sqrt(
- variation.x * variation.x +
- variation.y * variation.y +
- variation.z * variation.z
- );
- #define MAGVAR_SMOOTH_FACTOR 0.9
- static double mv_smoothed = DOUBLE_EMPTY;
- if ( mv_smoothed == DOUBLE_EMPTY )
- {
- mv_smoothed = mag_variation;
- return;
- }
- mv_smoothed = MAGVAR_SMOOTH_FACTOR * mv_smoothed + (1. - MAGVAR_SMOOTH_FACTOR) * mag_variation;
- // see if it's just passed a peak
- {
- static double varSmoothed_last = DOUBLE_EMPTY;
- if ( varSmoothed_last == DOUBLE_EMPTY )
- {
- varSmoothed_last = mv_smoothed;
- return;
- }
- static double varSmoothed_preLast = DOUBLE_EMPTY;
- if ( varSmoothed_preLast == DOUBLE_EMPTY )
- {
- varSmoothed_preLast = varSmoothed_last;
- varSmoothed_last = mv_smoothed;
- return;
- }
- #define THRESHOLD_IMPULSE .05
- if ( varSmoothed_last > varSmoothed_preLast
- && varSmoothed_last > mv_smoothed
- && varSmoothed_last > THRESHOLD_IMPULSE )
- {
- LOG ( @"PotPeak @ %f", varSmoothed_last );
- // hit a peak at imp_last
- if (1)
- [self peakedWithImpulse: varSmoothed_last ];
- }
- varSmoothed_preLast = varSmoothed_last;
- varSmoothed_last = mv_smoothed;
- }
- //printf( "%f, ", (float) mag_variation );
- }
Add Comment
Please, Sign In to add comment