fix_math.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. #ifndef _FIX_MATH_H__
  2. #define _FIX_MATH_H__
  3. #include "bsp/bsp.h"
  4. #include "os/os_types.h"
  5. #include "libs/utils.h"
  6. typedef signed short s16q14_t;
  7. typedef signed short s16q10_t;
  8. typedef signed short s16q5_t;
  9. //typedef signed short s16q4_t;
  10. typedef int32_t s32q4_t;
  11. typedef int32_t s32q14_t;
  12. typedef int32_t s32q5_t;
  13. typedef int32_t s32q19_t;
  14. //#define S16Q4(A) (signed short)((A) * 16.0F)
  15. #define S16Q5(A) (signed short)((A) * 32.0F)
  16. #define S16Q5toF(A) ((float)(A)/32.0f)
  17. #define S16Q10(A) (signed short)((A) * 1024.0F)
  18. #define S16Q10toF(A) ((float)(A)/1024.0f)
  19. #define S16Q14(A) (signed short)((A) * 16384.0F)
  20. #define S16Q14toF(A) ((float)(A)/16384.0f)
  21. #define S32Q4(A) (signed int)((A) * 16.0F)
  22. #define S32Q4toF(A) ((float)(A)/16.0f)
  23. #define S32Q5(A) (signed int)((A) * 32.0F)
  24. #define S32Q5toF(A) ((float)(A)/32.0f)
  25. #define S32Q14(A) (signed int)((A) * 16384.0F)
  26. #define S32Q14toF(A) ((float)(A)/16384.0f)
  27. #define S32Q19(A) (signed int)((A) * (16384.0F * 32.0F))
  28. #define S32Q19toF(A) ((float)(A)/(16384.0F * 32.0F))
  29. #define S32Q14_MUL(a, b) (((a)*(b)) >>14)
  30. #define S16_mul(a, b, q) (((s32)(a)*(b)) >> q)
  31. #define FtoS16(f) ((s16)f)
  32. #define FtoS16x1000(f) ((s16)(f * 100))
  33. #define LowPass_Filter_Fix(value, sample, filter_constant, shift) (value = ((s32)value * (S16Q14(1) - filter_constant) + (s32)sample * filter_constant) >> (shift))
  34. #define MIN_S16Q5 (1.0F/32.0F)
  35. /*
  36. static __INLINE float S16Q4toF(s16q4_t v) {
  37. s16 num = (v >> 4) & 0xFFFF;
  38. u16 tail = v & 0x000F;
  39. float f = num + (float)tail / 16.0f;
  40. return f;
  41. }
  42. static __INLINE float S16Q5toF(s16q5_t v) {
  43. s16 num = (v >> 5) & 0x3FF;
  44. u16 tail = v & 0x001F;
  45. float f = num + (float)tail / 32.0f;
  46. if (v & 0x8000) {
  47. return -f;
  48. }
  49. return f;
  50. }
  51. static __INLINE float S16Q10toF(s16q5_t v) {
  52. s16 num = (v >> 10) & 0x1F;
  53. u16 tail = v & 0x03FF;
  54. float f = num + (float)tail / 1024.0f;
  55. if (v & 0x8000) {
  56. return -f;
  57. }
  58. return f;
  59. }
  60. static __INLINE float S16Q14toF(s16q5_t v) {
  61. s16 num = (v >> 14) & 0x1;
  62. u16 tail = v & 0x03FFF;
  63. float f = num + (float)tail / 16384.0f;
  64. if (v & 0x8000) {
  65. return -f;
  66. }
  67. return f;
  68. }
  69. static __INLINE float S32Q4toF(s32q4_t v) {
  70. s32 num = (v >> 4) & 0x7FFFFFF;
  71. u16 tail = v & 0x000F;
  72. float f = num + (float)tail / 16.0f;
  73. if (v & 0x80000000) {
  74. return -f;
  75. }
  76. return f;
  77. }
  78. */
  79. #define MATH_sat(in, minOut, maxOut) (min((maxOut), MAX((in), (minOut))))
  80. static __INLINE u16 Sqrt_Fix( u32 wInput )
  81. {
  82. s32 wtemprootnew;
  83. u8 biter = 0u;
  84. s32 wtemproot;
  85. if ( wInput <= ( s32 )2097152 ) {
  86. wtemproot = ( s32 )128;
  87. } else {
  88. wtemproot = ( s32 )8192;
  89. }
  90. do {
  91. wtemprootnew = ( wtemproot + wInput / wtemproot ) / ( s32 )2;
  92. if ( wtemprootnew == wtemproot ) {
  93. biter = 6u;
  94. } else {
  95. biter ++;
  96. wtemproot = wtemprootnew;
  97. }
  98. } while ( biter < 6u );
  99. return ( wtemprootnew );
  100. }
  101. void SinCos_Lut(float angle, float *s, float *c);
  102. #endif /* _FIX_MATH_H__ */