arm_linear_interp_q31.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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 LinearInterpolate
  34. * @{
  35. */
  36. /**
  37. *
  38. * @brief Process function for the Q31 Linear Interpolation Function.
  39. * @param[in] pYData pointer to Q31 Linear Interpolation table
  40. * @param[in] x input sample to process
  41. * @param[in] nValues number of table values
  42. * @return y processed output sample.
  43. *
  44. * \par
  45. * Input sample <code>x</code> is in 12.20 format which contains 12 bits for table index and 20 bits for fractional part.
  46. * This function can support maximum of table size 2^12.
  47. *
  48. */
  49. q31_t arm_linear_interp_q31(
  50. const q31_t * pYData,
  51. q31_t x,
  52. uint32_t nValues)
  53. {
  54. q31_t y; /* output */
  55. q31_t y0, y1; /* Nearest output values */
  56. q31_t fract; /* fractional part */
  57. int32_t index; /* Index to read nearest output values */
  58. /* Input is in 12.20 format */
  59. /* 12 bits for the table index */
  60. /* Index value calculation */
  61. index = ((x & (q31_t)0xFFF00000) >> 20);
  62. if (index >= (int32_t)(nValues - 1))
  63. {
  64. return (pYData[nValues - 1]);
  65. }
  66. else if (index < 0)
  67. {
  68. return (pYData[0]);
  69. }
  70. else
  71. {
  72. /* 20 bits for the fractional part */
  73. /* shift left by 11 to keep fract in 1.31 format */
  74. fract = (x & 0x000FFFFF) << 11;
  75. /* Read two nearest output values from the index in 1.31(q31) format */
  76. y0 = pYData[index];
  77. y1 = pYData[index + 1];
  78. /* Calculation of y0 * (1-fract) and y is in 2.30 format */
  79. y = ((q31_t) ((q63_t) y0 * (0x7FFFFFFF - fract) >> 32));
  80. /* Calculation of y0 * (1-fract) + y1 *fract and y is in 2.30 format */
  81. y += ((q31_t) (((q63_t) y1 * fract) >> 32));
  82. /* Convert y to 1.31 format */
  83. return (y << 1U);
  84. }
  85. }
  86. /**
  87. * @} end of LinearInterpolate group
  88. */