arm_atan2_q31.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. /* ----------------------------------------------------------------------
  2. * Project: CMSIS DSP Library
  3. * Title: arm_atan2_q31.c
  4. * Description: float32 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. arm_negate_q31(&y,&y,1);
  70. sign=1-sign;
  71. }
  72. if (x < 0)
  73. {
  74. sign=1 - sign;
  75. arm_negate_q31(&x,&x,1);
  76. }
  77. if (y > x)
  78. {
  79. q31_t ratio;
  80. int16_t shift;
  81. arm_divide_q31(x,y,&ratio,&shift);
  82. arm_shift_q31(&ratio,shift,&ratio,1);
  83. res = PIHALF_Q29 - arm_atan_limited_q31(ratio);
  84. }
  85. else
  86. {
  87. q31_t ratio;
  88. int16_t shift;
  89. arm_divide_q31(y,x,&ratio,&shift);
  90. arm_shift_q31(&ratio,shift,&ratio,1);
  91. res = arm_atan_limited_q31(ratio);
  92. }
  93. if (sign)
  94. {
  95. arm_negate_q31(&res,&res,1);
  96. }
  97. return(res);
  98. }
  99. /**
  100. @ingroup groupFastMath
  101. */
  102. /**
  103. @addtogroup atan2
  104. @{
  105. */
  106. /**
  107. @brief Arc Tangent of y/x using sign of y and x to get right quadrant
  108. @param[in] y y coordinate
  109. @param[in] x x coordinate
  110. @param[out] result Result in Q2.29
  111. @return error status.
  112. @par Compute the Arc tangent of y/x:
  113. The sign of y and x are used to determine the right quadrant
  114. and compute the right angle.
  115. */
  116. arm_status arm_atan2_q31(q31_t y,q31_t x,q31_t *result)
  117. {
  118. if (x > 0)
  119. {
  120. *result=arm_atan_q31(y,x);
  121. return(ARM_MATH_SUCCESS);
  122. }
  123. if (x < 0)
  124. {
  125. if (y > 0)
  126. {
  127. *result=arm_atan_q31(y,x) + PIQ29;
  128. }
  129. else if (y < 0)
  130. {
  131. *result=arm_atan_q31(y,x) - PIQ29;
  132. }
  133. else
  134. {
  135. if (y<0)
  136. {
  137. *result= -PIQ29;
  138. }
  139. else
  140. {
  141. *result= PIQ29;
  142. }
  143. }
  144. return(ARM_MATH_SUCCESS);
  145. }
  146. if (x == 0)
  147. {
  148. if (y > 0)
  149. {
  150. *result=PIHALF_Q29;
  151. return(ARM_MATH_SUCCESS);
  152. }
  153. if (y < 0)
  154. {
  155. *result=-PIHALF_Q29;
  156. return(ARM_MATH_SUCCESS);
  157. }
  158. }
  159. return(ARM_MATH_NANINF);
  160. }
  161. /**
  162. @} end of atan2 group
  163. */