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