CMSIS DSP Library from CMSIS 2.0. See http://www.onarm.com/cmsis/ for full details

Dependents:   K22F_DSP_Matrix_least_square BNO055-ELEC3810 1BNO055 ECE4180Project--Slave2 ... more

Committer:
simon
Date:
Thu Mar 10 15:07:50 2011 +0000
Revision:
0:1014af42efd9

        

Who changed what in which revision?

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