Aded CMSIS5 DSP and NN folder. Needs some work
DSP/source/FastMathFunctions/arm_cos_q15.c@0:eedb7d567a5d, 2018-04-12 (annotated)
- Committer:
- robert_lp
- Date:
- Thu Apr 12 01:31:58 2018 +0000
- Revision:
- 0:eedb7d567a5d
CMSIS5 Library
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
robert_lp | 0:eedb7d567a5d | 1 | /* ---------------------------------------------------------------------- |
robert_lp | 0:eedb7d567a5d | 2 | * Project: CMSIS DSP Library |
robert_lp | 0:eedb7d567a5d | 3 | * Title: arm_cos_q15.c |
robert_lp | 0:eedb7d567a5d | 4 | * Description: Fast cosine calculation for Q15 values |
robert_lp | 0:eedb7d567a5d | 5 | * |
robert_lp | 0:eedb7d567a5d | 6 | * $Date: 27. January 2017 |
robert_lp | 0:eedb7d567a5d | 7 | * $Revision: V.1.5.1 |
robert_lp | 0:eedb7d567a5d | 8 | * |
robert_lp | 0:eedb7d567a5d | 9 | * Target Processor: Cortex-M cores |
robert_lp | 0:eedb7d567a5d | 10 | * -------------------------------------------------------------------- */ |
robert_lp | 0:eedb7d567a5d | 11 | /* |
robert_lp | 0:eedb7d567a5d | 12 | * Copyright (C) 2010-2017 ARM Limited or its affiliates. All rights reserved. |
robert_lp | 0:eedb7d567a5d | 13 | * |
robert_lp | 0:eedb7d567a5d | 14 | * SPDX-License-Identifier: Apache-2.0 |
robert_lp | 0:eedb7d567a5d | 15 | * |
robert_lp | 0:eedb7d567a5d | 16 | * Licensed under the Apache License, Version 2.0 (the License); you may |
robert_lp | 0:eedb7d567a5d | 17 | * not use this file except in compliance with the License. |
robert_lp | 0:eedb7d567a5d | 18 | * You may obtain a copy of the License at |
robert_lp | 0:eedb7d567a5d | 19 | * |
robert_lp | 0:eedb7d567a5d | 20 | * www.apache.org/licenses/LICENSE-2.0 |
robert_lp | 0:eedb7d567a5d | 21 | * |
robert_lp | 0:eedb7d567a5d | 22 | * Unless required by applicable law or agreed to in writing, software |
robert_lp | 0:eedb7d567a5d | 23 | * distributed under the License is distributed on an AS IS BASIS, WITHOUT |
robert_lp | 0:eedb7d567a5d | 24 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
robert_lp | 0:eedb7d567a5d | 25 | * See the License for the specific language governing permissions and |
robert_lp | 0:eedb7d567a5d | 26 | * limitations under the License. |
robert_lp | 0:eedb7d567a5d | 27 | */ |
robert_lp | 0:eedb7d567a5d | 28 | |
robert_lp | 0:eedb7d567a5d | 29 | #include "arm_math.h" |
robert_lp | 0:eedb7d567a5d | 30 | #include "arm_common_tables.h" |
robert_lp | 0:eedb7d567a5d | 31 | |
robert_lp | 0:eedb7d567a5d | 32 | /** |
robert_lp | 0:eedb7d567a5d | 33 | * @ingroup groupFastMath |
robert_lp | 0:eedb7d567a5d | 34 | */ |
robert_lp | 0:eedb7d567a5d | 35 | |
robert_lp | 0:eedb7d567a5d | 36 | /** |
robert_lp | 0:eedb7d567a5d | 37 | * @addtogroup cos |
robert_lp | 0:eedb7d567a5d | 38 | * @{ |
robert_lp | 0:eedb7d567a5d | 39 | */ |
robert_lp | 0:eedb7d567a5d | 40 | |
robert_lp | 0:eedb7d567a5d | 41 | /** |
robert_lp | 0:eedb7d567a5d | 42 | * @brief Fast approximation to the trigonometric cosine function for Q15 data. |
robert_lp | 0:eedb7d567a5d | 43 | * @param[in] x Scaled input value in radians. |
robert_lp | 0:eedb7d567a5d | 44 | * @return cos(x). |
robert_lp | 0:eedb7d567a5d | 45 | * |
robert_lp | 0:eedb7d567a5d | 46 | * The Q15 input value is in the range [0 +0.9999] and is mapped to a radian |
robert_lp | 0:eedb7d567a5d | 47 | * value in the range [0 2*pi). |
robert_lp | 0:eedb7d567a5d | 48 | */ |
robert_lp | 0:eedb7d567a5d | 49 | |
robert_lp | 0:eedb7d567a5d | 50 | q15_t arm_cos_q15( |
robert_lp | 0:eedb7d567a5d | 51 | q15_t x) |
robert_lp | 0:eedb7d567a5d | 52 | { |
robert_lp | 0:eedb7d567a5d | 53 | q15_t cosVal; /* Temporary variables for input, output */ |
robert_lp | 0:eedb7d567a5d | 54 | int32_t index; /* Index variables */ |
robert_lp | 0:eedb7d567a5d | 55 | q15_t a, b; /* Four nearest output values */ |
robert_lp | 0:eedb7d567a5d | 56 | q15_t fract; /* Temporary values for fractional values */ |
robert_lp | 0:eedb7d567a5d | 57 | |
robert_lp | 0:eedb7d567a5d | 58 | /* add 0.25 (pi/2) to read sine table */ |
robert_lp | 0:eedb7d567a5d | 59 | x = (uint16_t)x + 0x2000; |
robert_lp | 0:eedb7d567a5d | 60 | if (x < 0) |
robert_lp | 0:eedb7d567a5d | 61 | { /* convert negative numbers to corresponding positive ones */ |
robert_lp | 0:eedb7d567a5d | 62 | x = (uint16_t)x + 0x8000; |
robert_lp | 0:eedb7d567a5d | 63 | } |
robert_lp | 0:eedb7d567a5d | 64 | |
robert_lp | 0:eedb7d567a5d | 65 | /* Calculate the nearest index */ |
robert_lp | 0:eedb7d567a5d | 66 | index = (uint32_t)x >> FAST_MATH_Q15_SHIFT; |
robert_lp | 0:eedb7d567a5d | 67 | |
robert_lp | 0:eedb7d567a5d | 68 | /* Calculation of fractional value */ |
robert_lp | 0:eedb7d567a5d | 69 | fract = (x - (index << FAST_MATH_Q15_SHIFT)) << 9; |
robert_lp | 0:eedb7d567a5d | 70 | |
robert_lp | 0:eedb7d567a5d | 71 | /* Read two nearest values of input value from the sin table */ |
robert_lp | 0:eedb7d567a5d | 72 | a = sinTable_q15[index]; |
robert_lp | 0:eedb7d567a5d | 73 | b = sinTable_q15[index+1]; |
robert_lp | 0:eedb7d567a5d | 74 | |
robert_lp | 0:eedb7d567a5d | 75 | /* Linear interpolation process */ |
robert_lp | 0:eedb7d567a5d | 76 | cosVal = (q31_t)(0x8000-fract)*a >> 16; |
robert_lp | 0:eedb7d567a5d | 77 | cosVal = (q15_t)((((q31_t)cosVal << 16) + ((q31_t)fract*b)) >> 16); |
robert_lp | 0:eedb7d567a5d | 78 | |
robert_lp | 0:eedb7d567a5d | 79 | return cosVal << 1; |
robert_lp | 0:eedb7d567a5d | 80 | } |
robert_lp | 0:eedb7d567a5d | 81 | |
robert_lp | 0:eedb7d567a5d | 82 | /** |
robert_lp | 0:eedb7d567a5d | 83 | * @} end of cos group |
robert_lp | 0:eedb7d567a5d | 84 | */ |
robert_lp | 0:eedb7d567a5d | 85 |