CMSIS DSP library

Dependents:   performance_timer Surfboard_ gps2rtty Capstone ... more

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers arm_cos_f32.c Source File

arm_cos_f32.c

00001 /* ----------------------------------------------------------------------
00002 * Copyright (C) 2010-2014 ARM Limited. All rights reserved.
00003 *
00004 * $Date:        21. September 2015
00005 * $Revision:    V.1.4.5 a
00006 *
00007 * Project:      CMSIS DSP Library
00008 * Title:        arm_cos_f32.c
00009 *
00010 * Description:  Fast cosine calculation for floating-point 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  * @ingroup groupFastMath
00045  */
00046 
00047 /**
00048  * @defgroup cos Cosine
00049  *
00050  * Computes the trigonometric cosine function using a combination of table lookup
00051  * and linear interpolation.  There are separate functions for
00052  * Q15, Q31, and floating-point data types.
00053  * The input to the floating-point version is in radians while the
00054  * fixed-point Q15 and Q31 have a scaled input with the range
00055  * [0 +0.9999] mapping to [0 2*pi).  The fixed-point range is chosen so that a
00056  * value of 2*pi wraps around to 0.
00057  *
00058  * The implementation is based on table lookup using 256 values together with linear interpolation.
00059  * The steps used are:
00060  *  -# Calculation of the nearest integer table index
00061  *  -# Compute the fractional portion (fract) of the table index.
00062  *  -# The final result equals <code>(1.0f-fract)*a + fract*b;</code>
00063  *
00064  * where
00065  * <pre>
00066  *    b=Table[index+0];
00067  *    c=Table[index+1];
00068  * </pre>
00069  */
00070 
00071  /**
00072  * @addtogroup cos
00073  * @{
00074  */
00075 
00076 /**
00077  * @brief  Fast approximation to the trigonometric cosine function for floating-point data.
00078  * @param[in] x input value in radians.
00079  * @return cos(x).
00080  */
00081 
00082 float32_t arm_cos_f32(
00083   float32_t x)
00084 {
00085   float32_t cosVal, fract, in;                   /* Temporary variables for input, output */
00086   uint16_t index;                                /* Index variable */
00087   float32_t a, b;                                /* Two nearest output values */
00088   int32_t n;
00089   float32_t findex;
00090 
00091   /* input x is in radians */
00092   /* 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 */
00093   in = x * 0.159154943092f + 0.25f;
00094 
00095   /* Calculation of floor value of input */
00096   n = (int32_t) in;
00097 
00098   /* Make negative values towards -infinity */
00099   if(in < 0.0f)
00100   {
00101     n--;
00102   }
00103 
00104   /* Map input value to [0 1] */
00105   in = in - (float32_t) n;
00106 
00107   /* Calculation of index of the table */
00108   findex = (float32_t) FAST_MATH_TABLE_SIZE * in;
00109   index = ((uint16_t)findex) & 0x1ff;
00110 
00111   /* fractional value calculation */
00112   fract = findex - (float32_t) index;
00113 
00114   /* Read two nearest values of input value from the cos table */
00115   a = sinTable_f32[index];
00116   b = sinTable_f32[index+1];
00117 
00118   /* Linear interpolation process */
00119   cosVal = (1.0f-fract)*a + fract*b;
00120 
00121   /* Return the output value */
00122   return (cosVal);
00123 }
00124 
00125 /**
00126  * @} end of cos group
00127  */