/ Drivers / CMSIS / DSP / Examples / ARM / arm_sin_cos_example / arm_sin_cos_example_f32.c
arm_sin_cos_example_f32.c
  1  /* ----------------------------------------------------------------------
  2  * Copyright (C) 2010-2012 ARM Limited. All rights reserved.
  3  *
  4  * $Date:         12. March 2014
  5  * $Revision:     V1.4.3
  6  *
  7  * Project:       CMSIS DSP Library
  8  * Title:         arm_sin_cos_example_f32.c
  9  *
 10  * Description:   Example code demonstrating sin and cos calculation of input signal.
 11  *
 12  * Target Processor: Cortex-M4/Cortex-M3
 13  *
 14  * Redistribution and use in source and binary forms, with or without
 15  * modification, are permitted provided that the following conditions
 16  * are met:
 17  *   - Redistributions of source code must retain the above copyright
 18  *     notice, this list of conditions and the following disclaimer.
 19  *   - Redistributions in binary form must reproduce the above copyright
 20  *     notice, this list of conditions and the following disclaimer in
 21  *     the documentation and/or other materials provided with the
 22  *     distribution.
 23  *   - Neither the name of ARM LIMITED nor the names of its contributors
 24  *     may be used to endorse or promote products derived from this
 25  *     software without specific prior written permission.
 26  *
 27  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 28  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 29  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
 30  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
 31  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
 32  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
 33  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 34  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
 35  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 36  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
 37  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 38  * POSSIBILITY OF SUCH DAMAGE.
 39  * -------------------------------------------------------------------- */
 40  
 41  /**
 42   * @ingroup groupExamples
 43   */
 44  
 45  /**
 46   * @defgroup SinCosExample SineCosine Example
 47   *
 48   * \par Description:
 49   * \par
 50   * Demonstrates the Pythagorean trignometric identity with the use of Cosine, Sine, Vector
 51   * Multiplication, and Vector Addition functions.
 52   *
 53   * \par Algorithm:
 54   * \par
 55   * Mathematically, the Pythagorean trignometric identity is defined by the following equation:
 56   *  <pre>sin(x) * sin(x) + cos(x) * cos(x) = 1</pre>
 57   * where \c x is the angle in radians.
 58   *
 59   * \par Block Diagram:
 60   * \par
 61   * \image html sinCos.gif
 62   *
 63   * \par Variables Description:
 64   * \par
 65   * \li \c testInput_f32 array of input angle in radians
 66   * \li \c testOutput stores sum of the squares of sine and cosine values of input angle
 67   *
 68   * \par CMSIS DSP Software Library Functions Used:
 69   * \par
 70   * - arm_cos_f32()
 71   * - arm_sin_f32()
 72   * - arm_mult_f32()
 73   * - arm_add_f32()
 74   *
 75   * <b> Refer  </b>
 76   * \link arm_sin_cos_example_f32.c \endlink
 77   *
 78   */
 79  
 80  
 81  /** \example arm_sin_cos_example_f32.c
 82    */
 83  
 84  #include <math.h>
 85  #include "arm_math.h"
 86  
 87  #if defined(SEMIHOSTING)
 88  #include <stdio.h>
 89  #endif
 90  
 91  /* ----------------------------------------------------------------------
 92  * Defines each of the tests performed
 93  * ------------------------------------------------------------------- */
 94  #define MAX_BLOCKSIZE   32
 95  #define DELTA           (0.0001f)
 96  
 97  
 98  /* ----------------------------------------------------------------------
 99  * Test input data for Floating point sin_cos example for 32-blockSize
100  * Generated by the MATLAB randn() function
101  * ------------------------------------------------------------------- */
102  
103  const float32_t testInput_f32[MAX_BLOCKSIZE] =
104  {
105    -1.244916875853235400,  -4.793533929171324800,   0.360705030233248850,   0.827929644170887320,  -3.299532218312426900,   3.427441903227623800,   3.422401784294607700,  -0.108308165334010680,
106     0.941943896490312180,   0.502609575000365850,  -0.537345278736373500,   2.088817392965764500,  -1.693168684143455700,   6.283185307179590700,  -0.392545884746175080,   0.327893095115825040,
107     3.070147440456292300,   0.170611405884662230,  -0.275275082396073010,  -2.395492805446796300,   0.847311163536506600,  -3.845517018083148800,   2.055818378415868300,   4.672594161978930800,
108    -1.990923030266425800,   2.469305197656249500,   3.609002606064021000,  -4.586736582331667500,  -4.147080139136136300,   1.643756718868359500,  -1.150866392366494800,   1.985805026477433800
109  
110  
111  };
112  
113  const float32_t testRefOutput_f32 = 1.000000000;
114  
115  /* ----------------------------------------------------------------------
116  * Declare Global variables
117  * ------------------------------------------------------------------- */
118  uint32_t blockSize = 32;
119  float32_t  testOutput;
120  float32_t  cosOutput;
121  float32_t  sinOutput;
122  float32_t  cosSquareOutput;
123  float32_t  sinSquareOutput;
124  
125  /* ----------------------------------------------------------------------
126  * Max magnitude FFT Bin test
127  * ------------------------------------------------------------------- */
128  
129  arm_status status;
130  
131  int32_t main(void)
132  {
133    float32_t diff;
134    uint32_t i;
135  
136    for(i=0; i< blockSize; i++)
137    {
138      cosOutput = arm_cos_f32(testInput_f32[i]);
139      sinOutput = arm_sin_f32(testInput_f32[i]);
140  
141      arm_mult_f32(&cosOutput, &cosOutput, &cosSquareOutput, 1);
142      arm_mult_f32(&sinOutput, &sinOutput, &sinSquareOutput, 1);
143  
144      arm_add_f32(&cosSquareOutput, &sinSquareOutput, &testOutput, 1);
145  
146      /* absolute value of difference between ref and test */
147      diff = fabsf(testRefOutput_f32 - testOutput);
148  
149      /* Comparison of sin_cos value with reference */
150      status = (diff > DELTA) ? ARM_MATH_TEST_FAILURE : ARM_MATH_SUCCESS;
151  
152      if ( status == ARM_MATH_TEST_FAILURE)
153      {
154         break;
155      }
156    }
157  
158    if (status != ARM_MATH_SUCCESS)
159    {
160  #if defined (SEMIHOSTING)
161      printf("FAILURE\n");
162  #else
163      while (1);                             /* main function does not return */
164  #endif
165    }
166    else
167    {
168  #if defined (SEMIHOSTING)
169      printf("SUCCESS\n");
170  #else
171      while (1);                             /* main function does not return */
172  #endif
173    }
174  
175  }
176  
177   /** \endlink */