1 /* ----------------------------------------------------------------------
2  * Project:      CMSIS DSP Library
3  * Title:        arm_mfcc_q31.c
4  * Description:  MFCC function for the q31 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 Q31 implementation */
39 #define LOG2TOLOG_Q31 0x02C5C860
40 #define MICRO_Q31 0x08637BD0
41 #define SHIFT_MELFILTER_SATURATION_Q31 10
42 /**
43   @ingroup MFCC
44  */
45 
46 
47 
48 /**
49   @addtogroup MFCCQ31
50   @{
51  */
52 
53 /**
54   @brief         MFCC Q31
55   @param[in]    S       points to the mfcc instance structure
56   @param[in]     pSrc points to the input samples in Q31
57   @param[out]     pDst  points to the output MFCC values in q8.23 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  */
73 
arm_mfcc_q31(const arm_mfcc_instance_q31 * S,q31_t * pSrc,q31_t * pDst,q31_t * pTmp)74 arm_status arm_mfcc_q31(
75   const arm_mfcc_instance_q31 * S,
76   q31_t *pSrc,
77   q31_t *pDst,
78   q31_t *pTmp
79   )
80 {
81     q31_t m;
82     uint32_t index;
83     uint32_t fftShift=0;
84     q31_t logExponent;
85     q63_t result;
86     arm_matrix_instance_q31 pDctMat;
87     uint32_t i;
88     uint32_t coefsPos;
89     uint32_t filterLimit;
90     q31_t *pTmp2=(q31_t*)pTmp;
91 
92     arm_status status = ARM_MATH_SUCCESS;
93 
94     // q31
95     arm_absmax_q31(pSrc,S->fftLen,&m,&index);
96 
97     if ((m != 0) && (m != 0x7FFFFFFF))
98     {
99        q31_t quotient;
100        int16_t shift;
101 
102        status = arm_divide_q31(0x7FFFFFFF,m,&quotient,&shift);
103        if (status != ARM_MATH_SUCCESS)
104        {
105           return(status);
106        }
107 
108        arm_scale_q31(pSrc,quotient,shift,pSrc,S->fftLen);
109     }
110 
111 
112     // q31
113     arm_mult_q31(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_q31(&(S->cfft),pTmp2,0,1);
134 #else
135     /* Default RFFT based implementation */
136     arm_rfft_q31(&(S->rfft),pSrc,pTmp2);
137 #endif
138     filterLimit = 1 + (S->fftLen >> 1);
139 
140 
141     // q31 - fftShift
142     arm_cmplx_mag_q31(pTmp2,pSrc,filterLimit);
143     // q30 - fftShift
144 
145 
146     /* Apply MEL filters */
147     coefsPos = 0;
148     for(i=0; i<S->nbMelFilters; i++)
149     {
150       arm_dot_prod_q31(pSrc+S->filterPos[i],
151         &(S->filterCoefs[coefsPos]),
152         S->filterLengths[i],
153         &result);
154 
155 
156       coefsPos += S->filterLengths[i];
157 
158       // q16.48 - fftShift
159       result += MICRO_Q31;
160       result >>= (SHIFT_MELFILTER_SATURATION_Q31 + 18);
161       // q16.29 - fftShift - satShift
162       pTmp[i] = __SSAT(result,31) ;
163 
164     }
165 
166     if ((m != 0) && (m != 0x7FFFFFFF))
167     {
168       arm_scale_q31(pTmp,m,0,pTmp,S->nbMelFilters);
169     }
170 
171     // q16.29 - fftShift - satShift
172     /* Compute the log */
173     arm_vlog_q31(pTmp,pTmp,S->nbMelFilters);
174 
175 
176     // q5.26
177 
178     logExponent = fftShift + 2 + SHIFT_MELFILTER_SATURATION_Q31;
179     logExponent = logExponent * LOG2TOLOG_Q31;
180 
181 
182     // q5.26
183     arm_offset_q31(pTmp,logExponent,pTmp,S->nbMelFilters);
184     arm_shift_q31(pTmp,-3,pTmp,S->nbMelFilters);
185 
186 
187     // q8.23
188 
189     pDctMat.numRows=S->nbDctOutputs;
190     pDctMat.numCols=S->nbMelFilters;
191     pDctMat.pData=(q31_t*)S->dctCoefs;
192 
193     arm_mat_vec_mult_q31(&pDctMat, pTmp, pDst);
194 
195     return(status);
196 }
197 
198 /**
199   @} end of MFCCQ31 group
200  */
201