arm_linear_interp_q7.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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 LinearInterpolate
  34. * @{
  35. */
  36. /**
  37. *
  38. * @brief Process function for the Q7 Linear Interpolation Function.
  39. * @param[in] pYData pointer to Q7 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. q7_t arm_linear_interp_q7(
  49. const q7_t * pYData,
  50. q31_t x,
  51. uint32_t nValues)
  52. {
  53. q31_t y; /* output */
  54. q7_t y0, y1; /* Nearest output values */
  55. q31_t fract; /* fractional part */
  56. uint32_t index; /* Index to read nearest output values */
  57. /* Input is in 12.20 format */
  58. /* 12 bits for the table index */
  59. /* Index value calculation */
  60. if (x < 0)
  61. {
  62. return (pYData[0]);
  63. }
  64. index = (x >> 20) & 0xfff;
  65. if (index >= (nValues - 1))
  66. {
  67. return (pYData[nValues - 1]);
  68. }
  69. else
  70. {
  71. /* 20 bits for the fractional part */
  72. /* fract is in 12.20 format */
  73. fract = (x & 0x000FFFFF);
  74. /* Read two nearest output values from the index and are in 1.7(q7) format */
  75. y0 = pYData[index];
  76. y1 = pYData[index + 1];
  77. /* Calculation of y0 * (1-fract ) and y is in 13.27(q27) format */
  78. y = ((y0 * (0xFFFFF - fract)));
  79. /* Calculation of y1 * fract + y0 * (1-fract) and y is in 13.27(q27) format */
  80. y += (y1 * fract);
  81. /* convert y to 1.7(q7) format */
  82. return (q7_t) (y >> 20);
  83. }
  84. }
  85. /**
  86. * @} end of LinearInterpolate group
  87. */