fix_math.h 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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 S32Q14(A) (signed int)((A) * 16384.0F)
  24. #define S32Q14toF(A) ((float)(A)/16384.0f)
  25. #define S32Q19(A) (signed int)((A) * (16384.0F * 32.0F))
  26. #define S32Q19toF(A) ((float)(A)/(16384.0F * 32.0F))
  27. #define S32Q14_MUL(a, b) (((a)*(b)) >>14)
  28. #define S16_mul(a, b, q) (((s32)(a)*(b)) >> q)
  29. #define LowPass_Filter_Fix(value, sample, filter_constant) (value = ((s32)value * (S16Q14(1) - filter_constant) + (s32)sample * filter_constant) >> 14)
  30. /*
  31. static __INLINE float S16Q4toF(s16q4_t v) {
  32. s16 num = (v >> 4) & 0xFFFF;
  33. u16 tail = v & 0x000F;
  34. float f = num + (float)tail / 16.0f;
  35. return f;
  36. }
  37. static __INLINE float S16Q5toF(s16q5_t v) {
  38. s16 num = (v >> 5) & 0x3FF;
  39. u16 tail = v & 0x001F;
  40. float f = num + (float)tail / 32.0f;
  41. if (v & 0x8000) {
  42. return -f;
  43. }
  44. return f;
  45. }
  46. static __INLINE float S16Q10toF(s16q5_t v) {
  47. s16 num = (v >> 10) & 0x1F;
  48. u16 tail = v & 0x03FF;
  49. float f = num + (float)tail / 1024.0f;
  50. if (v & 0x8000) {
  51. return -f;
  52. }
  53. return f;
  54. }
  55. static __INLINE float S16Q14toF(s16q5_t v) {
  56. s16 num = (v >> 14) & 0x1;
  57. u16 tail = v & 0x03FFF;
  58. float f = num + (float)tail / 16384.0f;
  59. if (v & 0x8000) {
  60. return -f;
  61. }
  62. return f;
  63. }
  64. static __INLINE float S32Q4toF(s32q4_t v) {
  65. s32 num = (v >> 4) & 0x7FFFFFF;
  66. u16 tail = v & 0x000F;
  67. float f = num + (float)tail / 16.0f;
  68. if (v & 0x80000000) {
  69. return -f;
  70. }
  71. return f;
  72. }
  73. */
  74. #define MATH_sat(in, minOut, maxOut) (min((in), MAX((in), (minOut))))
  75. static __INLINE u16 Sqrt_Fix( u32 wInput )
  76. {
  77. s32 wtemprootnew;
  78. u8 biter = 0u;
  79. s32 wtemproot;
  80. if ( wInput <= ( s32 )2097152 ) {
  81. wtemproot = ( s32 )128;
  82. } else {
  83. wtemproot = ( s32 )8192;
  84. }
  85. do {
  86. wtemprootnew = ( wtemproot + wInput / wtemproot ) / ( s32 )2;
  87. if ( wtemprootnew == wtemproot ) {
  88. biter = 6u;
  89. } else {
  90. biter ++;
  91. wtemproot = wtemprootnew;
  92. }
  93. } while ( biter < 6u );
  94. return ( wtemprootnew );
  95. }
  96. void SinCos_Lut(s16q5_t angle, s16q14_t *s, s16q14_t *c);
  97. #endif /* _FIX_MATH_H__ */