• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* ----------------------------------------------------------------------
2  * Project:      CMSIS DSP Library
3  * Title:        arm_linear_interp_q15.c
4  * Description:  Q15 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    * @addtogroup LinearInterpolate
37    * @{
38    */
39 
40   /**
41    *
42    * @brief  Process function for the Q15 Linear Interpolation Function.
43    * @param[in] pYData   pointer to Q15 Linear Interpolation table
44    * @param[in] x        input sample to process
45    * @param[in] nValues  number of table values
46    * @return y processed output sample.
47    *
48    * \par
49    * Input sample <code>x</code> is in 12.20 format which contains 12 bits for table index and 20 bits for fractional part.
50    * This function can support maximum of table size 2^12.
51    *
52    */
arm_linear_interp_q15(q15_t * pYData,q31_t x,uint32_t nValues)53   q15_t arm_linear_interp_q15(
54   q15_t * pYData,
55   q31_t x,
56   uint32_t nValues)
57   {
58     q63_t y;                                     /* output */
59     q15_t y0, y1;                                /* Nearest output values */
60     q31_t fract;                                 /* fractional part */
61     int32_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     index = ((x & (int32_t)0xFFF00000) >> 20);
67 
68     if (index >= (int32_t)(nValues - 1))
69     {
70       return (pYData[nValues - 1]);
71     }
72     else if (index < 0)
73     {
74       return (pYData[0]);
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 */
83       y0 = pYData[index];
84       y1 = pYData[index + 1];
85 
86       /* Calculation of y0 * (1-fract) and y is in 13.35 format */
87       y = ((q63_t) y0 * (0xFFFFF - fract));
88 
89       /* Calculation of (y0 * (1-fract) + y1 * fract) and y is in 13.35 format */
90       y += ((q63_t) y1 * (fract));
91 
92       /* convert y to 1.15 format */
93       return (q15_t) (y >> 20);
94     }
95   }
96 
97 
98   /**
99    * @} end of LinearInterpolate group
100    */
101 
102