arm_cos_f32.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /* ----------------------------------------------------------------------
  2. * Project: CMSIS DSP Library
  3. * Title: arm_cos_f32.c
  4. * Description: Fast 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 groupFastMath
  32. */
  33. /**
  34. @defgroup cos Cosine
  35. Computes the trigonometric cosine function using a combination of table lookup
  36. and linear interpolation. There are separate functions for
  37. Q15, Q31, and floating-point data types.
  38. The input to the floating-point version is in radians while the
  39. fixed-point Q15 and Q31 have a scaled input with the range
  40. [0 +0.9999] mapping to [0 2*pi). The fixed-point range is chosen so that a
  41. value of 2*pi wraps around to 0.
  42. The implementation is based on table lookup using 256 values together with linear interpolation.
  43. The steps used are:
  44. -# Calculation of the nearest integer table index
  45. -# Compute the fractional portion (fract) of the table index.
  46. -# The final result equals <code>(1.0f-fract)*a + fract*b;</code>
  47. where
  48. <pre>
  49. a = Table[index];
  50. b = Table[index+1];
  51. </pre>
  52. */
  53. /**
  54. @addtogroup cos
  55. @{
  56. */
  57. /**
  58. @brief Fast approximation to the trigonometric cosine function for floating-point data.
  59. @param[in] x input value in radians
  60. @return cos(x)
  61. */
  62. float32_t arm_cos_f32(
  63. float32_t x)
  64. {
  65. float32_t cosVal, fract, in; /* Temporary input, output variables */
  66. uint16_t index; /* Index variable */
  67. float32_t a, b; /* Two nearest output values */
  68. int32_t n;
  69. float32_t findex;
  70. /* input x is in radians */
  71. /* Scale input to [0 1] range from [0 2*PI] , divide input by 2*pi, add 0.25 (pi/2) to read sine table */
  72. in = x * 0.159154943092f + 0.25f;
  73. /* Calculation of floor value of input */
  74. n = (int32_t) in;
  75. /* Make negative values towards -infinity */
  76. if (in < 0.0f)
  77. {
  78. n--;
  79. }
  80. /* Map input value to [0 1] */
  81. in = in - (float32_t) n;
  82. /* Calculation of index of the table */
  83. findex = (float32_t)FAST_MATH_TABLE_SIZE * in;
  84. index = (uint16_t)findex;
  85. /* when "in" is exactly 1, we need to rotate the index down to 0 */
  86. if (index >= FAST_MATH_TABLE_SIZE) {
  87. index = 0;
  88. findex -= (float32_t)FAST_MATH_TABLE_SIZE;
  89. }
  90. /* fractional value calculation */
  91. fract = findex - (float32_t) index;
  92. /* Read two nearest values of input value from the cos table */
  93. a = sinTable_f32[index];
  94. b = sinTable_f32[index+1];
  95. /* Linear interpolation process */
  96. cosVal = (1.0f - fract) * a + fract * b;
  97. /* Return output value */
  98. return (cosVal);
  99. }
  100. /**
  101. @} end of cos group
  102. */