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