CMSIS DSP library
Dependents: KL25Z_FFT_Demo Hat_Board_v5_1 KL25Z_FFT_Demo_tony KL25Z_FFT_Demo_tony ... more
Fork of mbed-dsp by
cmsis_dsp/FastMathFunctions/arm_sin_f32.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_f32.c |
emilmont | 1:fdd22bb7aa52 | 9 | * |
emilmont | 2:da51fb522205 | 10 | * Description: Fast sine calculation for floating-point 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 | * @defgroup sin Sine |
emilmont | 1:fdd22bb7aa52 | 49 | * |
emilmont | 1:fdd22bb7aa52 | 50 | * Computes the trigonometric sine function using a combination of table lookup |
emilmont | 1:fdd22bb7aa52 | 51 | * and cubic interpolation. There are separate functions for |
emilmont | 1:fdd22bb7aa52 | 52 | * Q15, Q31, and floating-point data types. |
emilmont | 1:fdd22bb7aa52 | 53 | * The input to the floating-point version is in radians while the |
emilmont | 1:fdd22bb7aa52 | 54 | * fixed-point Q15 and Q31 have a scaled input with the range |
mbed_official | 3:7a284390b0ce | 55 | * [0 +0.9999] mapping to [0 2*pi). The fixed-point range is chosen so that a |
mbed_official | 3:7a284390b0ce | 56 | * value of 2*pi wraps around to 0. |
emilmont | 1:fdd22bb7aa52 | 57 | * |
emilmont | 1:fdd22bb7aa52 | 58 | * The implementation is based on table lookup using 256 values together with cubic interpolation. |
emilmont | 1:fdd22bb7aa52 | 59 | * The steps used are: |
emilmont | 1:fdd22bb7aa52 | 60 | * -# Calculation of the nearest integer table index |
emilmont | 1:fdd22bb7aa52 | 61 | * -# Fetch the four table values a, b, c, and d |
emilmont | 1:fdd22bb7aa52 | 62 | * -# Compute the fractional portion (fract) of the table index. |
emilmont | 1:fdd22bb7aa52 | 63 | * -# Calculation of wa, wb, wc, wd |
emilmont | 1:fdd22bb7aa52 | 64 | * -# The final result equals <code>a*wa + b*wb + c*wc + d*wd</code> |
emilmont | 1:fdd22bb7aa52 | 65 | * |
emilmont | 1:fdd22bb7aa52 | 66 | * where |
emilmont | 1:fdd22bb7aa52 | 67 | * <pre> |
emilmont | 1:fdd22bb7aa52 | 68 | * a=Table[index-1]; |
emilmont | 1:fdd22bb7aa52 | 69 | * b=Table[index+0]; |
emilmont | 1:fdd22bb7aa52 | 70 | * c=Table[index+1]; |
emilmont | 1:fdd22bb7aa52 | 71 | * d=Table[index+2]; |
emilmont | 1:fdd22bb7aa52 | 72 | * </pre> |
emilmont | 1:fdd22bb7aa52 | 73 | * and |
emilmont | 1:fdd22bb7aa52 | 74 | * <pre> |
emilmont | 1:fdd22bb7aa52 | 75 | * wa=-(1/6)*fract.^3 + (1/2)*fract.^2 - (1/3)*fract; |
emilmont | 1:fdd22bb7aa52 | 76 | * wb=(1/2)*fract.^3 - fract.^2 - (1/2)*fract + 1; |
emilmont | 1:fdd22bb7aa52 | 77 | * wc=-(1/2)*fract.^3+(1/2)*fract.^2+fract; |
emilmont | 1:fdd22bb7aa52 | 78 | * wd=(1/6)*fract.^3 - (1/6)*fract; |
emilmont | 1:fdd22bb7aa52 | 79 | * </pre> |
emilmont | 1:fdd22bb7aa52 | 80 | */ |
emilmont | 1:fdd22bb7aa52 | 81 | |
emilmont | 1:fdd22bb7aa52 | 82 | /** |
emilmont | 1:fdd22bb7aa52 | 83 | * @addtogroup sin |
emilmont | 1:fdd22bb7aa52 | 84 | * @{ |
emilmont | 1:fdd22bb7aa52 | 85 | */ |
emilmont | 1:fdd22bb7aa52 | 86 | |
emilmont | 1:fdd22bb7aa52 | 87 | |
emilmont | 1:fdd22bb7aa52 | 88 | /** |
emilmont | 1:fdd22bb7aa52 | 89 | * \par |
mbed_official | 3:7a284390b0ce | 90 | * Example code for the generation of the floating-point sine table: |
mbed_official | 3:7a284390b0ce | 91 | * <pre> |
emilmont | 1:fdd22bb7aa52 | 92 | * tableSize = 256; |
mbed_official | 3:7a284390b0ce | 93 | * for(n = -1; n < (tableSize + 1); n++) |
emilmont | 1:fdd22bb7aa52 | 94 | * { |
emilmont | 2:da51fb522205 | 95 | * sinTable[n+1]=sin(2*pi*n/tableSize); |
emilmont | 1:fdd22bb7aa52 | 96 | * }</pre> |
emilmont | 1:fdd22bb7aa52 | 97 | * \par |
emilmont | 1:fdd22bb7aa52 | 98 | * where pi value is 3.14159265358979 |
emilmont | 1:fdd22bb7aa52 | 99 | */ |
emilmont | 1:fdd22bb7aa52 | 100 | |
emilmont | 1:fdd22bb7aa52 | 101 | static const float32_t sinTable[259] = { |
emilmont | 1:fdd22bb7aa52 | 102 | -0.024541229009628296f, 0.000000000000000000f, 0.024541229009628296f, |
emilmont | 1:fdd22bb7aa52 | 103 | 0.049067676067352295f, 0.073564566671848297f, 0.098017141222953796f, |
emilmont | 1:fdd22bb7aa52 | 104 | 0.122410677373409270f, 0.146730467677116390f, |
emilmont | 1:fdd22bb7aa52 | 105 | 0.170961886644363400f, 0.195090323686599730f, 0.219101235270500180f, |
emilmont | 1:fdd22bb7aa52 | 106 | 0.242980182170867920f, 0.266712754964828490f, 0.290284663438797000f, |
emilmont | 1:fdd22bb7aa52 | 107 | 0.313681751489639280f, 0.336889863014221190f, |
emilmont | 1:fdd22bb7aa52 | 108 | 0.359895050525665280f, 0.382683426141738890f, 0.405241310596466060f, |
emilmont | 1:fdd22bb7aa52 | 109 | 0.427555084228515630f, 0.449611335992813110f, 0.471396744251251220f, |
emilmont | 1:fdd22bb7aa52 | 110 | 0.492898195981979370f, 0.514102756977081300f, |
emilmont | 1:fdd22bb7aa52 | 111 | 0.534997642040252690f, 0.555570244789123540f, 0.575808167457580570f, |
emilmont | 1:fdd22bb7aa52 | 112 | 0.595699310302734380f, 0.615231573581695560f, 0.634393274784088130f, |
emilmont | 1:fdd22bb7aa52 | 113 | 0.653172850608825680f, 0.671558976173400880f, |
emilmont | 1:fdd22bb7aa52 | 114 | 0.689540565013885500f, 0.707106769084930420f, 0.724247097969055180f, |
emilmont | 1:fdd22bb7aa52 | 115 | 0.740951120853424070f, 0.757208824157714840f, 0.773010432720184330f, |
emilmont | 1:fdd22bb7aa52 | 116 | 0.788346409797668460f, 0.803207516670227050f, |
emilmont | 1:fdd22bb7aa52 | 117 | 0.817584812641143800f, 0.831469595432281490f, 0.844853579998016360f, |
emilmont | 1:fdd22bb7aa52 | 118 | 0.857728600502014160f, 0.870086967945098880f, 0.881921291351318360f, |
emilmont | 1:fdd22bb7aa52 | 119 | 0.893224298954010010f, 0.903989315032958980f, |
emilmont | 1:fdd22bb7aa52 | 120 | 0.914209783077239990f, 0.923879504203796390f, 0.932992815971374510f, |
emilmont | 1:fdd22bb7aa52 | 121 | 0.941544055938720700f, 0.949528157711029050f, 0.956940352916717530f, |
emilmont | 1:fdd22bb7aa52 | 122 | 0.963776051998138430f, 0.970031261444091800f, |
emilmont | 1:fdd22bb7aa52 | 123 | 0.975702106952667240f, 0.980785250663757320f, 0.985277652740478520f, |
emilmont | 1:fdd22bb7aa52 | 124 | 0.989176511764526370f, 0.992479562759399410f, 0.995184719562530520f, |
emilmont | 1:fdd22bb7aa52 | 125 | 0.997290432453155520f, 0.998795449733734130f, |
emilmont | 1:fdd22bb7aa52 | 126 | 0.999698817729949950f, 1.000000000000000000f, 0.999698817729949950f, |
emilmont | 1:fdd22bb7aa52 | 127 | 0.998795449733734130f, 0.997290432453155520f, 0.995184719562530520f, |
emilmont | 1:fdd22bb7aa52 | 128 | 0.992479562759399410f, 0.989176511764526370f, |
emilmont | 1:fdd22bb7aa52 | 129 | 0.985277652740478520f, 0.980785250663757320f, 0.975702106952667240f, |
emilmont | 1:fdd22bb7aa52 | 130 | 0.970031261444091800f, 0.963776051998138430f, 0.956940352916717530f, |
emilmont | 1:fdd22bb7aa52 | 131 | 0.949528157711029050f, 0.941544055938720700f, |
emilmont | 1:fdd22bb7aa52 | 132 | 0.932992815971374510f, 0.923879504203796390f, 0.914209783077239990f, |
emilmont | 1:fdd22bb7aa52 | 133 | 0.903989315032958980f, 0.893224298954010010f, 0.881921291351318360f, |
emilmont | 1:fdd22bb7aa52 | 134 | 0.870086967945098880f, 0.857728600502014160f, |
emilmont | 1:fdd22bb7aa52 | 135 | 0.844853579998016360f, 0.831469595432281490f, 0.817584812641143800f, |
emilmont | 1:fdd22bb7aa52 | 136 | 0.803207516670227050f, 0.788346409797668460f, 0.773010432720184330f, |
emilmont | 1:fdd22bb7aa52 | 137 | 0.757208824157714840f, 0.740951120853424070f, |
emilmont | 1:fdd22bb7aa52 | 138 | 0.724247097969055180f, 0.707106769084930420f, 0.689540565013885500f, |
emilmont | 1:fdd22bb7aa52 | 139 | 0.671558976173400880f, 0.653172850608825680f, 0.634393274784088130f, |
emilmont | 1:fdd22bb7aa52 | 140 | 0.615231573581695560f, 0.595699310302734380f, |
emilmont | 1:fdd22bb7aa52 | 141 | 0.575808167457580570f, 0.555570244789123540f, 0.534997642040252690f, |
emilmont | 1:fdd22bb7aa52 | 142 | 0.514102756977081300f, 0.492898195981979370f, 0.471396744251251220f, |
emilmont | 1:fdd22bb7aa52 | 143 | 0.449611335992813110f, 0.427555084228515630f, |
emilmont | 1:fdd22bb7aa52 | 144 | 0.405241310596466060f, 0.382683426141738890f, 0.359895050525665280f, |
emilmont | 1:fdd22bb7aa52 | 145 | 0.336889863014221190f, 0.313681751489639280f, 0.290284663438797000f, |
emilmont | 1:fdd22bb7aa52 | 146 | 0.266712754964828490f, 0.242980182170867920f, |
emilmont | 1:fdd22bb7aa52 | 147 | 0.219101235270500180f, 0.195090323686599730f, 0.170961886644363400f, |
emilmont | 1:fdd22bb7aa52 | 148 | 0.146730467677116390f, 0.122410677373409270f, 0.098017141222953796f, |
emilmont | 1:fdd22bb7aa52 | 149 | 0.073564566671848297f, 0.049067676067352295f, |
emilmont | 1:fdd22bb7aa52 | 150 | 0.024541229009628296f, 0.000000000000000122f, -0.024541229009628296f, |
emilmont | 1:fdd22bb7aa52 | 151 | -0.049067676067352295f, -0.073564566671848297f, -0.098017141222953796f, |
emilmont | 1:fdd22bb7aa52 | 152 | -0.122410677373409270f, -0.146730467677116390f, |
emilmont | 1:fdd22bb7aa52 | 153 | -0.170961886644363400f, -0.195090323686599730f, -0.219101235270500180f, |
emilmont | 1:fdd22bb7aa52 | 154 | -0.242980182170867920f, -0.266712754964828490f, -0.290284663438797000f, |
emilmont | 1:fdd22bb7aa52 | 155 | -0.313681751489639280f, -0.336889863014221190f, |
emilmont | 1:fdd22bb7aa52 | 156 | -0.359895050525665280f, -0.382683426141738890f, -0.405241310596466060f, |
emilmont | 1:fdd22bb7aa52 | 157 | -0.427555084228515630f, -0.449611335992813110f, -0.471396744251251220f, |
emilmont | 1:fdd22bb7aa52 | 158 | -0.492898195981979370f, -0.514102756977081300f, |
emilmont | 1:fdd22bb7aa52 | 159 | -0.534997642040252690f, -0.555570244789123540f, -0.575808167457580570f, |
emilmont | 1:fdd22bb7aa52 | 160 | -0.595699310302734380f, -0.615231573581695560f, -0.634393274784088130f, |
emilmont | 1:fdd22bb7aa52 | 161 | -0.653172850608825680f, -0.671558976173400880f, |
emilmont | 1:fdd22bb7aa52 | 162 | -0.689540565013885500f, -0.707106769084930420f, -0.724247097969055180f, |
emilmont | 1:fdd22bb7aa52 | 163 | -0.740951120853424070f, -0.757208824157714840f, -0.773010432720184330f, |
emilmont | 1:fdd22bb7aa52 | 164 | -0.788346409797668460f, -0.803207516670227050f, |
emilmont | 1:fdd22bb7aa52 | 165 | -0.817584812641143800f, -0.831469595432281490f, -0.844853579998016360f, |
emilmont | 1:fdd22bb7aa52 | 166 | -0.857728600502014160f, -0.870086967945098880f, -0.881921291351318360f, |
emilmont | 1:fdd22bb7aa52 | 167 | -0.893224298954010010f, -0.903989315032958980f, |
emilmont | 1:fdd22bb7aa52 | 168 | -0.914209783077239990f, -0.923879504203796390f, -0.932992815971374510f, |
emilmont | 1:fdd22bb7aa52 | 169 | -0.941544055938720700f, -0.949528157711029050f, -0.956940352916717530f, |
emilmont | 1:fdd22bb7aa52 | 170 | -0.963776051998138430f, -0.970031261444091800f, |
emilmont | 1:fdd22bb7aa52 | 171 | -0.975702106952667240f, -0.980785250663757320f, -0.985277652740478520f, |
emilmont | 1:fdd22bb7aa52 | 172 | -0.989176511764526370f, -0.992479562759399410f, -0.995184719562530520f, |
emilmont | 1:fdd22bb7aa52 | 173 | -0.997290432453155520f, -0.998795449733734130f, |
emilmont | 1:fdd22bb7aa52 | 174 | -0.999698817729949950f, -1.000000000000000000f, -0.999698817729949950f, |
emilmont | 1:fdd22bb7aa52 | 175 | -0.998795449733734130f, -0.997290432453155520f, -0.995184719562530520f, |
emilmont | 1:fdd22bb7aa52 | 176 | -0.992479562759399410f, -0.989176511764526370f, |
emilmont | 1:fdd22bb7aa52 | 177 | -0.985277652740478520f, -0.980785250663757320f, -0.975702106952667240f, |
emilmont | 1:fdd22bb7aa52 | 178 | -0.970031261444091800f, -0.963776051998138430f, -0.956940352916717530f, |
emilmont | 1:fdd22bb7aa52 | 179 | -0.949528157711029050f, -0.941544055938720700f, |
emilmont | 1:fdd22bb7aa52 | 180 | -0.932992815971374510f, -0.923879504203796390f, -0.914209783077239990f, |
emilmont | 1:fdd22bb7aa52 | 181 | -0.903989315032958980f, -0.893224298954010010f, -0.881921291351318360f, |
emilmont | 1:fdd22bb7aa52 | 182 | -0.870086967945098880f, -0.857728600502014160f, |
emilmont | 1:fdd22bb7aa52 | 183 | -0.844853579998016360f, -0.831469595432281490f, -0.817584812641143800f, |
emilmont | 1:fdd22bb7aa52 | 184 | -0.803207516670227050f, -0.788346409797668460f, -0.773010432720184330f, |
emilmont | 1:fdd22bb7aa52 | 185 | -0.757208824157714840f, -0.740951120853424070f, |
emilmont | 1:fdd22bb7aa52 | 186 | -0.724247097969055180f, -0.707106769084930420f, -0.689540565013885500f, |
emilmont | 1:fdd22bb7aa52 | 187 | -0.671558976173400880f, -0.653172850608825680f, -0.634393274784088130f, |
emilmont | 1:fdd22bb7aa52 | 188 | -0.615231573581695560f, -0.595699310302734380f, |
emilmont | 1:fdd22bb7aa52 | 189 | -0.575808167457580570f, -0.555570244789123540f, -0.534997642040252690f, |
emilmont | 1:fdd22bb7aa52 | 190 | -0.514102756977081300f, -0.492898195981979370f, -0.471396744251251220f, |
emilmont | 1:fdd22bb7aa52 | 191 | -0.449611335992813110f, -0.427555084228515630f, |
emilmont | 1:fdd22bb7aa52 | 192 | -0.405241310596466060f, -0.382683426141738890f, -0.359895050525665280f, |
emilmont | 1:fdd22bb7aa52 | 193 | -0.336889863014221190f, -0.313681751489639280f, -0.290284663438797000f, |
emilmont | 1:fdd22bb7aa52 | 194 | -0.266712754964828490f, -0.242980182170867920f, |
emilmont | 1:fdd22bb7aa52 | 195 | -0.219101235270500180f, -0.195090323686599730f, -0.170961886644363400f, |
emilmont | 1:fdd22bb7aa52 | 196 | -0.146730467677116390f, -0.122410677373409270f, -0.098017141222953796f, |
emilmont | 1:fdd22bb7aa52 | 197 | -0.073564566671848297f, -0.049067676067352295f, |
emilmont | 1:fdd22bb7aa52 | 198 | -0.024541229009628296f, -0.000000000000000245f, 0.024541229009628296f |
emilmont | 1:fdd22bb7aa52 | 199 | }; |
emilmont | 1:fdd22bb7aa52 | 200 | |
emilmont | 1:fdd22bb7aa52 | 201 | |
emilmont | 1:fdd22bb7aa52 | 202 | /** |
emilmont | 1:fdd22bb7aa52 | 203 | * @brief Fast approximation to the trigonometric sine function for floating-point data. |
emilmont | 1:fdd22bb7aa52 | 204 | * @param[in] x input value in radians. |
emilmont | 1:fdd22bb7aa52 | 205 | * @return sin(x). |
emilmont | 1:fdd22bb7aa52 | 206 | */ |
emilmont | 1:fdd22bb7aa52 | 207 | |
emilmont | 1:fdd22bb7aa52 | 208 | float32_t arm_sin_f32( |
emilmont | 1:fdd22bb7aa52 | 209 | float32_t x) |
emilmont | 1:fdd22bb7aa52 | 210 | { |
emilmont | 1:fdd22bb7aa52 | 211 | float32_t sinVal, fract, in; /* Temporary variables for input, output */ |
emilmont | 1:fdd22bb7aa52 | 212 | int32_t index; /* Index variable */ |
emilmont | 1:fdd22bb7aa52 | 213 | uint32_t tableSize = (uint32_t) TABLE_SIZE; /* Initialise tablesize */ |
emilmont | 1:fdd22bb7aa52 | 214 | float32_t wa, wb, wc, wd; /* Cubic interpolation coefficients */ |
emilmont | 1:fdd22bb7aa52 | 215 | float32_t a, b, c, d; /* Four nearest output values */ |
emilmont | 1:fdd22bb7aa52 | 216 | float32_t *tablePtr; /* Pointer to table */ |
emilmont | 1:fdd22bb7aa52 | 217 | int32_t n; |
emilmont | 1:fdd22bb7aa52 | 218 | float32_t fractsq, fractby2, fractby6, fractby3, fractsqby2; |
emilmont | 1:fdd22bb7aa52 | 219 | float32_t oneminusfractby2; |
emilmont | 1:fdd22bb7aa52 | 220 | float32_t frby2xfrsq, frby6xfrsq; |
emilmont | 1:fdd22bb7aa52 | 221 | |
emilmont | 1:fdd22bb7aa52 | 222 | /* input x is in radians */ |
emilmont | 1:fdd22bb7aa52 | 223 | /* Scale the input to [0 1] range from [0 2*PI] , divide input by 2*pi */ |
emilmont | 1:fdd22bb7aa52 | 224 | in = x * 0.159154943092f; |
emilmont | 1:fdd22bb7aa52 | 225 | |
emilmont | 1:fdd22bb7aa52 | 226 | /* Calculation of floor value of input */ |
emilmont | 1:fdd22bb7aa52 | 227 | n = (int32_t) in; |
emilmont | 1:fdd22bb7aa52 | 228 | |
emilmont | 1:fdd22bb7aa52 | 229 | /* Make negative values towards -infinity */ |
emilmont | 1:fdd22bb7aa52 | 230 | if(x < 0.0f) |
emilmont | 1:fdd22bb7aa52 | 231 | { |
emilmont | 1:fdd22bb7aa52 | 232 | n = n - 1; |
emilmont | 1:fdd22bb7aa52 | 233 | } |
emilmont | 1:fdd22bb7aa52 | 234 | |
emilmont | 1:fdd22bb7aa52 | 235 | /* Map input value to [0 1] */ |
emilmont | 1:fdd22bb7aa52 | 236 | in = in - (float32_t) n; |
emilmont | 1:fdd22bb7aa52 | 237 | |
emilmont | 1:fdd22bb7aa52 | 238 | /* Calculation of index of the table */ |
emilmont | 1:fdd22bb7aa52 | 239 | index = (uint32_t) (tableSize * in); |
emilmont | 1:fdd22bb7aa52 | 240 | |
emilmont | 1:fdd22bb7aa52 | 241 | /* fractional value calculation */ |
emilmont | 1:fdd22bb7aa52 | 242 | fract = ((float32_t) tableSize * in) - (float32_t) index; |
emilmont | 1:fdd22bb7aa52 | 243 | |
emilmont | 1:fdd22bb7aa52 | 244 | /* Checking min and max index of table */ |
emilmont | 1:fdd22bb7aa52 | 245 | if(index < 0) |
emilmont | 1:fdd22bb7aa52 | 246 | { |
emilmont | 1:fdd22bb7aa52 | 247 | index = 0; |
emilmont | 1:fdd22bb7aa52 | 248 | } |
emilmont | 1:fdd22bb7aa52 | 249 | else if(index > 256) |
emilmont | 1:fdd22bb7aa52 | 250 | { |
emilmont | 1:fdd22bb7aa52 | 251 | index = 256; |
emilmont | 1:fdd22bb7aa52 | 252 | } |
emilmont | 1:fdd22bb7aa52 | 253 | |
emilmont | 1:fdd22bb7aa52 | 254 | /* Initialise table pointer */ |
emilmont | 1:fdd22bb7aa52 | 255 | tablePtr = (float32_t *) & sinTable[index]; |
emilmont | 1:fdd22bb7aa52 | 256 | |
emilmont | 1:fdd22bb7aa52 | 257 | /* Read four nearest values of input value from the sin table */ |
emilmont | 1:fdd22bb7aa52 | 258 | a = tablePtr[0]; |
emilmont | 1:fdd22bb7aa52 | 259 | b = tablePtr[1]; |
emilmont | 1:fdd22bb7aa52 | 260 | c = tablePtr[2]; |
emilmont | 1:fdd22bb7aa52 | 261 | d = tablePtr[3]; |
emilmont | 1:fdd22bb7aa52 | 262 | |
emilmont | 1:fdd22bb7aa52 | 263 | /* Cubic interpolation process */ |
emilmont | 1:fdd22bb7aa52 | 264 | fractsq = fract * fract; |
emilmont | 1:fdd22bb7aa52 | 265 | fractby2 = fract * 0.5f; |
emilmont | 1:fdd22bb7aa52 | 266 | fractby6 = fract * 0.166666667f; |
emilmont | 1:fdd22bb7aa52 | 267 | fractby3 = fract * 0.3333333333333f; |
emilmont | 1:fdd22bb7aa52 | 268 | fractsqby2 = fractsq * 0.5f; |
emilmont | 1:fdd22bb7aa52 | 269 | frby2xfrsq = (fractby2) * fractsq; |
emilmont | 1:fdd22bb7aa52 | 270 | frby6xfrsq = (fractby6) * fractsq; |
emilmont | 1:fdd22bb7aa52 | 271 | oneminusfractby2 = 1.0f - fractby2; |
emilmont | 1:fdd22bb7aa52 | 272 | wb = fractsqby2 - fractby3; |
emilmont | 1:fdd22bb7aa52 | 273 | wc = (fractsqby2 + fract); |
emilmont | 1:fdd22bb7aa52 | 274 | wa = wb - frby6xfrsq; |
emilmont | 1:fdd22bb7aa52 | 275 | wb = frby2xfrsq - fractsq; |
emilmont | 1:fdd22bb7aa52 | 276 | sinVal = wa * a; |
emilmont | 1:fdd22bb7aa52 | 277 | wc = wc - frby2xfrsq; |
emilmont | 1:fdd22bb7aa52 | 278 | wd = (frby6xfrsq) - fractby6; |
emilmont | 1:fdd22bb7aa52 | 279 | wb = wb + oneminusfractby2; |
emilmont | 1:fdd22bb7aa52 | 280 | |
emilmont | 1:fdd22bb7aa52 | 281 | /* Calculate sin value */ |
emilmont | 1:fdd22bb7aa52 | 282 | sinVal = (sinVal + (b * wb)) + ((c * wc) + (d * wd)); |
emilmont | 1:fdd22bb7aa52 | 283 | |
emilmont | 1:fdd22bb7aa52 | 284 | /* Return the output value */ |
emilmont | 1:fdd22bb7aa52 | 285 | return (sinVal); |
emilmont | 1:fdd22bb7aa52 | 286 | |
emilmont | 1:fdd22bb7aa52 | 287 | } |
emilmont | 1:fdd22bb7aa52 | 288 | |
emilmont | 1:fdd22bb7aa52 | 289 | /** |
emilmont | 1:fdd22bb7aa52 | 290 | * @} end of sin group |
emilmont | 1:fdd22bb7aa52 | 291 | */ |