arm_sin_cos_f32.c 4.7 KB

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