/ Drivers / CMSIS / DSP / Source / InterpolationFunctions / arm_bilinear_interp_q31.c
arm_bilinear_interp_q31.c
  1  /* ----------------------------------------------------------------------
  2   * Project:      CMSIS DSP Library
  3   * Title:        arm_linear_interp_q31.c
  4   * Description:  Q31 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  Q31 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    q31_t arm_bilinear_interp_q31(
 49    arm_bilinear_interp_instance_q31 * S,
 50    q31_t X,
 51    q31_t Y)
 52    {
 53      q31_t out;                                   /* Temporary output */
 54      q31_t acc = 0;                               /* output */
 55      q31_t xfract, yfract;                        /* X, Y fractional parts */
 56      q31_t x1, x2, y1, y2;                        /* Nearest output values */
 57      int32_t rI, cI;                              /* Row and column indices */
 58      q31_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      /* shift left xfract by 11 to keep 1.31 format */
 80      xfract = (X & 0x000FFFFF) << 11U;
 81  
 82      /* Read two nearest output values from the index */
 83      x1 = pYData[(rI) + (int32_t)nCols * (cI)    ];
 84      x2 = pYData[(rI) + (int32_t)nCols * (cI) + 1];
 85  
 86      /* 20 bits for the fractional part */
 87      /* shift left yfract by 11 to keep 1.31 format */
 88      yfract = (Y & 0x000FFFFF) << 11U;
 89  
 90      /* Read two nearest output values from the index */
 91      y1 = pYData[(rI) + (int32_t)nCols * (cI + 1)    ];
 92      y2 = pYData[(rI) + (int32_t)nCols * (cI + 1) + 1];
 93  
 94      /* Calculation of x1 * (1-xfract ) * (1-yfract) and acc is in 3.29(q29) format */
 95      out = ((q31_t) (((q63_t) x1  * (0x7FFFFFFF - xfract)) >> 32));
 96      acc = ((q31_t) (((q63_t) out * (0x7FFFFFFF - yfract)) >> 32));
 97  
 98      /* x2 * (xfract) * (1-yfract)  in 3.29(q29) and adding to acc */
 99      out = ((q31_t) ((q63_t) x2 * (0x7FFFFFFF - yfract) >> 32));
100      acc += ((q31_t) ((q63_t) out * (xfract) >> 32));
101  
102      /* y1 * (1 - xfract) * (yfract)  in 3.29(q29) and adding to acc */
103      out = ((q31_t) ((q63_t) y1 * (0x7FFFFFFF - xfract) >> 32));
104      acc += ((q31_t) ((q63_t) out * (yfract) >> 32));
105  
106      /* y2 * (xfract) * (yfract)  in 3.29(q29) and adding to acc */
107      out = ((q31_t) ((q63_t) y2 * (xfract) >> 32));
108      acc += ((q31_t) ((q63_t) out * (yfract) >> 32));
109  
110      /* Convert acc to 1.31(q31) format */
111      return ((q31_t)(acc << 2));
112    }
113  
114  
115  
116    /**
117     * @} end of BilinearInterpolate group
118     */
119