| CMSIS-DSP Verison 1.4.1 CMSIS DSP Software Library

- CMSIS
- CORE
- DSP
- RTOS API
- SVD

- Main Page
- Usage and Description
- Reference

All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
Functions | Variables
Cosine
Fast Math Functions

## Functions
float32_t | arm_cos_f32 (float32_t x)
| Fast approximation to the trigonometric cosine function for floating-point data. More...
q15_t | arm_cos_q15 (q15_t x)
| Fast approximation to the trigonometric cosine function for Q15 data. More...
q31_t | arm_cos_q31 (q31_t x)
| Fast approximation to the trigonometric cosine function for Q31 data. More...

## Variables
static const float32_t | cosTable [260]
static const q15_t | cosTableQ15 [259]
static const q31_t | cosTableQ31 [259]

## Description

Computes the trigonometric cosine function using a combination of table lookup and cubic interpolation. There are separate functions for Q15, Q31, and floating-point data types. The input to the floating-point version is in radians while the fixed-point Q15 and Q31 have a scaled input with the range [0 +0.9999] mapping to [0 2*pi). The fixed-point range is chosen so that a value of 2*pi wraps around to 0.

The implementation is based on table lookup using 256 values together with cubic interpolation. The steps used are:

1. Calculation of the nearest integer table index
2. Fetch the four table values a, b, c, and d
3. Compute the fractional portion (fract) of the table index.
4. Calculation of wa, wb, wc, wd
5. The final result equals `a*wa + b*wb + c*wc + d*wd`

where

       a=Table[index-1];
       b=Table[index+0];
       c=Table[index+1];
       d=Table[index+2];

and

       wa=-(1/6)*fract.^3 + (1/2)*fract.^2 - (1/3)*fract;
       wb=(1/2)*fract.^3 - fract.^2 - (1/2)*fract + 1;
       wc=-(1/2)*fract.^3+(1/2)*fract.^2+fract;
       wd=(1/6)*fract.^3 - (1/6)*fract;

## Function Documentation

float32_t arm_cos_f32 | ( | float32_t | x | ) |

Parameters

  [in] | x | input value in radians.

Returns
  cos(x).

Examples:
  arm_sin_cos_example_f32.c.

References cosTable, and TABLE_SIZE.

Referenced by main().

q15_t arm_cos_q15 | ( | q15_t | x | ) |

Parameters

  [in] | x | Scaled input value in radians.

Returns
  cos(x).

The Q15 input value is in the range [0 +0.9999] and is mapped to a radian value in the range [0 2*pi).

References cosTableQ15, and TABLE_SPACING_Q15.

q31_t arm_cos_q31 | ( | q31_t | x | ) |

Parameters

  [in] | x | Scaled input value in radians.

Returns
  cos(x).

The Q31 input value is in the range [0 +0.9999] and is mapped to a radian value in the range [0 2*pi).

References cosTableQ31, and TABLE_SPACING_Q31.

## Variable Documentation

const float32_t cosTable[260]
| static

  Example code for Generation of Cos Table:

      tableSize = 256;
      for(n = -1; n < (tableSize + 2); n++)
      {
            cosTable[n+1]= cos(2*pi*n/tableSize);
      }

  where pi value is 3.14159265358979

Referenced by arm_cos_f32().

const q15_t cosTableQ15[259]
| static

  Table values are in Q15 (1.15 fixed-point format) and generation is done in three steps. First, generate cos values in floating point:

       tableSize = 256;
       for(n = -1; n < (tableSize + 1); n++)
       {
              cosTable[n+1]= cos(2*pi*n/tableSize);
       }

  where pi value is 3.14159265358979

  Second, convert floating-point to Q15 (fixed-point): (cosTable[i] * pow(2, 15))

  Finally, round to the nearest integer value: cosTable[i] += (cosTable[i] > 0 ? 0.5 :-0.5);

Referenced by arm_cos_q15().

const q31_t cosTableQ31[259]
| static

  Table values are in Q31 (1.31 fixed-point format) and generation is done in three steps. First, generate cos values in floating point:

      tableSize = 256;
      for(n = -1; n < (tableSize + 1); n++)
      {
           cosTable[n+1]= cos(2*pi*n/tableSize);
      }

  where pi value is 3.14159265358979

  Second, convert floating-point to Q31 (Fixed point): (cosTable[i] * pow(2, 31))

  Finally, round to the nearest integer value: cosTable[i] += (cosTable[i] > 0 ? 0.5 :-0.5);

Referenced by arm_cos_q31().