arm_bilinear_interp_f32.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /* ----------------------------------------------------------------------
  2. * Project: CMSIS DSP Library
  3. * Title: arm_bilinear_interp_f32.c
  4. * Description: Floating-point bilinear 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. * @defgroup BilinearInterpolate Bilinear Interpolation
  34. *
  35. * Bilinear interpolation is an extension of linear interpolation applied to a two dimensional grid.
  36. * The underlying function <code>f(x, y)</code> is sampled on a regular grid and the interpolation process
  37. * determines values between the grid points.
  38. * Bilinear interpolation is equivalent to two step linear interpolation, first in the x-dimension and then in the y-dimension.
  39. * Bilinear interpolation is often used in image processing to rescale images.
  40. * The CMSIS DSP library provides bilinear interpolation functions for Q7, Q15, Q31, and floating-point data types.
  41. *
  42. * <b>Algorithm</b>
  43. * \par
  44. * The instance structure used by the bilinear interpolation functions describes a two dimensional data table.
  45. * For floating-point, the instance structure is defined as:
  46. * <pre>
  47. * typedef struct
  48. * {
  49. * uint16_t numRows;
  50. * uint16_t numCols;
  51. * float32_t *pData;
  52. * } arm_bilinear_interp_instance_f32;
  53. * </pre>
  54. *
  55. * \par
  56. * where <code>numRows</code> specifies the number of rows in the table;
  57. * <code>numCols</code> specifies the number of columns in the table;
  58. * and <code>pData</code> points to an array of size <code>numRows*numCols</code> values.
  59. * The data table <code>pTable</code> is organized in row order and the supplied data values fall on integer indexes.
  60. * That is, table element (x,y) is located at <code>pTable[x + y*numCols]</code> where x and y are integers.
  61. *
  62. * \par
  63. * Let <code>(x, y)</code> specify the desired interpolation point. Then define:
  64. * <pre>
  65. * XF = floor(x)
  66. * YF = floor(y)
  67. * </pre>
  68. * \par
  69. * The interpolated output point is computed as:
  70. * <pre>
  71. * f(x, y) = f(XF, YF) * (1-(x-XF)) * (1-(y-YF))
  72. * + f(XF+1, YF) * (x-XF)*(1-(y-YF))
  73. * + f(XF, YF+1) * (1-(x-XF))*(y-YF)
  74. * + f(XF+1, YF+1) * (x-XF)*(y-YF)
  75. * </pre>
  76. * Note that the coordinates (x, y) contain integer and fractional components.
  77. * The integer components specify which portion of the table to use while the
  78. * fractional components control the interpolation processor.
  79. *
  80. * \par
  81. * if (x,y) are outside of the table boundary, Bilinear interpolation returns zero output.
  82. */
  83. /**
  84. * @addtogroup BilinearInterpolate
  85. * @{
  86. */
  87. /**
  88. * @brief Floating-point bilinear interpolation.
  89. * @param[in,out] S points to an instance of the interpolation structure.
  90. * @param[in] X interpolation coordinate.
  91. * @param[in] Y interpolation coordinate.
  92. * @return out interpolated value.
  93. */
  94. float32_t arm_bilinear_interp_f32(
  95. const arm_bilinear_interp_instance_f32 * S,
  96. float32_t X,
  97. float32_t Y)
  98. {
  99. float32_t out;
  100. float32_t f00, f01, f10, f11;
  101. float32_t *pData = S->pData;
  102. int32_t xIndex, yIndex, index;
  103. float32_t xdiff, ydiff;
  104. float32_t b1, b2, b3, b4;
  105. xIndex = (int32_t) X;
  106. yIndex = (int32_t) Y;
  107. /* Care taken for table outside boundary */
  108. /* Returns zero output when values are outside table boundary */
  109. if (xIndex < 0 || xIndex > (S->numCols - 2) || yIndex < 0 || yIndex > (S->numRows - 2))
  110. {
  111. return (0);
  112. }
  113. /* Calculation of index for two nearest points in X-direction */
  114. index = (xIndex ) + (yIndex ) * S->numCols;
  115. /* Read two nearest points in X-direction */
  116. f00 = pData[index];
  117. f01 = pData[index + 1];
  118. /* Calculation of index for two nearest points in Y-direction */
  119. index = (xIndex ) + (yIndex+1) * S->numCols;
  120. /* Read two nearest points in Y-direction */
  121. f10 = pData[index];
  122. f11 = pData[index + 1];
  123. /* Calculation of intermediate values */
  124. b1 = f00;
  125. b2 = f01 - f00;
  126. b3 = f10 - f00;
  127. b4 = f00 - f01 - f10 + f11;
  128. /* Calculation of fractional part in X */
  129. xdiff = X - xIndex;
  130. /* Calculation of fractional part in Y */
  131. ydiff = Y - yIndex;
  132. /* Calculation of bi-linear interpolated output */
  133. out = b1 + b2 * xdiff + b3 * ydiff + b4 * xdiff * ydiff;
  134. /* return to application */
  135. return (out);
  136. }
  137. /**
  138. * @} end of BilinearInterpolate group
  139. */