Dependencies: platform_drivers LTC26X6 AD77681
CMSIS-DSP_Lib/Source/FastMathFunctions/arm_cos_q31.c@1:9dd7c64b4a64, 2021-12-06 (annotated)
- Committer:
- jngarlitos
- Date:
- Mon Dec 06 05:22:28 2021 +0000
- Revision:
- 1:9dd7c64b4a64
EVAL-CN0540-ARDZ mbed example program Initial Commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
jngarlitos | 1:9dd7c64b4a64 | 1 | /* ---------------------------------------------------------------------- |
jngarlitos | 1:9dd7c64b4a64 | 2 | * Copyright (C) 2010-2014 ARM Limited. All rights reserved. |
jngarlitos | 1:9dd7c64b4a64 | 3 | * |
jngarlitos | 1:9dd7c64b4a64 | 4 | * $Date: 07. September 2015 |
jngarlitos | 1:9dd7c64b4a64 | 5 | * $Revision: V.1.4.5 a |
jngarlitos | 1:9dd7c64b4a64 | 6 | * |
jngarlitos | 1:9dd7c64b4a64 | 7 | * Project: CMSIS DSP Library |
jngarlitos | 1:9dd7c64b4a64 | 8 | * Title: arm_cos_q31.c |
jngarlitos | 1:9dd7c64b4a64 | 9 | * |
jngarlitos | 1:9dd7c64b4a64 | 10 | * Description: Fast cosine calculation for Q31 values. |
jngarlitos | 1:9dd7c64b4a64 | 11 | * |
jngarlitos | 1:9dd7c64b4a64 | 12 | * Target Processor: Cortex-M4/Cortex-M3/Cortex-M0 |
jngarlitos | 1:9dd7c64b4a64 | 13 | * |
jngarlitos | 1:9dd7c64b4a64 | 14 | * Redistribution and use in source and binary forms, with or without |
jngarlitos | 1:9dd7c64b4a64 | 15 | * modification, are permitted provided that the following conditions |
jngarlitos | 1:9dd7c64b4a64 | 16 | * are met: |
jngarlitos | 1:9dd7c64b4a64 | 17 | * - Redistributions of source code must retain the above copyright |
jngarlitos | 1:9dd7c64b4a64 | 18 | * notice, this list of conditions and the following disclaimer. |
jngarlitos | 1:9dd7c64b4a64 | 19 | * - Redistributions in binary form must reproduce the above copyright |
jngarlitos | 1:9dd7c64b4a64 | 20 | * notice, this list of conditions and the following disclaimer in |
jngarlitos | 1:9dd7c64b4a64 | 21 | * the documentation and/or other materials provided with the |
jngarlitos | 1:9dd7c64b4a64 | 22 | * distribution. |
jngarlitos | 1:9dd7c64b4a64 | 23 | * - Neither the name of ARM LIMITED nor the names of its contributors |
jngarlitos | 1:9dd7c64b4a64 | 24 | * may be used to endorse or promote products derived from this |
jngarlitos | 1:9dd7c64b4a64 | 25 | * software without specific prior written permission. |
jngarlitos | 1:9dd7c64b4a64 | 26 | * |
jngarlitos | 1:9dd7c64b4a64 | 27 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
jngarlitos | 1:9dd7c64b4a64 | 28 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
jngarlitos | 1:9dd7c64b4a64 | 29 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
jngarlitos | 1:9dd7c64b4a64 | 30 | * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
jngarlitos | 1:9dd7c64b4a64 | 31 | * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
jngarlitos | 1:9dd7c64b4a64 | 32 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
jngarlitos | 1:9dd7c64b4a64 | 33 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
jngarlitos | 1:9dd7c64b4a64 | 34 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
jngarlitos | 1:9dd7c64b4a64 | 35 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
jngarlitos | 1:9dd7c64b4a64 | 36 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN |
jngarlitos | 1:9dd7c64b4a64 | 37 | * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
jngarlitos | 1:9dd7c64b4a64 | 38 | * POSSIBILITY OF SUCH DAMAGE. |
jngarlitos | 1:9dd7c64b4a64 | 39 | * -------------------------------------------------------------------- */ |
jngarlitos | 1:9dd7c64b4a64 | 40 | |
jngarlitos | 1:9dd7c64b4a64 | 41 | #include "arm_math.h" |
jngarlitos | 1:9dd7c64b4a64 | 42 | #include "arm_common_tables.h" |
jngarlitos | 1:9dd7c64b4a64 | 43 | |
jngarlitos | 1:9dd7c64b4a64 | 44 | /** |
jngarlitos | 1:9dd7c64b4a64 | 45 | * @ingroup groupFastMath |
jngarlitos | 1:9dd7c64b4a64 | 46 | */ |
jngarlitos | 1:9dd7c64b4a64 | 47 | |
jngarlitos | 1:9dd7c64b4a64 | 48 | /** |
jngarlitos | 1:9dd7c64b4a64 | 49 | * @addtogroup cos |
jngarlitos | 1:9dd7c64b4a64 | 50 | * @{ |
jngarlitos | 1:9dd7c64b4a64 | 51 | */ |
jngarlitos | 1:9dd7c64b4a64 | 52 | |
jngarlitos | 1:9dd7c64b4a64 | 53 | /** |
jngarlitos | 1:9dd7c64b4a64 | 54 | * @brief Fast approximation to the trigonometric cosine function for Q31 data. |
jngarlitos | 1:9dd7c64b4a64 | 55 | * @param[in] x Scaled input value in radians. |
jngarlitos | 1:9dd7c64b4a64 | 56 | * @return cos(x). |
jngarlitos | 1:9dd7c64b4a64 | 57 | * |
jngarlitos | 1:9dd7c64b4a64 | 58 | * The Q31 input value is in the range [0 +0.9999] and is mapped to a radian |
jngarlitos | 1:9dd7c64b4a64 | 59 | * value in the range [0 2*pi). |
jngarlitos | 1:9dd7c64b4a64 | 60 | */ |
jngarlitos | 1:9dd7c64b4a64 | 61 | |
jngarlitos | 1:9dd7c64b4a64 | 62 | q31_t arm_cos_q31( |
jngarlitos | 1:9dd7c64b4a64 | 63 | q31_t x) |
jngarlitos | 1:9dd7c64b4a64 | 64 | { |
jngarlitos | 1:9dd7c64b4a64 | 65 | q31_t cosVal; /* Temporary variables for input, output */ |
jngarlitos | 1:9dd7c64b4a64 | 66 | int32_t index; /* Index variables */ |
jngarlitos | 1:9dd7c64b4a64 | 67 | q31_t a, b; /* Four nearest output values */ |
jngarlitos | 1:9dd7c64b4a64 | 68 | q31_t fract; /* Temporary values for fractional values */ |
jngarlitos | 1:9dd7c64b4a64 | 69 | |
jngarlitos | 1:9dd7c64b4a64 | 70 | /* add 0.25 (pi/2) to read sine table */ |
jngarlitos | 1:9dd7c64b4a64 | 71 | x = (uint32_t)x + 0x20000000; |
jngarlitos | 1:9dd7c64b4a64 | 72 | if(x < 0) |
jngarlitos | 1:9dd7c64b4a64 | 73 | { /* convert negative numbers to corresponding positive ones */ |
jngarlitos | 1:9dd7c64b4a64 | 74 | x = (uint32_t)x + 0x80000000; |
jngarlitos | 1:9dd7c64b4a64 | 75 | } |
jngarlitos | 1:9dd7c64b4a64 | 76 | |
jngarlitos | 1:9dd7c64b4a64 | 77 | /* Calculate the nearest index */ |
jngarlitos | 1:9dd7c64b4a64 | 78 | index = (uint32_t)x >> FAST_MATH_Q31_SHIFT; |
jngarlitos | 1:9dd7c64b4a64 | 79 | |
jngarlitos | 1:9dd7c64b4a64 | 80 | /* Calculation of fractional value */ |
jngarlitos | 1:9dd7c64b4a64 | 81 | fract = (x - (index << FAST_MATH_Q31_SHIFT)) << 9; |
jngarlitos | 1:9dd7c64b4a64 | 82 | |
jngarlitos | 1:9dd7c64b4a64 | 83 | /* Read two nearest values of input value from the sin table */ |
jngarlitos | 1:9dd7c64b4a64 | 84 | a = sinTable_q31[index]; |
jngarlitos | 1:9dd7c64b4a64 | 85 | b = sinTable_q31[index+1]; |
jngarlitos | 1:9dd7c64b4a64 | 86 | |
jngarlitos | 1:9dd7c64b4a64 | 87 | /* Linear interpolation process */ |
jngarlitos | 1:9dd7c64b4a64 | 88 | cosVal = (q63_t)(0x80000000-fract)*a >> 32; |
jngarlitos | 1:9dd7c64b4a64 | 89 | cosVal = (q31_t)((((q63_t)cosVal << 32) + ((q63_t)fract*b)) >> 32); |
jngarlitos | 1:9dd7c64b4a64 | 90 | |
jngarlitos | 1:9dd7c64b4a64 | 91 | return cosVal << 1; |
jngarlitos | 1:9dd7c64b4a64 | 92 | } |
jngarlitos | 1:9dd7c64b4a64 | 93 | |
jngarlitos | 1:9dd7c64b4a64 | 94 | /** |
jngarlitos | 1:9dd7c64b4a64 | 95 | * @} end of cos group |
jngarlitos | 1:9dd7c64b4a64 | 96 | */ |