arm_mfcc_f32.c 3.9 KB

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