arm_bilinear_interp_q31.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. #include "dsp/interpolation_functions.h"
  29. /**
  30. @ingroup groupInterpolation
  31. */
  32. /**
  33. * @addtogroup BilinearInterpolate
  34. * @{
  35. */
  36. /**
  37. * @brief Q31 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. q31_t arm_bilinear_interp_q31(
  44. arm_bilinear_interp_instance_q31 * S,
  45. q31_t X,
  46. q31_t Y)
  47. {
  48. q31_t out; /* Temporary output */
  49. q31_t acc = 0; /* output */
  50. q31_t xfract, yfract; /* X, Y fractional parts */
  51. q31_t x1, x2, y1, y2; /* Nearest output values */
  52. int32_t rI, cI; /* Row and column indices */
  53. q31_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. /* shift left xfract by 11 to keep 1.31 format */
  71. xfract = (X & 0x000FFFFF) << 11U;
  72. /* Read two nearest output values from the index */
  73. x1 = pYData[(rI) + (int32_t)nCols * (cI) ];
  74. x2 = pYData[(rI) + (int32_t)nCols * (cI) + 1];
  75. /* 20 bits for the fractional part */
  76. /* shift left yfract by 11 to keep 1.31 format */
  77. yfract = (Y & 0x000FFFFF) << 11U;
  78. /* Read two nearest output values from the index */
  79. y1 = pYData[(rI) + (int32_t)nCols * (cI + 1) ];
  80. y2 = pYData[(rI) + (int32_t)nCols * (cI + 1) + 1];
  81. /* Calculation of x1 * (1-xfract ) * (1-yfract) and acc is in 3.29(q29) format */
  82. out = ((q31_t) (((q63_t) x1 * (0x7FFFFFFF - xfract)) >> 32));
  83. acc = ((q31_t) (((q63_t) out * (0x7FFFFFFF - yfract)) >> 32));
  84. /* x2 * (xfract) * (1-yfract) in 3.29(q29) and adding to acc */
  85. out = ((q31_t) ((q63_t) x2 * (0x7FFFFFFF - yfract) >> 32));
  86. acc += ((q31_t) ((q63_t) out * (xfract) >> 32));
  87. /* y1 * (1 - xfract) * (yfract) in 3.29(q29) and adding to acc */
  88. out = ((q31_t) ((q63_t) y1 * (0x7FFFFFFF - xfract) >> 32));
  89. acc += ((q31_t) ((q63_t) out * (yfract) >> 32));
  90. /* y2 * (xfract) * (yfract) in 3.29(q29) and adding to acc */
  91. out = ((q31_t) ((q63_t) y2 * (xfract) >> 32));
  92. acc += ((q31_t) ((q63_t) out * (yfract) >> 32));
  93. /* Convert acc to 1.31(q31) format */
  94. return ((q31_t)(acc << 2));
  95. }
  96. /**
  97. * @} end of BilinearInterpolate group
  98. */