arm_cos_q15.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. /* ----------------------------------------------------------------------
  2. * Project: CMSIS DSP Library
  3. * Title: arm_cos_q15.c
  4. * Description: Fast cosine calculation for Q15 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. @addtogroup cos
  35. @{
  36. */
  37. /**
  38. @brief Fast approximation to the trigonometric cosine function for Q15 data.
  39. @param[in] x Scaled input value in radians
  40. @return cos(x)
  41. The Q15 input value is in the range [0 +0.9999] and is mapped to a radian value in the range [0 2*PI).
  42. */
  43. q15_t arm_cos_q15(
  44. q15_t x)
  45. {
  46. q15_t cosVal; /* Temporary input, output variables */
  47. int32_t index; /* Index variable */
  48. q15_t a, b; /* Two nearest output values */
  49. q15_t fract; /* Temporary values for fractional values */
  50. /* add 0.25 (pi/2) to read sine table */
  51. x = (uint16_t)x + 0x2000;
  52. if (x < 0)
  53. { /* convert negative numbers to corresponding positive ones */
  54. x = (uint16_t)x + 0x8000;
  55. }
  56. /* Calculate the nearest index */
  57. index = (uint32_t)x >> FAST_MATH_Q15_SHIFT;
  58. /* Calculation of fractional value */
  59. fract = (x - (index << FAST_MATH_Q15_SHIFT)) << 9;
  60. /* Read two nearest values of input value from the sin table */
  61. a = sinTable_q15[index];
  62. b = sinTable_q15[index+1];
  63. /* Linear interpolation process */
  64. cosVal = (q31_t) (0x8000 - fract) * a >> 16;
  65. cosVal = (q15_t) ((((q31_t) cosVal << 16) + ((q31_t) fract * b)) >> 16);
  66. /* Return output value */
  67. return (cosVal << 1);
  68. }
  69. /**
  70. @} end of cos group
  71. */