arm_linear_interp_f32.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /* ----------------------------------------------------------------------
  2. * Project: CMSIS DSP Library
  3. * Title: arm_linear_interp_f32.c
  4. * Description: Floating-point 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. * @defgroup LinearInterpolate Linear Interpolation
  34. *
  35. * Linear interpolation is a method of curve fitting using linear polynomials.
  36. * Linear interpolation works by effectively drawing a straight line between two neighboring samples and returning the appropriate point along that line
  37. *
  38. * \par
  39. * \image html LinearInterp.gif "Linear interpolation"
  40. *
  41. * \par
  42. * A Linear Interpolate function calculates an output value(y), for the input(x)
  43. * using linear interpolation of the input values x0, x1( nearest input values) and the output values y0 and y1(nearest output values)
  44. *
  45. * \par Algorithm:
  46. * <pre>
  47. * y = y0 + (x - x0) * ((y1 - y0)/(x1-x0))
  48. * where x0, x1 are nearest values of input x
  49. * y0, y1 are nearest values to output y
  50. * </pre>
  51. *
  52. * \par
  53. * This set of functions implements Linear interpolation process
  54. * for Q7, Q15, Q31, and floating-point data types. The functions operate on a single
  55. * sample of data and each call to the function returns a single processed value.
  56. * <code>S</code> points to an instance of the Linear Interpolate function data structure.
  57. * <code>x</code> is the input sample value. The functions returns the output value.
  58. *
  59. * \par
  60. * if x is outside of the table boundary, Linear interpolation returns first value of the table
  61. * if x is below input range and returns last value of table if x is above range.
  62. */
  63. /**
  64. * @addtogroup LinearInterpolate
  65. * @{
  66. */
  67. /**
  68. * @brief Process function for the floating-point Linear Interpolation Function.
  69. * @param[in,out] S is an instance of the floating-point Linear Interpolation structure
  70. * @param[in] x input sample to process
  71. * @return y processed output sample.
  72. *
  73. */
  74. float32_t arm_linear_interp_f32(
  75. arm_linear_interp_instance_f32 * S,
  76. float32_t x)
  77. {
  78. float32_t y;
  79. float32_t x0, x1; /* Nearest input values */
  80. float32_t y0, y1; /* Nearest output values */
  81. float32_t xSpacing = S->xSpacing; /* spacing between input values */
  82. int32_t i; /* Index variable */
  83. float32_t *pYData = S->pYData; /* pointer to output table */
  84. /* Calculation of index */
  85. i = (int32_t) ((x - S->x1) / xSpacing);
  86. if (i < 0)
  87. {
  88. /* Iniatilize output for below specified range as least output value of table */
  89. y = pYData[0];
  90. }
  91. else if ((uint32_t)i >= (S->nValues - 1))
  92. {
  93. /* Iniatilize output for above specified range as last output value of table */
  94. y = pYData[S->nValues - 1];
  95. }
  96. else
  97. {
  98. /* Calculation of nearest input values */
  99. x0 = S->x1 + i * xSpacing;
  100. x1 = S->x1 + (i + 1) * xSpacing;
  101. /* Read of nearest output values */
  102. y0 = pYData[i];
  103. y1 = pYData[i + 1];
  104. /* Calculation of output */
  105. y = y0 + (x - x0) * ((y1 - y0) / (x1 - x0));
  106. }
  107. /* returns output value */
  108. return (y);
  109. }
  110. /**
  111. * @} end of LinearInterpolate group
  112. */