1 /* ----------------------------------------------------------------------
2  * Project:      CMSIS DSP Library
3  * Title:        arm_linear_interp_f32.c
4  * Description:  Floating-point linear interpolation
5  *
6  * $Date:        23 April 2021
7  * $Revision:    V1.9.0
8  *
9  * Target Processor: Cortex-M and Cortex-A cores
10  * -------------------------------------------------------------------- */
11 /*
12  * Copyright (C) 2010-2021 ARM Limited or its affiliates. All rights reserved.
13  *
14  * SPDX-License-Identifier: Apache-2.0
15  *
16  * Licensed under the Apache License, Version 2.0 (the License); you may
17  * not use this file except in compliance with the License.
18  * You may obtain a copy of the License at
19  *
20  * www.apache.org/licenses/LICENSE-2.0
21  *
22  * Unless required by applicable law or agreed to in writing, software
23  * distributed under the License is distributed on an AS IS BASIS, WITHOUT
24  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
25  * See the License for the specific language governing permissions and
26  * limitations under the License.
27  */
28 
29 #include "dsp/interpolation_functions.h"
30 
31 /**
32   @ingroup groupInterpolation
33  */
34 
35 /**
36    * @defgroup LinearInterpolate Linear Interpolation
37    *
38    * Linear interpolation is a method of curve fitting using linear polynomials.
39    * Linear interpolation works by effectively drawing a straight line between two neighboring samples and returning the appropriate point along that line
40    *
41    * \par
42    * \image html LinearInterp.gif "Linear interpolation"
43    *
44    * \par
45    * A  Linear Interpolate function calculates an output value(y), for the input(x)
46    * using linear interpolation of the input values x0, x1( nearest input values) and the output values y0 and y1(nearest output values)
47    *
48    * \par Algorithm:
49    * <pre>
50    *       y = y0 + (x - x0) * ((y1 - y0)/(x1-x0))
51    *       where x0, x1 are nearest values of input x
52    *             y0, y1 are nearest values to output y
53    * </pre>
54    *
55    * \par
56    * This set of functions implements Linear interpolation process
57    * for Q7, Q15, Q31, and floating-point data types.  The functions operate on a single
58    * sample of data and each call to the function returns a single processed value.
59    * <code>S</code> points to an instance of the Linear Interpolate function data structure.
60    * <code>x</code> is the input sample value. The functions returns the output value.
61    *
62    * \par
63    * if x is outside of the table boundary, Linear interpolation returns first value of the table
64    * if x is below input range and returns last value of table if x is above range.
65    */
66 
67 /**
68    * @addtogroup LinearInterpolate
69    * @{
70    */
71 
72   /**
73    * @brief  Process function for the floating-point Linear Interpolation Function.
74    * @param[in,out] S  is an instance of the floating-point Linear Interpolation structure
75    * @param[in]     x  input sample to process
76    * @return y processed output sample.
77    *
78    */
arm_linear_interp_f32(arm_linear_interp_instance_f32 * S,float32_t x)79   float32_t arm_linear_interp_f32(
80   arm_linear_interp_instance_f32 * S,
81   float32_t x)
82   {
83     float32_t y;
84     float32_t x0, x1;                            /* Nearest input values */
85     float32_t y0, y1;                            /* Nearest output values */
86     float32_t xSpacing = S->xSpacing;            /* spacing between input values */
87     int32_t i;                                   /* Index variable */
88     const float32_t *pYData = S->pYData;               /* pointer to output table */
89 
90     /* Calculation of index */
91     i = (int32_t) ((x - S->x1) / xSpacing);
92 
93     if (x < S->x1)
94     {
95       /* Iniatilize output for below specified range as least output value of table */
96       y = pYData[0];
97     }
98     else if ((uint32_t)i >= (S->nValues - 1))
99     {
100       /* Iniatilize output for above specified range as last output value of table */
101       y = pYData[S->nValues - 1];
102     }
103     else
104     {
105       /* Calculation of nearest input values */
106       x0 = S->x1 +  i      * xSpacing;
107       x1 = S->x1 + (i + 1) * xSpacing;
108 
109       /* Read of nearest output values */
110       y0 = pYData[i];
111       y1 = pYData[i + 1];
112 
113       /* Calculation of output */
114       y = y0 + (x - x0) * ((y1 - y0) / (x1 - x0));
115 
116     }
117 
118     /* returns output value */
119     return (y);
120   }
121 
122   /**
123    * @} end of LinearInterpolate group
124    */
125 
126