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  * @addtogroup groupExamples
45  * @{
46  *
47  * @defgroup LinearInterpExample Linear Interpolate Example
48  *
49  * <b> CMSIS DSP Software Library -- Linear Interpolate Example  </b>
50  *
51  * <b> Description </b>
52  * This example demonstrates usage of linear interpolate modules and fast math modules.
53  * Method 1 uses fast math sine function to calculate sine values using cubic interpolation and method 2 uses
54  * linear interpolation function and results are compared to reference output.
55  * Example shows linear interpolation function can be used to get higher precision compared to fast math sin calculation.
56  *
57  * \par Block Diagram:
58  * \par
59  * \image html linearInterpExampleMethod1.gif "Method 1: Sine caluclation using fast math"
60  * \par
61  * \image html linearInterpExampleMethod2.gif "Method 2: Sine caluclation using interpolation function"
62  *
63  * \par Variables Description:
64  * \par
65  * \li \c testInputSin_f32         points to the input values for sine calculation
66  * \li \c testRefSinOutput32_f32   points to the reference values caculated from sin() matlab function
67  * \li \c testOutput               points to output buffer calculation from cubic interpolation
68  * \li \c testLinIntOutput         points to output buffer calculation from linear interpolation
69  * \li \c snr1                     Signal to noise ratio for reference and cubic interpolation output
70  * \li \c snr2                     Signal to noise ratio for reference and linear interpolation output
71  *
72  * \par CMSIS DSP Software Library Functions Used:
73  * \par
74  * - arm_sin_f32()
75  * - arm_linear_interp_f32()
76  *
77  * <b> Refer  </b>
78  * \link arm_linear_interp_example_f32.c \endlink
79  *
80  * \example arm_linear_interp_example_f32.c
81  *
82  * @} */
83 
84 #include "arm_math.h"
85 #include "math_helper.h"
86 
87 #if defined(SEMIHOSTING)
88 #include <stdio.h>
89 #endif
90 
91 #define SNR_THRESHOLD           90
92 #define TEST_LENGTH_SAMPLES     10
93 #define XSPACING               (0.005f)
94 
95 /* ----------------------------------------------------------------------
96 * Test input data for F32 SIN function
97 * Generated by the MATLAB rand() function
98 * randn('state', 0)
99 * xi = (((1/4.18318581819710)* randn(blockSize, 1) * 2* pi));
100 * --------------------------------------------------------------------*/
101 float32_t testInputSin_f32[TEST_LENGTH_SAMPLES] =
102 {
103    -0.649716504673081170, -2.501723745497831200,
104     0.188250329003310100,  0.432092748487532540,
105    -1.722010988459680800,  1.788766476323060600,
106     1.786136060975809500, -0.056525543169408797,
107     0.491596272728153760,  0.262309671126153390
108 };
109 
110 /*------------------------------------------------------------------------------
111 *  Reference out of SIN F32 function for Block Size = 10
112 *  Calculated from sin(testInputSin_f32)
113 *------------------------------------------------------------------------------*/
114 float32_t testRefSinOutput32_f32[TEST_LENGTH_SAMPLES] =
115 {
116    -0.604960695383043530, -0.597090287967934840,
117     0.187140422442966500,  0.418772124875992690,
118    -0.988588831792106880,  0.976338412038794010,
119     0.976903856413481100, -0.056495446835214236,
120     0.472033731854734240,  0.259311907228582830
121 };
122 
123 /*------------------------------------------------------------------------------
124 *  Method 1: Test out Buffer Calculated from Cubic Interpolation
125 *------------------------------------------------------------------------------*/
126 float32_t testOutput[TEST_LENGTH_SAMPLES];
127 
128 /*------------------------------------------------------------------------------
129 *  Method 2: Test out buffer Calculated from Linear Interpolation
130 *------------------------------------------------------------------------------*/
131 float32_t testLinIntOutput[TEST_LENGTH_SAMPLES];
132 
133 /*------------------------------------------------------------------------------
134 *  External table used for linear interpolation
135 *------------------------------------------------------------------------------*/
136 extern const float arm_linear_interep_table[1884];
137 
138 /* ----------------------------------------------------------------------
139 * Global Variables for caluclating SNR's for Method1 & Method 2
140 * ------------------------------------------------------------------- */
141 float32_t snr1;
142 float32_t snr2;
143 
144 /* ----------------------------------------------------------------------------
145 * Calculation of Sine values from Cubic Interpolation and Linear interpolation
146 * ---------------------------------------------------------------------------- */
main(void)147 int32_t main(void)
148 {
149   uint32_t i;
150   arm_status status;
151 
152   arm_linear_interp_instance_f32 S = {1884, -3.141592653589793238, XSPACING, (float*)&arm_linear_interep_table[0]};
153 
154   /*------------------------------------------------------------------------------
155   *  Method 1: Test out Calculated from Cubic Interpolation
156   *------------------------------------------------------------------------------*/
157   for(i=0; i< TEST_LENGTH_SAMPLES; i++)
158   {
159     testOutput[i] = arm_sin_f32(testInputSin_f32[i]);
160   }
161 
162   /*------------------------------------------------------------------------------
163   *  Method 2: Test out Calculated from Cubic Interpolation and Linear interpolation
164   *------------------------------------------------------------------------------*/
165 
166   for(i=0; i< TEST_LENGTH_SAMPLES; i++)
167   {
168       testLinIntOutput[i] = arm_linear_interp_f32(&S, testInputSin_f32[i]);
169   }
170 
171   /*------------------------------------------------------------------------------
172   *            SNR calculation for method 1
173   *------------------------------------------------------------------------------*/
174   snr1 = arm_snr_f32(testRefSinOutput32_f32, testOutput, 2);
175 
176   /*------------------------------------------------------------------------------
177   *            SNR calculation for method 2
178   *------------------------------------------------------------------------------*/
179   snr2 = arm_snr_f32(testRefSinOutput32_f32, testLinIntOutput, 2);
180 
181   /*------------------------------------------------------------------------------
182   *            Initialise status depending on SNR calculations
183   *------------------------------------------------------------------------------*/
184   status = (snr2 <= snr1) ? ARM_MATH_TEST_FAILURE : ARM_MATH_SUCCESS;
185 
186   if (status != ARM_MATH_SUCCESS)
187   {
188 #if defined (SEMIHOSTING)
189     printf("FAILURE\n");
190 #else
191     while (1);                             /* main function does not return */
192 #endif
193   }
194   else
195   {
196 #if defined (SEMIHOSTING)
197     printf("SUCCESS\n");
198 #else
199     while (1);                             /* main function does not return */
200 #endif
201   }
202 
203 }
204 
205  /** \endlink */
206