arm_sin_cos_f32.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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: 23 April 2021
  7. * $Revision: V1.9.0
  8. *
  9. * Target Processor: Cortex-M and Cortex-A cores
  10. * -------------------------------------------------------------------- */
  11. /*
  12. * Copyright (C) 2010-2021 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/controller_functions.h"
  29. #include "arm_common_tables.h"
  30. /**
  31. @addtogroup SinCos
  32. @{
  33. */
  34. /**
  35. @brief Floating-point sin_cos function.
  36. @param[in] theta input value in degrees
  37. @param[out] pSinVal points to processed sine output
  38. @param[out] pCosVal points to processed cosine output
  39. */
  40. void arm_sin_cos_f32(
  41. float32_t theta,
  42. float32_t * pSinVal,
  43. float32_t * pCosVal)
  44. {
  45. float32_t fract, in; /* Temporary input, output variables */
  46. uint16_t indexS, indexC; /* Index variable */
  47. float32_t f1, f2, d1, d2; /* Two nearest output values */
  48. float32_t Dn, Df;
  49. float32_t temp, findex;
  50. /* input x is in degrees */
  51. /* Scale input, divide input by 360, for cosine add 0.25 (pi/2) to read sine table */
  52. in = theta * 0.00277777777778f;
  53. if (in < 0.0f)
  54. {
  55. in = -in;
  56. }
  57. in = in - (int32_t)in;
  58. /* Calculate the nearest index */
  59. findex = (float32_t)FAST_MATH_TABLE_SIZE * in;
  60. indexS = ((uint16_t)findex) & 0x1ff;
  61. indexC = (indexS + (FAST_MATH_TABLE_SIZE / 4)) & 0x1ff;
  62. /* Calculation of fractional value */
  63. fract = findex - (float32_t) indexS;
  64. /* Read two nearest values of input value from the cos & sin tables */
  65. f1 = sinTable_f32[indexC ];
  66. f2 = sinTable_f32[indexC+1];
  67. d1 = -sinTable_f32[indexS ];
  68. d2 = -sinTable_f32[indexS+1];
  69. Dn = 0.0122718463030f; /* delta between the two points (fixed), in this case 2*pi/FAST_MATH_TABLE_SIZE */
  70. Df = f2 - f1; /* delta between the values of the functions */
  71. temp = Dn * (d1 + d2) - 2 * Df;
  72. temp = fract * temp + (3 * Df - (d2 + 2 * d1) * Dn);
  73. temp = fract * temp + d1 * Dn;
  74. /* Calculation of cosine value */
  75. *pCosVal = fract * temp + f1;
  76. /* Read two nearest values of input value from the cos & sin tables */
  77. f1 = sinTable_f32[indexS ];
  78. f2 = sinTable_f32[indexS+1];
  79. d1 = sinTable_f32[indexC ];
  80. d2 = sinTable_f32[indexC+1];
  81. Df = f2 - f1; // delta between the values of the functions
  82. temp = Dn * (d1 + d2) - 2 * Df;
  83. temp = fract * temp + (3 * Df - (d2 + 2 * d1) * Dn);
  84. temp = fract * temp + d1 * Dn;
  85. /* Calculation of sine value */
  86. *pSinVal = fract * temp + f1;
  87. if (theta < 0.0f)
  88. {
  89. *pSinVal = -*pSinVal;
  90. }
  91. }
  92. /**
  93. @} end of SinCos group
  94. */