arm_sin_q31.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. /* ----------------------------------------------------------------------
  2. * Project: CMSIS DSP Library
  3. * Title: arm_sin_q31.c
  4. * Description: Fast 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 groupFastMath
  32. */
  33. /**
  34. * @addtogroup sin
  35. * @{
  36. */
  37. /**
  38. * @brief Fast approximation to the trigonometric sine function for Q31 data.
  39. * @param[in] x Scaled input value in radians.
  40. * @return sin(x).
  41. *
  42. * The Q31 input value is in the range [0 +0.9999] and is mapped to a radian value in the range [0 2*pi). */
  43. q31_t arm_sin_q31(
  44. q31_t x)
  45. {
  46. q31_t sinVal; /* Temporary variables for input, output */
  47. int32_t index; /* Index variables */
  48. q31_t a, b; /* Four nearest output values */
  49. q31_t fract; /* Temporary values for fractional values */
  50. /* Calculate the nearest index */
  51. index = (uint32_t)x >> FAST_MATH_Q31_SHIFT;
  52. /* Calculation of fractional value */
  53. fract = (x - (index << FAST_MATH_Q31_SHIFT)) << 9;
  54. /* Read two nearest values of input value from the sin table */
  55. a = sinTable_q31[index];
  56. b = sinTable_q31[index+1];
  57. /* Linear interpolation process */
  58. sinVal = (q63_t)(0x80000000-fract)*a >> 32;
  59. sinVal = (q31_t)((((q63_t)sinVal << 32) + ((q63_t)fract*b)) >> 32);
  60. return sinVal << 1;
  61. }
  62. /**
  63. * @} end of sin group
  64. */