Subversion Repositories CharLCD

Rev

Blame | Last modification | View Log | Download | RSS feed

  1. /* ----------------------------------------------------------------------
  2. * Copyright (C) 2010-2012 ARM Limited. All rights reserved.
  3. *
  4. * $Date:         17. January 2013
  5. * $Revision:     V1.4.0
  6. *
  7. * Project:       CMSIS DSP Library
  8. * Title:         arm_linear_interp_example_f32.c
  9. *
  10. * Description:   Example code demonstrating usage of sin function
  11. *                and uses linear interpolation to get higher precision
  12. *
  13. * Target Processor: Cortex-M4/Cortex-M3
  14. *
  15. * Redistribution and use in source and binary forms, with or without
  16. * modification, are permitted provided that the following conditions
  17. * are met:
  18. *   - Redistributions of source code must retain the above copyright
  19. *     notice, this list of conditions and the following disclaimer.
  20. *   - Redistributions in binary form must reproduce the above copyright
  21. *     notice, this list of conditions and the following disclaimer in
  22. *     the documentation and/or other materials provided with the
  23. *     distribution.
  24. *   - Neither the name of ARM LIMITED nor the names of its contributors
  25. *     may be used to endorse or promote products derived from this
  26. *     software without specific prior written permission.
  27. *
  28. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  29. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  30. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  31. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  32. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  33. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  34. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  35. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  36. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  37. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  38. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  39. * POSSIBILITY OF SUCH DAMAGE.
  40.  * -------------------------------------------------------------------- */
  41.  
  42.  
  43. /**
  44.  * @ingroup groupExamples
  45.  */
  46.  
  47. /**
  48.  * @defgroup LinearInterpExample Linear Interpolate Example
  49.  *
  50.  * <b> CMSIS DSP Software Library -- Linear Interpolate Example  </b>
  51.  *
  52.  * <b> Description </b>
  53.  * This example demonstrates usage of linear interpolate modules and fast math modules.
  54.  * Method 1 uses fast math sine function to calculate sine values using cubic interpolation and method 2 uses
  55.  * linear interpolation function and results are compared to reference output.
  56.  * Example shows linear interpolation function can be used to get higher precision compared to fast math sin calculation.
  57.  *
  58.  * \par Block Diagram:
  59.  * \par
  60.  * \image html linearInterpExampleMethod1.gif "Method 1: Sine caluclation using fast math"
  61.  * \par
  62.  * \image html linearInterpExampleMethod2.gif "Method 2: Sine caluclation using interpolation function"
  63.  *
  64.  * \par Variables Description:
  65.  * \par
  66.  * \li \c testInputSin_f32         points to the input values for sine calculation
  67.  * \li \c testRefSinOutput32_f32   points to the reference values caculated from sin() matlab function
  68.  * \li \c testOutput               points to output buffer calculation from cubic interpolation
  69.  * \li \c testLinIntOutput         points to output buffer calculation from linear interpolation
  70.  * \li \c snr1                     Signal to noise ratio for reference and cubic interpolation output
  71.  * \li \c snr2                     Signal to noise ratio for reference and linear interpolation output
  72.  *
  73.  * \par CMSIS DSP Software Library Functions Used:
  74.  * \par
  75.  * - arm_sin_f32()
  76.  * - arm_linear_interp_f32()
  77.  *
  78.  * <b> Refer  </b>
  79.  * \link arm_linear_interp_example_f32.c \endlink
  80.  *
  81.  */
  82.  
  83.  
  84. /** \example arm_linear_interp_example_f32.c
  85.   */
  86.  
  87. #include "arm_math.h"
  88. #include "math_helper.h"
  89.  
  90. #define SNR_THRESHOLD           90
  91. #define TEST_LENGTH_SAMPLES     10
  92. #define XSPACING               (0.00005f)
  93.  
  94. /* ----------------------------------------------------------------------
  95. * Test input data for F32 SIN function
  96. * Generated by the MATLAB rand() function
  97. * randn('state', 0)
  98. * xi = (((1/4.18318581819710)* randn(blockSize, 1) * 2* pi));
  99. * --------------------------------------------------------------------*/
  100. float32_t testInputSin_f32[TEST_LENGTH_SAMPLES] =
  101. {
  102.    -0.649716504673081170, -2.501723745497831200,
  103.     0.188250329003310100,  0.432092748487532540,
  104.    -1.722010988459680800,  1.788766476323060600,
  105.     1.786136060975809500, -0.056525543169408797,
  106.     0.491596272728153760,  0.262309671126153390
  107. };
  108.  
  109. /*------------------------------------------------------------------------------
  110. *  Reference out of SIN F32 function for Block Size = 10
  111. *  Calculated from sin(testInputSin_f32)
  112. *------------------------------------------------------------------------------*/
  113. float32_t testRefSinOutput32_f32[TEST_LENGTH_SAMPLES] =
  114. {
  115.    -0.604960695383043530, -0.597090287967934840,
  116.     0.187140422442966500,  0.418772124875992690,
  117.    -0.988588831792106880,  0.976338412038794010,
  118.     0.976903856413481100, -0.056495446835214236,
  119.     0.472033731854734240,  0.259311907228582830
  120. };
  121.  
  122. /*------------------------------------------------------------------------------
  123. *  Method 1: Test out Buffer Calculated from Cubic Interpolation
  124. *------------------------------------------------------------------------------*/
  125. float32_t testOutput[TEST_LENGTH_SAMPLES];
  126.  
  127. /*------------------------------------------------------------------------------
  128. *  Method 2: Test out buffer Calculated from Linear Interpolation
  129. *------------------------------------------------------------------------------*/
  130. float32_t testLinIntOutput[TEST_LENGTH_SAMPLES];
  131.  
  132. /*------------------------------------------------------------------------------
  133. *  External table used for linear interpolation
  134. *------------------------------------------------------------------------------*/
  135. extern float arm_linear_interep_table[188495];
  136.  
  137. /* ----------------------------------------------------------------------
  138. * Global Variables for caluclating SNR's for Method1 & Method 2
  139. * ------------------------------------------------------------------- */
  140. float32_t snr1;
  141. float32_t snr2;
  142.  
  143. /* ----------------------------------------------------------------------------
  144. * Calculation of Sine values from Cubic Interpolation and Linear interpolation
  145. * ---------------------------------------------------------------------------- */
  146. int32_t main(void)
  147. {
  148.   uint32_t i;
  149.   arm_status status;
  150.  
  151.   arm_linear_interp_instance_f32 S = {188495, -3.141592653589793238, XSPACING, &arm_linear_interep_table[0]};
  152.  
  153.   /*------------------------------------------------------------------------------
  154.   *  Method 1: Test out Calculated from Cubic Interpolation
  155.   *------------------------------------------------------------------------------*/
  156.   for(i=0; i< TEST_LENGTH_SAMPLES; i++)
  157.   {
  158.     testOutput[i] = arm_sin_f32(testInputSin_f32[i]);
  159.   }
  160.  
  161.   /*------------------------------------------------------------------------------
  162.   *  Method 2: Test out Calculated from Cubic Interpolation and Linear interpolation
  163.   *------------------------------------------------------------------------------*/
  164.  
  165.   for(i=0; i< TEST_LENGTH_SAMPLES; i++)
  166.   {
  167.       testLinIntOutput[i] = arm_linear_interp_f32(&S, testInputSin_f32[i]);
  168.   }
  169.  
  170.   /*------------------------------------------------------------------------------
  171.   *            SNR calculation for method 1
  172.   *------------------------------------------------------------------------------*/
  173.   snr1 = arm_snr_f32(testRefSinOutput32_f32, testOutput, 2);
  174.  
  175.   /*------------------------------------------------------------------------------
  176.   *            SNR calculation for method 2
  177.   *------------------------------------------------------------------------------*/
  178.   snr2 = arm_snr_f32(testRefSinOutput32_f32, testLinIntOutput, 2);
  179.  
  180.   /*------------------------------------------------------------------------------
  181.   *            Initialise status depending on SNR calculations
  182.   *------------------------------------------------------------------------------*/
  183.   if ( snr2 > snr1)
  184.   {
  185.     status = ARM_MATH_SUCCESS;
  186.   }
  187.   else
  188.   {
  189.     status = ARM_MATH_TEST_FAILURE;
  190.   }
  191.  
  192.   /* ----------------------------------------------------------------------
  193.   ** Loop here if the signals fail the PASS check.
  194.   ** This denotes a test failure
  195.   ** ------------------------------------------------------------------- */
  196.   if ( status != ARM_MATH_SUCCESS)
  197.   {
  198.     while (1);
  199.   }
  200.  
  201.   while (1);                             /* main function does not return */
  202. }
  203.  
  204.  /** \endlink */
  205.