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_sin_q15.c
simon 0:1014af42efd9 9 *
simon 0:1014af42efd9 10 * Description: Fast sine calculation for Q15 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 sin
simon 0:1014af42efd9 35 * @{
simon 0:1014af42efd9 36 */
simon 0:1014af42efd9 37
simon 0:1014af42efd9 38
simon 0:1014af42efd9 39 /**
simon 0:1014af42efd9 40 * \par
simon 0:1014af42efd9 41 * Example code for Generation of Q15 Sin Table:
simon 0:1014af42efd9 42 * \par
simon 0:1014af42efd9 43 * <pre>tableSize = 256;
simon 0:1014af42efd9 44 * for(n = -1; n < (tableSize + 1); n++)
simon 0:1014af42efd9 45 * {
simon 0:1014af42efd9 46 * sinTable[n+1]=sin(2*pi*n/tableSize);
simon 0:1014af42efd9 47 * } </pre>
simon 0:1014af42efd9 48 * where pi value is 3.14159265358979
simon 0:1014af42efd9 49 * \par
simon 0:1014af42efd9 50 * Convert Floating point to Q15(Fixed point):
simon 0:1014af42efd9 51 * (sinTable[i] * pow(2, 15))
simon 0:1014af42efd9 52 * \par
simon 0:1014af42efd9 53 * rounding to nearest integer is done
simon 0:1014af42efd9 54 * sinTable[i] += (sinTable[i] > 0 ? 0.5 :-0.5);
simon 0:1014af42efd9 55 */
simon 0:1014af42efd9 56
simon 0:1014af42efd9 57
simon 0:1014af42efd9 58 static const q15_t sinTableQ15[259] = {
simon 0:1014af42efd9 59 0xfcdc, 0x0, 0x324, 0x648, 0x96b, 0xc8c, 0xfab, 0x12c8,
simon 0:1014af42efd9 60 0x15e2, 0x18f9, 0x1c0c, 0x1f1a, 0x2224, 0x2528, 0x2827, 0x2b1f,
simon 0:1014af42efd9 61 0x2e11, 0x30fc, 0x33df, 0x36ba, 0x398d, 0x3c57, 0x3f17, 0x41ce,
simon 0:1014af42efd9 62 0x447b, 0x471d, 0x49b4, 0x4c40, 0x4ec0, 0x5134, 0x539b, 0x55f6,
simon 0:1014af42efd9 63 0x5843, 0x5a82, 0x5cb4, 0x5ed7, 0x60ec, 0x62f2, 0x64e9, 0x66d0,
simon 0:1014af42efd9 64 0x68a7, 0x6a6e, 0x6c24, 0x6dca, 0x6f5f, 0x70e3, 0x7255, 0x73b6,
simon 0:1014af42efd9 65 0x7505, 0x7642, 0x776c, 0x7885, 0x798a, 0x7a7d, 0x7b5d, 0x7c2a,
simon 0:1014af42efd9 66 0x7ce4, 0x7d8a, 0x7e1e, 0x7e9d, 0x7f0a, 0x7f62, 0x7fa7, 0x7fd9,
simon 0:1014af42efd9 67 0x7ff6, 0x7fff, 0x7ff6, 0x7fd9, 0x7fa7, 0x7f62, 0x7f0a, 0x7e9d,
simon 0:1014af42efd9 68 0x7e1e, 0x7d8a, 0x7ce4, 0x7c2a, 0x7b5d, 0x7a7d, 0x798a, 0x7885,
simon 0:1014af42efd9 69 0x776c, 0x7642, 0x7505, 0x73b6, 0x7255, 0x70e3, 0x6f5f, 0x6dca,
simon 0:1014af42efd9 70 0x6c24, 0x6a6e, 0x68a7, 0x66d0, 0x64e9, 0x62f2, 0x60ec, 0x5ed7,
simon 0:1014af42efd9 71 0x5cb4, 0x5a82, 0x5843, 0x55f6, 0x539b, 0x5134, 0x4ec0, 0x4c40,
simon 0:1014af42efd9 72 0x49b4, 0x471d, 0x447b, 0x41ce, 0x3f17, 0x3c57, 0x398d, 0x36ba,
simon 0:1014af42efd9 73 0x33df, 0x30fc, 0x2e11, 0x2b1f, 0x2827, 0x2528, 0x2224, 0x1f1a,
simon 0:1014af42efd9 74 0x1c0c, 0x18f9, 0x15e2, 0x12c8, 0xfab, 0xc8c, 0x96b, 0x648,
simon 0:1014af42efd9 75 0x324, 0x0, 0xfcdc, 0xf9b8, 0xf695, 0xf374, 0xf055, 0xed38,
simon 0:1014af42efd9 76 0xea1e, 0xe707, 0xe3f4, 0xe0e6, 0xdddc, 0xdad8, 0xd7d9, 0xd4e1,
simon 0:1014af42efd9 77 0xd1ef, 0xcf04, 0xcc21, 0xc946, 0xc673, 0xc3a9, 0xc0e9, 0xbe32,
simon 0:1014af42efd9 78 0xbb85, 0xb8e3, 0xb64c, 0xb3c0, 0xb140, 0xaecc, 0xac65, 0xaa0a,
simon 0:1014af42efd9 79 0xa7bd, 0xa57e, 0xa34c, 0xa129, 0x9f14, 0x9d0e, 0x9b17, 0x9930,
simon 0:1014af42efd9 80 0x9759, 0x9592, 0x93dc, 0x9236, 0x90a1, 0x8f1d, 0x8dab, 0x8c4a,
simon 0:1014af42efd9 81 0x8afb, 0x89be, 0x8894, 0x877b, 0x8676, 0x8583, 0x84a3, 0x83d6,
simon 0:1014af42efd9 82 0x831c, 0x8276, 0x81e2, 0x8163, 0x80f6, 0x809e, 0x8059, 0x8027,
simon 0:1014af42efd9 83 0x800a, 0x8000, 0x800a, 0x8027, 0x8059, 0x809e, 0x80f6, 0x8163,
simon 0:1014af42efd9 84 0x81e2, 0x8276, 0x831c, 0x83d6, 0x84a3, 0x8583, 0x8676, 0x877b,
simon 0:1014af42efd9 85 0x8894, 0x89be, 0x8afb, 0x8c4a, 0x8dab, 0x8f1d, 0x90a1, 0x9236,
simon 0:1014af42efd9 86 0x93dc, 0x9592, 0x9759, 0x9930, 0x9b17, 0x9d0e, 0x9f14, 0xa129,
simon 0:1014af42efd9 87 0xa34c, 0xa57e, 0xa7bd, 0xaa0a, 0xac65, 0xaecc, 0xb140, 0xb3c0,
simon 0:1014af42efd9 88 0xb64c, 0xb8e3, 0xbb85, 0xbe32, 0xc0e9, 0xc3a9, 0xc673, 0xc946,
simon 0:1014af42efd9 89 0xcc21, 0xcf04, 0xd1ef, 0xd4e1, 0xd7d9, 0xdad8, 0xdddc, 0xe0e6,
simon 0:1014af42efd9 90 0xe3f4, 0xe707, 0xea1e, 0xed38, 0xf055, 0xf374, 0xf695, 0xf9b8,
simon 0:1014af42efd9 91 0xfcdc, 0x0, 0x324
simon 0:1014af42efd9 92 };
simon 0:1014af42efd9 93
simon 0:1014af42efd9 94
simon 0:1014af42efd9 95 /**
simon 0:1014af42efd9 96 * @brief Fast approximation to the trigonometric sine function for Q15 data.
simon 0:1014af42efd9 97 * @param[in] x Scaled input value in radians.
simon 0:1014af42efd9 98 * @return sin(x).
simon 0:1014af42efd9 99 *
simon 0:1014af42efd9 100 * The Q15 input value is in the range [0 +1) and is mapped to a radian value in the range [0 2*pi).
simon 0:1014af42efd9 101 */
simon 0:1014af42efd9 102
simon 0:1014af42efd9 103 q15_t arm_sin_q15(
simon 0:1014af42efd9 104 q15_t x)
simon 0:1014af42efd9 105 {
simon 0:1014af42efd9 106 q31_t sinVal; /* Temporary variables output */
simon 0:1014af42efd9 107 q15_t *tablePtr; /* Pointer to table */
simon 0:1014af42efd9 108 q15_t fract, in, in2; /* Temporary variables for input, output */
simon 0:1014af42efd9 109 q31_t wa, wb, wc, wd; /* Cubic interpolation coefficients */
simon 0:1014af42efd9 110 q15_t a, b, c, d; /* Four nearest output values */
simon 0:1014af42efd9 111 q15_t fractCube, fractSquare; /* Temporary values for fractional value */
simon 0:1014af42efd9 112 q15_t oneBy6 = 0x1555; /* Fixed point value of 1/6 */
simon 0:1014af42efd9 113 q15_t tableSpacing = TABLE_SPACING_Q15; /* Table spacing */
simon 0:1014af42efd9 114 int32_t index; /* Index variable */
simon 0:1014af42efd9 115
simon 0:1014af42efd9 116 in = x;
simon 0:1014af42efd9 117
simon 0:1014af42efd9 118 /* Calculate the nearest index */
simon 0:1014af42efd9 119 index = (int32_t) in / tableSpacing;
simon 0:1014af42efd9 120
simon 0:1014af42efd9 121 /* Calculate the nearest value of input */
simon 0:1014af42efd9 122 in2 = (q15_t) ((index) * tableSpacing);
simon 0:1014af42efd9 123
simon 0:1014af42efd9 124 /* Calculation of fractional value */
simon 0:1014af42efd9 125 fract = (in - in2) << 8;
simon 0:1014af42efd9 126
simon 0:1014af42efd9 127 /* fractSquare = fract * fract */
simon 0:1014af42efd9 128 fractSquare = (q15_t) ((fract * fract) >> 15);
simon 0:1014af42efd9 129
simon 0:1014af42efd9 130 /* fractCube = fract * fract * fract */
simon 0:1014af42efd9 131 fractCube = (q15_t) ((fractSquare * fract) >> 15);
simon 0:1014af42efd9 132
simon 0:1014af42efd9 133 /* Initialise table pointer */
simon 0:1014af42efd9 134 tablePtr = (q15_t *) & sinTableQ15[index];
simon 0:1014af42efd9 135
simon 0:1014af42efd9 136 /* Cubic interpolation process */
simon 0:1014af42efd9 137 /* Calculation of wa */
simon 0:1014af42efd9 138 /* wa = -(oneBy6)*fractCube + (fractSquare >> 1u) - (0x2AAA)*fract; */
simon 0:1014af42efd9 139 wa = (q31_t) oneBy6 *fractCube;
simon 0:1014af42efd9 140 wa += (q31_t) 0x2AAA * fract;
simon 0:1014af42efd9 141 wa = -(wa >> 15);
simon 0:1014af42efd9 142 wa += ((q31_t) fractSquare >> 1u);
simon 0:1014af42efd9 143
simon 0:1014af42efd9 144 /* Read first nearest value of output from the sin table */
simon 0:1014af42efd9 145 a = *tablePtr++;
simon 0:1014af42efd9 146
simon 0:1014af42efd9 147 /* sinVal = a * wa */
simon 0:1014af42efd9 148 sinVal = a * wa;
simon 0:1014af42efd9 149
simon 0:1014af42efd9 150 /* Calculation of wb */
simon 0:1014af42efd9 151 wb = (((q31_t) fractCube >> 1u) - (q31_t) fractSquare) -
simon 0:1014af42efd9 152 (((q31_t) fract >> 1u) - 0x7FFF);
simon 0:1014af42efd9 153
simon 0:1014af42efd9 154 /* Read second nearest value of output from the sin table */
simon 0:1014af42efd9 155 b = *tablePtr++;
simon 0:1014af42efd9 156
simon 0:1014af42efd9 157 /* sinVal += b*wb */
simon 0:1014af42efd9 158 sinVal += b * wb;
simon 0:1014af42efd9 159
simon 0:1014af42efd9 160
simon 0:1014af42efd9 161 /* Calculation of wc */
simon 0:1014af42efd9 162 wc = -(q31_t) fractCube + fractSquare;
simon 0:1014af42efd9 163 wc = (wc >> 1u) + fract;
simon 0:1014af42efd9 164
simon 0:1014af42efd9 165 /* Read third nearest value of output from the sin table */
simon 0:1014af42efd9 166 c = *tablePtr++;
simon 0:1014af42efd9 167
simon 0:1014af42efd9 168 /* sinVal += c*wc */
simon 0:1014af42efd9 169 sinVal += c * wc;
simon 0:1014af42efd9 170
simon 0:1014af42efd9 171 /* Calculation of wd */
simon 0:1014af42efd9 172 /* wd = (oneBy6)*fractCube - (oneBy6)*fract; */
simon 0:1014af42efd9 173 fractCube = fractCube - fract;
simon 0:1014af42efd9 174 wd = ((q15_t) (((q31_t) oneBy6 * fractCube) >> 15));
simon 0:1014af42efd9 175
simon 0:1014af42efd9 176 /* Read fourth nearest value of output from the sin table */
simon 0:1014af42efd9 177 d = *tablePtr++;
simon 0:1014af42efd9 178
simon 0:1014af42efd9 179 /* sinVal += d*wd; */
simon 0:1014af42efd9 180 sinVal += d * wd;
simon 0:1014af42efd9 181
simon 0:1014af42efd9 182 /* Return the output value in 1.15(q15) format */
simon 0:1014af42efd9 183 return ((q15_t) (sinVal >> 15u));
simon 0:1014af42efd9 184
simon 0:1014af42efd9 185 }
simon 0:1014af42efd9 186
simon 0:1014af42efd9 187 /**
simon 0:1014af42efd9 188 * @} end of sin group
simon 0:1014af42efd9 189 */