mbed-os 6.10 versione

Committer:
emilmont
Date:
Wed Nov 28 12:30:09 2012 +0000
Revision:
1:fdd22bb7aa52
Child:
2:da51fb522205
DSP library code

Who changed what in which revision?

UserRevisionLine numberNew contents of line
emilmont 1:fdd22bb7aa52 1 /* ----------------------------------------------------------------------
emilmont 1:fdd22bb7aa52 2 * Copyright (C) 2010 ARM Limited. All rights reserved.
emilmont 1:fdd22bb7aa52 3 *
emilmont 1:fdd22bb7aa52 4 * $Date: 15. February 2012
emilmont 1:fdd22bb7aa52 5 * $Revision: V1.1.0
emilmont 1:fdd22bb7aa52 6 *
emilmont 1:fdd22bb7aa52 7 * Project: CMSIS DSP Library
emilmont 1:fdd22bb7aa52 8 * Title: arm_cos_q31.c
emilmont 1:fdd22bb7aa52 9 *
emilmont 1:fdd22bb7aa52 10 * Description: Fast cosine calculation for Q31 values.
emilmont 1:fdd22bb7aa52 11 *
emilmont 1:fdd22bb7aa52 12 * Target Processor: Cortex-M4/Cortex-M3/Cortex-M0
emilmont 1:fdd22bb7aa52 13 *
emilmont 1:fdd22bb7aa52 14 * Version 1.1.0 2012/02/15
emilmont 1:fdd22bb7aa52 15 * Updated with more optimizations, bug fixes and minor API changes.
emilmont 1:fdd22bb7aa52 16 *
emilmont 1:fdd22bb7aa52 17 * Version 1.0.10 2011/7/15
emilmont 1:fdd22bb7aa52 18 * Big Endian support added and Merged M0 and M3/M4 Source code.
emilmont 1:fdd22bb7aa52 19 *
emilmont 1:fdd22bb7aa52 20 * Version 1.0.3 2010/11/29
emilmont 1:fdd22bb7aa52 21 * Re-organized the CMSIS folders and updated documentation.
emilmont 1:fdd22bb7aa52 22 *
emilmont 1:fdd22bb7aa52 23 * Version 1.0.2 2010/11/11
emilmont 1:fdd22bb7aa52 24 * Documentation updated.
emilmont 1:fdd22bb7aa52 25 *
emilmont 1:fdd22bb7aa52 26 * Version 1.0.1 2010/10/05
emilmont 1:fdd22bb7aa52 27 * Production release and review comments incorporated.
emilmont 1:fdd22bb7aa52 28 *
emilmont 1:fdd22bb7aa52 29 * Version 1.0.0 2010/09/20
emilmont 1:fdd22bb7aa52 30 * Production release and review comments incorporated.
emilmont 1:fdd22bb7aa52 31 * -------------------------------------------------------------------- */
emilmont 1:fdd22bb7aa52 32
emilmont 1:fdd22bb7aa52 33 #include "arm_math.h"
emilmont 1:fdd22bb7aa52 34
emilmont 1:fdd22bb7aa52 35 /**
emilmont 1:fdd22bb7aa52 36 * @ingroup groupFastMath
emilmont 1:fdd22bb7aa52 37 */
emilmont 1:fdd22bb7aa52 38
emilmont 1:fdd22bb7aa52 39 /**
emilmont 1:fdd22bb7aa52 40 * @addtogroup cos
emilmont 1:fdd22bb7aa52 41 * @{
emilmont 1:fdd22bb7aa52 42 */
emilmont 1:fdd22bb7aa52 43
emilmont 1:fdd22bb7aa52 44 /**
emilmont 1:fdd22bb7aa52 45 * \par
emilmont 1:fdd22bb7aa52 46 * Table Values are in Q31(1.31 Fixed point format) and generation is done in three steps
emilmont 1:fdd22bb7aa52 47 * First Generate cos values in floating point:
emilmont 1:fdd22bb7aa52 48 * tableSize = 256;
emilmont 1:fdd22bb7aa52 49 * <pre>for(n = -1; n < (tableSize + 1); n++)
emilmont 1:fdd22bb7aa52 50 * {
emilmont 1:fdd22bb7aa52 51 * cosTable[n+1]= cos(2*pi*n/tableSize);
emilmont 1:fdd22bb7aa52 52 * } </pre>
emilmont 1:fdd22bb7aa52 53 * where pi value is 3.14159265358979
emilmont 1:fdd22bb7aa52 54 * \par
emilmont 1:fdd22bb7aa52 55 * Secondly Convert Floating point to Q31(Fixed point):
emilmont 1:fdd22bb7aa52 56 * (cosTable[i] * pow(2, 31))
emilmont 1:fdd22bb7aa52 57 * \par
emilmont 1:fdd22bb7aa52 58 * Finally Rounding to nearest integer is done
emilmont 1:fdd22bb7aa52 59 * cosTable[i] += (cosTable[i] > 0 ? 0.5 :-0.5);
emilmont 1:fdd22bb7aa52 60 */
emilmont 1:fdd22bb7aa52 61
emilmont 1:fdd22bb7aa52 62
emilmont 1:fdd22bb7aa52 63 static const q31_t cosTableQ31[259] = {
emilmont 1:fdd22bb7aa52 64 0x7ff62182, 0x7fffffff, 0x7ff62182, 0x7fd8878e, 0x7fa736b4, 0x7f62368f,
emilmont 1:fdd22bb7aa52 65 0x7f0991c4, 0x7e9d55fc,
emilmont 1:fdd22bb7aa52 66 0x7e1d93ea, 0x7d8a5f40, 0x7ce3ceb2, 0x7c29fbee, 0x7b5d039e, 0x7a7d055b,
emilmont 1:fdd22bb7aa52 67 0x798a23b1, 0x78848414,
emilmont 1:fdd22bb7aa52 68 0x776c4edb, 0x7641af3d, 0x7504d345, 0x73b5ebd1, 0x72552c85, 0x70e2cbc6,
emilmont 1:fdd22bb7aa52 69 0x6f5f02b2, 0x6dca0d14,
emilmont 1:fdd22bb7aa52 70 0x6c242960, 0x6a6d98a4, 0x68a69e81, 0x66cf8120, 0x64e88926, 0x62f201ac,
emilmont 1:fdd22bb7aa52 71 0x60ec3830, 0x5ed77c8a,
emilmont 1:fdd22bb7aa52 72 0x5cb420e0, 0x5a82799a, 0x5842dd54, 0x55f5a4d2, 0x539b2af0, 0x5133cc94,
emilmont 1:fdd22bb7aa52 73 0x4ebfe8a5, 0x4c3fdff4,
emilmont 1:fdd22bb7aa52 74 0x49b41533, 0x471cece7, 0x447acd50, 0x41ce1e65, 0x3f1749b8, 0x3c56ba70,
emilmont 1:fdd22bb7aa52 75 0x398cdd32, 0x36ba2014,
emilmont 1:fdd22bb7aa52 76 0x33def287, 0x30fbc54d, 0x2e110a62, 0x2b1f34eb, 0x2826b928, 0x25280c5e,
emilmont 1:fdd22bb7aa52 77 0x2223a4c5, 0x1f19f97b,
emilmont 1:fdd22bb7aa52 78 0x1c0b826a, 0x18f8b83c, 0x15e21445, 0x12c8106f, 0xfab272b, 0xc8bd35e,
emilmont 1:fdd22bb7aa52 79 0x96a9049, 0x647d97c,
emilmont 1:fdd22bb7aa52 80 0x3242abf, 0x0, 0xfcdbd541, 0xf9b82684, 0xf6956fb7, 0xf3742ca2, 0xf054d8d5,
emilmont 1:fdd22bb7aa52 81 0xed37ef91,
emilmont 1:fdd22bb7aa52 82 0xea1debbb, 0xe70747c4, 0xe3f47d96, 0xe0e60685, 0xdddc5b3b, 0xdad7f3a2,
emilmont 1:fdd22bb7aa52 83 0xd7d946d8, 0xd4e0cb15,
emilmont 1:fdd22bb7aa52 84 0xd1eef59e, 0xcf043ab3, 0xcc210d79, 0xc945dfec, 0xc67322ce, 0xc3a94590,
emilmont 1:fdd22bb7aa52 85 0xc0e8b648, 0xbe31e19b,
emilmont 1:fdd22bb7aa52 86 0xbb8532b0, 0xb8e31319, 0xb64beacd, 0xb3c0200c, 0xb140175b, 0xaecc336c,
emilmont 1:fdd22bb7aa52 87 0xac64d510, 0xaa0a5b2e,
emilmont 1:fdd22bb7aa52 88 0xa7bd22ac, 0xa57d8666, 0xa34bdf20, 0xa1288376, 0x9f13c7d0, 0x9d0dfe54,
emilmont 1:fdd22bb7aa52 89 0x9b1776da, 0x99307ee0,
emilmont 1:fdd22bb7aa52 90 0x9759617f, 0x9592675c, 0x93dbd6a0, 0x9235f2ec, 0x90a0fd4e, 0x8f1d343a,
emilmont 1:fdd22bb7aa52 91 0x8daad37b, 0x8c4a142f,
emilmont 1:fdd22bb7aa52 92 0x8afb2cbb, 0x89be50c3, 0x8893b125, 0x877b7bec, 0x8675dc4f, 0x8582faa5,
emilmont 1:fdd22bb7aa52 93 0x84a2fc62, 0x83d60412,
emilmont 1:fdd22bb7aa52 94 0x831c314e, 0x8275a0c0, 0x81e26c16, 0x8162aa04, 0x80f66e3c, 0x809dc971,
emilmont 1:fdd22bb7aa52 95 0x8058c94c, 0x80277872,
emilmont 1:fdd22bb7aa52 96 0x8009de7e, 0x80000000, 0x8009de7e, 0x80277872, 0x8058c94c, 0x809dc971,
emilmont 1:fdd22bb7aa52 97 0x80f66e3c, 0x8162aa04,
emilmont 1:fdd22bb7aa52 98 0x81e26c16, 0x8275a0c0, 0x831c314e, 0x83d60412, 0x84a2fc62, 0x8582faa5,
emilmont 1:fdd22bb7aa52 99 0x8675dc4f, 0x877b7bec,
emilmont 1:fdd22bb7aa52 100 0x8893b125, 0x89be50c3, 0x8afb2cbb, 0x8c4a142f, 0x8daad37b, 0x8f1d343a,
emilmont 1:fdd22bb7aa52 101 0x90a0fd4e, 0x9235f2ec,
emilmont 1:fdd22bb7aa52 102 0x93dbd6a0, 0x9592675c, 0x9759617f, 0x99307ee0, 0x9b1776da, 0x9d0dfe54,
emilmont 1:fdd22bb7aa52 103 0x9f13c7d0, 0xa1288376,
emilmont 1:fdd22bb7aa52 104 0xa34bdf20, 0xa57d8666, 0xa7bd22ac, 0xaa0a5b2e, 0xac64d510, 0xaecc336c,
emilmont 1:fdd22bb7aa52 105 0xb140175b, 0xb3c0200c,
emilmont 1:fdd22bb7aa52 106 0xb64beacd, 0xb8e31319, 0xbb8532b0, 0xbe31e19b, 0xc0e8b648, 0xc3a94590,
emilmont 1:fdd22bb7aa52 107 0xc67322ce, 0xc945dfec,
emilmont 1:fdd22bb7aa52 108 0xcc210d79, 0xcf043ab3, 0xd1eef59e, 0xd4e0cb15, 0xd7d946d8, 0xdad7f3a2,
emilmont 1:fdd22bb7aa52 109 0xdddc5b3b, 0xe0e60685,
emilmont 1:fdd22bb7aa52 110 0xe3f47d96, 0xe70747c4, 0xea1debbb, 0xed37ef91, 0xf054d8d5, 0xf3742ca2,
emilmont 1:fdd22bb7aa52 111 0xf6956fb7, 0xf9b82684,
emilmont 1:fdd22bb7aa52 112 0xfcdbd541, 0x0, 0x3242abf, 0x647d97c, 0x96a9049, 0xc8bd35e, 0xfab272b,
emilmont 1:fdd22bb7aa52 113 0x12c8106f,
emilmont 1:fdd22bb7aa52 114 0x15e21445, 0x18f8b83c, 0x1c0b826a, 0x1f19f97b, 0x2223a4c5, 0x25280c5e,
emilmont 1:fdd22bb7aa52 115 0x2826b928, 0x2b1f34eb,
emilmont 1:fdd22bb7aa52 116 0x2e110a62, 0x30fbc54d, 0x33def287, 0x36ba2014, 0x398cdd32, 0x3c56ba70,
emilmont 1:fdd22bb7aa52 117 0x3f1749b8, 0x41ce1e65,
emilmont 1:fdd22bb7aa52 118 0x447acd50, 0x471cece7, 0x49b41533, 0x4c3fdff4, 0x4ebfe8a5, 0x5133cc94,
emilmont 1:fdd22bb7aa52 119 0x539b2af0, 0x55f5a4d2,
emilmont 1:fdd22bb7aa52 120 0x5842dd54, 0x5a82799a, 0x5cb420e0, 0x5ed77c8a, 0x60ec3830, 0x62f201ac,
emilmont 1:fdd22bb7aa52 121 0x64e88926, 0x66cf8120,
emilmont 1:fdd22bb7aa52 122 0x68a69e81, 0x6a6d98a4, 0x6c242960, 0x6dca0d14, 0x6f5f02b2, 0x70e2cbc6,
emilmont 1:fdd22bb7aa52 123 0x72552c85, 0x73b5ebd1,
emilmont 1:fdd22bb7aa52 124 0x7504d345, 0x7641af3d, 0x776c4edb, 0x78848414, 0x798a23b1, 0x7a7d055b,
emilmont 1:fdd22bb7aa52 125 0x7b5d039e, 0x7c29fbee,
emilmont 1:fdd22bb7aa52 126 0x7ce3ceb2, 0x7d8a5f40, 0x7e1d93ea, 0x7e9d55fc, 0x7f0991c4, 0x7f62368f,
emilmont 1:fdd22bb7aa52 127 0x7fa736b4, 0x7fd8878e,
emilmont 1:fdd22bb7aa52 128 0x7ff62182, 0x7fffffff, 0x7ff62182
emilmont 1:fdd22bb7aa52 129 };
emilmont 1:fdd22bb7aa52 130
emilmont 1:fdd22bb7aa52 131 /**
emilmont 1:fdd22bb7aa52 132 * @brief Fast approximation to the trigonometric cosine function for Q31 data.
emilmont 1:fdd22bb7aa52 133 * @param[in] x Scaled input value in radians.
emilmont 1:fdd22bb7aa52 134 * @return cos(x).
emilmont 1:fdd22bb7aa52 135 *
emilmont 1:fdd22bb7aa52 136 * The Q31 input value is in the range [0 +0.9999] and is mapped to a radian value in the range [0 2*pi), Here range excludes 2*pi.
emilmont 1:fdd22bb7aa52 137 */
emilmont 1:fdd22bb7aa52 138
emilmont 1:fdd22bb7aa52 139 q31_t arm_cos_q31(
emilmont 1:fdd22bb7aa52 140 q31_t x)
emilmont 1:fdd22bb7aa52 141 {
emilmont 1:fdd22bb7aa52 142 q31_t cosVal, in, in2; /* Temporary variables for input, output */
emilmont 1:fdd22bb7aa52 143 q31_t wa, wb, wc, wd; /* Cubic interpolation coefficients */
emilmont 1:fdd22bb7aa52 144 q31_t a, b, c, d; /* Four nearest output values */
emilmont 1:fdd22bb7aa52 145 q31_t *tablePtr; /* Pointer to table */
emilmont 1:fdd22bb7aa52 146 q31_t fract, fractCube, fractSquare; /* Temporary values for fractional values */
emilmont 1:fdd22bb7aa52 147 q31_t oneBy6 = 0x15555555; /* Fixed point value of 1/6 */
emilmont 1:fdd22bb7aa52 148 q31_t tableSpacing = TABLE_SPACING_Q31; /* Table spacing */
emilmont 1:fdd22bb7aa52 149 q31_t temp; /* Temporary variable for intermediate process */
emilmont 1:fdd22bb7aa52 150 int32_t index; /* Index variable */
emilmont 1:fdd22bb7aa52 151
emilmont 1:fdd22bb7aa52 152 in = x;
emilmont 1:fdd22bb7aa52 153
emilmont 1:fdd22bb7aa52 154 /* Calculate the nearest index */
emilmont 1:fdd22bb7aa52 155 index = in / tableSpacing;
emilmont 1:fdd22bb7aa52 156
emilmont 1:fdd22bb7aa52 157 /* Calculate the nearest value of input */
emilmont 1:fdd22bb7aa52 158 in2 = ((q31_t) index) * tableSpacing;
emilmont 1:fdd22bb7aa52 159
emilmont 1:fdd22bb7aa52 160 /* Calculation of fractional value */
emilmont 1:fdd22bb7aa52 161 fract = (in - in2) << 8;
emilmont 1:fdd22bb7aa52 162
emilmont 1:fdd22bb7aa52 163 /* fractSquare = fract * fract */
emilmont 1:fdd22bb7aa52 164 fractSquare = ((q31_t) (((q63_t) fract * fract) >> 32));
emilmont 1:fdd22bb7aa52 165 fractSquare = fractSquare << 1;
emilmont 1:fdd22bb7aa52 166
emilmont 1:fdd22bb7aa52 167 /* fractCube = fract * fract * fract */
emilmont 1:fdd22bb7aa52 168 fractCube = ((q31_t) (((q63_t) fractSquare * fract) >> 32));
emilmont 1:fdd22bb7aa52 169 fractCube = fractCube << 1;
emilmont 1:fdd22bb7aa52 170
emilmont 1:fdd22bb7aa52 171 /* Checking min and max index of table */
emilmont 1:fdd22bb7aa52 172 if(index < 0)
emilmont 1:fdd22bb7aa52 173 {
emilmont 1:fdd22bb7aa52 174 index = 0;
emilmont 1:fdd22bb7aa52 175 }
emilmont 1:fdd22bb7aa52 176 else if(index > 256)
emilmont 1:fdd22bb7aa52 177 {
emilmont 1:fdd22bb7aa52 178 index = 256;
emilmont 1:fdd22bb7aa52 179 }
emilmont 1:fdd22bb7aa52 180
emilmont 1:fdd22bb7aa52 181 /* Initialise table pointer */
emilmont 1:fdd22bb7aa52 182 tablePtr = (q31_t *) & cosTableQ31[index];
emilmont 1:fdd22bb7aa52 183
emilmont 1:fdd22bb7aa52 184 /* Cubic interpolation process */
emilmont 1:fdd22bb7aa52 185 /* Calculation of wa */
emilmont 1:fdd22bb7aa52 186 /* wa = -(oneBy6)*fractCube + (fractSquare >> 1u) - (0x2AAAAAAA)*fract; */
emilmont 1:fdd22bb7aa52 187 wa = ((q31_t) (((q63_t) oneBy6 * fractCube) >> 32));
emilmont 1:fdd22bb7aa52 188 temp = 0x2AAAAAAA;
emilmont 1:fdd22bb7aa52 189 wa = (q31_t) ((((q63_t) wa << 32) + ((q63_t) temp * fract)) >> 32);
emilmont 1:fdd22bb7aa52 190 wa = -(wa << 1u);
emilmont 1:fdd22bb7aa52 191 wa += (fractSquare >> 1u);
emilmont 1:fdd22bb7aa52 192
emilmont 1:fdd22bb7aa52 193 /* Read first nearest value of output from the cos table */
emilmont 1:fdd22bb7aa52 194 a = *tablePtr++;
emilmont 1:fdd22bb7aa52 195
emilmont 1:fdd22bb7aa52 196 /* cosVal = a*wa */
emilmont 1:fdd22bb7aa52 197 cosVal = ((q31_t) (((q63_t) a * wa) >> 32));
emilmont 1:fdd22bb7aa52 198
emilmont 1:fdd22bb7aa52 199 /* q31(1.31) Fixed point value of 1 */
emilmont 1:fdd22bb7aa52 200 temp = 0x7FFFFFFF;
emilmont 1:fdd22bb7aa52 201
emilmont 1:fdd22bb7aa52 202 /* Calculation of wb */
emilmont 1:fdd22bb7aa52 203 wb = ((fractCube >> 1u) - (fractSquare + (fract >> 1u))) + temp;
emilmont 1:fdd22bb7aa52 204 /* Read second nearest value of output from the cos table */
emilmont 1:fdd22bb7aa52 205 b = *tablePtr++;
emilmont 1:fdd22bb7aa52 206
emilmont 1:fdd22bb7aa52 207 /* cosVal += b*wb */
emilmont 1:fdd22bb7aa52 208 cosVal = (q31_t) ((((q63_t) cosVal << 32) + ((q63_t) b * (wb))) >> 32);
emilmont 1:fdd22bb7aa52 209
emilmont 1:fdd22bb7aa52 210 /* Calculation of wc */
emilmont 1:fdd22bb7aa52 211 wc = -fractCube + fractSquare;
emilmont 1:fdd22bb7aa52 212 wc = (wc >> 1u) + fract;
emilmont 1:fdd22bb7aa52 213 /* Read third nearest values of output value from the cos table */
emilmont 1:fdd22bb7aa52 214 c = *tablePtr++;
emilmont 1:fdd22bb7aa52 215
emilmont 1:fdd22bb7aa52 216 /* cosVal += c*wc */
emilmont 1:fdd22bb7aa52 217 cosVal = (q31_t) ((((q63_t) cosVal << 32) + ((q63_t) c * (wc))) >> 32);
emilmont 1:fdd22bb7aa52 218
emilmont 1:fdd22bb7aa52 219 /* Calculation of wd */
emilmont 1:fdd22bb7aa52 220 /* wd = (oneBy6)*fractCube - (oneBy6)*fract; */
emilmont 1:fdd22bb7aa52 221 fractCube = fractCube - fract;
emilmont 1:fdd22bb7aa52 222 wd = ((q31_t) (((q63_t) oneBy6 * fractCube) >> 32));
emilmont 1:fdd22bb7aa52 223 wd = (wd << 1u);
emilmont 1:fdd22bb7aa52 224
emilmont 1:fdd22bb7aa52 225 /* Read fourth nearest value of output from the cos table */
emilmont 1:fdd22bb7aa52 226 d = *tablePtr++;
emilmont 1:fdd22bb7aa52 227
emilmont 1:fdd22bb7aa52 228 /* cosVal += d*wd; */
emilmont 1:fdd22bb7aa52 229 cosVal = (q31_t) ((((q63_t) cosVal << 32) + ((q63_t) d * (wd))) >> 32);
emilmont 1:fdd22bb7aa52 230
emilmont 1:fdd22bb7aa52 231
emilmont 1:fdd22bb7aa52 232 /* convert cosVal in 2.30 format to 1.31 format */
emilmont 1:fdd22bb7aa52 233 return (__QADD(cosVal, cosVal));
emilmont 1:fdd22bb7aa52 234
emilmont 1:fdd22bb7aa52 235 }
emilmont 1:fdd22bb7aa52 236
emilmont 1:fdd22bb7aa52 237 /**
emilmont 1:fdd22bb7aa52 238 * @} end of cos group
emilmont 1:fdd22bb7aa52 239 */