/ Drivers / CMSIS / DSP / Examples / ARM / arm_linear_interp_example / arm_linear_interp_example_f32.c
arm_linear_interp_example_f32.c
  1  /* ----------------------------------------------------------------------
  2  * Copyright (C) 2010-2020 ARM Limited. All rights reserved.
  3  *
  4  * $Date:         23. March 2020
  5  * $Revision:     V1.7.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-M55/Cortex-M7/Cortex-M4/Cortex-M3/Cortex-M0
 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  #if defined(SEMIHOSTING)
 91  #include <stdio.h>
 92  #endif
 93  
 94  #define SNR_THRESHOLD           90
 95  #define TEST_LENGTH_SAMPLES     10
 96  #define XSPACING               (0.005f)
 97  
 98  /* ----------------------------------------------------------------------
 99  * Test input data for F32 SIN function
100  * Generated by the MATLAB rand() function
101  * randn('state', 0)
102  * xi = (((1/4.18318581819710)* randn(blockSize, 1) * 2* pi));
103  * --------------------------------------------------------------------*/
104  float32_t testInputSin_f32[TEST_LENGTH_SAMPLES] =
105  {
106     -0.649716504673081170, -2.501723745497831200,
107      0.188250329003310100,  0.432092748487532540,
108     -1.722010988459680800,  1.788766476323060600,
109      1.786136060975809500, -0.056525543169408797,
110      0.491596272728153760,  0.262309671126153390
111  };
112  
113  /*------------------------------------------------------------------------------
114  *  Reference out of SIN F32 function for Block Size = 10
115  *  Calculated from sin(testInputSin_f32)
116  *------------------------------------------------------------------------------*/
117  float32_t testRefSinOutput32_f32[TEST_LENGTH_SAMPLES] =
118  {
119     -0.604960695383043530, -0.597090287967934840,
120      0.187140422442966500,  0.418772124875992690,
121     -0.988588831792106880,  0.976338412038794010,
122      0.976903856413481100, -0.056495446835214236,
123      0.472033731854734240,  0.259311907228582830
124  };
125  
126  /*------------------------------------------------------------------------------
127  *  Method 1: Test out Buffer Calculated from Cubic Interpolation
128  *------------------------------------------------------------------------------*/
129  float32_t testOutput[TEST_LENGTH_SAMPLES];
130  
131  /*------------------------------------------------------------------------------
132  *  Method 2: Test out buffer Calculated from Linear Interpolation
133  *------------------------------------------------------------------------------*/
134  float32_t testLinIntOutput[TEST_LENGTH_SAMPLES];
135  
136  /*------------------------------------------------------------------------------
137  *  External table used for linear interpolation
138  *------------------------------------------------------------------------------*/
139  extern const float arm_linear_interep_table[1884];
140  
141  /* ----------------------------------------------------------------------
142  * Global Variables for caluclating SNR's for Method1 & Method 2
143  * ------------------------------------------------------------------- */
144  float32_t snr1;
145  float32_t snr2;
146  
147  /* ----------------------------------------------------------------------------
148  * Calculation of Sine values from Cubic Interpolation and Linear interpolation
149  * ---------------------------------------------------------------------------- */
150  int32_t main(void)
151  {
152    uint32_t i;
153    arm_status status;
154  
155    arm_linear_interp_instance_f32 S = {1884, -3.141592653589793238, XSPACING, (float*)&arm_linear_interep_table[0]};
156  
157    /*------------------------------------------------------------------------------
158    *  Method 1: Test out Calculated from Cubic Interpolation
159    *------------------------------------------------------------------------------*/
160    for(i=0; i< TEST_LENGTH_SAMPLES; i++)
161    {
162      testOutput[i] = arm_sin_f32(testInputSin_f32[i]);
163    }
164  
165    /*------------------------------------------------------------------------------
166    *  Method 2: Test out Calculated from Cubic Interpolation and Linear interpolation
167    *------------------------------------------------------------------------------*/
168  
169    for(i=0; i< TEST_LENGTH_SAMPLES; i++)
170    {
171        testLinIntOutput[i] = arm_linear_interp_f32(&S, testInputSin_f32[i]);
172    }
173  
174    /*------------------------------------------------------------------------------
175    *            SNR calculation for method 1
176    *------------------------------------------------------------------------------*/
177    snr1 = arm_snr_f32(testRefSinOutput32_f32, testOutput, 2);
178  
179    /*------------------------------------------------------------------------------
180    *            SNR calculation for method 2
181    *------------------------------------------------------------------------------*/
182    snr2 = arm_snr_f32(testRefSinOutput32_f32, testLinIntOutput, 2);
183  
184    /*------------------------------------------------------------------------------
185    *            Initialise status depending on SNR calculations
186    *------------------------------------------------------------------------------*/
187    status = (snr2 <= snr1) ? ARM_MATH_TEST_FAILURE : ARM_MATH_SUCCESS;
188  
189    if (status != ARM_MATH_SUCCESS)
190    {
191  #if defined (SEMIHOSTING)
192      printf("FAILURE\n");
193  #else
194      while (1);                             /* main function does not return */
195  #endif
196    }
197    else
198    {
199  #if defined (SEMIHOSTING)
200      printf("SUCCESS\n");
201  #else
202      while (1);                             /* main function does not return */
203  #endif
204    }
205  
206  }
207  
208   /** \endlink */