Aded CMSIS5 DSP and NN folder. Needs some work

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers arm_sin_cos_f32.c Source File

arm_sin_cos_f32.c

00001 /* ----------------------------------------------------------------------
00002  * Project:      CMSIS DSP Library
00003  * Title:        arm_sin_cos_f32.c
00004  * Description:  Sine and Cosine calculation for floating-point values
00005  *
00006  * $Date:        27. January 2017
00007  * $Revision:    V.1.5.1
00008  *
00009  * Target Processor: Cortex-M cores
00010  * -------------------------------------------------------------------- */
00011 /*
00012  * Copyright (C) 2010-2017 ARM Limited or its affiliates. All rights reserved.
00013  *
00014  * SPDX-License-Identifier: Apache-2.0
00015  *
00016  * Licensed under the Apache License, Version 2.0 (the License); you may
00017  * not use this file except in compliance with the License.
00018  * You may obtain a copy of the License at
00019  *
00020  * www.apache.org/licenses/LICENSE-2.0
00021  *
00022  * Unless required by applicable law or agreed to in writing, software
00023  * distributed under the License is distributed on an AS IS BASIS, WITHOUT
00024  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00025  * See the License for the specific language governing permissions and
00026  * limitations under the License.
00027  */
00028 
00029 #include "arm_math.h"
00030 #include "arm_common_tables.h"
00031 
00032 /**
00033  * @ingroup groupController
00034  */
00035 
00036 /**
00037  * @defgroup SinCos Sine Cosine
00038  *
00039  * Computes the trigonometric sine and cosine values using a combination of table lookup
00040  * and linear interpolation.
00041  * There are separate functions for Q31 and floating-point data types.
00042  * The input to the floating-point version is in degrees while the
00043  * fixed-point Q31 have a scaled input with the range
00044  * [-1 0.9999] mapping to [-180 +180] degrees.
00045  *
00046  * The floating point function also allows values that are out of the usual range. When this happens, the function will
00047  * take extra time to adjust the input value to the range of [-180 180].
00048  *
00049  * The result is accurate to 5 digits after the decimal point.
00050  *
00051  * The implementation is based on table lookup using 360 values together with linear interpolation.
00052  * The steps used are:
00053  *  -# Calculation of the nearest integer table index.
00054  *  -# Compute the fractional portion (fract) of the input.
00055  *  -# Fetch the value corresponding to \c index from sine table to \c y0 and also value from \c index+1 to \c y1.
00056  *  -# Sine value is computed as <code> *psinVal = y0 + (fract * (y1 - y0))</code>.
00057  *  -# Fetch the value corresponding to \c index from cosine table to \c y0 and also value from \c index+1 to \c y1.
00058  *  -# Cosine value is computed as <code> *pcosVal = y0 + (fract * (y1 - y0))</code>.
00059  */
00060 
00061  /**
00062  * @addtogroup SinCos
00063  * @{
00064  */
00065 
00066 /**
00067  * @brief  Floating-point sin_cos function.
00068  * @param[in]  theta    input value in degrees
00069  * @param[out] *pSinVal points to the processed sine output.
00070  * @param[out] *pCosVal points to the processed cos output.
00071  * @return none.
00072  */
00073 
00074 void arm_sin_cos_f32(
00075                       float32_t theta,
00076                       float32_t * pSinVal,
00077                       float32_t * pCosVal)
00078 {
00079     float32_t fract, in;                             /* Temporary variables for input, output */
00080     uint16_t indexS, indexC;                         /* Index variable */
00081     float32_t f1, f2, d1, d2;                        /* Two nearest output values */
00082     float32_t findex, Dn, Df, temp;
00083 
00084     /* input x is in degrees */
00085     /* Scale the input, divide input by 360, for cosine add 0.25 (pi/2) to read sine table */
00086     in = theta * 0.00277777777778f;
00087 
00088     if (in < 0.0f)
00089     {
00090         in = -in;
00091     }
00092 
00093     in = in - (int32_t)in;
00094 
00095     /* Calculation of index of the table */
00096     findex = (float32_t) FAST_MATH_TABLE_SIZE * in;
00097     indexS = ((uint16_t)findex) & 0x1ff;
00098     indexC = (indexS + (FAST_MATH_TABLE_SIZE / 4)) & 0x1ff;
00099 
00100     /* fractional value calculation */
00101     fract = findex - (float32_t) indexS;
00102 
00103     /* Read two nearest values of input value from the cos & sin tables */
00104     f1 = sinTable_f32[indexC+0];
00105     f2 = sinTable_f32[indexC+1];
00106     d1 = -sinTable_f32[indexS+0];
00107     d2 = -sinTable_f32[indexS+1];
00108 
00109     temp = (1.0f - fract) * f1 + fract * f2;
00110 
00111     Dn = 0.0122718463030f; // delta between the two points (fixed), in this case 2*pi/FAST_MATH_TABLE_SIZE
00112     Df = f2 - f1;          // delta between the values of the functions
00113 
00114     temp = Dn *(d1 + d2) - 2 * Df;
00115     temp = fract * temp + (3 * Df - (d2 + 2 * d1) * Dn);
00116     temp = fract * temp + d1 * Dn;
00117 
00118     /* Calculation of cosine value */
00119     *pCosVal = fract * temp + f1;
00120 
00121     /* Read two nearest values of input value from the cos & sin tables */
00122     f1 = sinTable_f32[indexS+0];
00123     f2 = sinTable_f32[indexS+1];
00124     d1 = sinTable_f32[indexC+0];
00125     d2 = sinTable_f32[indexC+1];
00126 
00127     temp = (1.0f - fract) * f1 + fract * f2;
00128 
00129     Df = f2 - f1; // delta between the values of the functions
00130     temp = Dn*(d1 + d2) - 2*Df;
00131     temp = fract*temp + (3*Df - (d2 + 2*d1)*Dn);
00132     temp = fract*temp + d1*Dn;
00133 
00134     /* Calculation of sine value */
00135     *pSinVal = fract*temp + f1;
00136 
00137     if (theta < 0.0f)
00138     {
00139         *pSinVal = -*pSinVal;
00140     }
00141 }
00142 /**
00143  * @} end of SinCos group
00144  */
00145