MFCC.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. /* ----------------------------------------------------------------------
  2. * Project: CMSIS DSP Library
  3. * Title: MFCC.h
  4. * Description: Node for CMSIS-DSP MFCC
  5. *
  6. *
  7. * Target Processor: Cortex-M and Cortex-A cores
  8. * --------------------------------------------------------------------
  9. *
  10. * Copyright (C) 2021-2023 ARM Limited or its affiliates. All rights reserved.
  11. *
  12. * SPDX-License-Identifier: Apache-2.0
  13. *
  14. * Licensed under the Apache License, Version 2.0 (the License); you may
  15. * not use this file except in compliance with the License.
  16. * You may obtain a copy of the License at
  17. *
  18. * www.apache.org/licenses/LICENSE-2.0
  19. *
  20. * Unless required by applicable law or agreed to in writing, software
  21. * distributed under the License is distributed on an AS IS BASIS, WITHOUT
  22. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  23. * See the License for the specific language governing permissions and
  24. * limitations under the License.
  25. */
  26. #ifndef _MFCC_H_
  27. #define _MFCC_H_
  28. #include <vector>
  29. template<typename IN, int inputSize,typename OUT,int outputSize>
  30. class MFCC;
  31. /*
  32. The MFCC configuration data has to be generated with the script DSP/Scripts/GenMFCCDataForCPP.py.
  33. It is using a yaml file to describe the configuration
  34. */
  35. /*
  36. The CMSIS-DSP MFCC F32
  37. */
  38. template<int inputSize,int outputSize>
  39. class MFCC<float32_t,inputSize,float32_t,outputSize>: public GenericNode<float32_t,inputSize,float32_t,outputSize>
  40. {
  41. public:
  42. MFCC(FIFOBase<float32_t> &src,FIFOBase<float32_t> &dst,const arm_mfcc_instance_f32 *config):GenericNode<float32_t,inputSize,float32_t,outputSize>(src,dst){
  43. mfccConfig = config;
  44. #if defined(ARM_MFCC_CFFT_BASED)
  45. memory.resize(2*inputSize);
  46. #else
  47. memory.resize(inputSize + 2);
  48. #endif
  49. };
  50. int prepareForRunning() final
  51. {
  52. if (this->willOverflow() ||
  53. this->willUnderflow()
  54. )
  55. {
  56. return(CG_SKIP_EXECUTION_ID_CODE); // Skip execution
  57. }
  58. return(0);
  59. };
  60. int run() final
  61. {
  62. float32_t *a=this->getReadBuffer();
  63. float32_t *b=this->getWriteBuffer();
  64. arm_mfcc_f32(mfccConfig,a,b,memory.data());
  65. return(0);
  66. };
  67. const arm_mfcc_instance_f32 *mfccConfig;
  68. std::vector<float32_t> memory;
  69. };
  70. #if defined(ARM_FLOAT16_SUPPORTED)
  71. /*
  72. The CMSIS-DSP MFCC F16
  73. */
  74. template<int inputSize,int outputSize>
  75. class MFCC<float16_t,inputSize,float16_t,outputSize>: public GenericNode<float16_t,inputSize,float16_t,outputSize>
  76. {
  77. public:
  78. MFCC(FIFOBase<float16_t> &src,FIFOBase<float16_t> &dst,const arm_mfcc_instance_f16 *config):GenericNode<float16_t,inputSize,float16_t,outputSize>(src,dst){
  79. mfccConfig = config;
  80. #if defined(ARM_MFCC_CFFT_BASED)
  81. memory.resize(2*inputSize);
  82. #else
  83. memory.resize(inputSize + 2);
  84. #endif
  85. };
  86. int prepareForRunning() final
  87. {
  88. if (this->willOverflow() ||
  89. this->willUnderflow()
  90. )
  91. {
  92. return(CG_SKIP_EXECUTION_ID_CODE); // Skip execution
  93. }
  94. return(0);
  95. };
  96. int run() final
  97. {
  98. float16_t *a=this->getReadBuffer();
  99. float16_t *b=this->getWriteBuffer();
  100. arm_mfcc_f16(mfccConfig,a,b,memory.data());
  101. return(0);
  102. };
  103. const arm_mfcc_instance_f16 *mfccConfig;
  104. std::vector<float16_t> memory;
  105. };
  106. #endif
  107. /*
  108. The CMSIS-DSP MFCC Q31
  109. */
  110. template<int inputSize,int outputSize>
  111. class MFCC<q31_t,inputSize,q31_t,outputSize>: public GenericNode<q31_t,inputSize,q31_t,outputSize>
  112. {
  113. public:
  114. MFCC(FIFOBase<q31_t> &src,FIFOBase<q31_t> &dst,const arm_mfcc_instance_q31 *config):GenericNode<q31_t,inputSize,q31_t,outputSize>(src,dst){
  115. mfccConfig = config;
  116. memory.resize(2*inputSize);
  117. };
  118. int prepareForRunning() final
  119. {
  120. if (this->willOverflow() ||
  121. this->willUnderflow()
  122. )
  123. {
  124. return(CG_SKIP_EXECUTION_ID_CODE); // Skip execution
  125. }
  126. return(0);
  127. };
  128. int run() final
  129. {
  130. q31_t *a=this->getReadBuffer();
  131. q31_t *b=this->getWriteBuffer();
  132. arm_mfcc_q31(mfccConfig,a,b,memory.data());
  133. return(0);
  134. };
  135. const arm_mfcc_instance_q31 *mfccConfig;
  136. std::vector<q31_t> memory;
  137. };
  138. /*
  139. The CMSIS-DSP MFCC Q15
  140. */
  141. template<int inputSize,int outputSize>
  142. class MFCC<q15_t,inputSize,q15_t,outputSize>: public GenericNode<q15_t,inputSize,q15_t,outputSize>
  143. {
  144. public:
  145. MFCC(FIFOBase<q15_t> &src,FIFOBase<q15_t> &dst,const arm_mfcc_instance_q15 *config):GenericNode<q15_t,inputSize,q15_t,outputSize>(src,dst){
  146. mfccConfig = config;
  147. memory.resize(2*inputSize);
  148. };
  149. int prepareForRunning() final
  150. {
  151. if (this->willOverflow() ||
  152. this->willUnderflow()
  153. )
  154. {
  155. return(CG_SKIP_EXECUTION_ID_CODE); // Skip execution
  156. }
  157. return(0);
  158. };
  159. int run() final
  160. {
  161. q15_t *a=this->getReadBuffer();
  162. q15_t *b=this->getWriteBuffer();
  163. arm_mfcc_q15(mfccConfig,a,b,memory.data());
  164. return(0);
  165. };
  166. const arm_mfcc_instance_q15 *mfccConfig;
  167. std::vector<q31_t> memory;
  168. };
  169. #endif