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 BilinearInterpolate
37    * @{
38    */
39 
40   /**
41   * @brief  Q15 bilinear interpolation.
42   * @param[in,out] S  points to an instance of the interpolation structure.
43   * @param[in]     X  interpolation coordinate in 12.20 format.
44   * @param[in]     Y  interpolation coordinate in 12.20 format.
45   * @return out interpolated value.
46   */
arm_bilinear_interp_q15(arm_bilinear_interp_instance_q15 * S,q31_t X,q31_t Y)47   q15_t arm_bilinear_interp_q15(
48   arm_bilinear_interp_instance_q15 * S,
49   q31_t X,
50   q31_t Y)
51   {
52     q63_t acc = 0;                               /* output */
53     q31_t out;                                   /* Temporary output */
54     q15_t x1, x2, y1, y2;                        /* Nearest output values */
55     q31_t xfract, yfract;                        /* X, Y fractional parts */
56     int32_t rI, cI;                              /* Row and column indices */
57     q15_t *pYData = S->pData;                    /* pointer to output table values */
58     uint32_t nCols = S->numCols;                 /* num of rows */
59 
60     /* Input is in 12.20 format */
61     /* 12 bits for the table index */
62     /* Index value calculation */
63     rI = ((X & (q31_t)0xFFF00000) >> 20);
64 
65     /* Input is in 12.20 format */
66     /* 12 bits for the table index */
67     /* Index value calculation */
68     cI = ((Y & (q31_t)0xFFF00000) >> 20);
69 
70     /* Care taken for table outside boundary */
71     /* Returns zero output when values are outside table boundary */
72     if (rI < 0 || rI > (S->numCols - 2) || cI < 0 || cI > (S->numRows - 2))
73     {
74       return (0);
75     }
76 
77     /* 20 bits for the fractional part */
78     /* xfract should be in 12.20 format */
79     xfract = (X & 0x000FFFFF);
80 
81     /* Read two nearest output values from the index */
82     x1 = pYData[((uint32_t)rI) + nCols * ((uint32_t)cI)    ];
83     x2 = pYData[((uint32_t)rI) + nCols * ((uint32_t)cI) + 1];
84 
85     /* 20 bits for the fractional part */
86     /* yfract should be in 12.20 format */
87     yfract = (Y & 0x000FFFFF);
88 
89     /* Read two nearest output values from the index */
90     y1 = pYData[((uint32_t)rI) + nCols * ((uint32_t)cI + 1)    ];
91     y2 = pYData[((uint32_t)rI) + nCols * ((uint32_t)cI + 1) + 1];
92 
93     /* Calculation of x1 * (1-xfract ) * (1-yfract) and acc is in 13.51 format */
94 
95     /* x1 is in 1.15(q15), xfract in 12.20 format and out is in 13.35 format */
96     /* convert 13.35 to 13.31 by right shifting  and out is in 1.31 */
97     out = (q31_t) (((q63_t) x1 * (0x0FFFFF - xfract)) >> 4U);
98     acc = ((q63_t) out * (0x0FFFFF - yfract));
99 
100     /* x2 * (xfract) * (1-yfract)  in 1.51 and adding to acc */
101     out = (q31_t) (((q63_t) x2 * (0x0FFFFF - yfract)) >> 4U);
102     acc += ((q63_t) out * (xfract));
103 
104     /* y1 * (1 - xfract) * (yfract)  in 1.51 and adding to acc */
105     out = (q31_t) (((q63_t) y1 * (0x0FFFFF - xfract)) >> 4U);
106     acc += ((q63_t) out * (yfract));
107 
108     /* y2 * (xfract) * (yfract)  in 1.51 and adding to acc */
109     out = (q31_t) (((q63_t) y2 * (xfract)) >> 4U);
110     acc += ((q63_t) out * (yfract));
111 
112     /* acc is in 13.51 format and down shift acc by 36 times */
113     /* Convert out to 1.15 format */
114     return ((q15_t)(acc >> 36));
115   }
116 
117 
118   /**
119    * @} end of BilinearInterpolate group
120    */
121 
122