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