arm_sin_cos_f32.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /* ----------------------------------------------------------------------
  2. * Project: CMSIS DSP Library
  3. * Title: arm_sin_cos_f32.c
  4. * Description: Sine and Cosine calculation for floating-point values
  5. *
  6. * $Date: 27. January 2017
  7. * $Revision: V.1.5.1
  8. *
  9. * Target Processor: Cortex-M cores
  10. * -------------------------------------------------------------------- */
  11. /*
  12. * Copyright (C) 2010-2017 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 "arm_math.h"
  29. #include "arm_common_tables.h"
  30. /**
  31. * @ingroup groupController
  32. */
  33. /**
  34. * @defgroup SinCos Sine Cosine
  35. *
  36. * Computes the trigonometric sine and cosine values using a combination of table lookup
  37. * and linear interpolation.
  38. * There are separate functions for Q31 and floating-point data types.
  39. * The input to the floating-point version is in degrees while the
  40. * fixed-point Q31 have a scaled input with the range
  41. * [-1 0.9999] mapping to [-180 +180] degrees.
  42. *
  43. * The floating point function also allows values that are out of the usual range. When this happens, the function will
  44. * take extra time to adjust the input value to the range of [-180 180].
  45. *
  46. * The result is accurate to 5 digits after the decimal point.
  47. *
  48. * The implementation is based on table lookup using 360 values together with linear interpolation.
  49. * The steps used are:
  50. * -# Calculation of the nearest integer table index.
  51. * -# Compute the fractional portion (fract) of the input.
  52. * -# Fetch the value corresponding to \c index from sine table to \c y0 and also value from \c index+1 to \c y1.
  53. * -# Sine value is computed as <code> *psinVal = y0 + (fract * (y1 - y0))</code>.
  54. * -# Fetch the value corresponding to \c index from cosine table to \c y0 and also value from \c index+1 to \c y1.
  55. * -# Cosine value is computed as <code> *pcosVal = y0 + (fract * (y1 - y0))</code>.
  56. */
  57. /**
  58. * @addtogroup SinCos
  59. * @{
  60. */
  61. /**
  62. * @brief Floating-point sin_cos function.
  63. * @param[in] theta input value in degrees
  64. * @param[out] *pSinVal points to the processed sine output.
  65. * @param[out] *pCosVal points to the processed cos output.
  66. * @return none.
  67. */
  68. void arm_sin_cos_f32(
  69. float32_t theta,
  70. float32_t * pSinVal,
  71. float32_t * pCosVal)
  72. {
  73. float32_t fract, in; /* Temporary variables for input, output */
  74. uint16_t indexS, indexC; /* Index variable */
  75. float32_t f1, f2, d1, d2; /* Two nearest output values */
  76. float32_t findex, Dn, Df, temp;
  77. /* input x is in degrees */
  78. /* Scale the input, divide input by 360, for cosine add 0.25 (pi/2) to read sine table */
  79. in = theta * 0.00277777777778f;
  80. if (in < 0.0f)
  81. {
  82. in = -in;
  83. }
  84. in = in - (int32_t)in;
  85. /* Calculation of index of the table */
  86. findex = (float32_t) FAST_MATH_TABLE_SIZE * in;
  87. indexS = ((uint16_t)findex) & 0x1ff;
  88. indexC = (indexS + (FAST_MATH_TABLE_SIZE / 4)) & 0x1ff;
  89. /* fractional value calculation */
  90. fract = findex - (float32_t) indexS;
  91. /* Read two nearest values of input value from the cos & sin tables */
  92. f1 = sinTable_f32[indexC+0];
  93. f2 = sinTable_f32[indexC+1];
  94. d1 = -sinTable_f32[indexS+0];
  95. d2 = -sinTable_f32[indexS+1];
  96. temp = (1.0f - fract) * f1 + fract * f2;
  97. Dn = 0.0122718463030f; // delta between the two points (fixed), in this case 2*pi/FAST_MATH_TABLE_SIZE
  98. Df = f2 - f1; // delta between the values of the functions
  99. temp = Dn *(d1 + d2) - 2 * Df;
  100. temp = fract * temp + (3 * Df - (d2 + 2 * d1) * Dn);
  101. temp = fract * temp + d1 * Dn;
  102. /* Calculation of cosine value */
  103. *pCosVal = fract * temp + f1;
  104. /* Read two nearest values of input value from the cos & sin tables */
  105. f1 = sinTable_f32[indexS+0];
  106. f2 = sinTable_f32[indexS+1];
  107. d1 = sinTable_f32[indexC+0];
  108. d2 = sinTable_f32[indexC+1];
  109. temp = (1.0f - fract) * f1 + fract * f2;
  110. Df = f2 - f1; // delta between the values of the functions
  111. temp = Dn*(d1 + d2) - 2*Df;
  112. temp = fract*temp + (3*Df - (d2 + 2*d1)*Dn);
  113. temp = fract*temp + d1*Dn;
  114. /* Calculation of sine value */
  115. *pSinVal = fract*temp + f1;
  116. if (theta < 0.0f)
  117. {
  118. *pSinVal = -*pSinVal;
  119. }
  120. }
  121. /**
  122. * @} end of SinCos group
  123. */