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