arm_sin_f32.c 3.4 KB

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