arm_mfcc_f32.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /* ----------------------------------------------------------------------
  2. * Project: CMSIS DSP Library
  3. * Title: arm_mfcc_f32.c
  4. * Description: MFCC function for the f32 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. /**
  35. @ingroup MFCC
  36. */
  37. /**
  38. @addtogroup MFCCF32
  39. @{
  40. */
  41. /**
  42. @brief MFCC F32
  43. @param[in] S points to the mfcc instance structure
  44. @param[in] pSrc points to the input samples
  45. @param[out] pDst points to the output MFCC values
  46. @param[inout] pTmp points to a temporary buffer of complex
  47. @par Description
  48. The number of input samples if the FFT length used
  49. when initializing the instance data structure.
  50. The temporary buffer has a 2*fft length size when MFCC
  51. is implemented with CFFT.
  52. It has length FFT Length + 2 when implemented with RFFT
  53. (default implementation).
  54. The source buffer is modified by this function.
  55. */
  56. void arm_mfcc_f32(
  57. const arm_mfcc_instance_f32 * S,
  58. float32_t *pSrc,
  59. float32_t *pDst,
  60. float32_t *pTmp
  61. )
  62. {
  63. float32_t maxValue;
  64. uint32_t index;
  65. uint32_t i;
  66. float32_t result;
  67. const float32_t *coefs=S->filterCoefs;
  68. arm_matrix_instance_f32 pDctMat;
  69. /* Normalize */
  70. arm_absmax_f32(pSrc,S->fftLen,&maxValue,&index);
  71. if (maxValue != 0.0f)
  72. {
  73. arm_scale_f32(pSrc,1.0f/maxValue,pSrc,S->fftLen);
  74. }
  75. /* Multiply by window */
  76. arm_mult_f32(pSrc,S->windowCoefs,pSrc,S->fftLen);
  77. /* Compute spectrum magnitude
  78. */
  79. #if defined(ARM_MFCC_CFFT_BASED)
  80. /* some HW accelerator for CMSIS-DSP used in some boards
  81. are only providing acceleration for CFFT.
  82. With ARM_MFCC_CFFT_BASED enabled, CFFT is used and the MFCC
  83. will be accelerated on those boards.
  84. The default is to use RFFT
  85. */
  86. /* Convert from real to complex */
  87. for(i=0; i < S->fftLen ; i++)
  88. {
  89. pTmp[2*i] = pSrc[i];
  90. pTmp[2*i+1] = 0.0f;
  91. }
  92. arm_cfft_f32(&(S->cfft),pTmp,0,1);
  93. #else
  94. /* Default RFFT based implementation */
  95. arm_rfft_fast_f32(&(S->rfft),pSrc,pTmp,0);
  96. /* Unpack real values */
  97. pTmp[S->fftLen]=pTmp[1];
  98. pTmp[S->fftLen+1]=0.0f;
  99. pTmp[1]=0.0f;
  100. #endif
  101. arm_cmplx_mag_f32(pTmp,pSrc,S->fftLen);
  102. if (maxValue != 0.0f)
  103. {
  104. arm_scale_f32(pSrc,maxValue,pSrc,S->fftLen);
  105. }
  106. /* Apply MEL filters */
  107. for(i=0; i<S->nbMelFilters; i++)
  108. {
  109. arm_dot_prod_f32(pSrc+S->filterPos[i],
  110. coefs,
  111. S->filterLengths[i],
  112. &result);
  113. coefs += S->filterLengths[i];
  114. pTmp[i] = result;
  115. }
  116. /* Compute the log */
  117. arm_offset_f32(pTmp,1.0e-6f,pTmp,S->nbMelFilters);
  118. arm_vlog_f32(pTmp,pTmp,S->nbMelFilters);
  119. /* Multiply with the DCT matrix */
  120. pDctMat.numRows=S->nbDctOutputs;
  121. pDctMat.numCols=S->nbMelFilters;
  122. pDctMat.pData=(float32_t*)S->dctCoefs;
  123. arm_mat_vec_mult_f32(&pDctMat, pTmp, pDst);
  124. }
  125. /**
  126. @} end of MFCCF32 group
  127. */