arm_mfcc_q31.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. /* ----------------------------------------------------------------------
  2. * Project: CMSIS DSP Library
  3. * Title: arm_mfcc_q31.c
  4. * Description: MFCC function for the q31 version
  5. *
  6. * $Date: 07 September 2021
  7. * $Revision: V1.10.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/transform_functions.h"
  29. #include "dsp/statistics_functions.h"
  30. #include "dsp/basic_math_functions.h"
  31. #include "dsp/complex_math_functions.h"
  32. #include "dsp/fast_math_functions.h"
  33. #include "dsp/matrix_functions.h"
  34. /* Constants for Q31 implementation */
  35. #define LOG2TOLOG_Q31 0x02C5C860
  36. #define MICRO_Q31 0x08637BD0
  37. #define SHIFT_MELFILTER_SATURATION_Q31 10
  38. /**
  39. @ingroup groupTransforms
  40. */
  41. /**
  42. @defgroup MFCC MFCC
  43. MFCC Transform
  44. There are separate functions for floating-point, Q31, and Q31 data types.
  45. */
  46. /**
  47. @addtogroup MFCC
  48. @{
  49. */
  50. /**
  51. @brief MFCC Q31
  52. @param[in] S points to the mfcc instance structure
  53. @param[in] pSrc points to the input samples in Q31
  54. @param[out] pDst points to the output MFCC values in q8.23 format
  55. @param[inout] pTmp points to a temporary buffer of complex
  56. @return none
  57. @par Description
  58. The number of input samples is the FFT length used
  59. when initializing the instance data structure.
  60. The temporary buffer has a 2*fft length.
  61. The source buffer is modified by this function.
  62. The function may saturate. If the FFT length is too
  63. big and the number of MEL filters too small then the fixed
  64. point computations may saturate.
  65. */
  66. arm_status arm_mfcc_q31(
  67. const arm_mfcc_instance_q31 * S,
  68. q31_t *pSrc,
  69. q31_t *pDst,
  70. q31_t *pTmp
  71. )
  72. {
  73. q31_t m;
  74. uint32_t index;
  75. uint32_t fftShift=0;
  76. q31_t logExponent;
  77. q63_t result;
  78. arm_matrix_instance_q31 pDctMat;
  79. uint32_t i;
  80. uint32_t coefsPos;
  81. uint32_t filterLimit;
  82. q31_t *pTmp2=(q31_t*)pTmp;
  83. arm_status status = ARM_MATH_SUCCESS;
  84. // q31
  85. arm_absmax_q31(pSrc,S->fftLen,&m,&index);
  86. if (m !=0)
  87. {
  88. q31_t quotient;
  89. int16_t shift;
  90. status = arm_divide_q31(0x7FFFFFFF,m,&quotient,&shift);
  91. if (status != ARM_MATH_SUCCESS)
  92. {
  93. return(status);
  94. }
  95. arm_scale_q31(pSrc,quotient,shift,pSrc,S->fftLen);
  96. }
  97. // q31
  98. arm_mult_q31(pSrc,S->windowCoefs, pSrc, S->fftLen);
  99. /* Compute spectrum magnitude
  100. */
  101. fftShift = 31 - __CLZ(S->fftLen);
  102. #if defined(ARM_MFCC_CFFT_BASED)
  103. /* some HW accelerator for CMSIS-DSP used in some boards
  104. are only providing acceleration for CFFT.
  105. With ARM_MFCC_CFFT_BASED enabled, CFFT is used and the MFCC
  106. will be accelerated on those boards.
  107. The default is to use RFFT
  108. */
  109. /* Convert from real to complex */
  110. for(i=0; i < S->fftLen ; i++)
  111. {
  112. pTmp2[2*i] = pSrc[i];
  113. pTmp2[2*i+1] = 0;
  114. }
  115. arm_cfft_q31(&(S->cfft),pTmp2,0,1);
  116. #else
  117. /* Default RFFT based implementation */
  118. arm_rfft_q31(&(S->rfft),pSrc,pTmp2);
  119. #endif
  120. filterLimit = 1 + (S->fftLen >> 1);
  121. // q31 - fftShift
  122. arm_cmplx_mag_q31(pTmp2,pSrc,filterLimit);
  123. // q30 - fftShift
  124. /* Apply MEL filters */
  125. coefsPos = 0;
  126. for(i=0; i<S->nbMelFilters; i++)
  127. {
  128. arm_dot_prod_q31(pSrc+S->filterPos[i],
  129. &(S->filterCoefs[coefsPos]),
  130. S->filterLengths[i],
  131. &result);
  132. coefsPos += S->filterLengths[i];
  133. // q16.48 - fftShift
  134. result += MICRO_Q31;
  135. result >>= (SHIFT_MELFILTER_SATURATION_Q31 + 18);
  136. // q16.29 - fftShift - satShift
  137. pTmp[i] = __SSAT(result,31) ;
  138. }
  139. // q16.29 - fftShift - satShift
  140. /* Compute the log */
  141. arm_vlog_q31(pTmp,pTmp,S->nbMelFilters);
  142. // q5.26
  143. logExponent = fftShift + 2 + SHIFT_MELFILTER_SATURATION_Q31;
  144. logExponent = logExponent * LOG2TOLOG_Q31;
  145. // q5.26
  146. arm_offset_q31(pTmp,logExponent,pTmp,S->nbMelFilters);
  147. arm_shift_q31(pTmp,-3,pTmp,S->nbMelFilters);
  148. // q8.23
  149. pDctMat.numRows=S->nbDctOutputs;
  150. pDctMat.numCols=S->nbMelFilters;
  151. pDctMat.pData=(q31_t*)S->dctCoefs;
  152. arm_mat_vec_mult_q31(&pDctMat, pTmp, pDst);
  153. return(status);
  154. }
  155. /**
  156. @} end of MFCC group
  157. */