Guest User

Untitled

a guest
Oct 17th, 2017
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.29 KB | None | 0 0
  1. typedef
  2. struct {
  3. double x,y,z;
  4. }
  5. vec_d3;
  6.  
  7. #define RECENT_COUNT 10
  8.  
  9. - (void) accelerometer: (UIAccelerometer *) A
  10. didAccelerate: (UIAcceleration *) a
  11. {
  12. static vec_d3 recent[ RECENT_COUNT ];
  13.  
  14. static int recentPtr = 0;
  15.  
  16.  
  17. // get |smoothed-acc|
  18. static vec_d3 smooth = { DOUBLE_EMPTY, DOUBLE_EMPTY, DOUBLE_EMPTY };
  19.  
  20. {
  21. if ( smooth.x == DOUBLE_EMPTY )
  22. {
  23. smooth.x = a.x;
  24. smooth.y = a.y;
  25. smooth.z = a.z;
  26.  
  27. return;
  28. }
  29.  
  30. #define SMOOTHING_FACTOR_XYZ 0.9
  31. smooth.x = SMOOTHING_FACTOR_XYZ * smooth.x + (1.-SMOOTHING_FACTOR_XYZ) * a.x;
  32. smooth.y = SMOOTHING_FACTOR_XYZ * smooth.y + (1.-SMOOTHING_FACTOR_XYZ) * a.y;
  33. smooth.z = SMOOTHING_FACTOR_XYZ * smooth.z + (1.-SMOOTHING_FACTOR_XYZ) * a.z;
  34. }
  35.  
  36. static BOOL gotEnoughData = NO;
  37.  
  38. recent[ recentPtr ] = smooth;
  39.  
  40. recentPtr++;
  41. if ( recentPtr == RECENT_COUNT )
  42. {
  43. recentPtr = 0;
  44. if ( ! gotEnoughData )
  45. gotEnoughData = YES;
  46. }
  47.  
  48. if ( ! gotEnoughData )
  49. return;
  50.  
  51. vec_d3 min = smooth, max = smooth;
  52.  
  53. for ( int i=0; i < RECENT_COUNT; i++ )
  54. {
  55. min.x = MIN( min.x, recent[ i ].x );
  56. min.y = MIN( min.y, recent[ i ].y );
  57. min.z = MIN( min.z, recent[ i ].z );
  58.  
  59. max.x = MAX( max.x, recent[ i ].x );
  60. max.y = MAX( max.y, recent[ i ].y );
  61. max.z = MAX( max.z, recent[ i ].z );
  62. }
  63.  
  64. vec_d3 variation =
  65. {
  66. .x = max.x - min.x,
  67. .y = max.y - min.y,
  68. .z = max.z - min.z
  69. };
  70.  
  71. double mag_variation = sqrt(
  72. variation.x * variation.x +
  73. variation.y * variation.y +
  74. variation.z * variation.z
  75. );
  76.  
  77. #define MAGVAR_SMOOTH_FACTOR 0.9
  78. static double mv_smoothed = DOUBLE_EMPTY;
  79. if ( mv_smoothed == DOUBLE_EMPTY )
  80. {
  81. mv_smoothed = mag_variation;
  82. return;
  83. }
  84. mv_smoothed = MAGVAR_SMOOTH_FACTOR * mv_smoothed + (1. - MAGVAR_SMOOTH_FACTOR) * mag_variation;
  85.  
  86.  
  87. // see if it's just passed a peak
  88. {
  89. static double varSmoothed_last = DOUBLE_EMPTY;
  90. if ( varSmoothed_last == DOUBLE_EMPTY )
  91. {
  92. varSmoothed_last = mv_smoothed;
  93. return;
  94. }
  95.  
  96. static double varSmoothed_preLast = DOUBLE_EMPTY;
  97. if ( varSmoothed_preLast == DOUBLE_EMPTY )
  98. {
  99. varSmoothed_preLast = varSmoothed_last;
  100. varSmoothed_last = mv_smoothed;
  101. return;
  102. }
  103.  
  104. #define THRESHOLD_IMPULSE .05
  105.  
  106. if ( varSmoothed_last > varSmoothed_preLast
  107. && varSmoothed_last > mv_smoothed
  108. && varSmoothed_last > THRESHOLD_IMPULSE )
  109. {
  110. LOG ( @"PotPeak @ %f", varSmoothed_last );
  111.  
  112. // hit a peak at imp_last
  113. if (1)
  114. [self peakedWithImpulse: varSmoothed_last ];
  115. }
  116.  
  117. varSmoothed_preLast = varSmoothed_last;
  118. varSmoothed_last = mv_smoothed;
  119. }
  120.  
  121.  
  122. //printf( "%f, ", (float) mag_variation );
  123. }
Add Comment
Please, Sign In to add comment