/ Drivers / CMSIS / DSP / Source / InterpolationFunctions / arm_linear_interp_f16.c
arm_linear_interp_f16.c
  1  /* ----------------------------------------------------------------------
  2   * Project:      CMSIS DSP Library
  3   * Title:        arm_linear_interp_f16.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_f16.h"
 30  
 31  #if defined(ARM_FLOAT16_SUPPORTED)
 32  
 33  
 34  /**
 35    @ingroup groupInterpolation
 36   */
 37  
 38  /**
 39     * @defgroup LinearInterpolate Linear Interpolation
 40     *
 41     * Linear interpolation is a method of curve fitting using linear polynomials.
 42     * Linear interpolation works by effectively drawing a straight line between two neighboring samples and returning the appropriate point along that line
 43     *
 44     * \par
 45     * \image html LinearInterp.gif "Linear interpolation"
 46     *
 47     * \par
 48     * A  Linear Interpolate function calculates an output value(y), for the input(x)
 49     * using linear interpolation of the input values x0, x1( nearest input values) and the output values y0 and y1(nearest output values)
 50     *
 51     * \par Algorithm:
 52     * <pre>
 53     *       y = y0 + (x - x0) * ((y1 - y0)/(x1-x0))
 54     *       where x0, x1 are nearest values of input x
 55     *             y0, y1 are nearest values to output y
 56     * </pre>
 57     *
 58     * \par
 59     * This set of functions implements Linear interpolation process
 60     * for Q7, Q15, Q31, and floating-point data types.  The functions operate on a single
 61     * sample of data and each call to the function returns a single processed value.
 62     * <code>S</code> points to an instance of the Linear Interpolate function data structure.
 63     * <code>x</code> is the input sample value. The functions returns the output value.
 64     *
 65     * \par
 66     * if x is outside of the table boundary, Linear interpolation returns first value of the table
 67     * if x is below input range and returns last value of table if x is above range.
 68     */
 69  
 70  /**
 71     * @addtogroup LinearInterpolate
 72     * @{
 73     */
 74  
 75    /**
 76     * @brief  Process function for the floating-point Linear Interpolation Function.
 77     * @param[in,out] S  is an instance of the floating-point Linear Interpolation structure
 78     * @param[in]     x  input sample to process
 79     * @return y processed output sample.
 80     *
 81     */
 82    float16_t arm_linear_interp_f16(
 83    arm_linear_interp_instance_f16 * S,
 84    float16_t x)
 85    {
 86      float16_t y;
 87      float16_t x0, x1;                            /* Nearest input values */
 88      float16_t y0, y1;                            /* Nearest output values */
 89      float16_t xSpacing = S->xSpacing;            /* spacing between input values */
 90      int32_t i;                                   /* Index variable */
 91      float16_t *pYData = S->pYData;               /* pointer to output table */
 92  
 93      /* Calculation of index */
 94      i = (int32_t) (((_Float16)x - (_Float16)S->x1) / (_Float16)xSpacing);
 95  
 96      if (i < 0)
 97      {
 98        /* Iniatilize output for below specified range as least output value of table */
 99        y = pYData[0];
100      }
101      else if ((uint32_t)i >= (S->nValues - 1))
102      {
103        /* Iniatilize output for above specified range as last output value of table */
104        y = pYData[S->nValues - 1];
105      }
106      else
107      {
108        /* Calculation of nearest input values */
109        x0 = (_Float16)S->x1 +  (_Float16)i      * (_Float16)xSpacing;
110        x1 = (_Float16)S->x1 + (_Float16)(i + 1) * (_Float16)xSpacing;
111  
112        /* Read of nearest output values */
113        y0 = pYData[i];
114        y1 = pYData[i + 1];
115  
116        /* Calculation of output */
117        y = (_Float16)y0 + ((_Float16)x - (_Float16)x0) * 
118        (((_Float16)y1 - (_Float16)y0) / ((_Float16)x1 - (_Float16)x0));
119  
120      }
121  
122      /* returns output value */
123      return (y);
124    }
125  
126    /**
127     * @} end of LinearInterpolate group
128     */
129  
130  
131  #endif /* #if defined(ARM_FLOAT16_SUPPORTED) */ 
132