Robert Lopez / CMSIS5
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers arm_sin_cos_q31.c Source File

arm_sin_cos_q31.c

00001 /* ----------------------------------------------------------------------
00002  * Project:      CMSIS DSP Library
00003  * Title:        arm_sin_cos_q31.c
00004  * Description:  Cosine & Sine calculation for Q31 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  * @addtogroup SinCos
00038  * @{
00039  */
00040 
00041 /**
00042  * @brief  Q31 sin_cos function.
00043  * @param[in]  theta    scaled input value in degrees
00044  * @param[out] *pSinVal points to the processed sine output.
00045  * @param[out] *pCosVal points to the processed cosine output.
00046  * @return none.
00047  *
00048  * The Q31 input value is in the range [-1 0.999999] and is mapped to a degree value in the range [-180 179].
00049  *
00050  */
00051 
00052 void arm_sin_cos_q31(
00053   q31_t theta,
00054   q31_t * pSinVal,
00055   q31_t * pCosVal)
00056 {
00057   q31_t fract;                                 /* Temporary variables for input, output */
00058   uint16_t indexS, indexC;                     /* Index variable */
00059   q31_t f1, f2, d1, d2;                        /* Two nearest output values */
00060   q31_t Dn, Df;
00061   q63_t temp;
00062 
00063   /* Calculate the nearest index */
00064   indexS = (uint32_t)theta >> CONTROLLER_Q31_SHIFT;
00065   indexC = (indexS + 128) & 0x1ff;
00066 
00067   /* Calculation of fractional value */
00068   fract = (theta - (indexS << CONTROLLER_Q31_SHIFT)) << 8;
00069 
00070   /* Read two nearest values of input value from the cos & sin tables */
00071   f1 = sinTable_q31[indexC+0];
00072   f2 = sinTable_q31[indexC+1];
00073   d1 = -sinTable_q31[indexS+0];
00074   d2 = -sinTable_q31[indexS+1];
00075 
00076   Dn = 0x1921FB5; // delta between the two points (fixed), in this case 2*pi/FAST_MATH_TABLE_SIZE
00077   Df = f2 - f1; // delta between the values of the functions
00078   temp = Dn*((q63_t)d1 + d2);
00079   temp = temp - ((q63_t)Df << 32);
00080   temp = (q63_t)fract*(temp >> 31);
00081   temp = temp + ((3*(q63_t)Df << 31) - (d2 + ((q63_t)d1 << 1))*Dn);
00082   temp = (q63_t)fract*(temp >> 31);
00083   temp = temp + (q63_t)d1*Dn;
00084   temp = (q63_t)fract*(temp >> 31);
00085 
00086   /* Calculation of cosine value */
00087   *pCosVal = clip_q63_to_q31((temp >> 31) + (q63_t)f1);
00088 
00089   /* Read two nearest values of input value from the cos & sin tables */
00090   f1 = sinTable_q31[indexS+0];
00091   f2 = sinTable_q31[indexS+1];
00092   d1 = sinTable_q31[indexC+0];
00093   d2 = sinTable_q31[indexC+1];
00094 
00095   Df = f2 - f1; // delta between the values of the functions
00096   temp = Dn*((q63_t)d1 + d2);
00097   temp = temp - ((q63_t)Df << 32);
00098   temp = (q63_t)fract*(temp >> 31);
00099   temp = temp + ((3*(q63_t)Df << 31) - (d2 + ((q63_t)d1 << 1))*Dn);
00100   temp = (q63_t)fract*(temp >> 31);
00101   temp = temp + (q63_t)d1*Dn;
00102   temp = (q63_t)fract*(temp >> 31);
00103 
00104   /* Calculation of sine value */
00105   *pSinVal = clip_q63_to_q31((temp >> 31) + (q63_t)f1);
00106 }
00107 
00108 /**
00109  * @} end of SinCos group
00110  */
00111