CMSIS DSP library
Dependents: performance_timer Surfboard_ gps2rtty Capstone ... more
Legacy Warning
This is an mbed 2 library. To learn more about mbed OS 5, visit the docs.
cmsis_dsp/FastMathFunctions/arm_cos_f32.c@5:3762170b6d4d, 2015-11-20 (annotated)
- Committer:
- mbed_official
- Date:
- Fri Nov 20 08:45:18 2015 +0000
- Revision:
- 5:3762170b6d4d
- Parent:
- 3:7a284390b0ce
Synchronized with git revision 2eb940b9a73af188d3004a2575fdfbb05febe62b
Full URL: https://github.com/mbedmicro/mbed/commit/2eb940b9a73af188d3004a2575fdfbb05febe62b/
Added option to build rpc library. closes #1426
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mbed_official | 5:3762170b6d4d | 1 | /* ---------------------------------------------------------------------- |
mbed_official | 5:3762170b6d4d | 2 | * Copyright (C) 2010-2014 ARM Limited. All rights reserved. |
mbed_official | 5:3762170b6d4d | 3 | * |
mbed_official | 5:3762170b6d4d | 4 | * $Date: 21. September 2015 |
mbed_official | 5:3762170b6d4d | 5 | * $Revision: V.1.4.5 a |
mbed_official | 5:3762170b6d4d | 6 | * |
mbed_official | 5:3762170b6d4d | 7 | * Project: CMSIS DSP Library |
mbed_official | 5:3762170b6d4d | 8 | * Title: arm_cos_f32.c |
mbed_official | 5:3762170b6d4d | 9 | * |
mbed_official | 5:3762170b6d4d | 10 | * Description: Fast cosine calculation for floating-point values. |
mbed_official | 5:3762170b6d4d | 11 | * |
emilmont | 1:fdd22bb7aa52 | 12 | * Target Processor: Cortex-M4/Cortex-M3/Cortex-M0 |
mbed_official | 5:3762170b6d4d | 13 | * |
mbed_official | 5:3762170b6d4d | 14 | * Redistribution and use in source and binary forms, with or without |
mbed_official | 3:7a284390b0ce | 15 | * modification, are permitted provided that the following conditions |
mbed_official | 3:7a284390b0ce | 16 | * are met: |
mbed_official | 3:7a284390b0ce | 17 | * - Redistributions of source code must retain the above copyright |
mbed_official | 3:7a284390b0ce | 18 | * notice, this list of conditions and the following disclaimer. |
mbed_official | 3:7a284390b0ce | 19 | * - Redistributions in binary form must reproduce the above copyright |
mbed_official | 3:7a284390b0ce | 20 | * notice, this list of conditions and the following disclaimer in |
mbed_official | 5:3762170b6d4d | 21 | * the documentation and/or other materials provided with the |
mbed_official | 3:7a284390b0ce | 22 | * distribution. |
mbed_official | 3:7a284390b0ce | 23 | * - Neither the name of ARM LIMITED nor the names of its contributors |
mbed_official | 3:7a284390b0ce | 24 | * may be used to endorse or promote products derived from this |
mbed_official | 3:7a284390b0ce | 25 | * software without specific prior written permission. |
mbed_official | 3:7a284390b0ce | 26 | * |
mbed_official | 3:7a284390b0ce | 27 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
mbed_official | 3:7a284390b0ce | 28 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
mbed_official | 3:7a284390b0ce | 29 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
mbed_official | 5:3762170b6d4d | 30 | * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
mbed_official | 3:7a284390b0ce | 31 | * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
mbed_official | 3:7a284390b0ce | 32 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
mbed_official | 3:7a284390b0ce | 33 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
mbed_official | 3:7a284390b0ce | 34 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
mbed_official | 3:7a284390b0ce | 35 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
mbed_official | 3:7a284390b0ce | 36 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN |
mbed_official | 3:7a284390b0ce | 37 | * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
mbed_official | 5:3762170b6d4d | 38 | * POSSIBILITY OF SUCH DAMAGE. |
emilmont | 1:fdd22bb7aa52 | 39 | * -------------------------------------------------------------------- */ |
emilmont | 1:fdd22bb7aa52 | 40 | |
emilmont | 1:fdd22bb7aa52 | 41 | #include "arm_math.h" |
mbed_official | 5:3762170b6d4d | 42 | #include "arm_common_tables.h" |
mbed_official | 5:3762170b6d4d | 43 | /** |
mbed_official | 5:3762170b6d4d | 44 | * @ingroup groupFastMath |
emilmont | 1:fdd22bb7aa52 | 45 | */ |
emilmont | 1:fdd22bb7aa52 | 46 | |
mbed_official | 5:3762170b6d4d | 47 | /** |
mbed_official | 5:3762170b6d4d | 48 | * @defgroup cos Cosine |
mbed_official | 5:3762170b6d4d | 49 | * |
mbed_official | 5:3762170b6d4d | 50 | * Computes the trigonometric cosine function using a combination of table lookup |
mbed_official | 5:3762170b6d4d | 51 | * and linear interpolation. There are separate functions for |
mbed_official | 5:3762170b6d4d | 52 | * Q15, Q31, and floating-point data types. |
mbed_official | 5:3762170b6d4d | 53 | * The input to the floating-point version is in radians while the |
mbed_official | 5:3762170b6d4d | 54 | * fixed-point Q15 and Q31 have a scaled input with the range |
mbed_official | 5:3762170b6d4d | 55 | * [0 +0.9999] mapping to [0 2*pi). The fixed-point range is chosen so that a |
mbed_official | 5:3762170b6d4d | 56 | * value of 2*pi wraps around to 0. |
mbed_official | 5:3762170b6d4d | 57 | * |
mbed_official | 5:3762170b6d4d | 58 | * The implementation is based on table lookup using 256 values together with linear interpolation. |
mbed_official | 5:3762170b6d4d | 59 | * The steps used are: |
mbed_official | 5:3762170b6d4d | 60 | * -# Calculation of the nearest integer table index |
mbed_official | 5:3762170b6d4d | 61 | * -# Compute the fractional portion (fract) of the table index. |
mbed_official | 5:3762170b6d4d | 62 | * -# The final result equals <code>(1.0f-fract)*a + fract*b;</code> |
mbed_official | 5:3762170b6d4d | 63 | * |
mbed_official | 5:3762170b6d4d | 64 | * where |
mbed_official | 5:3762170b6d4d | 65 | * <pre> |
mbed_official | 5:3762170b6d4d | 66 | * b=Table[index+0]; |
mbed_official | 5:3762170b6d4d | 67 | * c=Table[index+1]; |
mbed_official | 5:3762170b6d4d | 68 | * </pre> |
emilmont | 1:fdd22bb7aa52 | 69 | */ |
emilmont | 1:fdd22bb7aa52 | 70 | |
mbed_official | 5:3762170b6d4d | 71 | /** |
mbed_official | 5:3762170b6d4d | 72 | * @addtogroup cos |
mbed_official | 5:3762170b6d4d | 73 | * @{ |
mbed_official | 5:3762170b6d4d | 74 | */ |
mbed_official | 5:3762170b6d4d | 75 | |
mbed_official | 5:3762170b6d4d | 76 | /** |
mbed_official | 5:3762170b6d4d | 77 | * @brief Fast approximation to the trigonometric cosine function for floating-point data. |
mbed_official | 5:3762170b6d4d | 78 | * @param[in] x input value in radians. |
mbed_official | 5:3762170b6d4d | 79 | * @return cos(x). |
mbed_official | 5:3762170b6d4d | 80 | */ |
emilmont | 1:fdd22bb7aa52 | 81 | |
emilmont | 1:fdd22bb7aa52 | 82 | float32_t arm_cos_f32( |
emilmont | 1:fdd22bb7aa52 | 83 | float32_t x) |
emilmont | 1:fdd22bb7aa52 | 84 | { |
mbed_official | 5:3762170b6d4d | 85 | float32_t cosVal, fract, in; /* Temporary variables for input, output */ |
mbed_official | 5:3762170b6d4d | 86 | uint16_t index; /* Index variable */ |
mbed_official | 5:3762170b6d4d | 87 | float32_t a, b; /* Two nearest output values */ |
emilmont | 1:fdd22bb7aa52 | 88 | int32_t n; |
mbed_official | 5:3762170b6d4d | 89 | float32_t findex; |
emilmont | 1:fdd22bb7aa52 | 90 | |
emilmont | 1:fdd22bb7aa52 | 91 | /* input x is in radians */ |
mbed_official | 5:3762170b6d4d | 92 | /* Scale the input to [0 1] range from [0 2*PI] , divide input by 2*pi, add 0.25 (pi/2) to read sine table */ |
mbed_official | 5:3762170b6d4d | 93 | in = x * 0.159154943092f + 0.25f; |
emilmont | 1:fdd22bb7aa52 | 94 | |
emilmont | 1:fdd22bb7aa52 | 95 | /* Calculation of floor value of input */ |
emilmont | 1:fdd22bb7aa52 | 96 | n = (int32_t) in; |
emilmont | 1:fdd22bb7aa52 | 97 | |
emilmont | 1:fdd22bb7aa52 | 98 | /* Make negative values towards -infinity */ |
mbed_official | 5:3762170b6d4d | 99 | if(in < 0.0f) |
emilmont | 1:fdd22bb7aa52 | 100 | { |
mbed_official | 5:3762170b6d4d | 101 | n--; |
emilmont | 1:fdd22bb7aa52 | 102 | } |
emilmont | 1:fdd22bb7aa52 | 103 | |
emilmont | 1:fdd22bb7aa52 | 104 | /* Map input value to [0 1] */ |
emilmont | 1:fdd22bb7aa52 | 105 | in = in - (float32_t) n; |
emilmont | 1:fdd22bb7aa52 | 106 | |
emilmont | 1:fdd22bb7aa52 | 107 | /* Calculation of index of the table */ |
mbed_official | 5:3762170b6d4d | 108 | findex = (float32_t) FAST_MATH_TABLE_SIZE * in; |
mbed_official | 5:3762170b6d4d | 109 | index = ((uint16_t)findex) & 0x1ff; |
emilmont | 1:fdd22bb7aa52 | 110 | |
emilmont | 1:fdd22bb7aa52 | 111 | /* fractional value calculation */ |
mbed_official | 5:3762170b6d4d | 112 | fract = findex - (float32_t) index; |
emilmont | 1:fdd22bb7aa52 | 113 | |
mbed_official | 5:3762170b6d4d | 114 | /* Read two nearest values of input value from the cos table */ |
mbed_official | 5:3762170b6d4d | 115 | a = sinTable_f32[index]; |
mbed_official | 5:3762170b6d4d | 116 | b = sinTable_f32[index+1]; |
emilmont | 1:fdd22bb7aa52 | 117 | |
mbed_official | 5:3762170b6d4d | 118 | /* Linear interpolation process */ |
mbed_official | 5:3762170b6d4d | 119 | cosVal = (1.0f-fract)*a + fract*b; |
emilmont | 1:fdd22bb7aa52 | 120 | |
emilmont | 1:fdd22bb7aa52 | 121 | /* Return the output value */ |
emilmont | 1:fdd22bb7aa52 | 122 | return (cosVal); |
emilmont | 1:fdd22bb7aa52 | 123 | } |
emilmont | 1:fdd22bb7aa52 | 124 | |
mbed_official | 5:3762170b6d4d | 125 | /** |
mbed_official | 5:3762170b6d4d | 126 | * @} end of cos group |
emilmont | 1:fdd22bb7aa52 | 127 | */ |