/ Drivers / CMSIS / DSP / Source / InterpolationFunctions / arm_linear_interp_q7.c
arm_linear_interp_q7.c
 1  /* ----------------------------------------------------------------------
 2   * Project:      CMSIS DSP Library
 3   * Title:        arm_linear_interp_q7.c
 4   * Description:  Q7 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  /**
37     * @addtogroup LinearInterpolate
38     * @{
39     */
40  
41    /**
42     *
43     * @brief  Process function for the Q7 Linear Interpolation Function.
44     * @param[in] pYData   pointer to Q7 Linear Interpolation table
45     * @param[in] x        input sample to process
46     * @param[in] nValues  number of table values
47     * @return y processed output sample.
48     *
49     * \par
50     * Input sample <code>x</code> is in 12.20 format which contains 12 bits for table index and 20 bits for fractional part.
51     * This function can support maximum of table size 2^12.
52     */
53    q7_t arm_linear_interp_q7(
54    const q7_t * pYData,
55    q31_t x,
56    uint32_t nValues)
57    {
58      q31_t y;                                     /* output */
59      q7_t y0, y1;                                 /* Nearest output values */
60      q31_t fract;                                 /* fractional part */
61      uint32_t index;                              /* Index to read nearest output values */
62  
63      /* Input is in 12.20 format */
64      /* 12 bits for the table index */
65      /* Index value calculation */
66      if (x < 0)
67      {
68        return (pYData[0]);
69      }
70      index = (x >> 20) & 0xfff;
71  
72      if (index >= (nValues - 1))
73      {
74        return (pYData[nValues - 1]);
75      }
76      else
77      {
78        /* 20 bits for the fractional part */
79        /* fract is in 12.20 format */
80        fract = (x & 0x000FFFFF);
81  
82        /* Read two nearest output values from the index and are in 1.7(q7) format */
83        y0 = pYData[index];
84        y1 = pYData[index + 1];
85  
86        /* Calculation of y0 * (1-fract ) and y is in 13.27(q27) format */
87        y = ((y0 * (0xFFFFF - fract)));
88  
89        /* Calculation of y1 * fract + y0 * (1-fract) and y is in 13.27(q27) format */
90        y += (y1 * fract);
91  
92        /* convert y to 1.7(q7) format */
93        return (q7_t) (y >> 20);
94       }
95    }
96    /**
97     * @} end of LinearInterpolate group
98     */
99