特别说明:完整45期数字信号处理教程,原创高性能示波器代码全开源地址:链接
第22章 math_help中函数的使用
本期教程主要讲解math_help文件中函数的使用,这个文件也是ARM官方提供的,这些函数相对都比较容易,同时使用频率也很高。希望初学的同学学习并掌握。 22.1 函数讲解 22.3 总结
22.1 函数讲解
22.1.1 函数目录 在文件math_help文件中主要有以下函数: - float arm_snr_f32(float *pRef, float *pTest, uint32_t buffSize);
- void arm_float_to_q12_20(float *pIn, q31_t * pOut, uint32_t numSamples);
- void arm_provide_guard_bits_q15(q15_t *input_buf, uint32_t blockSize, uint32_t guard_bits);
- void arm_provide_guard_bits_q31(q31_t *input_buf, uint32_t blockSize, uint32_t guard_bits);
- void arm_float_to_q14(float *pIn, q15_t *pOut, uint32_t numSamples);
- void arm_float_to_q30(float *pIn, q31_t *pOut, uint32_t numSamples);
- void arm_clip_f32(float *pIn, uint32_t numSamples);
- uint32_t arm_calc_guard_bits(uint32_t num_adds);
- void arm_apply_guard_bits (float32_t * pIn, uint32_t numSamples, uint32_t guard_bits);
- uint32_t arm_compare_fixed_q15(q15_t *pIn, q15_t * pOut, uint32_t numSamples);
- uint32_t arm_compare_fixed_q31(q31_t *pIn, q31_t *pOut, uint32_t numSamples);
- uint32_t arm_calc_2pow(uint32_t guard_bits);
复制代码
22.1.2 arm_snr_f32 这个函数用于求信噪比: - /** (1)
- * @brief Caluclation of SNR
- * @param float* Pointer to the reference buffer
- * @param float* Pointer to the test buffer
- * @param uint32_t total number of samples
- * @return float SNR
- * The function Caluclates signal to noise ratio for the reference output
- * and test output
- */
-
- float arm_snr_f32(float *pRef, float *pTest, uint32_t buffSize)
- {
- float EnergySignal = 0.0, EnergyError = 0.0;
- uint32_t i;
- float SNR;
- int temp;
- int *test;
-
- for (i = 0; i < buffSize; i++)
- {
- /* Checking for a NAN value in pRef array */
- test = (int *)(&pRef[i]);
- temp = *test;
-
- if(temp == 0x7FC00000)
- {
- return(0);
- }
-
- /* Checking for a NAN value in pTest array */
- test = (int *)(&pTest[i]);
- temp = *test;
-
- if(temp == 0x7FC00000)
- {
- return(0);
- }
- EnergySignal += pRef[i] * pRef[i];
- EnergyError += (pRef[i] - pTest[i]) * (pRef[i] - pTest[i]);
- }
-
- /* Checking for a NAN value in EnergyError */
- test = (int *)(&EnergyError);
- temp = *test;
-
- if(temp == 0x7FC00000)
- {
- return(0);
- }
-
- SNR = 10 * log10 (EnergySignal / EnergyError); (2)
-
- return (SNR);
-
- }
复制代码1. 这里先补充一些信噪比方面的基础知识: 信噪比,即SNR(Signal to Noise Ratio),又称为讯噪比。反映摄像机成像的抗干扰能力,反应在画质上就是画面是否干净无噪点;狭义来讲是指 放大器的输出 信号的功率与同时输出的 噪声功率的比,常常用分贝数表示,设备的信噪比越高表明它产生的杂音越少。一般来说,信噪比越大,说明混在 信号里的噪声越小,声音回放的音质量越高,否则相反。信噪比一般不应该低于70dB,高保真 音箱的信噪比应达到110dB以上。公式如下:
关于信噪比更详细的知识,大家可以查阅相关资料进行了解。 2. 这里实现的就是上面所说的信噪比公式。
22.1.3 arm_float_to_q12_20- /**
- * @brief Converts float to fixed in q12.20 format (1)
- * @param uint32_t number of samples in the buffer
- * @return none
- * The function converts floating point values to fixed point(q12.20) values
- */
-
- void arm_float_to_q12_20(float *pIn, q31_t * pOut, uint32_t numSamples)
- {
- uint32_t i;
-
- for (i = 0; i < numSamples; i++)
- {
- /* 1048576.0f corresponds to pow(2, 20) */
- pOut[i] = (q31_t) (pIn[i] * 1048576.0f); (2)
-
- pOut[i] += pIn[i] > 0 ? 0.5 : -0.5;
-
- if (pIn[i] == (float) 1.0)
- {
- pOut[i] = 0x000FFFFF;
- }
- }
- }
复制代码1. 这个函数用于将浮点数转换成Q12.20格式的数据。 2. 浮点数转换成Q12.20格式需要乘以2^20,然后再做舍入处理,特别注意这里的浮点数末尾要加符号f,这样才能保证是单精度。
22.1.4 arm_provide_guard_bits_q15- /**
- * @brief Provide guard bits for Input buffer
- * @param q15_t* Pointer to input buffer
- * @param uint32_t blockSize
- * @param uint32_t guard_bits
- * @return none
- * The function Provides the guard bits for the buffer
- * to avoid overflow
- */
-
- void arm_provide_guard_bits_q15 (q15_t * input_buf, uint32_t blockSize,
- uint32_t guard_bits)
- {
- uint32_t i;
-
- for (i = 0; i < blockSize; i++)
- {
- input_buf[i] = input_buf[i] >> guard_bits;
- }
- }
复制代码1. 这个函数用于给q15_t类型的数据提供保护位,防止溢出。从函数的实现上看,保护位的实现是通过右移数据实现的。
22.1.5 arm_provide_guard_bits_q31- /**
- * @brief Provide guard bits for Input buffer
- * @param q31_t* Pointer to input buffer
- * @param uint32_t blockSize
- * @param uint32_t guard_bits
- * @return none
- * The function Provides the guard bits for the buffer
- * to avoid overflow
- */
-
- void arm_provide_guard_bits_q31 (q31_t * input_buf,
- uint32_t blockSize,
- uint32_t guard_bits)
- {
- uint32_t i;
-
- for (i = 0; i < blockSize; i++)
- {
- input_buf[i] = input_buf[i] >> guard_bits;
- }
- }
复制代码1. 这个函数用于给q31_t类型的数据提供保护位,防止溢出。从函数的实现上看,保护位的实现是通过右移数据实现的。
22.1.6 arm_float_to_q14- /**
- * @brief Converts float to fixed q14
- * @param uint32_t number of samples in the buffer
- * @return none
- * The function converts floating point values to fixed point values
- */
-
- void arm_float_to_q14 (float *pIn, q15_t * pOut,
- uint32_t numSamples)
- {
- uint32_t i;
-
- for (i = 0; i < numSamples; i++)
- {
- /* 16384.0f corresponds to pow(2, 14) */
- pOut[i] = (q15_t) (pIn[i] * 16384.0f);
-
- pOut[i] += pIn[i] > 0 ? 0.5 : -0.5;
-
- if (pIn[i] == (float) 2.0)
- {
- pOut[i] = 0x7FFF;
- }
-
- }
-
- }
复制代码1. 这个函数用于将浮点数转换成Q14格式的数据。 2. 浮点数转换成Q14格式需要乘以2^14,然后再做舍入处理,特别注意这里的浮点数末尾要加符号f,这样才能保证是单精度。
|
22.1.8 arm_calc_guard_bits
22.1.9 arm_apply_guard_bits
22.1.10 arm_calc_2pow
22.1.11 arm_compare_fixed_q15
22.1.12 arm_compare_fixed_q15
22.2 总结