arm_sin_cos_q31.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /* ----------------------------------------------------------------------
  2. * Project: CMSIS DSP Library
  3. * Title: arm_sin_cos_q31.c
  4. * Description: Cosine & Sine calculation for Q31 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. * @addtogroup SinCos
  35. * @{
  36. */
  37. /**
  38. * @brief Q31 sin_cos function.
  39. * @param[in] theta scaled input value in degrees
  40. * @param[out] *pSinVal points to the processed sine output.
  41. * @param[out] *pCosVal points to the processed cosine output.
  42. * @return none.
  43. *
  44. * The Q31 input value is in the range [-1 0.999999] and is mapped to a degree value in the range [-180 179].
  45. *
  46. */
  47. void arm_sin_cos_q31(
  48. q31_t theta,
  49. q31_t * pSinVal,
  50. q31_t * pCosVal)
  51. {
  52. q31_t fract; /* Temporary variables for input, output */
  53. uint16_t indexS, indexC; /* Index variable */
  54. q31_t f1, f2, d1, d2; /* Two nearest output values */
  55. q31_t Dn, Df;
  56. q63_t temp;
  57. /* Calculate the nearest index */
  58. indexS = (uint32_t)theta >> CONTROLLER_Q31_SHIFT;
  59. indexC = (indexS + 128) & 0x1ff;
  60. /* Calculation of fractional value */
  61. fract = (theta - (indexS << CONTROLLER_Q31_SHIFT)) << 8;
  62. /* Read two nearest values of input value from the cos & sin tables */
  63. f1 = sinTable_q31[indexC+0];
  64. f2 = sinTable_q31[indexC+1];
  65. d1 = -sinTable_q31[indexS+0];
  66. d2 = -sinTable_q31[indexS+1];
  67. Dn = 0x1921FB5; // delta between the two points (fixed), in this case 2*pi/FAST_MATH_TABLE_SIZE
  68. Df = f2 - f1; // delta between the values of the functions
  69. temp = Dn*((q63_t)d1 + d2);
  70. temp = temp - ((q63_t)Df << 32);
  71. temp = (q63_t)fract*(temp >> 31);
  72. temp = temp + ((3*(q63_t)Df << 31) - (d2 + ((q63_t)d1 << 1))*Dn);
  73. temp = (q63_t)fract*(temp >> 31);
  74. temp = temp + (q63_t)d1*Dn;
  75. temp = (q63_t)fract*(temp >> 31);
  76. /* Calculation of cosine value */
  77. *pCosVal = clip_q63_to_q31((temp >> 31) + (q63_t)f1);
  78. /* Read two nearest values of input value from the cos & sin tables */
  79. f1 = sinTable_q31[indexS+0];
  80. f2 = sinTable_q31[indexS+1];
  81. d1 = sinTable_q31[indexC+0];
  82. d2 = sinTable_q31[indexC+1];
  83. Df = f2 - f1; // delta between the values of the functions
  84. temp = Dn*((q63_t)d1 + d2);
  85. temp = temp - ((q63_t)Df << 32);
  86. temp = (q63_t)fract*(temp >> 31);
  87. temp = temp + ((3*(q63_t)Df << 31) - (d2 + ((q63_t)d1 << 1))*Dn);
  88. temp = (q63_t)fract*(temp >> 31);
  89. temp = temp + (q63_t)d1*Dn;
  90. temp = (q63_t)fract*(temp >> 31);
  91. /* Calculation of sine value */
  92. *pSinVal = clip_q63_to_q31((temp >> 31) + (q63_t)f1);
  93. }
  94. /**
  95. * @} end of SinCos group
  96. */