• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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   @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  */
75 
arm_mfcc_q31(const arm_mfcc_instance_q31 * S,q31_t * pSrc,q31_t * pDst,q31_t * pTmp)76 arm_status arm_mfcc_q31(
77   const arm_mfcc_instance_q31 * S,
78   q31_t *pSrc,
79   q31_t *pDst,
80   q31_t *pTmp
81   )
82 {
83     q31_t m;
84     uint32_t index;
85     uint32_t fftShift=0;
86     q31_t logExponent;
87     q63_t result;
88     arm_matrix_instance_q31 pDctMat;
89     uint32_t i;
90     uint32_t coefsPos;
91     uint32_t filterLimit;
92     q31_t *pTmp2=(q31_t*)pTmp;
93 
94     arm_status status = ARM_MATH_SUCCESS;
95 
96     // q31
97     arm_absmax_q31(pSrc,S->fftLen,&m,&index);
98 
99     if ((m != 0) && (m != 0x7FFFFFFF))
100     {
101        q31_t quotient;
102        int16_t shift;
103 
104        status = arm_divide_q31(0x7FFFFFFF,m,&quotient,&shift);
105        if (status != ARM_MATH_SUCCESS)
106        {
107           return(status);
108        }
109 
110        arm_scale_q31(pSrc,quotient,shift,pSrc,S->fftLen);
111     }
112 
113 
114     // q31
115     arm_mult_q31(pSrc,S->windowCoefs, pSrc, S->fftLen);
116 
117 
118     /* Compute spectrum magnitude
119     */
120     fftShift = 31 - __CLZ(S->fftLen);
121 #if defined(ARM_MFCC_CFFT_BASED)
122     /* some HW accelerator for CMSIS-DSP used in some boards
123        are only providing acceleration for CFFT.
124        With ARM_MFCC_CFFT_BASED enabled, CFFT is used and the MFCC
125        will be accelerated on those boards.
126 
127        The default is to use RFFT
128     */
129     /* Convert from real to complex */
130     for(i=0; i < S->fftLen ; i++)
131     {
132       pTmp2[2*i] = pSrc[i];
133       pTmp2[2*i+1] = 0;
134     }
135     arm_cfft_q31(&(S->cfft),pTmp2,0,1);
136 #else
137     /* Default RFFT based implementation */
138     arm_rfft_q31(&(S->rfft),pSrc,pTmp2);
139 #endif
140     filterLimit = 1 + (S->fftLen >> 1);
141 
142 
143     // q31 - fftShift
144     arm_cmplx_mag_q31(pTmp2,pSrc,filterLimit);
145     // q30 - fftShift
146 
147 
148     /* Apply MEL filters */
149     coefsPos = 0;
150     for(i=0; i<S->nbMelFilters; i++)
151     {
152       arm_dot_prod_q31(pSrc+S->filterPos[i],
153         &(S->filterCoefs[coefsPos]),
154         S->filterLengths[i],
155         &result);
156 
157 
158       coefsPos += S->filterLengths[i];
159 
160       // q16.48 - fftShift
161       result += MICRO_Q31;
162       result >>= (SHIFT_MELFILTER_SATURATION_Q31 + 18);
163       // q16.29 - fftShift - satShift
164       pTmp[i] = __SSAT(result,31) ;
165 
166     }
167 
168     if ((m != 0) && (m != 0x7FFFFFFF))
169     {
170       arm_scale_q31(pTmp,m,0,pTmp,S->nbMelFilters);
171     }
172 
173     // q16.29 - fftShift - satShift
174     /* Compute the log */
175     arm_vlog_q31(pTmp,pTmp,S->nbMelFilters);
176 
177 
178     // q5.26
179 
180     logExponent = fftShift + 2 + SHIFT_MELFILTER_SATURATION_Q31;
181     logExponent = logExponent * LOG2TOLOG_Q31;
182 
183 
184     // q5.26
185     arm_offset_q31(pTmp,logExponent,pTmp,S->nbMelFilters);
186     arm_shift_q31(pTmp,-3,pTmp,S->nbMelFilters);
187 
188 
189     // q8.23
190 
191     pDctMat.numRows=S->nbDctOutputs;
192     pDctMat.numCols=S->nbMelFilters;
193     pDctMat.pData=(q31_t*)S->dctCoefs;
194 
195     arm_mat_vec_mult_q31(&pDctMat, pTmp, pDst);
196 
197     return(status);
198 }
199 
200 /**
201   @} end of MFCCQ31 group
202  */
203