arm_atan2_f32.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. /* ----------------------------------------------------------------------
  2. * Project: CMSIS DSP Library
  3. * Title: arm_atan2_f32.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. /*
  30. atan for argument between in [0, 1.0]
  31. */
  32. #define ATANHALFF32 0.463648f
  33. #define PIHALFF32 1.5707963267948966192313f
  34. #define ATAN2_NB_COEFS_F32 10
  35. static const float32_t atan2_coefs_f32[ATAN2_NB_COEFS_F32]={0.0f
  36. ,1.0000001638308195518f
  37. ,-0.0000228941363602264f
  38. ,-0.3328086544578890873f
  39. ,-0.004404814619311061f
  40. ,0.2162217461808173258f
  41. ,-0.0207504842057097504f
  42. ,-0.1745263362250363339f
  43. ,0.1340557235283553386f
  44. ,-0.0323664125927477625f
  45. };
  46. __STATIC_FORCEINLINE float32_t arm_atan_limited_f32(float32_t x)
  47. {
  48. float32_t res=atan2_coefs_f32[ATAN2_NB_COEFS_F32-1];
  49. int i=1;
  50. for(i=1;i<ATAN2_NB_COEFS_F32;i++)
  51. {
  52. res = x*res + atan2_coefs_f32[ATAN2_NB_COEFS_F32-1-i];
  53. }
  54. return(res);
  55. }
  56. __STATIC_FORCEINLINE float32_t arm_atan_f32(float32_t x)
  57. {
  58. int sign=0;
  59. float32_t res=0.0f;
  60. if (x < 0.0f)
  61. {
  62. sign=1;
  63. x=-x;
  64. }
  65. if (x > 1.0f)
  66. {
  67. x = 1.0f / x;
  68. res = PIHALFF32 - arm_atan_limited_f32(x);
  69. }
  70. else
  71. {
  72. res += arm_atan_limited_f32(x);
  73. }
  74. if (sign)
  75. {
  76. res = -res;
  77. }
  78. return(res);
  79. }
  80. /**
  81. @ingroup groupFastMath
  82. */
  83. /**
  84. @defgroup atan2 ArcTan2
  85. Computing Arc tangent only using the ratio y/x is not enough to determine the angle
  86. since there is an indeterminacy. Opposite quadrants are giving the same ratio.
  87. ArcTan2 is not using y/x to compute the angle but y and x and use the sign of y and x
  88. to determine the quadrant.
  89. */
  90. /**
  91. @addtogroup atan2
  92. @{
  93. */
  94. /**
  95. @brief Arc Tangent of y/x using sign of y and x to get right quadrant
  96. @param[in] y y coordinate
  97. @param[in] x x coordinate
  98. @param[out] result Result
  99. @return error status.
  100. @par Compute the Arc tangent of y/x:
  101. The sign of y and x are used to determine the right quadrant
  102. and compute the right angle.
  103. */
  104. arm_status arm_atan2_f32(float32_t y,float32_t x,float32_t *result)
  105. {
  106. if (x > 0.0f)
  107. {
  108. *result=arm_atan_f32(y/x);
  109. return(ARM_MATH_SUCCESS);
  110. }
  111. if (x < 0.0f)
  112. {
  113. if (y > 0.0f)
  114. {
  115. *result=arm_atan_f32(y/x) + PI;
  116. }
  117. else if (y < 0.0f)
  118. {
  119. *result=arm_atan_f32(y/x) - PI;
  120. }
  121. else
  122. {
  123. if (signbit(y))
  124. {
  125. *result= -PI;
  126. }
  127. else
  128. {
  129. *result= PI;
  130. }
  131. }
  132. return(ARM_MATH_SUCCESS);
  133. }
  134. if (x == 0.0f)
  135. {
  136. if (y > 0.0f)
  137. {
  138. *result=PIHALFF32;
  139. return(ARM_MATH_SUCCESS);
  140. }
  141. if (y < 0.0f)
  142. {
  143. *result=-PIHALFF32;
  144. return(ARM_MATH_SUCCESS);
  145. }
  146. }
  147. return(ARM_MATH_NANINF);
  148. }
  149. /**
  150. @} end of atan2 group
  151. */