arm_atan2_q15.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. /* ----------------------------------------------------------------------
  2. * Project: CMSIS DSP Library
  3. * Title: arm_atan2_q15.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.13 */
  34. #define ATANHALFQ13 0xed6
  35. #define PIHALFQ13 0x3244
  36. #define PIQ13 0x6488
  37. #define ATAN2_NB_COEFS_Q15 10
  38. static const q15_t atan2_coefs_q15[ATAN2_NB_COEFS_Q15]={0x0000
  39. ,0x7fff
  40. ,0xffff
  41. ,0xd567
  42. ,0xff70
  43. ,0x1bad
  44. ,0xfd58
  45. ,0xe9a9
  46. ,0x1129
  47. ,0xfbdb
  48. };
  49. __STATIC_FORCEINLINE q15_t arm_atan_limited_q15(q15_t x)
  50. {
  51. q31_t res=(q31_t)atan2_coefs_q15[ATAN2_NB_COEFS_Q15-1];
  52. int i=1;
  53. for(i=1;i<ATAN2_NB_COEFS_Q15;i++)
  54. {
  55. res = ((q31_t) x * res) >> 15U;
  56. res = res + ((q31_t) atan2_coefs_q15[ATAN2_NB_COEFS_Q15-1-i]) ;
  57. }
  58. res = __SSAT(res>>2,16);
  59. return(res);
  60. }
  61. __STATIC_FORCEINLINE q15_t arm_atan_q15(q15_t y,q15_t x)
  62. {
  63. int sign=0;
  64. q15_t res=0;
  65. if (y<0)
  66. {
  67. arm_negate_q15(&y,&y,1);
  68. sign=1-sign;
  69. }
  70. if (x < 0)
  71. {
  72. sign=1 - sign;
  73. arm_negate_q15(&x,&x,1);
  74. }
  75. if (y > x)
  76. {
  77. q15_t ratio;
  78. int16_t shift;
  79. arm_divide_q15(x,y,&ratio,&shift);
  80. arm_shift_q15(&ratio,shift,&ratio,1);
  81. res = PIHALFQ13 - arm_atan_limited_q15(ratio);
  82. }
  83. else
  84. {
  85. q15_t ratio;
  86. int16_t shift;
  87. arm_divide_q15(y,x,&ratio,&shift);
  88. arm_shift_q15(&ratio,shift,&ratio,1);
  89. res = arm_atan_limited_q15(ratio);
  90. }
  91. if (sign)
  92. {
  93. arm_negate_q15(&res,&res,1);
  94. }
  95. return(res);
  96. }
  97. /**
  98. @ingroup groupFastMath
  99. */
  100. /**
  101. @addtogroup atan2
  102. @{
  103. */
  104. /**
  105. @brief Arc Tangent of y/x using sign of y and x to get right quadrant
  106. @param[in] y y coordinate
  107. @param[in] x x coordinate
  108. @param[out] result Result in Q2.13
  109. @return error status.
  110. @par Compute the Arc tangent of y/x:
  111. The sign of y and x are used to determine the right quadrant
  112. and compute the right angle.
  113. */
  114. arm_status arm_atan2_q15(q15_t y,q15_t x,q15_t *result)
  115. {
  116. if (x > 0)
  117. {
  118. *result=arm_atan_q15(y,x);
  119. return(ARM_MATH_SUCCESS);
  120. }
  121. if (x < 0)
  122. {
  123. if (y > 0)
  124. {
  125. *result=arm_atan_q15(y,x) + PIQ13;
  126. }
  127. else if (y < 0)
  128. {
  129. *result=arm_atan_q15(y,x) - PIQ13;
  130. }
  131. else
  132. {
  133. if (y<0)
  134. {
  135. *result= -PIQ13;
  136. }
  137. else
  138. {
  139. *result= PIQ13;
  140. }
  141. }
  142. return(ARM_MATH_SUCCESS);
  143. }
  144. if (x == 0)
  145. {
  146. if (y > 0)
  147. {
  148. *result=PIHALFQ13;
  149. return(ARM_MATH_SUCCESS);
  150. }
  151. if (y < 0)
  152. {
  153. *result=-PIHALFQ13;
  154. return(ARM_MATH_SUCCESS);
  155. }
  156. }
  157. return(ARM_MATH_NANINF);
  158. }
  159. /**
  160. @} end of atan2 group
  161. */