完善资料让更多小伙伴认识你,还能领取20积分哦, 立即完善>
本期教程主要讲解math_help文件中函数的使用,这个文件也是ARM官方提供的,这些函数相对都比较容易,同时使用频率也很高。希望初学的同学学习并掌握。
23.1 初学者重要提示 math_help文件路径,本教程配套的任意一个例子如下路径都有此文件:LibrariesCMSISDSPExamplesARMarm_graphic_equalizer_example。 23.2 函数目录 在文件math_help文件中主要有以下函数: float arm_snr_f32(float *pRef, float *pTest, uint32_t buffSize) uint32_t arm_compare_fixed_q31(q31_t *pIn, q31_t * pOut, uint32_t numSamples) uint32_t arm_compare_fixed_q15(q15_t *pIn, q15_t *pOut, uint32_t numSamples) void arm_provide_guard_bits_q31 (q31_t * input_buf, uint32_t blockSize,uint32_t guard_bits) void arm_provide_guard_bits_q15 (q15_t * input_buf, uint32_t blockSize, uint32_t guard_bits) void arm_provide_guard_bits_q7 (q7_t * input_buf, uint32_t blockSize, uint32_t guard_bits) 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_calc_2pow(uint32_t numShifts) void arm_clip_f32 (float *pIn, uint32_t numSamples) void arm_float_to_q12_20(float *pIn, q31_t * pOut, uint32_t numSamples) void arm_float_to_q14 (float *pIn, q15_t *pOut, uint32_t numSamples) void arm_float_to_q28 (float *pIn, q31_t *pOut, uint32_t numSamples) void arm_float_to_q29 (float *pIn, q31_t *pOut, uint32_t numSamples) void arm_float_to_q30 (float *pIn, q31_t * pOut, uint32_t numSamples) 23.3 函数讲解 23.3.1 函数arm_snr_f32 这个函数用于求信噪比: /** * @brief Caluclation of SNR * @param[in] pRef Pointer to the reference buffer * @param[in] pTest Pointer to the test buffer * @param[in] buffSize total number of samples * @return 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++) { /* 检测是否为NAN非值 */ test = (int *)(&pRef); temp = *test; if (temp == 0x7FC00000) { return(0); } test = (int *)(&pTest); temp = *test; if (temp == 0x7FC00000) { return(0); } EnergySignal += pRef * pRef; EnergyError += (pRef - pTest) * (pRef - pTest); } test = (int *)(&EnergyError); temp = *test; if (temp == 0x7FC00000) { return(0); } SNR = 10 * log10 (EnergySignal / EnergyError); return (SNR); } 信噪比,即SNR(Signal to Noise Ratio)是指一个电子设备或者电子系统中信号与噪声的比例。这里面的信号指的是来自设备外部需要通过这台设备进行处理的电子信号,噪声是指经过该设备后产生的原信号中并不存在的无规则的额外信号(或信息),并且该种信号并不随原信号的变化而变化。 狭义来讲是指放大器的输出信号功率与输出噪声功率的比,常常用分贝数表示,设备的信噪比越高表明它产生的杂音越少。一般来说,信噪比越大,说明混在信号里的噪声越小。 公式如下:有用信号功率(Power of Signal)与噪声功率(Power of Noise)的比,那么幅度(Amplitude)平方的比如下: 单位一般使用分贝,其值为十倍对数的信号功率与噪声功率比: 其中 程序里面的10 * log10 (EnergySignal / EnergyError)对应的就是上面的公式实现。 23.3.2 函数arm_compare_fixed_q31 /** * @brief Compare MATLAB Reference Output and ARM Test output * @param q31_t* Pointer to Ref buffer * @param q31_t* Pointer to Test buffer * @param uint32_t number of samples in the buffer * @return none */ uint32_t arm_compare_fixed_q31(q31_t *pIn, q31_t * pOut, uint32_t numSamples) { uint32_t i; int32_t diff, diffCrnt = 0; uint32_t maxDiff = 0; for (i = 0; i < numSamples; i++) { diff = pIn - pOut; diffCrnt = (diff > 0) ? diff : -diff; if(diffCrnt > maxDiff) { maxDiff = diffCrnt; } } return(maxDiff); } 这个函数用于对比matlab和ARM实际计算的输出,并返回最大的差值(Q31)。 23.3.3 函数arm_compare_fixed_q15 /** * @brief Compare MATLAB Reference Output and ARM Test output * @param q15_t* Pointer to Ref buffer * @param q15_t* Pointer to Test buffer * @param uint32_t number of samples in the buffer * @return none */ uint32_t arm_compare_fixed_q15(q15_t *pIn, q15_t * pOut, uint32_t numSamples) { uint32_t i; int32_t diff, diffCrnt = 0; uint32_t maxDiff = 0; for (i = 0; i < numSamples; i++) { diff = pIn - pOut; diffCrnt = (diff > 0) ? diff : -diff; if(diffCrnt > maxDiff) { maxDiff = diffCrnt; } } return(maxDiff); } 这个函数用于对比matlab和ARM实际计算的输出,并返回最大的差值(Q15)。 23.3.4 函数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 = input_buf >> guard_bits; } } 这个函数用于给q31_t类型的数据提供保护位,防止溢出。从函数的实现上看,保护位的实现是通过右移数据实现的。 23.3.5 函数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 = input_buf >> guard_bits; } } 这个函数用于给q15_t类型的数据提供保护位,防止溢出。 23.3.6 函数arm_provide_guard_bits_q7 /** * @brief Provide guard bits for Input buffer * @param[in,out] input_buf Pointer to input buffer * @param[in] blockSize block Size * @param[in] guard_bits guard bits * @return none * The function Provides the guard bits for the buffer * to avoid overflow */ void arm_provide_guard_bits_q7 (q7_t * input_buf, uint32_t blockSize, uint32_t guard_bits) { uint32_t i; for (i = 0; i < blockSize; i++) { input_buf = input_buf >> guard_bits; } } 这个函数用于给q7_t类型的数据提供保护位,防止溢出。 23.3.7 函数arm_calc_guard_bits /** * @brief Caluclates number of guard bits * @param[in] num_adds number of additions * @return guard bits * The function Caluclates the number of guard bits * depending on the numtaps */ uint32_t arm_calc_guard_bits (uint32_t num_adds) { uint32_t i = 1, j = 0; if (num_adds == 1) { return (0); } while (i < num_adds) { i = i * 2; j++; } return (j); } 这个函数是根据加数的个数来计算最终结果需要的保护位格式,从而防止溢出。 23.3.8 函数arm_apply_guard_bits /** * @brief Apply guard bits to buffer * @param[in,out] pIn pointer to input buffer * @param[in] numSamples number of samples in the input buffer * @param[in] guard_bits guard bits * @return none */ void arm_apply_guard_bits (float32_t *pIn, uint32_t numSamples, uint32_t guard_bits) { uint32_t i; for (i = 0; i < numSamples; i++) { pIn = pIn * arm_calc_2pow(guard_bits); } } 这个函数不是很理解在实际应用中的作用。 23.3.9 函数arm_calc_2pow /** * @brief Calculates pow(2, numShifts) * @param uint32_t number of shifts * @return pow(2, numShifts) */ uint32_t arm_calc_2pow(uint32_t numShifts) { uint32_t i, val = 1; for (i = 0; i < numShifts; i++) { val = val * 2; } return(val); } 这个函数用于求解2的n次方。 23.3.10 函数arm_clip_f32 /** * @brief Clip the float values to +/- 1 * @param[in,out] pIn input buffer * @param[in] numSamples number of samples in the buffer * @return none * The function converts floating point values to fixed point values */ void arm_clip_f32 (float *pIn, uint32_t numSamples) { uint32_t i; for (i = 0; i < numSamples; i++) { if (pIn > 1.0f) { pIn = 1.0; } else if ( pIn < -1.0f) { pIn = -1.0; } } } 浮点数裁剪函数,大于1的赋值为1,小于-1的赋值为-1。 23.3.11 函数arm_float_to_q12_20 /** * @brief Converts float to fixed in q12.20 format * @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 = (q31_t) (pIn * 1048576.0f); pOut += pIn > 0 ? 0.5 : -0.5; if (pIn == (float) 1.0) { pOut = 0x000FFFFF; } } } 这个函数用于将浮点数转换成Q12.20格式的数据。 浮点数转换成Q12.20格式需要乘以2^20,然后再做舍入处理,特别注意这里的浮点数末尾要加符号f,这样才能保证是单精度。 23.3.12 函数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 = (q15_t) (pIn * 16384.0f); pOut += pIn > 0 ? 0.5 : -0.5; if (pIn == (float) 2.0) { pOut = 0x7FFF; } } } 这个函数用于将浮点数转换成Q14格式的数据。 浮点数转换成Q14格式需要乘以2^14,然后再做舍入处理。 23.3.13 函数arm_float_to_q28 /** * @brief Converts float to fixed q28 format * @param[in] pIn pointer to input buffer * @param[out] pOut pointer to output buffer * @param[in] numSamples number of samples in the buffer * @return none * The function converts floating point values to fixed point values */ void arm_float_to_q28 (float *pIn, q31_t *pOut, uint32_t numSamples) { uint32_t i; for (i = 0; i < numSamples; i++) { /* 268435456.0f corresponds to pow(2, 28) */ pOut = (q31_t) (pIn * 268435456.0f); pOut += pIn > 0 ? 0.5 : -0.5; if (pIn == (float) 8.0) { pOut = 0x7FFFFFFF; } } } 这个函数用于将浮点数转换成Q28格式的数据。 浮点数转换成Q28格式需要乘以2^28,然后再做舍入处理。 23.3.14 函数arm_float_to_q29 /** * @brief Converts float to fixed q30 format * @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_q29 (float *pIn, q31_t * pOut, uint32_t numSamples) { uint32_t i; for (i = 0; i < numSamples; i++) { /* 536870912.0f corresponds to pow(2, 29) */ pOut = (q31_t) (pIn * 536870912.0f); pOut += pIn > 0 ? 0.5 : -0.5; if (pIn == (float) 4.0) { pOut = 0x7FFFFFFF; } } } 这个函数用于将浮点数转换成Q29格式的数据。 浮点数转换成Q29格式需要乘以2^29,然后再做舍入处理。 23.3.15 函数arm_float_to_q30 /** * @brief Converts float to fixed q30 format * @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_q29 (float *pIn, q31_t * pOut, uint32_t numSamples) { uint32_t i; for (i = 0; i < numSamples; i++) { /* 1073741824.0f corresponds to pow(2, 30) */ pOut = (q31_t) (pIn * 536870912.0f); pOut += pIn > 0 ? 0.5 : -0.5; if (pIn == (float) 4.0) { pOut = 0x7FFFFFFF; } } } 这个函数用于将浮点数转换成Q30格式的数据。 浮点数转换成Q30格式需要乘以2^30,然后再做舍入处理。 23.4 总结 本期教程就跟大家讲这么多,本期教程就不配套例子了,这些函数会在后面的应用中都用到。 |
|
|
|
只有小组成员才能发言,加入小组>>
调试STM32H750的FMC总线读写PSRAM遇到的问题求解?
1980 浏览 1 评论
X-NUCLEO-IHM08M1板文档中输出电流为15Arms,15Arms是怎么得出来的呢?
1764 浏览 1 评论
1233 浏览 2 评论
STM32F030F4 HSI时钟温度测试过不去是怎么回事?
819 浏览 2 评论
ST25R3916能否对ISO15693的标签芯片进行分区域写密码?
1777 浏览 2 评论
2016浏览 9评论
STM32仿真器是选择ST-LINK还是选择J-LINK?各有什么优势啊?
895浏览 4评论
stm32f4下spi+dma读取数据不对是什么原因导致的?
320浏览 3评论
STM32F0_TIM2输出pwm2后OLED变暗或者系统重启是怎么回事?
673浏览 3评论
662浏览 3评论
小黑屋| 手机版| Archiver| 电子发烧友 ( 湘ICP备2023018690号 )
GMT+8, 2025-2-24 19:51 , Processed in 0.856370 second(s), Total 77, Slave 61 queries .
Powered by 电子发烧友网
© 2015 bbs.elecfans.com
关注我们的微信
下载发烧友APP
电子发烧友观察
版权所有 © 湖南华秋数字科技有限公司
电子发烧友 (电路图) 湘公网安备 43011202000918 号 电信与信息服务业务经营许可证:合字B2-20210191