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.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.

arm_cos_q31.c 9.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. /* ----------------------------------------------------------------------
  2. * Copyright (C) 2010-2013 ARM Limited. All rights reserved.
  3. *
  4. * $Date: 17. January 2013
  5. * $Revision: V1.4.1
  6. *
  7. * Project: CMSIS DSP Library
  8. * Title: arm_cos_q31.c
  9. *
  10. * Description: Fast cosine calculation for Q31 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. /**
  42. * @ingroup groupFastMath
  43. */
  44. /**
  45. * @addtogroup cos
  46. * @{
  47. */
  48. /**
  49. * \par
  50. * Table values are in Q31 (1.31 fixed-point format) and generation is done in
  51. * three steps. First, generate cos values in floating point:
  52. * <pre>
  53. * tableSize = 256;
  54. * for(n = -1; n < (tableSize + 1); n++)
  55. * {
  56. * cosTable[n+1]= cos(2*pi*n/tableSize);
  57. * } </pre>
  58. * where pi value is 3.14159265358979
  59. * \par
  60. * Second, convert floating-point to Q31 (Fixed point):
  61. * (cosTable[i] * pow(2, 31))
  62. * \par
  63. * Finally, round to the nearest integer value:
  64. * cosTable[i] += (cosTable[i] > 0 ? 0.5 :-0.5);
  65. */
  66. static const q31_t cosTableQ31[259] = {
  67. 0x7ff62182, 0x7fffffff, 0x7ff62182, 0x7fd8878e, 0x7fa736b4, 0x7f62368f,
  68. 0x7f0991c4, 0x7e9d55fc,
  69. 0x7e1d93ea, 0x7d8a5f40, 0x7ce3ceb2, 0x7c29fbee, 0x7b5d039e, 0x7a7d055b,
  70. 0x798a23b1, 0x78848414,
  71. 0x776c4edb, 0x7641af3d, 0x7504d345, 0x73b5ebd1, 0x72552c85, 0x70e2cbc6,
  72. 0x6f5f02b2, 0x6dca0d14,
  73. 0x6c242960, 0x6a6d98a4, 0x68a69e81, 0x66cf8120, 0x64e88926, 0x62f201ac,
  74. 0x60ec3830, 0x5ed77c8a,
  75. 0x5cb420e0, 0x5a82799a, 0x5842dd54, 0x55f5a4d2, 0x539b2af0, 0x5133cc94,
  76. 0x4ebfe8a5, 0x4c3fdff4,
  77. 0x49b41533, 0x471cece7, 0x447acd50, 0x41ce1e65, 0x3f1749b8, 0x3c56ba70,
  78. 0x398cdd32, 0x36ba2014,
  79. 0x33def287, 0x30fbc54d, 0x2e110a62, 0x2b1f34eb, 0x2826b928, 0x25280c5e,
  80. 0x2223a4c5, 0x1f19f97b,
  81. 0x1c0b826a, 0x18f8b83c, 0x15e21445, 0x12c8106f, 0xfab272b, 0xc8bd35e,
  82. 0x96a9049, 0x647d97c,
  83. 0x3242abf, 0x0, 0xfcdbd541, 0xf9b82684, 0xf6956fb7, 0xf3742ca2, 0xf054d8d5,
  84. 0xed37ef91,
  85. 0xea1debbb, 0xe70747c4, 0xe3f47d96, 0xe0e60685, 0xdddc5b3b, 0xdad7f3a2,
  86. 0xd7d946d8, 0xd4e0cb15,
  87. 0xd1eef59e, 0xcf043ab3, 0xcc210d79, 0xc945dfec, 0xc67322ce, 0xc3a94590,
  88. 0xc0e8b648, 0xbe31e19b,
  89. 0xbb8532b0, 0xb8e31319, 0xb64beacd, 0xb3c0200c, 0xb140175b, 0xaecc336c,
  90. 0xac64d510, 0xaa0a5b2e,
  91. 0xa7bd22ac, 0xa57d8666, 0xa34bdf20, 0xa1288376, 0x9f13c7d0, 0x9d0dfe54,
  92. 0x9b1776da, 0x99307ee0,
  93. 0x9759617f, 0x9592675c, 0x93dbd6a0, 0x9235f2ec, 0x90a0fd4e, 0x8f1d343a,
  94. 0x8daad37b, 0x8c4a142f,
  95. 0x8afb2cbb, 0x89be50c3, 0x8893b125, 0x877b7bec, 0x8675dc4f, 0x8582faa5,
  96. 0x84a2fc62, 0x83d60412,
  97. 0x831c314e, 0x8275a0c0, 0x81e26c16, 0x8162aa04, 0x80f66e3c, 0x809dc971,
  98. 0x8058c94c, 0x80277872,
  99. 0x8009de7e, 0x80000000, 0x8009de7e, 0x80277872, 0x8058c94c, 0x809dc971,
  100. 0x80f66e3c, 0x8162aa04,
  101. 0x81e26c16, 0x8275a0c0, 0x831c314e, 0x83d60412, 0x84a2fc62, 0x8582faa5,
  102. 0x8675dc4f, 0x877b7bec,
  103. 0x8893b125, 0x89be50c3, 0x8afb2cbb, 0x8c4a142f, 0x8daad37b, 0x8f1d343a,
  104. 0x90a0fd4e, 0x9235f2ec,
  105. 0x93dbd6a0, 0x9592675c, 0x9759617f, 0x99307ee0, 0x9b1776da, 0x9d0dfe54,
  106. 0x9f13c7d0, 0xa1288376,
  107. 0xa34bdf20, 0xa57d8666, 0xa7bd22ac, 0xaa0a5b2e, 0xac64d510, 0xaecc336c,
  108. 0xb140175b, 0xb3c0200c,
  109. 0xb64beacd, 0xb8e31319, 0xbb8532b0, 0xbe31e19b, 0xc0e8b648, 0xc3a94590,
  110. 0xc67322ce, 0xc945dfec,
  111. 0xcc210d79, 0xcf043ab3, 0xd1eef59e, 0xd4e0cb15, 0xd7d946d8, 0xdad7f3a2,
  112. 0xdddc5b3b, 0xe0e60685,
  113. 0xe3f47d96, 0xe70747c4, 0xea1debbb, 0xed37ef91, 0xf054d8d5, 0xf3742ca2,
  114. 0xf6956fb7, 0xf9b82684,
  115. 0xfcdbd541, 0x0, 0x3242abf, 0x647d97c, 0x96a9049, 0xc8bd35e, 0xfab272b,
  116. 0x12c8106f,
  117. 0x15e21445, 0x18f8b83c, 0x1c0b826a, 0x1f19f97b, 0x2223a4c5, 0x25280c5e,
  118. 0x2826b928, 0x2b1f34eb,
  119. 0x2e110a62, 0x30fbc54d, 0x33def287, 0x36ba2014, 0x398cdd32, 0x3c56ba70,
  120. 0x3f1749b8, 0x41ce1e65,
  121. 0x447acd50, 0x471cece7, 0x49b41533, 0x4c3fdff4, 0x4ebfe8a5, 0x5133cc94,
  122. 0x539b2af0, 0x55f5a4d2,
  123. 0x5842dd54, 0x5a82799a, 0x5cb420e0, 0x5ed77c8a, 0x60ec3830, 0x62f201ac,
  124. 0x64e88926, 0x66cf8120,
  125. 0x68a69e81, 0x6a6d98a4, 0x6c242960, 0x6dca0d14, 0x6f5f02b2, 0x70e2cbc6,
  126. 0x72552c85, 0x73b5ebd1,
  127. 0x7504d345, 0x7641af3d, 0x776c4edb, 0x78848414, 0x798a23b1, 0x7a7d055b,
  128. 0x7b5d039e, 0x7c29fbee,
  129. 0x7ce3ceb2, 0x7d8a5f40, 0x7e1d93ea, 0x7e9d55fc, 0x7f0991c4, 0x7f62368f,
  130. 0x7fa736b4, 0x7fd8878e,
  131. 0x7ff62182, 0x7fffffff, 0x7ff62182
  132. };
  133. /**
  134. * @brief Fast approximation to the trigonometric cosine function for Q31 data.
  135. * @param[in] x Scaled input value in radians.
  136. * @return cos(x).
  137. *
  138. * The Q31 input value is in the range [0 +0.9999] and is mapped to a radian
  139. * value in the range [0 2*pi).
  140. */
  141. q31_t arm_cos_q31(
  142. q31_t x)
  143. {
  144. q31_t cosVal, in, in2; /* Temporary variables for input, output */
  145. q31_t wa, wb, wc, wd; /* Cubic interpolation coefficients */
  146. q31_t a, b, c, d; /* Four nearest output values */
  147. q31_t *tablePtr; /* Pointer to table */
  148. q31_t fract, fractCube, fractSquare; /* Temporary values for fractional values */
  149. q31_t oneBy6 = 0x15555555; /* Fixed point value of 1/6 */
  150. q31_t tableSpacing = TABLE_SPACING_Q31; /* Table spacing */
  151. q31_t temp; /* Temporary variable for intermediate process */
  152. int32_t index; /* Index variable */
  153. in = x;
  154. /* Calculate the nearest index */
  155. index = in / tableSpacing;
  156. /* Calculate the nearest value of input */
  157. in2 = ((q31_t) index) * tableSpacing;
  158. /* Calculation of fractional value */
  159. fract = (in - in2) << 8;
  160. /* fractSquare = fract * fract */
  161. fractSquare = ((q31_t) (((q63_t) fract * fract) >> 32));
  162. fractSquare = fractSquare << 1;
  163. /* fractCube = fract * fract * fract */
  164. fractCube = ((q31_t) (((q63_t) fractSquare * fract) >> 32));
  165. fractCube = fractCube << 1;
  166. /* Checking min and max index of table */
  167. if(index < 0)
  168. {
  169. index = 0;
  170. }
  171. else if(index > 256)
  172. {
  173. index = 256;
  174. }
  175. /* Initialise table pointer */
  176. tablePtr = (q31_t *) & cosTableQ31[index];
  177. /* Cubic interpolation process */
  178. /* Calculation of wa */
  179. /* wa = -(oneBy6)*fractCube + (fractSquare >> 1u) - (0x2AAAAAAA)*fract; */
  180. wa = ((q31_t) (((q63_t) oneBy6 * fractCube) >> 32));
  181. temp = 0x2AAAAAAA;
  182. wa = (q31_t) ((((q63_t) wa << 32) + ((q63_t) temp * fract)) >> 32);
  183. wa = -(wa << 1u);
  184. wa += (fractSquare >> 1u);
  185. /* Read first nearest value of output from the cos table */
  186. a = *tablePtr++;
  187. /* cosVal = a*wa */
  188. cosVal = ((q31_t) (((q63_t) a * wa) >> 32));
  189. /* q31(1.31) Fixed point value of 1 */
  190. temp = 0x7FFFFFFF;
  191. /* Calculation of wb */
  192. wb = ((fractCube >> 1u) - (fractSquare + (fract >> 1u))) + temp;
  193. /* Read second nearest value of output from the cos table */
  194. b = *tablePtr++;
  195. /* cosVal += b*wb */
  196. cosVal = (q31_t) ((((q63_t) cosVal << 32) + ((q63_t) b * (wb))) >> 32);
  197. /* Calculation of wc */
  198. wc = -fractCube + fractSquare;
  199. wc = (wc >> 1u) + fract;
  200. /* Read third nearest values of output value from the cos table */
  201. c = *tablePtr++;
  202. /* cosVal += c*wc */
  203. cosVal = (q31_t) ((((q63_t) cosVal << 32) + ((q63_t) c * (wc))) >> 32);
  204. /* Calculation of wd */
  205. /* wd = (oneBy6)*fractCube - (oneBy6)*fract; */
  206. fractCube = fractCube - fract;
  207. wd = ((q31_t) (((q63_t) oneBy6 * fractCube) >> 32));
  208. wd = (wd << 1u);
  209. /* Read fourth nearest value of output from the cos table */
  210. d = *tablePtr++;
  211. /* cosVal += d*wd; */
  212. cosVal = (q31_t) ((((q63_t) cosVal << 32) + ((q63_t) d * (wd))) >> 32);
  213. /* convert cosVal in 2.30 format to 1.31 format */
  214. return (__QADD(cosVal, cosVal));
  215. }
  216. /**
  217. * @} end of cos group
  218. */