The CMSIS DSP 5 library
Dependents: Nucleo-Heart-Rate ejercicioVrms2 PROYECTOFINAL ejercicioVrms ... more
functions/ControllerFunctions/arm_sin_cos_q31.c@3:4098b9d3d571, 2018-06-21 (annotated)
- Committer:
- xorjoep
- Date:
- Thu Jun 21 11:56:27 2018 +0000
- Revision:
- 3:4098b9d3d571
- Parent:
- 1:24714b45cd1b
headers is a folder not a 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_q31.c |
xorjoep | 1:24714b45cd1b | 4 | * Description: Cosine & Sine calculation for Q31 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 | * @addtogroup SinCos |
xorjoep | 1:24714b45cd1b | 38 | * @{ |
xorjoep | 1:24714b45cd1b | 39 | */ |
xorjoep | 1:24714b45cd1b | 40 | |
xorjoep | 1:24714b45cd1b | 41 | /** |
xorjoep | 1:24714b45cd1b | 42 | * @brief Q31 sin_cos function. |
xorjoep | 1:24714b45cd1b | 43 | * @param[in] theta scaled input value in degrees |
xorjoep | 1:24714b45cd1b | 44 | * @param[out] *pSinVal points to the processed sine output. |
xorjoep | 1:24714b45cd1b | 45 | * @param[out] *pCosVal points to the processed cosine output. |
xorjoep | 1:24714b45cd1b | 46 | * @return none. |
xorjoep | 1:24714b45cd1b | 47 | * |
xorjoep | 1:24714b45cd1b | 48 | * The Q31 input value is in the range [-1 0.999999] and is mapped to a degree value in the range [-180 179]. |
xorjoep | 1:24714b45cd1b | 49 | * |
xorjoep | 1:24714b45cd1b | 50 | */ |
xorjoep | 1:24714b45cd1b | 51 | |
xorjoep | 1:24714b45cd1b | 52 | void arm_sin_cos_q31( |
xorjoep | 1:24714b45cd1b | 53 | q31_t theta, |
xorjoep | 1:24714b45cd1b | 54 | q31_t * pSinVal, |
xorjoep | 1:24714b45cd1b | 55 | q31_t * pCosVal) |
xorjoep | 1:24714b45cd1b | 56 | { |
xorjoep | 1:24714b45cd1b | 57 | q31_t fract; /* Temporary variables for input, output */ |
xorjoep | 1:24714b45cd1b | 58 | uint16_t indexS, indexC; /* Index variable */ |
xorjoep | 1:24714b45cd1b | 59 | q31_t f1, f2, d1, d2; /* Two nearest output values */ |
xorjoep | 1:24714b45cd1b | 60 | q31_t Dn, Df; |
xorjoep | 1:24714b45cd1b | 61 | q63_t temp; |
xorjoep | 1:24714b45cd1b | 62 | |
xorjoep | 1:24714b45cd1b | 63 | /* Calculate the nearest index */ |
xorjoep | 1:24714b45cd1b | 64 | indexS = (uint32_t)theta >> CONTROLLER_Q31_SHIFT; |
xorjoep | 1:24714b45cd1b | 65 | indexC = (indexS + 128) & 0x1ff; |
xorjoep | 1:24714b45cd1b | 66 | |
xorjoep | 1:24714b45cd1b | 67 | /* Calculation of fractional value */ |
xorjoep | 1:24714b45cd1b | 68 | fract = (theta - (indexS << CONTROLLER_Q31_SHIFT)) << 8; |
xorjoep | 1:24714b45cd1b | 69 | |
xorjoep | 1:24714b45cd1b | 70 | /* Read two nearest values of input value from the cos & sin tables */ |
xorjoep | 1:24714b45cd1b | 71 | f1 = sinTable_q31[indexC+0]; |
xorjoep | 1:24714b45cd1b | 72 | f2 = sinTable_q31[indexC+1]; |
xorjoep | 1:24714b45cd1b | 73 | d1 = -sinTable_q31[indexS+0]; |
xorjoep | 1:24714b45cd1b | 74 | d2 = -sinTable_q31[indexS+1]; |
xorjoep | 1:24714b45cd1b | 75 | |
xorjoep | 1:24714b45cd1b | 76 | Dn = 0x1921FB5; // delta between the two points (fixed), in this case 2*pi/FAST_MATH_TABLE_SIZE |
xorjoep | 1:24714b45cd1b | 77 | Df = f2 - f1; // delta between the values of the functions |
xorjoep | 1:24714b45cd1b | 78 | temp = Dn*((q63_t)d1 + d2); |
xorjoep | 1:24714b45cd1b | 79 | temp = temp - ((q63_t)Df << 32); |
xorjoep | 1:24714b45cd1b | 80 | temp = (q63_t)fract*(temp >> 31); |
xorjoep | 1:24714b45cd1b | 81 | temp = temp + ((3*(q63_t)Df << 31) - (d2 + ((q63_t)d1 << 1))*Dn); |
xorjoep | 1:24714b45cd1b | 82 | temp = (q63_t)fract*(temp >> 31); |
xorjoep | 1:24714b45cd1b | 83 | temp = temp + (q63_t)d1*Dn; |
xorjoep | 1:24714b45cd1b | 84 | temp = (q63_t)fract*(temp >> 31); |
xorjoep | 1:24714b45cd1b | 85 | |
xorjoep | 1:24714b45cd1b | 86 | /* Calculation of cosine value */ |
xorjoep | 1:24714b45cd1b | 87 | *pCosVal = clip_q63_to_q31((temp >> 31) + (q63_t)f1); |
xorjoep | 1:24714b45cd1b | 88 | |
xorjoep | 1:24714b45cd1b | 89 | /* Read two nearest values of input value from the cos & sin tables */ |
xorjoep | 1:24714b45cd1b | 90 | f1 = sinTable_q31[indexS+0]; |
xorjoep | 1:24714b45cd1b | 91 | f2 = sinTable_q31[indexS+1]; |
xorjoep | 1:24714b45cd1b | 92 | d1 = sinTable_q31[indexC+0]; |
xorjoep | 1:24714b45cd1b | 93 | d2 = sinTable_q31[indexC+1]; |
xorjoep | 1:24714b45cd1b | 94 | |
xorjoep | 1:24714b45cd1b | 95 | Df = f2 - f1; // delta between the values of the functions |
xorjoep | 1:24714b45cd1b | 96 | temp = Dn*((q63_t)d1 + d2); |
xorjoep | 1:24714b45cd1b | 97 | temp = temp - ((q63_t)Df << 32); |
xorjoep | 1:24714b45cd1b | 98 | temp = (q63_t)fract*(temp >> 31); |
xorjoep | 1:24714b45cd1b | 99 | temp = temp + ((3*(q63_t)Df << 31) - (d2 + ((q63_t)d1 << 1))*Dn); |
xorjoep | 1:24714b45cd1b | 100 | temp = (q63_t)fract*(temp >> 31); |
xorjoep | 1:24714b45cd1b | 101 | temp = temp + (q63_t)d1*Dn; |
xorjoep | 1:24714b45cd1b | 102 | temp = (q63_t)fract*(temp >> 31); |
xorjoep | 1:24714b45cd1b | 103 | |
xorjoep | 1:24714b45cd1b | 104 | /* Calculation of sine value */ |
xorjoep | 1:24714b45cd1b | 105 | *pSinVal = clip_q63_to_q31((temp >> 31) + (q63_t)f1); |
xorjoep | 1:24714b45cd1b | 106 | } |
xorjoep | 1:24714b45cd1b | 107 | |
xorjoep | 1:24714b45cd1b | 108 | /** |
xorjoep | 1:24714b45cd1b | 109 | * @} end of SinCos group |
xorjoep | 1:24714b45cd1b | 110 | */ |