fast_math.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. #ifndef _Fast_Math_H__
  2. #define _Fast_Math_H__
  3. #include <arm_math.h>
  4. #include "libs/utils.h"
  5. // Constants
  6. #define ONE_BY_SQRT3 (0.57735026919f) // 1/sqrt(3)
  7. #define TWO_BY_SQRT3 (2.0f * 0.57735026919f)
  8. #define SQRT3_BY_2 (0.86602540378f)
  9. #define SQRT3 (1.73205080757f)
  10. #define SQRT2_BY_SQRT3 (0.8164966f)
  11. #define M_PI (3.14159265f)
  12. #define ONE_BY_SQRT3_Q14 (9459L) //0.57735026919 * 16384.0F
  13. #define SQRT3_BY_2_Q14 (14189L)//0.86602540378 * 16384.0F
  14. #define TWO_BY_SQRT3_Q14 (18918L)
  15. #ifndef SQ
  16. #define SQ(x) ((x)*(x))
  17. #endif
  18. // nan and infinity check for floats
  19. #define UTILS_IS_INF(x) ((x) == (1.0F / 0.0F) || (x) == (-1.0F / 0.0F))
  20. #define UTILS_IS_NAN(x) ((x) != (x))
  21. #define UTILS_NAN_ZERO(x) (x = UTILS_IS_NAN(x) ? 0.0F : x)
  22. void fast_sincos(float angle, float *sin, float *cos);
  23. void SinCos_Lut(float angle, float *s, float *c);
  24. #define MATH_sat(in, minOut, maxOut) (min((maxOut), MAX((in), (minOut))))
  25. static __INLINE int32_t sclamp(int32_t v, int32_t minv, int32_t maxv) {
  26. if (v < minv) {
  27. return minv;
  28. }else if (v > maxv) {
  29. return maxv;
  30. }
  31. return v;
  32. }
  33. static __INLINE float fclamp(float v, float minv, float maxv) {
  34. if (v < minv) {
  35. return minv;
  36. }else if (v > maxv) {
  37. return maxv;
  38. }
  39. return v;
  40. }
  41. static void fast_norm_angle(float *angle) {
  42. *angle = fmodf(*angle, 360.0f);
  43. if (*angle < 0.0f) {
  44. *angle += 360.0f;
  45. }
  46. }
  47. /* 递增map */
  48. static __INLINE float f_map(float x, float in_min, float in_max, float out_min, float out_max) {
  49. return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
  50. }
  51. /* 递减map */
  52. static __INLINE float f_map_inv(float x, float in_min, float in_max, float out_min, float out_max) {
  53. return out_max - (x - in_min) * (out_max - out_min) / (in_max - in_min);
  54. }
  55. static __INLINE void step_towards(float *value, float goal, float step) {
  56. if (*value < goal) {
  57. if ((*value + step) < goal) {
  58. *value += step;
  59. } else {
  60. *value = goal;
  61. }
  62. } else if (*value > goal) {
  63. if ((*value - step) > goal) {
  64. *value -= step;
  65. } else {
  66. *value = goal;
  67. }
  68. }
  69. }
  70. /**
  71. * Fast atan2
  72. *
  73. * See http://www.dspguru.com/dsp/tricks/fixed-point-atan2-with-self-normalization
  74. *
  75. * @param y
  76. * y
  77. *
  78. * @param x
  79. * x
  80. *
  81. * @return
  82. * The angle in radians
  83. */
  84. static __INLINE float fast_atan2(float y, float x) {
  85. float abs_y = fabsf(y) + 1e-20f; // kludge to prevent 0/0 condition
  86. float angle;
  87. if (x >= 0) {
  88. float r = (x - abs_y) / (x + abs_y);
  89. float rsq = r * r;
  90. angle = ((0.1963f * rsq) - 0.9817f) * r + (M_PI / 4.0f);
  91. } else {
  92. float r = (x + abs_y) / (abs_y - x);
  93. float rsq = r * r;
  94. angle = ((0.1963f * rsq) - 0.9817f) * r + (3.0f * M_PI / 4.0f);
  95. }
  96. UTILS_NAN_ZERO(angle);
  97. if (y < 0) {
  98. return(-angle);
  99. } else {
  100. return(angle);
  101. }
  102. }
  103. static __INLINE float fast_atan_2(float y, float x) {
  104. // a := min (|x|, |y|) / max (|x|, |y|)
  105. float abs_y = ABS(y);
  106. float abs_x = ABS(x);
  107. // inject FLT_MIN in denominator to avoid division by zero
  108. float a = min(abs_x, abs_y) / (MAX(abs_x, abs_y) + 1e-20f);
  109. // s := a * a
  110. float s = a * a;
  111. // r := ((-0.0464964749 * s + 0.15931422) * s - 0.327622764) * s * a + a
  112. float r = ((-0.0464964749f * s + 0.15931422f) * s - 0.327622764f) * s * a + a;
  113. // if |y| > |x| then r := 1.57079637 - r
  114. if (abs_y > abs_x)
  115. r = 1.57079637f - r;
  116. // if x < 0 then r := 3.14159274 - r
  117. if (x < 0.0f)
  118. r = 3.14159274f - r;
  119. // if y < 0 then r := -r
  120. if (y < 0.0f)
  121. r = -r;
  122. return r;
  123. }
  124. static void normal_sincosf(float angle, float *sin, float *cos) {
  125. *sin = arm_sin_f32(angle);
  126. *cos = arm_cos_f32(angle);
  127. }
  128. #define degree_2_pi(d) ((float)(d) * M_PI / 180.0f)
  129. #define pi_2_degree(d) ((float)(d) * 180.0f / M_PI)
  130. #define INVALID_ANGLE 0x3DFF
  131. #define SIGN(x) (((x) < 0.0f) ? -1.0f : 1.0f)
  132. /**
  133. * A simple low pass filter.
  134. *
  135. * @param value
  136. * The filtered value.
  137. *
  138. * @param sample
  139. * Next sample.
  140. *
  141. * @param filter_constant
  142. * Filter constant. Range 0.0 to 1.0, where 1.0 gives the unfiltered value.
  143. */
  144. /* 前向差分离散化 */
  145. #define LowPass_Filter(value, sample, filter_constant) (value = ((float)sample - (float)value) * filter_constant + value)
  146. /* 后向差分离散化 */
  147. #define do_lpf(value, sample, filter_constant) ((sample * filter_constant + value)/(1.0f + filter_constant))
  148. #endif /* _Fast_Math_H__ */