1 /* ----------------------------------------------------------------------
2  * Project:      CMSIS DSP Library
3  * Title:        arm_mfcc_init_q31.c
4  * Description:  MFCC initialization 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  * @defgroup MFCCQ31 MFCC Q31
31  */
32 
33 
34 /**
35   @ingroup MFCC
36  */
37 
38 
39 /**
40   @addtogroup MFCCQ31
41   @{
42  */
43 
44 
45 #include "dsp/transform_functions.h"
46 
47 
48 
49 /**
50   @brief         Generic initialization of the MFCC Q31 instance structure
51   @param[out]    S       points to the mfcc instance structure
52   @param[in]     fftLen  fft length
53   @param[in]     nbMelFilters  number of Mel filters
54   @param[in]     nbDctOutputs  number of Dct outputs
55   @param[in]     dctCoefs  points to an array of DCT coefficients
56   @param[in]     filterPos  points of the array of filter positions
57   @param[in]     filterLengths  points to the array of filter lengths
58   @param[in]     filterCoefs  points to the array of filter coefficients
59   @param[in]     windowCoefs  points to the array of window coefficients
60 
61   @return        error status
62 
63   @par           Description
64                    The matrix of Mel filter coefficients is sparse.
65                    Most of the coefficients are zero.
66                    To avoid multiplying the spectrogram by those zeros, the
67                    filter is applied only to a given position in the spectrogram
68                    and on a given number of FFT bins (the filter length).
69                    It is the reason for the arrays filterPos and filterLengths.
70 
71                    window coefficients can describe (for instance) a Hamming window.
72                    The array has the same size as the FFT length.
73 
74                    The folder Scripts is containing a Python script which can be used
75                    to generate the filter, dct and window arrays.
76 
77   @par
78                 This function should be used only if you don't know the FFT sizes that
79                 you'll need at build time. The use of this function will prevent the
80                 linker from removing the FFT tables that are not needed and the library
81                 code size will be bigger than needed.
82 
83   @par
84                 If you use CMSIS-DSP as a static library, and if you know the MFCC sizes
85                 that you need at build time, then it is better to use the initialization
86                 functions defined for each MFCC size.
87 
88 
89  */
90 
arm_mfcc_init_q31(arm_mfcc_instance_q31 * S,uint32_t fftLen,uint32_t nbMelFilters,uint32_t nbDctOutputs,const q31_t * dctCoefs,const uint32_t * filterPos,const uint32_t * filterLengths,const q31_t * filterCoefs,const q31_t * windowCoefs)91 arm_status arm_mfcc_init_q31(
92   arm_mfcc_instance_q31 * S,
93   uint32_t fftLen,
94   uint32_t nbMelFilters,
95   uint32_t nbDctOutputs,
96   const q31_t *dctCoefs,
97   const uint32_t *filterPos,
98   const uint32_t *filterLengths,
99   const q31_t *filterCoefs,
100   const q31_t *windowCoefs
101   )
102 {
103  arm_status status;
104 
105  S->fftLen=fftLen;
106  S->nbMelFilters=nbMelFilters;
107  S->nbDctOutputs=nbDctOutputs;
108  S->dctCoefs=dctCoefs;
109  S->filterPos=filterPos;
110  S->filterLengths=filterLengths;
111  S->filterCoefs=filterCoefs;
112  S->windowCoefs=windowCoefs;
113 
114  #if defined(ARM_MFCC_CFFT_BASED)
115  status=arm_cfft_init_q31(&(S->cfft),fftLen);
116  #else
117  status=arm_rfft_init_q31(&(S->rfft),fftLen,0,1);
118  #endif
119 
120  return(status);
121 }
122 
123 #if defined(ARM_MFCC_CFFT_BASED)
124 #define MFCC_INIT_Q31(LEN)                    \
125 arm_status arm_mfcc_init_##LEN##_q31(         \
126   arm_mfcc_instance_q31 * S,                  \
127   uint32_t nbMelFilters,                      \
128   uint32_t nbDctOutputs,                      \
129   const q31_t *dctCoefs,                      \
130   const uint32_t *filterPos,                  \
131   const uint32_t *filterLengths,              \
132   const q31_t *filterCoefs,                   \
133   const q31_t *windowCoefs                    \
134   )                                           \
135 {                                             \
136  arm_status status;                           \
137                                               \
138  S->fftLen=LEN;                               \
139  S->nbMelFilters=nbMelFilters;                \
140  S->nbDctOutputs=nbDctOutputs;                \
141  S->dctCoefs=dctCoefs;                        \
142  S->filterPos=filterPos;                      \
143  S->filterLengths=filterLengths;              \
144  S->filterCoefs=filterCoefs;                  \
145  S->windowCoefs=windowCoefs;                  \
146                                               \
147  status=arm_cfft_init_##LEN##_q31(&(S->cfft));\
148                                               \
149  return(status);                              \
150 }
151 #else
152 #define MFCC_INIT_Q31(LEN)                        \
153 arm_status arm_mfcc_init_##LEN##_q31(             \
154   arm_mfcc_instance_q31 * S,                      \
155   uint32_t nbMelFilters,                          \
156   uint32_t nbDctOutputs,                          \
157   const q31_t *dctCoefs,                          \
158   const uint32_t *filterPos,                      \
159   const uint32_t *filterLengths,                  \
160   const q31_t *filterCoefs,                       \
161   const q31_t *windowCoefs                        \
162   )                                               \
163 {                                                 \
164  arm_status status;                               \
165                                                   \
166  S->fftLen=LEN;                                   \
167  S->nbMelFilters=nbMelFilters;                    \
168  S->nbDctOutputs=nbDctOutputs;                    \
169  S->dctCoefs=dctCoefs;                            \
170  S->filterPos=filterPos;                          \
171  S->filterLengths=filterLengths;                  \
172  S->filterCoefs=filterCoefs;                      \
173  S->windowCoefs=windowCoefs;                      \
174                                                   \
175  status=arm_rfft_init_##LEN##_q31(&(S->rfft),0,1);\
176                                                   \
177  return(status);                                  \
178 }
179 #endif
180 
181 /**
182   @brief         Initialization of the MFCC Q31 instance structure for 32 sample MFCC
183   @param[out]    S       points to the mfcc instance structure
184   @param[in]     nbMelFilters  number of Mel filters
185   @param[in]     nbDctOutputs  number of Dct outputs
186   @param[in]     dctCoefs  points to an array of DCT coefficients
187   @param[in]     filterPos  points of the array of filter positions
188   @param[in]     filterLengths  points to the array of filter lengths
189   @param[in]     filterCoefs  points to the array of filter coefficients
190   @param[in]     windowCoefs  points to the array of window coefficients
191 
192   @return        error status
193 
194   @par           Description
195                    The matrix of Mel filter coefficients is sparse.
196                    Most of the coefficients are zero.
197                    To avoid multiplying the spectrogram by those zeros, the
198                    filter is applied only to a given position in the spectrogram
199                    and on a given number of FFT bins (the filter length).
200                    It is the reason for the arrays filterPos and filterLengths.
201 
202                    window coefficients can describe (for instance) a Hamming window.
203                    The array has the same size as the FFT length.
204 
205                    The folder Scripts is containing a Python script which can be used
206                    to generate the filter, dct and window arrays.
207  */
208 MFCC_INIT_Q31(32);
209 
210 /**
211   @brief         Initialization of the MFCC Q31 instance structure for 64 sample MFCC
212   @param[out]    S       points to the mfcc instance structure
213   @param[in]     nbMelFilters  number of Mel filters
214   @param[in]     nbDctOutputs  number of Dct outputs
215   @param[in]     dctCoefs  points to an array of DCT coefficients
216   @param[in]     filterPos  points of the array of filter positions
217   @param[in]     filterLengths  points to the array of filter lengths
218   @param[in]     filterCoefs  points to the array of filter coefficients
219   @param[in]     windowCoefs  points to the array of window coefficients
220 
221   @return        error status
222 
223   @par           Description
224                    The matrix of Mel filter coefficients is sparse.
225                    Most of the coefficients are zero.
226                    To avoid multiplying the spectrogram by those zeros, the
227                    filter is applied only to a given position in the spectrogram
228                    and on a given number of FFT bins (the filter length).
229                    It is the reason for the arrays filterPos and filterLengths.
230 
231                    window coefficients can describe (for instance) a Hamming window.
232                    The array has the same size as the FFT length.
233 
234                    The folder Scripts is containing a Python script which can be used
235                    to generate the filter, dct and window arrays.
236  */
237 MFCC_INIT_Q31(64);
238 
239 /**
240   @brief         Initialization of the MFCC Q31 instance structure for 128 sample MFCC
241   @param[out]    S       points to the mfcc instance structure
242   @param[in]     nbMelFilters  number of Mel filters
243   @param[in]     nbDctOutputs  number of Dct outputs
244   @param[in]     dctCoefs  points to an array of DCT coefficients
245   @param[in]     filterPos  points of the array of filter positions
246   @param[in]     filterLengths  points to the array of filter lengths
247   @param[in]     filterCoefs  points to the array of filter coefficients
248   @param[in]     windowCoefs  points to the array of window coefficients
249 
250   @return        error status
251 
252   @par           Description
253                    The matrix of Mel filter coefficients is sparse.
254                    Most of the coefficients are zero.
255                    To avoid multiplying the spectrogram by those zeros, the
256                    filter is applied only to a given position in the spectrogram
257                    and on a given number of FFT bins (the filter length).
258                    It is the reason for the arrays filterPos and filterLengths.
259 
260                    window coefficients can describe (for instance) a Hamming window.
261                    The array has the same size as the FFT length.
262 
263                    The folder Scripts is containing a Python script which can be used
264                    to generate the filter, dct and window arrays.
265  */
266 MFCC_INIT_Q31(128);
267 
268 /**
269   @brief         Initialization of the MFCC Q31 instance structure for 256 sample MFCC
270   @param[out]    S       points to the mfcc instance structure
271   @param[in]     nbMelFilters  number of Mel filters
272   @param[in]     nbDctOutputs  number of Dct outputs
273   @param[in]     dctCoefs  points to an array of DCT coefficients
274   @param[in]     filterPos  points of the array of filter positions
275   @param[in]     filterLengths  points to the array of filter lengths
276   @param[in]     filterCoefs  points to the array of filter coefficients
277   @param[in]     windowCoefs  points to the array of window coefficients
278 
279   @return        error status
280 
281   @par           Description
282                    The matrix of Mel filter coefficients is sparse.
283                    Most of the coefficients are zero.
284                    To avoid multiplying the spectrogram by those zeros, the
285                    filter is applied only to a given position in the spectrogram
286                    and on a given number of FFT bins (the filter length).
287                    It is the reason for the arrays filterPos and filterLengths.
288 
289                    window coefficients can describe (for instance) a Hamming window.
290                    The array has the same size as the FFT length.
291 
292                    The folder Scripts is containing a Python script which can be used
293                    to generate the filter, dct and window arrays.
294  */
295 MFCC_INIT_Q31(256);
296 
297 /**
298   @brief         Initialization of the MFCC Q31 instance structure for 512 sample MFCC
299   @param[out]    S       points to the mfcc instance structure
300   @param[in]     nbMelFilters  number of Mel filters
301   @param[in]     nbDctOutputs  number of Dct outputs
302   @param[in]     dctCoefs  points to an array of DCT coefficients
303   @param[in]     filterPos  points of the array of filter positions
304   @param[in]     filterLengths  points to the array of filter lengths
305   @param[in]     filterCoefs  points to the array of filter coefficients
306   @param[in]     windowCoefs  points to the array of window coefficients
307 
308   @return        error status
309 
310   @par           Description
311                    The matrix of Mel filter coefficients is sparse.
312                    Most of the coefficients are zero.
313                    To avoid multiplying the spectrogram by those zeros, the
314                    filter is applied only to a given position in the spectrogram
315                    and on a given number of FFT bins (the filter length).
316                    It is the reason for the arrays filterPos and filterLengths.
317 
318                    window coefficients can describe (for instance) a Hamming window.
319                    The array has the same size as the FFT length.
320 
321                    The folder Scripts is containing a Python script which can be used
322                    to generate the filter, dct and window arrays.
323  */
324 MFCC_INIT_Q31(512);
325 
326 /**
327   @brief         Initialization of the MFCC Q31 instance structure for 1024 sample MFCC
328   @param[out]    S       points to the mfcc instance structure
329   @param[in]     nbMelFilters  number of Mel filters
330   @param[in]     nbDctOutputs  number of Dct outputs
331   @param[in]     dctCoefs  points to an array of DCT coefficients
332   @param[in]     filterPos  points of the array of filter positions
333   @param[in]     filterLengths  points to the array of filter lengths
334   @param[in]     filterCoefs  points to the array of filter coefficients
335   @param[in]     windowCoefs  points to the array of window coefficients
336 
337   @return        error status
338 
339   @par           Description
340                    The matrix of Mel filter coefficients is sparse.
341                    Most of the coefficients are zero.
342                    To avoid multiplying the spectrogram by those zeros, the
343                    filter is applied only to a given position in the spectrogram
344                    and on a given number of FFT bins (the filter length).
345                    It is the reason for the arrays filterPos and filterLengths.
346 
347                    window coefficients can describe (for instance) a Hamming window.
348                    The array has the same size as the FFT length.
349 
350                    The folder Scripts is containing a Python script which can be used
351                    to generate the filter, dct and window arrays.
352  */
353 MFCC_INIT_Q31(1024);
354 
355 /**
356   @brief         Initialization of the MFCC Q31 instance structure for 2048 sample MFCC
357   @param[out]    S       points to the mfcc instance structure
358   @param[in]     nbMelFilters  number of Mel filters
359   @param[in]     nbDctOutputs  number of Dct outputs
360   @param[in]     dctCoefs  points to an array of DCT coefficients
361   @param[in]     filterPos  points of the array of filter positions
362   @param[in]     filterLengths  points to the array of filter lengths
363   @param[in]     filterCoefs  points to the array of filter coefficients
364   @param[in]     windowCoefs  points to the array of window coefficients
365 
366   @return        error status
367 
368   @par           Description
369                    The matrix of Mel filter coefficients is sparse.
370                    Most of the coefficients are zero.
371                    To avoid multiplying the spectrogram by those zeros, the
372                    filter is applied only to a given position in the spectrogram
373                    and on a given number of FFT bins (the filter length).
374                    It is the reason for the arrays filterPos and filterLengths.
375 
376                    window coefficients can describe (for instance) a Hamming window.
377                    The array has the same size as the FFT length.
378 
379                    The folder Scripts is containing a Python script which can be used
380                    to generate the filter, dct and window arrays.
381  */
382 MFCC_INIT_Q31(2048);
383 
384 /**
385   @brief         Initialization of the MFCC Q31 instance structure for 4096 sample MFCC
386   @param[out]    S       points to the mfcc instance structure
387   @param[in]     nbMelFilters  number of Mel filters
388   @param[in]     nbDctOutputs  number of Dct outputs
389   @param[in]     dctCoefs  points to an array of DCT coefficients
390   @param[in]     filterPos  points of the array of filter positions
391   @param[in]     filterLengths  points to the array of filter lengths
392   @param[in]     filterCoefs  points to the array of filter coefficients
393   @param[in]     windowCoefs  points to the array of window coefficients
394 
395   @return        error status
396 
397   @par           Description
398                    The matrix of Mel filter coefficients is sparse.
399                    Most of the coefficients are zero.
400                    To avoid multiplying the spectrogram by those zeros, the
401                    filter is applied only to a given position in the spectrogram
402                    and on a given number of FFT bins (the filter length).
403                    It is the reason for the arrays filterPos and filterLengths.
404 
405                    window coefficients can describe (for instance) a Hamming window.
406                    The array has the same size as the FFT length.
407 
408                    The folder Scripts is containing a Python script which can be used
409                    to generate the filter, dct and window arrays.
410  */
411 MFCC_INIT_Q31(4096);
412 
413 /**
414   @} end of MFCCQ31 group
415  */
416