zoukankan      html  css  js  c++  java
  • 【STM32F429的DSP教程】第23章 DSP辅助运算-math_help中函数的使用

    完整版教程下载地址:http://www.armbbs.cn/forum.php?mod=viewthread&tid=94547

    第23章       DSP辅助运算-math_help中函数的使用

    本期教程主要讲解math_help文件中函数的使用,这个文件也是ARM官方提供的,这些函数相对都比较容易,同时使用频率也很高。希望初学的同学学习并掌握。

    23.1 初学者重要提示

    23.2 函数目录

    23.3 函数讲解

    23.4 总结

    23.1 初学者重要提示

    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[i]);
          temp =  *test;
    
          if (temp == 0x7FC00000)
          {
                  return(0);
          }
    
          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]);
        }
    
        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[i] - pOut[i];
          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[i] - pOut[i];
          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[i] = input_buf[i] >> 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[i] = input_buf[i] >> 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[i] = input_buf[i] >> 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[i] = pIn[i] * 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[i] > 1.0f)
          {
            pIn[i] = 1.0;
          }
          else if ( pIn[i] < -1.0f)
          {
            pIn[i] = -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[i] = (q31_t) (pIn[i] * 1048576.0f);                                                        
    
          pOut[i] += pIn[i] > 0 ? 0.5 : -0.5;
    
          if (pIn[i] == (float) 1.0)
            {
              pOut[i] = 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[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;
            }
        }
    }
    •   这个函数用于将浮点数转换成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[i] = (q31_t) (pIn[i] * 268435456.0f);
    
          pOut[i] += pIn[i] > 0 ? 0.5 : -0.5;
    
          if (pIn[i] == (float) 8.0)
            {
              pOut[i] = 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[i] = (q31_t) (pIn[i] * 536870912.0f);
    
          pOut[i] += pIn[i] > 0 ? 0.5 : -0.5;
    
          if (pIn[i] == (float) 4.0)
            {
              pOut[i] = 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[i] = (q31_t) (pIn[i] * 536870912.0f);
    
          pOut[i] += pIn[i] > 0 ? 0.5 : -0.5;
    
          if (pIn[i] == (float) 4.0)
            {
              pOut[i] = 0x7FFFFFFF;
            }
        }
    }
    •   这个函数用于将浮点数转换成Q30格式的数据。
    •   浮点数转换成Q30格式需要乘以2^30,然后再做舍入处理。

    23.4 总结

    本期教程就跟大家讲这么多,本期教程就不配套例子了,这些函数会在后面的应用中都用到。

  • 相关阅读:
    wap2app(五)-- 微信授权登录以及踩过的坑
    wap2app(六)-- wap2app的原生标题头无法隐藏
    创建对象的几种模式
    通过url动态获取图片大小方法总结
    wap2app(三)-- 添加引导页
    wap2app(二)-- 设置APP系统状态栏
    wap2app(一)-- 网站快速打包成app
    树叶飘落、雪花飘落等同时多个图片飘落
    基于bootstrap的双日历插件 daterangepicker
    FileProvider解决FileUriExposedException
  • 原文地址:https://www.cnblogs.com/armfly/p/12880981.html
Copyright © 2011-2022 走看看