arm_bilinear_interp_q7.c 3.9 KB

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