iOS:从加速度计输出准确确定凸起的能量

前端之家收集整理的这篇文章主要介绍了iOS:从加速度计输出准确确定凸起的能量前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在创建一个音叉应用程序,您可以将iPhone拍到另一只手的手掌上,或者在柔软的表面上拍摄,以便设置叉子.

所以我想检测每个’凹凸’中包含的能量

(编辑:删除了一吨gumpf)

任何人都可以帮我破解这个吗?

解决方法

感谢freenode #math频道上的一位向导(感谢Igor),我有一个非常好的工作解决方案.

您可以使用标准方法以尽可能最大的频率(100Hz)触发回调,该频率将包含瞬时加速度x,y,z值.

我会让代码说明一切,好的代码应该总是说明一切.

  1. typedef
  2. struct {
  3. double x,z;
  4. }
  5. vec_d3;
  6.  
  7. #define RECENT_COUNT 10
  8.  
  9. #define SMOOTH_IP( x,x_new,fac ) x = fac * x + ( 1. - fac ) * x_new
  10.  
  11. - (void) accelerometer: (UIAccelerometer *) accelerometer
  12. didAccelerate: (UIAcceleration *) acceleration
  13. {
  14. // smooth incoming acceleration values
  15. static vec_d3 smooth = { DOUBLE_EMPTY,0 };
  16.  
  17. {
  18. if ( smooth.x == DOUBLE_EMPTY )
  19. {
  20. smooth.x = acceleration.x;
  21. smooth.y = acceleration.y;
  22. smooth.z = acceleration.z;
  23.  
  24. return;
  25. }
  26.  
  27. SMOOTH_IP( smooth.x,acceleration.x,0.9 );
  28. SMOOTH_IP( smooth.y,acceleration.y,0.9 );
  29. SMOOTH_IP( smooth.z,acceleration.z,0.9 );
  30. }
  31.  
  32. // keep track of last k smoothed acceleration values
  33. static vec_d3 recent[ RECENT_COUNT ];
  34. {
  35. static int ptr = 0;
  36. static BOOL gotEnoughData = NO;
  37.  
  38. recent[ ptr ] = smooth;
  39.  
  40. ptr++;
  41. if ( ptr == RECENT_COUNT )
  42. {
  43. ptr = 0;
  44. gotEnoughData = YES;
  45. }
  46.  
  47. // return if array not filled yet
  48. if ( ! gotEnoughData )
  49. return;
  50. }
  51.  
  52. // get the resultant variation in acceleration over the whole array
  53. double variation;
  54. {
  55. vec_d3 min = smooth,max = smooth;
  56.  
  57. for ( int i=0; i < RECENT_COUNT; i++ )
  58. {
  59. min.x = MIN( min.x,recent[ i ].x );
  60. min.y = MIN( min.y,recent[ i ].y );
  61. min.z = MIN( min.z,recent[ i ].z );
  62.  
  63. max.x = MAX( max.x,recent[ i ].x );
  64. max.y = MAX( max.y,recent[ i ].y );
  65. max.z = MAX( max.z,recent[ i ].z );
  66. }
  67.  
  68. vec_d3 V = (vec_d3)
  69. {
  70. .x = max.x - min.x,.y = max.y - min.y,.z = max.z - min.z
  71. };
  72.  
  73. variation = sqrt(
  74. V.x * V.x +
  75. V.y * V.y +
  76. V.z * V.z
  77. );
  78. }
  79.  
  80. // smooth it
  81. static double var_smoothed = DOUBLE_EMPTY;
  82. {
  83. if ( var_smoothed == DOUBLE_EMPTY )
  84. {
  85. var_smoothed = variation;
  86. return;
  87. }
  88. SMOOTH_IP( var_smoothed,variation,0.9 );
  89. }
  90.  
  91.  
  92. // see if it's just passed a peak
  93. {
  94. static double varSmoothed_last = DOUBLE_EMPTY;
  95. if ( varSmoothed_last == DOUBLE_EMPTY )
  96. {
  97. varSmoothed_last = var_smoothed;
  98. return;
  99. }
  100.  
  101. static double varSmoothed_preLast = DOUBLE_EMPTY;
  102. if ( varSmoothed_preLast == DOUBLE_EMPTY )
  103. {
  104. varSmoothed_preLast = varSmoothed_last;
  105. varSmoothed_last = var_smoothed;
  106. return;
  107. }
  108.  
  109. #define THRESHOLD_IMPULSE .15
  110.  
  111. if ( varSmoothed_last > varSmoothed_preLast
  112. && varSmoothed_last > var_smoothed
  113. && varSmoothed_last > THRESHOLD_IMPULSE )
  114. {
  115. LOG ( @"PotPeak @ %f",varSmoothed_last );
  116.  
  117. // hit a peak at imp_last
  118. [self peakedWithImpulse: varSmoothed_last ];
  119. }
  120.  
  121. varSmoothed_preLast = varSmoothed_last;
  122. varSmoothed_last = var_smoothed;
  123. }
  124. }

猜你在找的iOS相关文章