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