sin_table.c 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. #include "os/os_types.h"
  2. #include "math/fix_math.h"
  3. #include "arm_math.h"
  4. /*
  5. 1.cos和sin转换公式一
  6. sin[(pi/2)-x]=cosx;
  7. cos[(pi/2)-x]=sinx;
  8. 2.cos和sin转换公式二
  9. cos[(pi/2)+x]=-sinx;
  10. sin[(pi/2)+x]=cosx;
  11. */
  12. #if 0
  13. static s16 sinTable[] =
  14. { 0, 286, 572, 857, 1143, 1428, 1713, 1997, 2280, 2563, 2845, 3126, 3406, 3686,
  15. 3964, 4240, 4516, 4790, 5063, 5334, 5604, 5872, 6138, 6402, 6664, 6924, 7182,
  16. 7438, 7692, 7943, 8192, 8438, 8682, 8923, 9162, 9397, 9630, 9860, 10087,
  17. 10311, 10531, 10749, 10963, 11174, 11381, 11585, 11786, 11982, 12176, 12365,
  18. 12551, 12733, 12911, 13085, 13255, 13421, 13583, 13741, 13894, 14044, 14189,
  19. 14330, 14466, 14598, 14726, 14849, 14968, 15082, 15191, 15296, 15396, 15491,
  20. 15582, 15668, 15749, 15826, 15897, 15964, 16026, 16083, 16135, 16182, 16225,
  21. 16262, 16294, 16322, 16344, 16362, 16374, 16382, 16384, 16382, 16374, 16362,
  22. 16344, 16322, 16294, 16262, 16225, 16182, 16135, 16083, 16026, 15964, 15897,
  23. 15826, 15749, 15668, 15582, 15491, 15396, 15296, 15191, 15082, 14968, 14849,
  24. 14726, 14598, 14466, 14330, 14189, 14044, 13894, 13741, 13583, 13421, 13255,
  25. 13085, 12911, 12733, 12551, 12365, 12176, 11982, 11786, 11585, 11381, 11174,
  26. 10963, 10749, 10531, 10311, 10087, 9860, 9630, 9397, 9162, 8923, 8682, 8438,
  27. 8192, 7943, 7692, 7438, 7182, 6924, 6664, 6402, 6138, 5872, 5604, 5334, 5063,
  28. 4790, 4516, 4240, 3964, 3686, 3406, 3126, 2845, 2563, 2280, 1997, 1713, 1428,
  29. 1143, 857, 572, 286, 0, -286, -572, -857, -1143, -1428, -1713, -1997, -2280,
  30. -2563, -2845, -3126, -3406, -3686, -3964, -4240, -4516, -4790, -5063, -5334,
  31. -5604, -5872, -6138, -6402, -6664, -6924, -7182, -7438, -7692, -7943, -8192,
  32. -8438, -8682, -8923, -9162, -9397, -9630, -9860, -10087, -10311, -10531,
  33. -10749, -10963, -11174, -11381, -11585, -11786, -11982, -12176, -12365,
  34. -12551, -12733, -12911, -13085, -13255, -13421, -13583, -13741, -13894,
  35. -14044, -14189, -14330, -14466, -14598, -14726, -14849, -14968, -15082,
  36. -15191, -15296, -15396, -15491, -15582, -15668, -15749, -15826, -15897,
  37. -15964, -16026, -16083, -16135, -16182, -16225, -16262, -16294, -16322,
  38. -16344, -16362, -16374, -16382, -16384, -16382, -16374, -16362, -16344,
  39. -16322, -16294, -16262, -16225, -16182, -16135, -16083, -16026, -15964,
  40. -15897, -15826, -15749, -15668, -15582, -15491, -15396, -15296, -15191,
  41. -15082, -14968, -14849, -14726, -14598, -14466, -14330, -14189, -14044,
  42. -13894, -13741, -13583, -13421, -13255, -13085, -12911, -12733, -12551,
  43. -12365, -12176, -11982, -11786, -11585, -11381, -11174, -10963, -10749,
  44. -10531, -10311, -10087, -9860, -9630, -9397, -9162, -8923, -8682, -8438,
  45. -8192, -7943, -7692, -7438, -7182, -6924, -6664, -6402, -6138, -5872, -5604,
  46. -5334, -5063, -4790, -4516, -4240, -3964, -3686, -3406, -3126, -2845, -2563,
  47. -2280, -1997, -1713, -1428, -1143, -857, -572, -286, 0 };
  48. void sin_cos_lut(float angle, float *s, float *c) {
  49. s16 angle_degree = (s16)angle; //去掉小数部分
  50. if ((angle - angle_degree) > 0.5f) {
  51. angle_degree += 1;
  52. }
  53. angle_degree = angle_degree % 360;
  54. *s = S16Q14toF(sinTable[angle_degree]);
  55. //cosx = sin[(pi/2)+x]
  56. angle_degree = (angle_degree + 90) % 360;
  57. *c = S16Q14toF(sinTable[angle_degree]);
  58. }
  59. #else
  60. /*120Mhz mcu, use 1.14us */
  61. void arm_sin_cos(float angle, float *s, float *c) {
  62. arm_sin_cos_f32(angle, s, c);
  63. }
  64. #endif