CMSIS DSP library

Dependents:   performance_timer Surfboard_ gps2rtty Capstone ... more

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 * Copyright (C) 2010-2014 ARM Limited. All rights reserved.    
00003 *    
00004 * $Date:        19. March 2015
00005 * $Revision:    V.1.4.5
00006 *    
00007 * Project:      CMSIS DSP Library    
00008 * Title:        arm_sin_cos_q31.c    
00009 *    
00010 * Description:  Cosine & Sine calculation for Q31 values.   
00011 *    
00012 * Target Processor: Cortex-M4/Cortex-M3/Cortex-M0
00013 *  
00014 * Redistribution and use in source and binary forms, with or without 
00015 * modification, are permitted provided that the following conditions
00016 * are met:
00017 *   - Redistributions of source code must retain the above copyright
00018 *     notice, this list of conditions and the following disclaimer.
00019 *   - Redistributions in binary form must reproduce the above copyright
00020 *     notice, this list of conditions and the following disclaimer in
00021 *     the documentation and/or other materials provided with the 
00022 *     distribution.
00023 *   - Neither the name of ARM LIMITED nor the names of its contributors
00024 *     may be used to endorse or promote products derived from this
00025 *     software without specific prior written permission.
00026 *
00027 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00028 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00029 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00030 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 
00031 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00032 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00033 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00034 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00035 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00036 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
00037 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00038 * POSSIBILITY OF SUCH DAMAGE.    
00039 * -------------------------------------------------------------------- */
00040 
00041 #include "arm_math.h"
00042 #include "arm_common_tables.h"
00043 
00044 /**    
00045  * @ingroup groupController    
00046  */
00047 
00048  /**    
00049  * @addtogroup SinCos    
00050  * @{    
00051  */
00052 
00053 /**    
00054  * @brief  Q31 sin_cos function.   
00055  * @param[in]  theta    scaled input value in degrees    
00056  * @param[out] *pSinVal points to the processed sine output.    
00057  * @param[out] *pCosVal points to the processed cosine output.    
00058  * @return none.   
00059  *    
00060  * The Q31 input value is in the range [-1 0.999999] and is mapped to a degree value in the range [-180 179].   
00061  *    
00062  */
00063 
00064 void arm_sin_cos_q31(
00065   q31_t theta,
00066   q31_t * pSinVal,
00067   q31_t * pCosVal)
00068 {
00069   q31_t fract;                                 /* Temporary variables for input, output */
00070   uint16_t indexS, indexC;                     /* Index variable */
00071   q31_t f1, f2, d1, d2;                        /* Two nearest output values */
00072   q31_t Dn, Df;
00073   q63_t temp;
00074   
00075   /* Calculate the nearest index */
00076   indexS = (uint32_t)theta >> CONTROLLER_Q31_SHIFT;
00077   indexC = (indexS + 128) & 0x1ff;
00078 
00079   /* Calculation of fractional value */
00080   fract = (theta - (indexS << CONTROLLER_Q31_SHIFT)) << 8;
00081   
00082   /* Read two nearest values of input value from the cos & sin tables */
00083   f1 = sinTable_q31[indexC+0];
00084   f2 = sinTable_q31[indexC+1];
00085   d1 = -sinTable_q31[indexS+0];
00086   d2 = -sinTable_q31[indexS+1];
00087 
00088   Dn = 0x1921FB5; // delta between the two points (fixed), in this case 2*pi/FAST_MATH_TABLE_SIZE
00089   Df = f2 - f1; // delta between the values of the functions
00090   temp = Dn*((q63_t)d1 + d2);
00091   temp = temp - ((q63_t)Df << 32);
00092   temp = (q63_t)fract*(temp >> 31);
00093   temp = temp + ((3*(q63_t)Df << 31) - (d2 + ((q63_t)d1 << 1))*Dn);
00094   temp = (q63_t)fract*(temp >> 31);
00095   temp = temp + (q63_t)d1*Dn;
00096   temp = (q63_t)fract*(temp >> 31);
00097 
00098   /* Calculation of cosine value */
00099   *pCosVal = clip_q63_to_q31((temp >> 31) + (q63_t)f1);
00100   
00101   /* Read two nearest values of input value from the cos & sin tables */
00102   f1 = sinTable_q31[indexS+0];
00103   f2 = sinTable_q31[indexS+1];
00104   d1 = sinTable_q31[indexC+0];
00105   d2 = sinTable_q31[indexC+1];
00106 
00107   Df = f2 - f1; // delta between the values of the functions
00108   temp = Dn*((q63_t)d1 + d2);
00109   temp = temp - ((q63_t)Df << 32);
00110   temp = (q63_t)fract*(temp >> 31);
00111   temp = temp + ((3*(q63_t)Df << 31) - (d2 + ((q63_t)d1 << 1))*Dn);
00112   temp = (q63_t)fract*(temp >> 31);
00113   temp = temp + (q63_t)d1*Dn;
00114   temp = (q63_t)fract*(temp >> 31);
00115   
00116   /* Calculation of sine value */
00117   *pSinVal = clip_q63_to_q31((temp >> 31) + (q63_t)f1);
00118 }
00119 
00120 /**    
00121  * @} end of SinCos group    
00122  */