arm_cos_f32.c 3.5 KB

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