arm_atan2_q31.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. /* ----------------------------------------------------------------------
  2. * Project: CMSIS DSP Library
  3. * Title: arm_atan2_q31.c
  4. * Description: q31 Arc tangent of y/x
  5. *
  6. * $Date: 22 April 2022
  7. * $Revision: V1.10.0
  8. *
  9. * Target Processor: Cortex-M and Cortex-A cores
  10. * -------------------------------------------------------------------- */
  11. /*
  12. * Copyright (C) 2010-2022 ARM Limited or its affiliates. All rights reserved.
  13. *
  14. * SPDX-License-Identifier: Apache-2.0
  15. *
  16. * Licensed under the Apache License, Version 2.0 (the License); you may
  17. * not use this file except in compliance with the License.
  18. * You may obtain a copy of the License at
  19. *
  20. * www.apache.org/licenses/LICENSE-2.0
  21. *
  22. * Unless required by applicable law or agreed to in writing, software
  23. * distributed under the License is distributed on an AS IS BASIS, WITHOUT
  24. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  25. * See the License for the specific language governing permissions and
  26. * limitations under the License.
  27. */
  28. #include "dsp/fast_math_functions.h"
  29. #include "dsp/utils.h"
  30. /*
  31. atan for argument between in [0, 1.0]
  32. */
  33. /* Q2.29 */
  34. #define ATANHALF_Q29 0xed63383
  35. #define PIHALF_Q29 0x3243f6a9
  36. #define PIQ29 0x6487ed51
  37. #define ATAN2_NB_COEFS_Q31 13
  38. static const q31_t atan2_coefs_q31[ATAN2_NB_COEFS_Q31]={0x00000000
  39. ,0x7ffffffe
  40. ,0x000001b6
  41. ,0xd555158e
  42. ,0x00036463
  43. ,0x1985f617
  44. ,0x001992ae
  45. ,0xeed53a7f
  46. ,0xf8f15245
  47. ,0x2215a3a4
  48. ,0xe0fab004
  49. ,0x0cdd4825
  50. ,0xfddbc054
  51. };
  52. __STATIC_FORCEINLINE q31_t arm_atan_limited_q31(q31_t x)
  53. {
  54. q63_t res=(q63_t)atan2_coefs_q31[ATAN2_NB_COEFS_Q31-1];
  55. int i=1;
  56. for(i=1;i<ATAN2_NB_COEFS_Q31;i++)
  57. {
  58. res = ((q63_t) x * res) >> 31U;
  59. res = res + ((q63_t) atan2_coefs_q31[ATAN2_NB_COEFS_Q31-1-i]) ;
  60. }
  61. return(clip_q63_to_q31(res>>2));
  62. }
  63. __STATIC_FORCEINLINE q31_t arm_atan_q31(q31_t y,q31_t x)
  64. {
  65. int sign=0;
  66. q31_t res=0;
  67. if (y<0)
  68. {
  69. /* Negate y */
  70. #if defined (ARM_MATH_DSP)
  71. y = __QSUB(0, y);
  72. #else
  73. y = (y == INT32_MIN) ? INT32_MAX : -y;
  74. #endif
  75. sign=1-sign;
  76. }
  77. if (x < 0)
  78. {
  79. sign=1 - sign;
  80. /* Negate x */
  81. #if defined (ARM_MATH_DSP)
  82. x = __QSUB(0, x);
  83. #else
  84. x = (x == INT32_MIN) ? INT32_MAX : -x;
  85. #endif
  86. }
  87. if (y > x)
  88. {
  89. q31_t ratio;
  90. int16_t shift;
  91. arm_divide_q31(x,y,&ratio,&shift);
  92. /* Shift ratio by shift */
  93. if (shift >= 0)
  94. {
  95. ratio = clip_q63_to_q31((q63_t) ratio << shift);
  96. }
  97. else
  98. {
  99. ratio = (ratio >> -shift);
  100. }
  101. res = PIHALF_Q29 - arm_atan_limited_q31(ratio);
  102. }
  103. else
  104. {
  105. q31_t ratio;
  106. int16_t shift;
  107. arm_divide_q31(y,x,&ratio,&shift);
  108. /* Shift ratio by shift */
  109. if (shift >= 0)
  110. {
  111. ratio = clip_q63_to_q31((q63_t) ratio << shift);
  112. }
  113. else
  114. {
  115. ratio = (ratio >> -shift);
  116. }
  117. res = arm_atan_limited_q31(ratio);
  118. }
  119. if (sign)
  120. {
  121. /* Negate res */
  122. #if defined (ARM_MATH_DSP)
  123. res = __QSUB(0, res);
  124. #else
  125. res = (res == INT32_MIN) ? INT32_MAX : -res;
  126. #endif
  127. }
  128. return(res);
  129. }
  130. /**
  131. @ingroup groupFastMath
  132. */
  133. /**
  134. @addtogroup atan2
  135. @{
  136. */
  137. /**
  138. @brief Arc Tangent of y/x using sign of y and x to get right quadrant
  139. @param[in] y y coordinate
  140. @param[in] x x coordinate
  141. @param[out] result Result in Q2.29
  142. @return error status.
  143. @par Compute the Arc tangent of y/x:
  144. The sign of y and x are used to determine the right quadrant
  145. and compute the right angle. Returned value is between -Pi and Pi.
  146. */
  147. arm_status arm_atan2_q31(q31_t y,q31_t x,q31_t *result)
  148. {
  149. if (x > 0)
  150. {
  151. *result=arm_atan_q31(y,x);
  152. return(ARM_MATH_SUCCESS);
  153. }
  154. if (x < 0)
  155. {
  156. if (y > 0)
  157. {
  158. *result=arm_atan_q31(y,x) + PIQ29;
  159. }
  160. else if (y < 0)
  161. {
  162. *result=arm_atan_q31(y,x) - PIQ29;
  163. }
  164. else
  165. {
  166. *result= PIQ29;
  167. }
  168. return(ARM_MATH_SUCCESS);
  169. }
  170. if (x == 0)
  171. {
  172. if (y > 0)
  173. {
  174. *result=PIHALF_Q29;
  175. return(ARM_MATH_SUCCESS);
  176. }
  177. if (y < 0)
  178. {
  179. *result=-PIHALF_Q29;
  180. return(ARM_MATH_SUCCESS);
  181. }
  182. }
  183. return(ARM_MATH_NANINF);
  184. }
  185. /**
  186. @} end of atan2 group
  187. */