]>
git.gir.st - tmk_keyboard.git/blob - tmk_core/tool/mbed/mbed-sdk/libraries/dsp/cmsis_dsp/SupportFunctions/math_helper.c
1 /* ----------------------------------------------------------------------
2 * Copyright (C) 2010-2012 ARM Limited. All rights reserved.
4 * $Date: 17. January 2013
7 * Project: CMSIS DSP Library
11 * Description: Definition of all helper functions required.
13 * Target Processor: Cortex-M4/Cortex-M3
15 * Redistribution and use in source and binary forms, with or without
16 * modification, are permitted provided that the following conditions
18 * - Redistributions of source code must retain the above copyright
19 * notice, this list of conditions and the following disclaimer.
20 * - Redistributions in binary form must reproduce the above copyright
21 * notice, this list of conditions and the following disclaimer in
22 * the documentation and/or other materials provided with the
24 * - Neither the name of ARM LIMITED nor the names of its contributors
25 * may be used to endorse or promote products derived from this
26 * software without specific prior written permission.
28 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
29 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
30 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
31 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
32 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
33 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
34 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
35 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
36 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
37 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
38 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
39 * POSSIBILITY OF SUCH DAMAGE.
40 * -------------------------------------------------------------------- */
42 /* ----------------------------------------------------------------------
43 * Include standard header files
44 * -------------------------------------------------------------------- */
47 /* ----------------------------------------------------------------------
48 * Include project header files
49 * -------------------------------------------------------------------- */
50 #include "math_helper.h"
53 * @brief Caluclation of SNR
54 * @param float* Pointer to the reference buffer
55 * @param float* Pointer to the test buffer
56 * @param uint32_t total number of samples
58 * The function Caluclates signal to noise ratio for the reference output
62 float arm_snr_f32(float *pRef
, float *pTest
, uint32_t buffSize
)
64 float EnergySignal
= 0.0, EnergyError
= 0.0;
70 for (i
= 0; i
< buffSize
; i
++)
72 /* Checking for a NAN value in pRef array */
73 test
= (int *)(&pRef
[i
]);
76 if(temp
== 0x7FC00000)
81 /* Checking for a NAN value in pTest array */
82 test
= (int *)(&pTest
[i
]);
85 if(temp
== 0x7FC00000)
89 EnergySignal
+= pRef
[i
] * pRef
[i
];
90 EnergyError
+= (pRef
[i
] - pTest
[i
]) * (pRef
[i
] - pTest
[i
]);
93 /* Checking for a NAN value in EnergyError */
94 test
= (int *)(&EnergyError
);
97 if(temp
== 0x7FC00000)
103 SNR
= 10 * log10 (EnergySignal
/ EnergyError
);
111 * @brief Provide guard bits for Input buffer
112 * @param q15_t* Pointer to input buffer
113 * @param uint32_t blockSize
114 * @param uint32_t guard_bits
116 * The function Provides the guard bits for the buffer
120 void arm_provide_guard_bits_q15 (q15_t
* input_buf
, uint32_t blockSize
,
125 for (i
= 0; i
< blockSize
; i
++)
127 input_buf
[i
] = input_buf
[i
] >> guard_bits
;
132 * @brief Converts float to fixed in q12.20 format
133 * @param uint32_t number of samples in the buffer
135 * The function converts floating point values to fixed point(q12.20) values
138 void arm_float_to_q12_20(float *pIn
, q31_t
* pOut
, uint32_t numSamples
)
142 for (i
= 0; i
< numSamples
; i
++)
144 /* 1048576.0f corresponds to pow(2, 20) */
145 pOut
[i
] = (q31_t
) (pIn
[i
] * 1048576.0f
);
147 pOut
[i
] += pIn
[i
] > 0 ? 0.5 : -0.5;
149 if (pIn
[i
] == (float) 1.0)
151 pOut
[i
] = 0x000FFFFF;
157 * @brief Compare MATLAB Reference Output and ARM Test output
158 * @param q15_t* Pointer to Ref buffer
159 * @param q15_t* Pointer to Test buffer
160 * @param uint32_t number of samples in the buffer
164 uint32_t arm_compare_fixed_q15(q15_t
*pIn
, q15_t
* pOut
, uint32_t numSamples
)
168 uint32_t diffCrnt
= 0;
169 uint32_t maxDiff
= 0;
171 for (i
= 0; i
< numSamples
; i
++)
173 diff
= pIn
[i
] - pOut
[i
];
174 diffCrnt
= (diff
> 0) ? diff
: -diff
;
176 if(diffCrnt
> maxDiff
)
186 * @brief Compare MATLAB Reference Output and ARM Test output
187 * @param q31_t* Pointer to Ref buffer
188 * @param q31_t* Pointer to Test buffer
189 * @param uint32_t number of samples in the buffer
193 uint32_t arm_compare_fixed_q31(q31_t
*pIn
, q31_t
* pOut
, uint32_t numSamples
)
197 uint32_t diffCrnt
= 0;
198 uint32_t maxDiff
= 0;
200 for (i
= 0; i
< numSamples
; i
++)
202 diff
= pIn
[i
] - pOut
[i
];
203 diffCrnt
= (diff
> 0) ? diff
: -diff
;
205 if(diffCrnt
> maxDiff
)
215 * @brief Provide guard bits for Input buffer
216 * @param q31_t* Pointer to input buffer
217 * @param uint32_t blockSize
218 * @param uint32_t guard_bits
220 * The function Provides the guard bits for the buffer
224 void arm_provide_guard_bits_q31 (q31_t
* input_buf
,
230 for (i
= 0; i
< blockSize
; i
++)
232 input_buf
[i
] = input_buf
[i
] >> guard_bits
;
237 * @brief Provide guard bits for Input buffer
238 * @param q31_t* Pointer to input buffer
239 * @param uint32_t blockSize
240 * @param uint32_t guard_bits
242 * The function Provides the guard bits for the buffer
246 void arm_provide_guard_bits_q7 (q7_t
* input_buf
,
252 for (i
= 0; i
< blockSize
; i
++)
254 input_buf
[i
] = input_buf
[i
] >> guard_bits
;
261 * @brief Caluclates number of guard bits
262 * @param uint32_t number of additions
264 * The function Caluclates the number of guard bits
265 * depending on the numtaps
268 uint32_t arm_calc_guard_bits (uint32_t num_adds
)
270 uint32_t i
= 1, j
= 0;
287 * @brief Converts Q15 to floating-point
288 * @param uint32_t number of samples in the buffer
292 void arm_apply_guard_bits (float32_t
* pIn
,
298 for (i
= 0; i
< numSamples
; i
++)
300 pIn
[i
] = pIn
[i
] * arm_calc_2pow(guard_bits
);
305 * @brief Calculates pow(2, numShifts)
306 * @param uint32_t number of shifts
307 * @return pow(2, numShifts)
309 uint32_t arm_calc_2pow(uint32_t numShifts
)
314 for (i
= 0; i
< numShifts
; i
++)
325 * @brief Converts float to fixed q14
326 * @param uint32_t number of samples in the buffer
328 * The function converts floating point values to fixed point values
331 void arm_float_to_q14 (float *pIn
, q15_t
* pOut
,
336 for (i
= 0; i
< numSamples
; i
++)
338 /* 16384.0f corresponds to pow(2, 14) */
339 pOut
[i
] = (q15_t
) (pIn
[i
] * 16384.0f
);
341 pOut
[i
] += pIn
[i
] > 0 ? 0.5 : -0.5;
343 if (pIn
[i
] == (float) 2.0)
354 * @brief Converts float to fixed q30 format
355 * @param uint32_t number of samples in the buffer
357 * The function converts floating point values to fixed point values
360 void arm_float_to_q30 (float *pIn
, q31_t
* pOut
,
365 for (i
= 0; i
< numSamples
; i
++)
367 /* 1073741824.0f corresponds to pow(2, 30) */
368 pOut
[i
] = (q31_t
) (pIn
[i
] * 1073741824.0f
);
370 pOut
[i
] += pIn
[i
] > 0 ? 0.5 : -0.5;
372 if (pIn
[i
] == (float) 2.0)
374 pOut
[i
] = 0x7FFFFFFF;
380 * @brief Converts float to fixed q30 format
381 * @param uint32_t number of samples in the buffer
383 * The function converts floating point values to fixed point values
386 void arm_float_to_q29 (float *pIn
, q31_t
* pOut
,
391 for (i
= 0; i
< numSamples
; i
++)
393 /* 1073741824.0f corresponds to pow(2, 30) */
394 pOut
[i
] = (q31_t
) (pIn
[i
] * 536870912.0f
);
396 pOut
[i
] += pIn
[i
] > 0 ? 0.5 : -0.5;
398 if (pIn
[i
] == (float) 4.0)
400 pOut
[i
] = 0x7FFFFFFF;
407 * @brief Converts float to fixed q28 format
408 * @param uint32_t number of samples in the buffer
410 * The function converts floating point values to fixed point values
413 void arm_float_to_q28 (float *pIn
, q31_t
* pOut
,
418 for (i
= 0; i
< numSamples
; i
++)
420 /* 268435456.0f corresponds to pow(2, 28) */
421 pOut
[i
] = (q31_t
) (pIn
[i
] * 268435456.0f
);
423 pOut
[i
] += pIn
[i
] > 0 ? 0.5 : -0.5;
425 if (pIn
[i
] == (float) 8.0)
427 pOut
[i
] = 0x7FFFFFFF;
433 * @brief Clip the float values to +/- 1
434 * @param pIn input buffer
435 * @param numSamples number of samples in the buffer
437 * The function converts floating point values to fixed point values
440 void arm_clip_f32 (float *pIn
, uint32_t numSamples
)
444 for (i
= 0; i
< numSamples
; i
++)
450 else if( pIn
[i
] < -1.0f
)