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