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