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