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