fast_math.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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 TWO_BY_THREE (0.66667f)
  12. #define M_PI (3.14159265f)
  13. #define ONE_BY_SQRT3_Q14 (9459L) //0.57735026919 * 16384.0F
  14. #define SQRT3_BY_2_Q14 (14189L)//0.86602540378 * 16384.0F
  15. #define TWO_BY_SQRT3_Q14 (18918L)
  16. #ifndef SQ
  17. #define SQ(x) ((x)*(x))
  18. #endif
  19. // nan and infinity check for floats
  20. #define UTILS_IS_INF(x) ((x) == (1.0F / 0.0F) || (x) == (-1.0F / 0.0F))
  21. #define UTILS_IS_NAN(x) ((x) != (x))
  22. #define UTILS_NAN_ZERO(x) (x = UTILS_IS_NAN(x) ? 0.0F : x)
  23. void fast_sincos(float angle, float *sin, float *cos);
  24. void arm_sin_cos(float angle, float *s, float *c);
  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. static __INLINE float f_map(float x, float in_min, float in_max, float out1, float out2) {
  48. if (out1 > out2) { /* 递增map */
  49. return out1 - (x - in_min) * (out1 - out2) / (in_max - in_min);
  50. }else { /* 递减map */
  51. return (x - in_min) * (out2 - out1) / (in_max - in_min) + out1;
  52. }
  53. }
  54. static __INLINE void step_towards(float *value, float goal, float step) {
  55. if (*value < goal) {
  56. if ((*value + step) < goal) {
  57. *value += step;
  58. } else {
  59. *value = goal;
  60. }
  61. } else if (*value > goal) {
  62. if ((*value - step) > goal) {
  63. *value -= step;
  64. } else {
  65. *value = goal;
  66. }
  67. }
  68. }
  69. static __INLINE void step_towards_s16(s16 *value, s16 goal, s16 step) {
  70. if (*value < goal) {
  71. if ((*value + step) < goal) {
  72. *value += step;
  73. } else {
  74. *value = goal;
  75. }
  76. } else if (*value > goal) {
  77. if ((*value - step) > goal) {
  78. *value -= step;
  79. } else {
  80. *value = goal;
  81. }
  82. }
  83. }
  84. static __INLINE s16 line_intp(s16 x, s16 x_l, s16 x_h, s16 y_l, s16 y_h) {
  85. float r = (float)(x - x_l) /(float)(x_h - x_l);
  86. return (s16)(r * (y_h - y_l)) + y_l;
  87. }
  88. /**
  89. * Fast atan2
  90. *
  91. * See http://www.dspguru.com/dsp/tricks/fixed-point-atan2-with-self-normalization
  92. *
  93. * @param y
  94. * y
  95. *
  96. * @param x
  97. * x
  98. *
  99. * @return
  100. * The angle in radians
  101. */
  102. static __INLINE float fast_atan2(float y, float x) {
  103. float abs_y = fabsf(y) + 1e-20f; // kludge to prevent 0/0 condition
  104. float angle;
  105. if (x >= 0) {
  106. float r = (x - abs_y) / (x + abs_y);
  107. float rsq = r * r;
  108. angle = ((0.1963f * rsq) - 0.9817f) * r + (M_PI / 4.0f);
  109. } else {
  110. float r = (x + abs_y) / (abs_y - x);
  111. float rsq = r * r;
  112. angle = ((0.1963f * rsq) - 0.9817f) * r + (3.0f * M_PI / 4.0f);
  113. }
  114. UTILS_NAN_ZERO(angle);
  115. if (y < 0) {
  116. return(-angle);
  117. } else {
  118. return(angle);
  119. }
  120. }
  121. static __INLINE float fast_atan_2(float y, float x) {
  122. // a := min (|x|, |y|) / max (|x|, |y|)
  123. float abs_y = ABS(y);
  124. float abs_x = ABS(x);
  125. // inject FLT_MIN in denominator to avoid division by zero
  126. float a = min(abs_x, abs_y) / (MAX(abs_x, abs_y) + 1e-20f);
  127. // s := a * a
  128. float s = a * a;
  129. // r := ((-0.0464964749 * s + 0.15931422) * s - 0.327622764) * s * a + a
  130. float r = ((-0.0464964749f * s + 0.15931422f) * s - 0.327622764f) * s * a + a;
  131. // if |y| > |x| then r := 1.57079637 - r
  132. if (abs_y > abs_x)
  133. r = 1.57079637f - r;
  134. // if x < 0 then r := 3.14159274 - r
  135. if (x < 0.0f)
  136. r = 3.14159274f - r;
  137. // if y < 0 then r := -r
  138. if (y < 0.0f)
  139. r = -r;
  140. return r;
  141. }
  142. static void normal_sincosf(float angle, float *sin, float *cos) {
  143. *sin = arm_sin_f32(angle);
  144. *cos = arm_cos_f32(angle);
  145. }
  146. #define degree_2_pi(d) ((float)(d) * M_PI / 180.0f)
  147. #define pi_2_degree(d) ((float)(d) * 180.0f / M_PI)
  148. #define INVALID_ANGLE 0x3DFF
  149. #define SIGN(x) (((x) < 0.0f) ? -1.0f : 1.0f)
  150. /**
  151. * A simple low pass filter.
  152. *
  153. * @param value
  154. * The filtered value.
  155. *
  156. * @param sample
  157. * Next sample.
  158. *
  159. * @param filter_constant
  160. * Filter constant. Range 0.0 to 1.0, where 1.0 gives the unfiltered value.
  161. */
  162. /* 前向差分离散化 */
  163. #define LowPass_Filter(value, sample, filter_constant) (value = ((float)sample - (float)value) * filter_constant + value)
  164. /* 后向差分离散化 */
  165. #define do_lpf(value, sample, filter_constant) ((sample * filter_constant + value)/(1.0f + filter_constant))
  166. #endif /* _Fast_Math_H__ */