fix_math.h 2.9 KB

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