MFCC.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. /* ----------------------------------------------------------------------
  2. * Project: CMSIS DSP Library
  3. * Title: MFCC.h
  4. * Description: Node for CMSIS-DSP MFCC
  5. *
  6. * $Date: 06 October 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. #ifndef _MFCC_H_
  29. #define _MFCC_H_
  30. #include <vector>
  31. template<typename IN, int inputSize,typename OUT,int outputSize>
  32. class MFCC;
  33. /*
  34. The MFCC configuration data has to be generated with the script DSP/Scripts/GenMFCCDataForCPP.py.
  35. It is using a yaml file to describe the configuration
  36. */
  37. /*
  38. The CMSIS-DSP MFCC F32
  39. */
  40. template<int inputSize,int outputSize>
  41. class MFCC<float32_t,inputSize,float32_t,outputSize>: public GenericNode<float32_t,inputSize,float32_t,outputSize>
  42. {
  43. public:
  44. MFCC(FIFOBase<float32_t> &src,FIFOBase<float32_t> &dst,const arm_mfcc_instance_f32 *config):GenericNode<float32_t,inputSize,float32_t,outputSize>(src,dst){
  45. mfccConfig = config;
  46. #if defined(ARM_MFCC_CFFT_BASED)
  47. memory.resize(2*inputSize);
  48. #else
  49. memory.resize(inputSize + 2);
  50. #endif
  51. };
  52. int prepareForRunning() override
  53. {
  54. if (this->willOverflow() ||
  55. this->willUnderflow()
  56. )
  57. {
  58. return(CG_SKIP_EXECUTION_ID_CODE); // Skip execution
  59. }
  60. return(0);
  61. };
  62. int run(){
  63. float32_t *a=this->getReadBuffer();
  64. float32_t *b=this->getWriteBuffer();
  65. arm_mfcc_f32(mfccConfig,a,b,memory.data());
  66. return(0);
  67. };
  68. const arm_mfcc_instance_f32 *mfccConfig;
  69. std::vector<float32_t> memory;
  70. };
  71. #if defined(ARM_FLOAT16_SUPPORTED)
  72. /*
  73. The CMSIS-DSP MFCC F16
  74. */
  75. template<int inputSize,int outputSize>
  76. class MFCC<float16_t,inputSize,float16_t,outputSize>: public GenericNode<float16_t,inputSize,float16_t,outputSize>
  77. {
  78. public:
  79. MFCC(FIFOBase<float16_t> &src,FIFOBase<float16_t> &dst,const arm_mfcc_instance_f16 *config):GenericNode<float16_t,inputSize,float16_t,outputSize>(src,dst){
  80. mfccConfig = config;
  81. #if defined(ARM_MFCC_CFFT_BASED)
  82. memory.resize(2*inputSize);
  83. #else
  84. memory.resize(inputSize + 2);
  85. #endif
  86. };
  87. int prepareForRunning() override
  88. {
  89. if (this->willOverflow() ||
  90. this->willUnderflow()
  91. )
  92. {
  93. return(CG_SKIP_EXECUTION_ID_CODE); // Skip execution
  94. }
  95. return(0);
  96. };
  97. int run(){
  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() override
  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(){
  129. q31_t *a=this->getReadBuffer();
  130. q31_t *b=this->getWriteBuffer();
  131. arm_mfcc_q31(mfccConfig,a,b,memory.data());
  132. return(0);
  133. };
  134. const arm_mfcc_instance_q31 *mfccConfig;
  135. std::vector<q31_t> memory;
  136. };
  137. /*
  138. The CMSIS-DSP MFCC Q15
  139. */
  140. template<int inputSize,int outputSize>
  141. class MFCC<q15_t,inputSize,q15_t,outputSize>: public GenericNode<q15_t,inputSize,q15_t,outputSize>
  142. {
  143. public:
  144. MFCC(FIFOBase<q15_t> &src,FIFOBase<q15_t> &dst,const arm_mfcc_instance_q15 *config):GenericNode<q15_t,inputSize,q15_t,outputSize>(src,dst){
  145. mfccConfig = config;
  146. memory.resize(2*inputSize);
  147. };
  148. int prepareForRunning() override
  149. {
  150. if (this->willOverflow() ||
  151. this->willUnderflow()
  152. )
  153. {
  154. return(CG_SKIP_EXECUTION_ID_CODE); // Skip execution
  155. }
  156. return(0);
  157. };
  158. int run(){
  159. q15_t *a=this->getReadBuffer();
  160. q15_t *b=this->getWriteBuffer();
  161. arm_mfcc_q15(mfccConfig,a,b,memory.data());
  162. return(0);
  163. };
  164. const arm_mfcc_instance_q15 *mfccConfig;
  165. std::vector<q31_t> memory;
  166. };
  167. #endif