mbed-os 6.10 versione
cmsis_dsp/FastMathFunctions/arm_sin_q15.c@3:7a284390b0ce, 2013-11-08 (annotated)
- Committer:
- mbed_official
- Date:
- Fri Nov 08 13:45:10 2013 +0000
- Revision:
- 3:7a284390b0ce
- Parent:
- 2:da51fb522205
Synchronized with git revision e69956aba2f68a2a26ac26b051f8d349deaa1ce8
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
emilmont | 1:fdd22bb7aa52 | 1 | /* ---------------------------------------------------------------------- |
mbed_official | 3:7a284390b0ce | 2 | * Copyright (C) 2010-2013 ARM Limited. All rights reserved. |
emilmont | 1:fdd22bb7aa52 | 3 | * |
mbed_official | 3:7a284390b0ce | 4 | * $Date: 17. January 2013 |
mbed_official | 3:7a284390b0ce | 5 | * $Revision: V1.4.1 |
emilmont | 1:fdd22bb7aa52 | 6 | * |
emilmont | 2:da51fb522205 | 7 | * Project: CMSIS DSP Library |
emilmont | 2:da51fb522205 | 8 | * Title: arm_sin_q15.c |
emilmont | 1:fdd22bb7aa52 | 9 | * |
emilmont | 2:da51fb522205 | 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 | * |
mbed_official | 3:7a284390b0ce | 14 | * Redistribution and use in source and binary forms, with or without |
mbed_official | 3:7a284390b0ce | 15 | * modification, are permitted provided that the following conditions |
mbed_official | 3:7a284390b0ce | 16 | * are met: |
mbed_official | 3:7a284390b0ce | 17 | * - Redistributions of source code must retain the above copyright |
mbed_official | 3:7a284390b0ce | 18 | * notice, this list of conditions and the following disclaimer. |
mbed_official | 3:7a284390b0ce | 19 | * - Redistributions in binary form must reproduce the above copyright |
mbed_official | 3:7a284390b0ce | 20 | * notice, this list of conditions and the following disclaimer in |
mbed_official | 3:7a284390b0ce | 21 | * the documentation and/or other materials provided with the |
mbed_official | 3:7a284390b0ce | 22 | * distribution. |
mbed_official | 3:7a284390b0ce | 23 | * - Neither the name of ARM LIMITED nor the names of its contributors |
mbed_official | 3:7a284390b0ce | 24 | * may be used to endorse or promote products derived from this |
mbed_official | 3:7a284390b0ce | 25 | * software without specific prior written permission. |
mbed_official | 3:7a284390b0ce | 26 | * |
mbed_official | 3:7a284390b0ce | 27 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
mbed_official | 3:7a284390b0ce | 28 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
mbed_official | 3:7a284390b0ce | 29 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
mbed_official | 3:7a284390b0ce | 30 | * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
mbed_official | 3:7a284390b0ce | 31 | * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
mbed_official | 3:7a284390b0ce | 32 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
mbed_official | 3:7a284390b0ce | 33 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
mbed_official | 3:7a284390b0ce | 34 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
mbed_official | 3:7a284390b0ce | 35 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
mbed_official | 3:7a284390b0ce | 36 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN |
mbed_official | 3:7a284390b0ce | 37 | * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
mbed_official | 3:7a284390b0ce | 38 | * POSSIBILITY OF SUCH DAMAGE. |
emilmont | 1:fdd22bb7aa52 | 39 | * -------------------------------------------------------------------- */ |
emilmont | 1:fdd22bb7aa52 | 40 | |
emilmont | 1:fdd22bb7aa52 | 41 | #include "arm_math.h" |
emilmont | 1:fdd22bb7aa52 | 42 | |
emilmont | 1:fdd22bb7aa52 | 43 | /** |
emilmont | 1:fdd22bb7aa52 | 44 | * @ingroup groupFastMath |
emilmont | 1:fdd22bb7aa52 | 45 | */ |
emilmont | 1:fdd22bb7aa52 | 46 | |
emilmont | 1:fdd22bb7aa52 | 47 | /** |
emilmont | 1:fdd22bb7aa52 | 48 | * @addtogroup sin |
emilmont | 1:fdd22bb7aa52 | 49 | * @{ |
emilmont | 1:fdd22bb7aa52 | 50 | */ |
emilmont | 1:fdd22bb7aa52 | 51 | |
emilmont | 1:fdd22bb7aa52 | 52 | |
mbed_official | 3:7a284390b0ce | 53 | /** |
mbed_official | 3:7a284390b0ce | 54 | * \par |
mbed_official | 3:7a284390b0ce | 55 | * Table values are in Q15 (1.15 fixed-point format) and generation is done in |
mbed_official | 3:7a284390b0ce | 56 | * three steps. First, generate sin values in floating point: |
mbed_official | 3:7a284390b0ce | 57 | * <pre> |
mbed_official | 3:7a284390b0ce | 58 | * tableSize = 256; |
emilmont | 1:fdd22bb7aa52 | 59 | * for(n = -1; n < (tableSize + 1); n++) |
emilmont | 1:fdd22bb7aa52 | 60 | * { |
mbed_official | 3:7a284390b0ce | 61 | * sinTable[n+1]= sin(2*pi*n/tableSize); |
mbed_official | 3:7a284390b0ce | 62 | * } </pre> |
emilmont | 1:fdd22bb7aa52 | 63 | * where pi value is 3.14159265358979 |
emilmont | 1:fdd22bb7aa52 | 64 | * \par |
mbed_official | 3:7a284390b0ce | 65 | * Second, convert floating-point to Q15 (fixed-point): |
emilmont | 2:da51fb522205 | 66 | * (sinTable[i] * pow(2, 15)) |
emilmont | 1:fdd22bb7aa52 | 67 | * \par |
mbed_official | 3:7a284390b0ce | 68 | * Finally, round to the nearest integer value: |
emilmont | 2:da51fb522205 | 69 | * sinTable[i] += (sinTable[i] > 0 ? 0.5 :-0.5); |
mbed_official | 3:7a284390b0ce | 70 | */ |
emilmont | 1:fdd22bb7aa52 | 71 | |
emilmont | 1:fdd22bb7aa52 | 72 | static const q15_t sinTableQ15[259] = { |
emilmont | 1:fdd22bb7aa52 | 73 | 0xfcdc, 0x0, 0x324, 0x648, 0x96b, 0xc8c, 0xfab, 0x12c8, |
emilmont | 1:fdd22bb7aa52 | 74 | 0x15e2, 0x18f9, 0x1c0c, 0x1f1a, 0x2224, 0x2528, 0x2827, 0x2b1f, |
emilmont | 1:fdd22bb7aa52 | 75 | 0x2e11, 0x30fc, 0x33df, 0x36ba, 0x398d, 0x3c57, 0x3f17, 0x41ce, |
emilmont | 1:fdd22bb7aa52 | 76 | 0x447b, 0x471d, 0x49b4, 0x4c40, 0x4ec0, 0x5134, 0x539b, 0x55f6, |
emilmont | 1:fdd22bb7aa52 | 77 | 0x5843, 0x5a82, 0x5cb4, 0x5ed7, 0x60ec, 0x62f2, 0x64e9, 0x66d0, |
emilmont | 1:fdd22bb7aa52 | 78 | 0x68a7, 0x6a6e, 0x6c24, 0x6dca, 0x6f5f, 0x70e3, 0x7255, 0x73b6, |
emilmont | 1:fdd22bb7aa52 | 79 | 0x7505, 0x7642, 0x776c, 0x7885, 0x798a, 0x7a7d, 0x7b5d, 0x7c2a, |
emilmont | 1:fdd22bb7aa52 | 80 | 0x7ce4, 0x7d8a, 0x7e1e, 0x7e9d, 0x7f0a, 0x7f62, 0x7fa7, 0x7fd9, |
emilmont | 1:fdd22bb7aa52 | 81 | 0x7ff6, 0x7fff, 0x7ff6, 0x7fd9, 0x7fa7, 0x7f62, 0x7f0a, 0x7e9d, |
emilmont | 1:fdd22bb7aa52 | 82 | 0x7e1e, 0x7d8a, 0x7ce4, 0x7c2a, 0x7b5d, 0x7a7d, 0x798a, 0x7885, |
emilmont | 1:fdd22bb7aa52 | 83 | 0x776c, 0x7642, 0x7505, 0x73b6, 0x7255, 0x70e3, 0x6f5f, 0x6dca, |
emilmont | 1:fdd22bb7aa52 | 84 | 0x6c24, 0x6a6e, 0x68a7, 0x66d0, 0x64e9, 0x62f2, 0x60ec, 0x5ed7, |
emilmont | 1:fdd22bb7aa52 | 85 | 0x5cb4, 0x5a82, 0x5843, 0x55f6, 0x539b, 0x5134, 0x4ec0, 0x4c40, |
emilmont | 1:fdd22bb7aa52 | 86 | 0x49b4, 0x471d, 0x447b, 0x41ce, 0x3f17, 0x3c57, 0x398d, 0x36ba, |
emilmont | 1:fdd22bb7aa52 | 87 | 0x33df, 0x30fc, 0x2e11, 0x2b1f, 0x2827, 0x2528, 0x2224, 0x1f1a, |
emilmont | 1:fdd22bb7aa52 | 88 | 0x1c0c, 0x18f9, 0x15e2, 0x12c8, 0xfab, 0xc8c, 0x96b, 0x648, |
emilmont | 1:fdd22bb7aa52 | 89 | 0x324, 0x0, 0xfcdc, 0xf9b8, 0xf695, 0xf374, 0xf055, 0xed38, |
emilmont | 1:fdd22bb7aa52 | 90 | 0xea1e, 0xe707, 0xe3f4, 0xe0e6, 0xdddc, 0xdad8, 0xd7d9, 0xd4e1, |
emilmont | 1:fdd22bb7aa52 | 91 | 0xd1ef, 0xcf04, 0xcc21, 0xc946, 0xc673, 0xc3a9, 0xc0e9, 0xbe32, |
emilmont | 1:fdd22bb7aa52 | 92 | 0xbb85, 0xb8e3, 0xb64c, 0xb3c0, 0xb140, 0xaecc, 0xac65, 0xaa0a, |
emilmont | 1:fdd22bb7aa52 | 93 | 0xa7bd, 0xa57e, 0xa34c, 0xa129, 0x9f14, 0x9d0e, 0x9b17, 0x9930, |
emilmont | 1:fdd22bb7aa52 | 94 | 0x9759, 0x9592, 0x93dc, 0x9236, 0x90a1, 0x8f1d, 0x8dab, 0x8c4a, |
emilmont | 1:fdd22bb7aa52 | 95 | 0x8afb, 0x89be, 0x8894, 0x877b, 0x8676, 0x8583, 0x84a3, 0x83d6, |
emilmont | 1:fdd22bb7aa52 | 96 | 0x831c, 0x8276, 0x81e2, 0x8163, 0x80f6, 0x809e, 0x8059, 0x8027, |
emilmont | 1:fdd22bb7aa52 | 97 | 0x800a, 0x8000, 0x800a, 0x8027, 0x8059, 0x809e, 0x80f6, 0x8163, |
emilmont | 1:fdd22bb7aa52 | 98 | 0x81e2, 0x8276, 0x831c, 0x83d6, 0x84a3, 0x8583, 0x8676, 0x877b, |
emilmont | 1:fdd22bb7aa52 | 99 | 0x8894, 0x89be, 0x8afb, 0x8c4a, 0x8dab, 0x8f1d, 0x90a1, 0x9236, |
emilmont | 1:fdd22bb7aa52 | 100 | 0x93dc, 0x9592, 0x9759, 0x9930, 0x9b17, 0x9d0e, 0x9f14, 0xa129, |
emilmont | 1:fdd22bb7aa52 | 101 | 0xa34c, 0xa57e, 0xa7bd, 0xaa0a, 0xac65, 0xaecc, 0xb140, 0xb3c0, |
emilmont | 1:fdd22bb7aa52 | 102 | 0xb64c, 0xb8e3, 0xbb85, 0xbe32, 0xc0e9, 0xc3a9, 0xc673, 0xc946, |
emilmont | 1:fdd22bb7aa52 | 103 | 0xcc21, 0xcf04, 0xd1ef, 0xd4e1, 0xd7d9, 0xdad8, 0xdddc, 0xe0e6, |
emilmont | 1:fdd22bb7aa52 | 104 | 0xe3f4, 0xe707, 0xea1e, 0xed38, 0xf055, 0xf374, 0xf695, 0xf9b8, |
emilmont | 1:fdd22bb7aa52 | 105 | 0xfcdc, 0x0, 0x324 |
emilmont | 1:fdd22bb7aa52 | 106 | }; |
emilmont | 1:fdd22bb7aa52 | 107 | |
emilmont | 1:fdd22bb7aa52 | 108 | |
emilmont | 1:fdd22bb7aa52 | 109 | /** |
emilmont | 1:fdd22bb7aa52 | 110 | * @brief Fast approximation to the trigonometric sine function for Q15 data. |
emilmont | 1:fdd22bb7aa52 | 111 | * @param[in] x Scaled input value in radians. |
emilmont | 1:fdd22bb7aa52 | 112 | * @return sin(x). |
emilmont | 1:fdd22bb7aa52 | 113 | * |
mbed_official | 3:7a284390b0ce | 114 | * The Q15 input value is in the range [0 +0.9999] and is mapped to a radian value in the range [0 2*pi). |
emilmont | 1:fdd22bb7aa52 | 115 | */ |
emilmont | 1:fdd22bb7aa52 | 116 | |
emilmont | 1:fdd22bb7aa52 | 117 | q15_t arm_sin_q15( |
emilmont | 1:fdd22bb7aa52 | 118 | q15_t x) |
emilmont | 1:fdd22bb7aa52 | 119 | { |
emilmont | 1:fdd22bb7aa52 | 120 | q31_t sinVal; /* Temporary variables output */ |
emilmont | 1:fdd22bb7aa52 | 121 | q15_t *tablePtr; /* Pointer to table */ |
emilmont | 1:fdd22bb7aa52 | 122 | q15_t fract, in, in2; /* Temporary variables for input, output */ |
emilmont | 1:fdd22bb7aa52 | 123 | q31_t wa, wb, wc, wd; /* Cubic interpolation coefficients */ |
emilmont | 1:fdd22bb7aa52 | 124 | q15_t a, b, c, d; /* Four nearest output values */ |
emilmont | 1:fdd22bb7aa52 | 125 | q15_t fractCube, fractSquare; /* Temporary values for fractional value */ |
emilmont | 1:fdd22bb7aa52 | 126 | q15_t oneBy6 = 0x1555; /* Fixed point value of 1/6 */ |
emilmont | 1:fdd22bb7aa52 | 127 | q15_t tableSpacing = TABLE_SPACING_Q15; /* Table spacing */ |
emilmont | 1:fdd22bb7aa52 | 128 | int32_t index; /* Index variable */ |
emilmont | 1:fdd22bb7aa52 | 129 | |
emilmont | 1:fdd22bb7aa52 | 130 | in = x; |
emilmont | 1:fdd22bb7aa52 | 131 | |
emilmont | 1:fdd22bb7aa52 | 132 | /* Calculate the nearest index */ |
emilmont | 1:fdd22bb7aa52 | 133 | index = (int32_t) in / tableSpacing; |
emilmont | 1:fdd22bb7aa52 | 134 | |
emilmont | 1:fdd22bb7aa52 | 135 | /* Calculate the nearest value of input */ |
emilmont | 1:fdd22bb7aa52 | 136 | in2 = (q15_t) ((index) * tableSpacing); |
emilmont | 1:fdd22bb7aa52 | 137 | |
emilmont | 1:fdd22bb7aa52 | 138 | /* Calculation of fractional value */ |
emilmont | 1:fdd22bb7aa52 | 139 | fract = (in - in2) << 8; |
emilmont | 1:fdd22bb7aa52 | 140 | |
emilmont | 1:fdd22bb7aa52 | 141 | /* fractSquare = fract * fract */ |
emilmont | 1:fdd22bb7aa52 | 142 | fractSquare = (q15_t) ((fract * fract) >> 15); |
emilmont | 1:fdd22bb7aa52 | 143 | |
emilmont | 1:fdd22bb7aa52 | 144 | /* fractCube = fract * fract * fract */ |
emilmont | 1:fdd22bb7aa52 | 145 | fractCube = (q15_t) ((fractSquare * fract) >> 15); |
emilmont | 1:fdd22bb7aa52 | 146 | |
emilmont | 1:fdd22bb7aa52 | 147 | /* Checking min and max index of table */ |
emilmont | 1:fdd22bb7aa52 | 148 | if(index < 0) |
emilmont | 1:fdd22bb7aa52 | 149 | { |
emilmont | 1:fdd22bb7aa52 | 150 | index = 0; |
emilmont | 1:fdd22bb7aa52 | 151 | } |
emilmont | 1:fdd22bb7aa52 | 152 | else if(index > 256) |
emilmont | 1:fdd22bb7aa52 | 153 | { |
emilmont | 1:fdd22bb7aa52 | 154 | index = 256; |
emilmont | 1:fdd22bb7aa52 | 155 | } |
emilmont | 1:fdd22bb7aa52 | 156 | |
emilmont | 1:fdd22bb7aa52 | 157 | /* Initialise table pointer */ |
emilmont | 1:fdd22bb7aa52 | 158 | tablePtr = (q15_t *) & sinTableQ15[index]; |
emilmont | 1:fdd22bb7aa52 | 159 | |
emilmont | 1:fdd22bb7aa52 | 160 | /* Cubic interpolation process */ |
emilmont | 1:fdd22bb7aa52 | 161 | /* Calculation of wa */ |
emilmont | 1:fdd22bb7aa52 | 162 | /* wa = -(oneBy6)*fractCube + (fractSquare >> 1u) - (0x2AAA)*fract; */ |
emilmont | 1:fdd22bb7aa52 | 163 | wa = (q31_t) oneBy6 *fractCube; |
emilmont | 1:fdd22bb7aa52 | 164 | wa += (q31_t) 0x2AAA *fract; |
emilmont | 1:fdd22bb7aa52 | 165 | wa = -(wa >> 15); |
emilmont | 1:fdd22bb7aa52 | 166 | wa += ((q31_t) fractSquare >> 1u); |
emilmont | 1:fdd22bb7aa52 | 167 | |
emilmont | 1:fdd22bb7aa52 | 168 | /* Read first nearest value of output from the sin table */ |
emilmont | 1:fdd22bb7aa52 | 169 | a = *tablePtr++; |
emilmont | 1:fdd22bb7aa52 | 170 | |
emilmont | 1:fdd22bb7aa52 | 171 | /* sinVal = a * wa */ |
emilmont | 1:fdd22bb7aa52 | 172 | sinVal = a * wa; |
emilmont | 1:fdd22bb7aa52 | 173 | |
emilmont | 1:fdd22bb7aa52 | 174 | /* Calculation of wb */ |
emilmont | 1:fdd22bb7aa52 | 175 | wb = (((q31_t) fractCube >> 1u) - (q31_t) fractSquare) - |
emilmont | 1:fdd22bb7aa52 | 176 | (((q31_t) fract >> 1u) - 0x7FFF); |
emilmont | 1:fdd22bb7aa52 | 177 | |
emilmont | 1:fdd22bb7aa52 | 178 | /* Read second nearest value of output from the sin table */ |
emilmont | 1:fdd22bb7aa52 | 179 | b = *tablePtr++; |
emilmont | 1:fdd22bb7aa52 | 180 | |
emilmont | 1:fdd22bb7aa52 | 181 | /* sinVal += b*wb */ |
emilmont | 1:fdd22bb7aa52 | 182 | sinVal += b * wb; |
emilmont | 1:fdd22bb7aa52 | 183 | |
emilmont | 1:fdd22bb7aa52 | 184 | |
emilmont | 1:fdd22bb7aa52 | 185 | /* Calculation of wc */ |
emilmont | 1:fdd22bb7aa52 | 186 | wc = -(q31_t) fractCube + fractSquare; |
emilmont | 1:fdd22bb7aa52 | 187 | wc = (wc >> 1u) + fract; |
emilmont | 1:fdd22bb7aa52 | 188 | |
emilmont | 1:fdd22bb7aa52 | 189 | /* Read third nearest value of output from the sin table */ |
emilmont | 1:fdd22bb7aa52 | 190 | c = *tablePtr++; |
emilmont | 1:fdd22bb7aa52 | 191 | |
emilmont | 1:fdd22bb7aa52 | 192 | /* sinVal += c*wc */ |
emilmont | 1:fdd22bb7aa52 | 193 | sinVal += c * wc; |
emilmont | 1:fdd22bb7aa52 | 194 | |
emilmont | 1:fdd22bb7aa52 | 195 | /* Calculation of wd */ |
emilmont | 1:fdd22bb7aa52 | 196 | /* wd = (oneBy6)*fractCube - (oneBy6)*fract; */ |
emilmont | 1:fdd22bb7aa52 | 197 | fractCube = fractCube - fract; |
emilmont | 1:fdd22bb7aa52 | 198 | wd = ((q15_t) (((q31_t) oneBy6 * fractCube) >> 15)); |
emilmont | 1:fdd22bb7aa52 | 199 | |
emilmont | 1:fdd22bb7aa52 | 200 | /* Read fourth nearest value of output from the sin table */ |
emilmont | 1:fdd22bb7aa52 | 201 | d = *tablePtr++; |
emilmont | 1:fdd22bb7aa52 | 202 | |
emilmont | 1:fdd22bb7aa52 | 203 | /* sinVal += d*wd; */ |
emilmont | 1:fdd22bb7aa52 | 204 | sinVal += d * wd; |
emilmont | 1:fdd22bb7aa52 | 205 | |
emilmont | 1:fdd22bb7aa52 | 206 | /* Convert output value in 1.15(q15) format and saturate */ |
emilmont | 1:fdd22bb7aa52 | 207 | sinVal = __SSAT((sinVal >> 15), 16); |
emilmont | 1:fdd22bb7aa52 | 208 | |
emilmont | 1:fdd22bb7aa52 | 209 | /* Return the output value in 1.15(q15) format */ |
emilmont | 1:fdd22bb7aa52 | 210 | return ((q15_t) sinVal); |
emilmont | 1:fdd22bb7aa52 | 211 | |
emilmont | 1:fdd22bb7aa52 | 212 | } |
emilmont | 1:fdd22bb7aa52 | 213 | |
emilmont | 1:fdd22bb7aa52 | 214 | /** |
emilmont | 1:fdd22bb7aa52 | 215 | * @} end of sin group |
emilmont | 1:fdd22bb7aa52 | 216 | */ |