1 /* ----------------------------------------------------------------------
2 * Project: CMSIS DSP Library
3 * Title: arm_mfcc_q15.c
4 * Description: MFCC function for the q15 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
29
30
31 #include "dsp/transform_functions.h"
32 #include "dsp/statistics_functions.h"
33 #include "dsp/basic_math_functions.h"
34 #include "dsp/complex_math_functions.h"
35 #include "dsp/fast_math_functions.h"
36 #include "dsp/matrix_functions.h"
37
38 /* Constants for Q15 implementation */
39 #define LOG2TOLOG_Q15 0x02C5C860
40 #define MICRO_Q15 0x00000219
41 #define SHIFT_MELFILTER_SATURATION_Q15 10
42 /**
43 @ingroup MFCC
44 */
45
46
47
48 /**
49 @addtogroup MFCCQ15
50 @{
51 */
52
53 /**
54 @brief MFCC Q15
55 @param[in] S points to the mfcc instance structure
56 @param[in] pSrc points to the input samples in Q15
57 @param[out] pDst points to the output MFCC values in q8.7 format
58 @param[inout] pTmp points to a temporary buffer of complex
59
60 @par Description
61 The number of input samples is the FFT length used
62 when initializing the instance data structure.
63
64 The temporary buffer has a 2*fft length.
65
66 The source buffer is modified by this function.
67
68 The function may saturate. If the FFT length is too
69 big and the number of MEL filters too small then the fixed
70 point computations may saturate.
71
72 */
arm_mfcc_q15(const arm_mfcc_instance_q15 * S,q15_t * pSrc,q15_t * pDst,q31_t * pTmp)73 arm_status arm_mfcc_q15(
74 const arm_mfcc_instance_q15 * S,
75 q15_t *pSrc,
76 q15_t *pDst,
77 q31_t *pTmp
78 )
79 {
80 q15_t m;
81 uint32_t index;
82 uint32_t fftShift=0;
83 q31_t logExponent;
84 q63_t result;
85 arm_matrix_instance_q15 pDctMat;
86 uint32_t i;
87 uint32_t coefsPos;
88 uint32_t filterLimit;
89 q15_t *pTmp2=(q15_t*)pTmp;
90
91 arm_status status = ARM_MATH_SUCCESS;
92
93 // q15
94 arm_absmax_q15(pSrc,S->fftLen,&m,&index);
95
96 if ((m != 0) && (m != 0x7FFF))
97 {
98 q15_t quotient;
99 int16_t shift;
100
101 status = arm_divide_q15(0x7FFF,m,"ient,&shift);
102 if (status != ARM_MATH_SUCCESS)
103 {
104 return(status);
105 }
106
107 arm_scale_q15(pSrc,quotient,shift,pSrc,S->fftLen);
108 }
109
110
111 // q15
112 arm_mult_q15(pSrc,S->windowCoefs, pSrc, S->fftLen);
113
114
115 /* Compute spectrum magnitude
116 */
117 fftShift = 31 - __CLZ(S->fftLen);
118 #if defined(ARM_MFCC_CFFT_BASED)
119 /* some HW accelerator for CMSIS-DSP used in some boards
120 are only providing acceleration for CFFT.
121 With ARM_MFCC_CFFT_BASED enabled, CFFT is used and the MFCC
122 will be accelerated on those boards.
123
124 The default is to use RFFT
125 */
126 /* Convert from real to complex */
127 for(i=0; i < S->fftLen ; i++)
128 {
129 pTmp2[2*i] = pSrc[i];
130 pTmp2[2*i+1] = 0;
131 }
132 arm_cfft_q15(&(S->cfft),pTmp2,0,1);
133 #else
134 /* Default RFFT based implementation */
135 arm_rfft_q15(&(S->rfft),pSrc,pTmp2);
136 #endif
137 filterLimit = 1 + (S->fftLen >> 1);
138
139
140 // q15 - fftShift
141 arm_cmplx_mag_q15(pTmp2,pSrc,filterLimit);
142 // q14 - fftShift
143
144 /* Apply MEL filters */
145 coefsPos = 0;
146 for(i=0; i<S->nbMelFilters; i++)
147 {
148 arm_dot_prod_q15(pSrc+S->filterPos[i],
149 &(S->filterCoefs[coefsPos]),
150 S->filterLengths[i],
151 &result);
152
153 coefsPos += S->filterLengths[i];
154
155 // q34.29 - fftShift
156 result += MICRO_Q15;
157 result >>= SHIFT_MELFILTER_SATURATION_Q15;
158 // q34.29 - fftShift - satShift
159 pTmp[i] = __SSAT(result,31) ;
160
161 }
162
163 if ((m != 0) && (m != 0x7FFF))
164 {
165 arm_scale_q31(pTmp,m<<16,0,pTmp,S->nbMelFilters);
166 }
167
168 // q34.29 - fftShift - satShift
169 /* Compute the log */
170 arm_vlog_q31(pTmp,pTmp,S->nbMelFilters);
171
172
173 // q5.26
174
175 logExponent = fftShift + 2 + SHIFT_MELFILTER_SATURATION_Q15;
176 logExponent = logExponent * LOG2TOLOG_Q15;
177
178
179 // q8.26
180 arm_offset_q31(pTmp,logExponent,pTmp,S->nbMelFilters);
181 arm_shift_q31(pTmp,-19,pTmp,S->nbMelFilters);
182 for(i=0; i<S->nbMelFilters; i++)
183 {
184 pSrc[i] = __SSAT((q15_t)pTmp[i],16);
185 }
186
187 // q8.7
188
189 pDctMat.numRows=S->nbDctOutputs;
190 pDctMat.numCols=S->nbMelFilters;
191 pDctMat.pData=(q15_t*)S->dctCoefs;
192
193 arm_mat_vec_mult_q15(&pDctMat, pSrc, pDst);
194
195 return(status);
196 }
197
198 /**
199 @} end of MFCCQ15 group
200 */
201