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