The CMSIS DSP 5 library
Dependents: Nucleo-Heart-Rate ejercicioVrms2 PROYECTOFINAL ejercicioVrms ... more
functions/ControllerFunctions/arm_sin_cos_f32.c@1:24714b45cd1b, 2018-06-20 (annotated)
- Committer:
- xorjoep
- Date:
- Wed Jun 20 11:21:31 2018 +0000
- Revision:
- 1:24714b45cd1b
The newest version of the CMSIS library
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
xorjoep | 1:24714b45cd1b | 1 | /* ---------------------------------------------------------------------- |
xorjoep | 1:24714b45cd1b | 2 | * Project: CMSIS DSP Library |
xorjoep | 1:24714b45cd1b | 3 | * Title: arm_sin_cos_f32.c |
xorjoep | 1:24714b45cd1b | 4 | * Description: Sine and Cosine calculation for floating-point values |
xorjoep | 1:24714b45cd1b | 5 | * |
xorjoep | 1:24714b45cd1b | 6 | * $Date: 27. January 2017 |
xorjoep | 1:24714b45cd1b | 7 | * $Revision: V.1.5.1 |
xorjoep | 1:24714b45cd1b | 8 | * |
xorjoep | 1:24714b45cd1b | 9 | * Target Processor: Cortex-M cores |
xorjoep | 1:24714b45cd1b | 10 | * -------------------------------------------------------------------- */ |
xorjoep | 1:24714b45cd1b | 11 | /* |
xorjoep | 1:24714b45cd1b | 12 | * Copyright (C) 2010-2017 ARM Limited or its affiliates. All rights reserved. |
xorjoep | 1:24714b45cd1b | 13 | * |
xorjoep | 1:24714b45cd1b | 14 | * SPDX-License-Identifier: Apache-2.0 |
xorjoep | 1:24714b45cd1b | 15 | * |
xorjoep | 1:24714b45cd1b | 16 | * Licensed under the Apache License, Version 2.0 (the License); you may |
xorjoep | 1:24714b45cd1b | 17 | * not use this file except in compliance with the License. |
xorjoep | 1:24714b45cd1b | 18 | * You may obtain a copy of the License at |
xorjoep | 1:24714b45cd1b | 19 | * |
xorjoep | 1:24714b45cd1b | 20 | * www.apache.org/licenses/LICENSE-2.0 |
xorjoep | 1:24714b45cd1b | 21 | * |
xorjoep | 1:24714b45cd1b | 22 | * Unless required by applicable law or agreed to in writing, software |
xorjoep | 1:24714b45cd1b | 23 | * distributed under the License is distributed on an AS IS BASIS, WITHOUT |
xorjoep | 1:24714b45cd1b | 24 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
xorjoep | 1:24714b45cd1b | 25 | * See the License for the specific language governing permissions and |
xorjoep | 1:24714b45cd1b | 26 | * limitations under the License. |
xorjoep | 1:24714b45cd1b | 27 | */ |
xorjoep | 1:24714b45cd1b | 28 | |
xorjoep | 1:24714b45cd1b | 29 | #include "arm_math.h" |
xorjoep | 1:24714b45cd1b | 30 | #include "arm_common_tables.h" |
xorjoep | 1:24714b45cd1b | 31 | |
xorjoep | 1:24714b45cd1b | 32 | /** |
xorjoep | 1:24714b45cd1b | 33 | * @ingroup groupController |
xorjoep | 1:24714b45cd1b | 34 | */ |
xorjoep | 1:24714b45cd1b | 35 | |
xorjoep | 1:24714b45cd1b | 36 | /** |
xorjoep | 1:24714b45cd1b | 37 | * @defgroup SinCos Sine Cosine |
xorjoep | 1:24714b45cd1b | 38 | * |
xorjoep | 1:24714b45cd1b | 39 | * Computes the trigonometric sine and cosine values using a combination of table lookup |
xorjoep | 1:24714b45cd1b | 40 | * and linear interpolation. |
xorjoep | 1:24714b45cd1b | 41 | * There are separate functions for Q31 and floating-point data types. |
xorjoep | 1:24714b45cd1b | 42 | * The input to the floating-point version is in degrees while the |
xorjoep | 1:24714b45cd1b | 43 | * fixed-point Q31 have a scaled input with the range |
xorjoep | 1:24714b45cd1b | 44 | * [-1 0.9999] mapping to [-180 +180] degrees. |
xorjoep | 1:24714b45cd1b | 45 | * |
xorjoep | 1:24714b45cd1b | 46 | * The floating point function also allows values that are out of the usual range. When this happens, the function will |
xorjoep | 1:24714b45cd1b | 47 | * take extra time to adjust the input value to the range of [-180 180]. |
xorjoep | 1:24714b45cd1b | 48 | * |
xorjoep | 1:24714b45cd1b | 49 | * The result is accurate to 5 digits after the decimal point. |
xorjoep | 1:24714b45cd1b | 50 | * |
xorjoep | 1:24714b45cd1b | 51 | * The implementation is based on table lookup using 360 values together with linear interpolation. |
xorjoep | 1:24714b45cd1b | 52 | * The steps used are: |
xorjoep | 1:24714b45cd1b | 53 | * -# Calculation of the nearest integer table index. |
xorjoep | 1:24714b45cd1b | 54 | * -# Compute the fractional portion (fract) of the input. |
xorjoep | 1:24714b45cd1b | 55 | * -# Fetch the value corresponding to \c index from sine table to \c y0 and also value from \c index+1 to \c y1. |
xorjoep | 1:24714b45cd1b | 56 | * -# Sine value is computed as <code> *psinVal = y0 + (fract * (y1 - y0))</code>. |
xorjoep | 1:24714b45cd1b | 57 | * -# Fetch the value corresponding to \c index from cosine table to \c y0 and also value from \c index+1 to \c y1. |
xorjoep | 1:24714b45cd1b | 58 | * -# Cosine value is computed as <code> *pcosVal = y0 + (fract * (y1 - y0))</code>. |
xorjoep | 1:24714b45cd1b | 59 | */ |
xorjoep | 1:24714b45cd1b | 60 | |
xorjoep | 1:24714b45cd1b | 61 | /** |
xorjoep | 1:24714b45cd1b | 62 | * @addtogroup SinCos |
xorjoep | 1:24714b45cd1b | 63 | * @{ |
xorjoep | 1:24714b45cd1b | 64 | */ |
xorjoep | 1:24714b45cd1b | 65 | |
xorjoep | 1:24714b45cd1b | 66 | /** |
xorjoep | 1:24714b45cd1b | 67 | * @brief Floating-point sin_cos function. |
xorjoep | 1:24714b45cd1b | 68 | * @param[in] theta input value in degrees |
xorjoep | 1:24714b45cd1b | 69 | * @param[out] *pSinVal points to the processed sine output. |
xorjoep | 1:24714b45cd1b | 70 | * @param[out] *pCosVal points to the processed cos output. |
xorjoep | 1:24714b45cd1b | 71 | * @return none. |
xorjoep | 1:24714b45cd1b | 72 | */ |
xorjoep | 1:24714b45cd1b | 73 | |
xorjoep | 1:24714b45cd1b | 74 | void arm_sin_cos_f32( |
xorjoep | 1:24714b45cd1b | 75 | float32_t theta, |
xorjoep | 1:24714b45cd1b | 76 | float32_t * pSinVal, |
xorjoep | 1:24714b45cd1b | 77 | float32_t * pCosVal) |
xorjoep | 1:24714b45cd1b | 78 | { |
xorjoep | 1:24714b45cd1b | 79 | float32_t fract, in; /* Temporary variables for input, output */ |
xorjoep | 1:24714b45cd1b | 80 | uint16_t indexS, indexC; /* Index variable */ |
xorjoep | 1:24714b45cd1b | 81 | float32_t f1, f2, d1, d2; /* Two nearest output values */ |
xorjoep | 1:24714b45cd1b | 82 | float32_t findex, Dn, Df, temp; |
xorjoep | 1:24714b45cd1b | 83 | |
xorjoep | 1:24714b45cd1b | 84 | /* input x is in degrees */ |
xorjoep | 1:24714b45cd1b | 85 | /* Scale the input, divide input by 360, for cosine add 0.25 (pi/2) to read sine table */ |
xorjoep | 1:24714b45cd1b | 86 | in = theta * 0.00277777777778f; |
xorjoep | 1:24714b45cd1b | 87 | |
xorjoep | 1:24714b45cd1b | 88 | if (in < 0.0f) |
xorjoep | 1:24714b45cd1b | 89 | { |
xorjoep | 1:24714b45cd1b | 90 | in = -in; |
xorjoep | 1:24714b45cd1b | 91 | } |
xorjoep | 1:24714b45cd1b | 92 | |
xorjoep | 1:24714b45cd1b | 93 | in = in - (int32_t)in; |
xorjoep | 1:24714b45cd1b | 94 | |
xorjoep | 1:24714b45cd1b | 95 | /* Calculation of index of the table */ |
xorjoep | 1:24714b45cd1b | 96 | findex = (float32_t) FAST_MATH_TABLE_SIZE * in; |
xorjoep | 1:24714b45cd1b | 97 | indexS = ((uint16_t)findex) & 0x1ff; |
xorjoep | 1:24714b45cd1b | 98 | indexC = (indexS + (FAST_MATH_TABLE_SIZE / 4)) & 0x1ff; |
xorjoep | 1:24714b45cd1b | 99 | |
xorjoep | 1:24714b45cd1b | 100 | /* fractional value calculation */ |
xorjoep | 1:24714b45cd1b | 101 | fract = findex - (float32_t) indexS; |
xorjoep | 1:24714b45cd1b | 102 | |
xorjoep | 1:24714b45cd1b | 103 | /* Read two nearest values of input value from the cos & sin tables */ |
xorjoep | 1:24714b45cd1b | 104 | f1 = sinTable_f32[indexC+0]; |
xorjoep | 1:24714b45cd1b | 105 | f2 = sinTable_f32[indexC+1]; |
xorjoep | 1:24714b45cd1b | 106 | d1 = -sinTable_f32[indexS+0]; |
xorjoep | 1:24714b45cd1b | 107 | d2 = -sinTable_f32[indexS+1]; |
xorjoep | 1:24714b45cd1b | 108 | |
xorjoep | 1:24714b45cd1b | 109 | temp = (1.0f - fract) * f1 + fract * f2; |
xorjoep | 1:24714b45cd1b | 110 | |
xorjoep | 1:24714b45cd1b | 111 | Dn = 0.0122718463030f; // delta between the two points (fixed), in this case 2*pi/FAST_MATH_TABLE_SIZE |
xorjoep | 1:24714b45cd1b | 112 | Df = f2 - f1; // delta between the values of the functions |
xorjoep | 1:24714b45cd1b | 113 | |
xorjoep | 1:24714b45cd1b | 114 | temp = Dn *(d1 + d2) - 2 * Df; |
xorjoep | 1:24714b45cd1b | 115 | temp = fract * temp + (3 * Df - (d2 + 2 * d1) * Dn); |
xorjoep | 1:24714b45cd1b | 116 | temp = fract * temp + d1 * Dn; |
xorjoep | 1:24714b45cd1b | 117 | |
xorjoep | 1:24714b45cd1b | 118 | /* Calculation of cosine value */ |
xorjoep | 1:24714b45cd1b | 119 | *pCosVal = fract * temp + f1; |
xorjoep | 1:24714b45cd1b | 120 | |
xorjoep | 1:24714b45cd1b | 121 | /* Read two nearest values of input value from the cos & sin tables */ |
xorjoep | 1:24714b45cd1b | 122 | f1 = sinTable_f32[indexS+0]; |
xorjoep | 1:24714b45cd1b | 123 | f2 = sinTable_f32[indexS+1]; |
xorjoep | 1:24714b45cd1b | 124 | d1 = sinTable_f32[indexC+0]; |
xorjoep | 1:24714b45cd1b | 125 | d2 = sinTable_f32[indexC+1]; |
xorjoep | 1:24714b45cd1b | 126 | |
xorjoep | 1:24714b45cd1b | 127 | temp = (1.0f - fract) * f1 + fract * f2; |
xorjoep | 1:24714b45cd1b | 128 | |
xorjoep | 1:24714b45cd1b | 129 | Df = f2 - f1; // delta between the values of the functions |
xorjoep | 1:24714b45cd1b | 130 | temp = Dn*(d1 + d2) - 2*Df; |
xorjoep | 1:24714b45cd1b | 131 | temp = fract*temp + (3*Df - (d2 + 2*d1)*Dn); |
xorjoep | 1:24714b45cd1b | 132 | temp = fract*temp + d1*Dn; |
xorjoep | 1:24714b45cd1b | 133 | |
xorjoep | 1:24714b45cd1b | 134 | /* Calculation of sine value */ |
xorjoep | 1:24714b45cd1b | 135 | *pSinVal = fract*temp + f1; |
xorjoep | 1:24714b45cd1b | 136 | |
xorjoep | 1:24714b45cd1b | 137 | if (theta < 0.0f) |
xorjoep | 1:24714b45cd1b | 138 | { |
xorjoep | 1:24714b45cd1b | 139 | *pSinVal = -*pSinVal; |
xorjoep | 1:24714b45cd1b | 140 | } |
xorjoep | 1:24714b45cd1b | 141 | } |
xorjoep | 1:24714b45cd1b | 142 | /** |
xorjoep | 1:24714b45cd1b | 143 | * @} end of SinCos group |
xorjoep | 1:24714b45cd1b | 144 | */ |