A bundled STM32F10x Std Periph and CMSIS library
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

139 lines
4.7 KiB

  1. /* ----------------------------------------------------------------------
  2. * Copyright (C) 2010-2014 ARM Limited. All rights reserved.
  3. *
  4. * $Date: 12. March 2014
  5. * $Revision: V1.4.4
  6. *
  7. * Project: CMSIS DSP Library
  8. * Title: arm_cos_f32.c
  9. *
  10. * Description: Fast cosine calculation for floating-point values.
  11. *
  12. * Target Processor: Cortex-M4/Cortex-M3/Cortex-M0
  13. *
  14. * Redistribution and use in source and binary forms, with or without
  15. * modification, are permitted provided that the following conditions
  16. * are met:
  17. * - Redistributions of source code must retain the above copyright
  18. * notice, this list of conditions and the following disclaimer.
  19. * - Redistributions in binary form must reproduce the above copyright
  20. * notice, this list of conditions and the following disclaimer in
  21. * the documentation and/or other materials provided with the
  22. * distribution.
  23. * - Neither the name of ARM LIMITED nor the names of its contributors
  24. * may be used to endorse or promote products derived from this
  25. * software without specific prior written permission.
  26. *
  27. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  28. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  29. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  30. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  31. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  32. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  33. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  34. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  35. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  36. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  37. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  38. * POSSIBILITY OF SUCH DAMAGE.
  39. * -------------------------------------------------------------------- */
  40. #include "arm_math.h"
  41. #include "arm_common_tables.h"
  42. /**
  43. * @ingroup groupFastMath
  44. */
  45. /**
  46. * @defgroup cos Cosine
  47. *
  48. * Computes the trigonometric cosine function using a combination of table lookup
  49. * and cubic interpolation. There are separate functions for
  50. * Q15, Q31, and floating-point data types.
  51. * The input to the floating-point version is in radians while the
  52. * fixed-point Q15 and Q31 have a scaled input with the range
  53. * [0 +0.9999] mapping to [0 2*pi). The fixed-point range is chosen so that a
  54. * value of 2*pi wraps around to 0.
  55. *
  56. * The implementation is based on table lookup using 256 values together with cubic interpolation.
  57. * The steps used are:
  58. * -# Calculation of the nearest integer table index
  59. * -# Fetch the four table values a, b, c, and d
  60. * -# Compute the fractional portion (fract) of the table index.
  61. * -# Calculation of wa, wb, wc, wd
  62. * -# The final result equals <code>a*wa + b*wb + c*wc + d*wd</code>
  63. *
  64. * where
  65. * <pre>
  66. * a=Table[index-1];
  67. * b=Table[index+0];
  68. * c=Table[index+1];
  69. * d=Table[index+2];
  70. * </pre>
  71. * and
  72. * <pre>
  73. * wa=-(1/6)*fract.^3 + (1/2)*fract.^2 - (1/3)*fract;
  74. * wb=(1/2)*fract.^3 - fract.^2 - (1/2)*fract + 1;
  75. * wc=-(1/2)*fract.^3+(1/2)*fract.^2+fract;
  76. * wd=(1/6)*fract.^3 - (1/6)*fract;
  77. * </pre>
  78. */
  79. /**
  80. * @addtogroup cos
  81. * @{
  82. */
  83. /**
  84. * @brief Fast approximation to the trigonometric cosine function for floating-point data.
  85. * @param[in] x input value in radians.
  86. * @return cos(x).
  87. */
  88. float32_t arm_cos_f32(
  89. float32_t x)
  90. {
  91. float32_t cosVal, fract, in; /* Temporary variables for input, output */
  92. uint16_t index; /* Index variable */
  93. float32_t a, b; /* Two nearest output values */
  94. int32_t n;
  95. float32_t findex;
  96. /* input x is in radians */
  97. /* 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 */
  98. in = x * 0.159154943092f + 0.25f;
  99. /* Calculation of floor value of input */
  100. n = (int32_t) in;
  101. /* Make negative values towards -infinity */
  102. if(in < 0.0f)
  103. {
  104. n--;
  105. }
  106. /* Map input value to [0 1] */
  107. in = in - (float32_t) n;
  108. /* Calculation of index of the table */
  109. findex = (float32_t) FAST_MATH_TABLE_SIZE * in;
  110. index = ((uint16_t)findex) & 0x1ff;
  111. /* fractional value calculation */
  112. fract = findex - (float32_t) index;
  113. /* Read two nearest values of input value from the cos table */
  114. a = sinTable_f32[index];
  115. b = sinTable_f32[index+1];
  116. /* Linear interpolation process */
  117. cosVal = (1.0f-fract)*a + fract*b;
  118. /* Return the output value */
  119. return (cosVal);
  120. }
  121. /**
  122. * @} end of cos group
  123. */