fix_math.h 2.6 KB

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