STM32/STM8技术论坛
直播中

硬汉Eric2013

9年用户 1035经验值
擅长:嵌入式技术
私信 关注
[经验]

【安富莱——DSP教程】第20章 MatrixFunctions的使用(二)

第20章  MatrixFunctions的使用(二)
    本期教程主要讲解矩阵运算中的放缩,乘法和转置。
    20.1 矩阵放缩MatScale
    20.2 矩阵乘法MatMult
    20.3 转置矩阵MatTrans
    20.4 总结

回帖(4)

硬汉Eric2013

2015-6-22 11:51:09
20.1  矩阵放缩 MatScale
20.1.1  arm_mat_scale_f32
公式描述:
20.1.png
                              
函数定义如下:
arm_status arm_mat_scale_f32(
  const arm_matrix_instance_f32 * pSrc,
  float32_t scale,
  arm_matrix_instance_f32 * pDst)
参数定义:
[in]       *pSrc   points to input matrix structure        
[in]       scale   scale factor to be applied         
[out]      *pDst  points to output matrix structure        
return    The function returns eitherARM_MATH_SIZE_MISMATCH   

20.1.2  arm_mat_scale_q31
函数定义如下:
arm_statusarm_mat_scale_q31(
  const arm_matrix_instance_q31 * pSrc,
  q31_t scaleFract,
  int32_t shift,
  arm_matrix_instance_q31 * pDst)
参数定义:
[in]      *pSrc      points to input matrix        
[in]      scaleFract  fractional portion of the scale factor        
[in]      shift       number of bits to shift the resultby        
[out]    *pDst      points to output matrix structure        
return     The function returns either   
注意事项:
1.    两个1.31格式的数据相乘产生2.62格式的数据,最终结果要做偏移和饱和运算产生1.31格式数据。
2.    定点数的最终放缩比例计算是:scale = scaleFract *2^shift.

20.1.3  arm_mat_scale_q15
函数定义如下:
arm_statusarm_mat_scale_q15(
  const arm_matrix_instance_q15 * pSrc,
  q15_t scaleFract,
  int32_t shift,
  arm_matrix_instance_q15 * pDst)
参数定义:
[in,out] *S         pointsto an instance of the floating-point matrix structure.   
[in]     nRows   number of rows in the matrix.   
[in]     nColumns number of columns in thematrix.   
[in]     *pData     points to the matrix data array.  
注意事项:
1.    两个1.15格式的数据相乘产生2.30格式的数据,最终结果要做偏移和饱和运算产生1.15格式数据。
2.    定点数的最终放缩比例计算是:scale = scaleFract *2^shift.

20.1.4  实例讲解
实验目的:
1. 学习MatrixFunctions中矩阵的放缩
实验内容:
       1. 按下按键K1, 串口打印函数DSP_MatScale的输出结果
实验现象:
       通过窗口上位机软件SecureCRT(V5光盘里面有此软件)查看打印信息现象如下:
          20.2.jpg
程序设计:
  1. /*
  2. *********************************************************************************************************
  3. *    函 数 名: DSP_MatScale
  4. *    功能说明: 矩阵放缩
  5. *    形    参:无
  6. *    返 回 值: 无
  7. *********************************************************************************************************
  8. */
  9. static void DSP_MatScale(void)
  10. {
  11.      uint8_t i;
  12.    
  13.      /****浮点数数组******************************************************************/
  14.      float32_t pDataA[9] = {1.1f, 1.1f, 2.1f, 2.1f, 3.1f, 3.1f, 4.1f, 4.1f, 5.1f};
  15.      float32_t scale = 1.1f;
  16.      float32_t pDataDst[9];
  17.    
  18.      arm_matrix_instance_f32 pSrcA; //3行3列数据
  19.      arm_matrix_instance_f32 pDst;
  20.    
  21.      /****定点数Q31数组******************************************************************/
  22.      q31_t pDataA1[9] = {1, 1, 2, 2, 3, 3, 4, 4, 5};
  23.      q31_t scaleFract = 10;
  24.      int32_t shift = 0;
  25.      q31_t pDataDst1[9];
  26.    
  27.      arm_matrix_instance_q31 pSrcA1; //3行3列数据
  28.      arm_matrix_instance_q31 pDst1;
  29.    
  30.      /****定点数Q15数组******************************************************************/
  31.      q15_t pDataA2[9] = {1, 1, 2, 2, 3, 3, 4, 4, 5};
  32.      q15_t scaleFract1 = 10;
  33.      int32_t shift1 = 0;
  34.      q15_t pDataDst2[9];
  35.    
  36.      arm_matrix_instance_q15 pSrcA2; //3行3列数据
  37.      arm_matrix_instance_q15 pDst2;
  38.    
  39.      /****浮点数***********************************************************************/
  40.      pSrcA.numCols = 3;
  41.      pSrcA.numRows = 3;
  42.      pSrcA.pData = pDataA;

  43.      pDst.numCols = 3;
  44.      pDst.numRows = 3;
  45.      pDst.pData = pDataDst;
  46.    
  47.      printf("****浮点数******************************************rn");
  48.      arm_mat_scale_f32(&pSrcA, scale, &pDst);
  49.      for(i = 0; i < 9; i++)
  50.      {
  51.          printf("pDataDst[%d] = %frn", i, pDataDst[i]);
  52.      }
  53.    
  54.    
  55.      /****定点数Q31***********************************************************************/
  56.      pSrcA1.numCols = 3;
  57.      pSrcA1.numRows = 3;
  58.      pSrcA1.pData = pDataA1;
  59.    
  60.      pDst1.numCols = 3;
  61.      pDst1.numRows = 3;
  62.      pDst1.pData = pDataDst1;
  63.    
  64.      printf("****定点数Q31******************************************rn");
  65.      arm_mat_scale_q31(&pSrcA1, scaleFract, shift, &pDst1);
  66.      for(i = 0; i < 9; i++)
  67.      {
  68.          printf("pDataDst1[%d] = %drn", i, pDataDst1[i]);
  69.      }
  70.    
  71.      /****定点数Q15***********************************************************************/
  72.      pSrcA2.numCols = 3;
  73.      pSrcA2.numRows = 3;
  74.      pSrcA2.pData = pDataA2;
  75.    
  76.      pDst2.numCols = 3;
  77.      pDst2.numRows = 3;
  78.      pDst2.pData = pDataDst2;
  79.    
  80.      printf("****定点数Q15******************************************rn");
  81.      arm_mat_scale_q15(&pSrcA2, scaleFract1, shift1, &pDst2);
  82.      for(i = 0; i < 9; i++)
  83.      {
  84.          printf("pDataDst2[%d] = %drn", i, pDataDst2[i]);
  85.      }
  86. }
1.    下面通过matlab来实现矩阵的放缩:
20.3.jpg

举报

硬汉Eric2013

2015-6-22 11:55:43
20.2  矩阵乘法 MatMult
20.2.1  arm_mat_mult_f32
公式描述:
20.4.png
                              
函数定义如下:
arm_statusarm_mat_mult_f32(
  const arm_matrix_instance_f32 * pSrcA,
  const arm_matrix_instance_f32 * pSrcB,
  arm_matrix_instance_f32 * pDst)
参数定义:
[in]       *pSrcA  points to the first input matrixstructure   
[in]       *pSrcB  points to the second input matrixstructure   
[out]      *pDst  points to output matrix structure   
return     The function returns either   
注意事项:
1.    两个矩阵M x N和N x P相乘的结果是M x P.(必须保证一个矩形的列数等于另一个矩阵的行数)。

20.2.2  arm_mat_mult_q31
函数定义如下:
arm_statusarm_mat_mult_q31(
  const arm_matrix_instance_q31 * pSrcA,
  const arm_matrix_instance_q31 * pSrcB,
  arm_matrix_instance_q31 * pDst)
参数定义:
[in]    *pSrcA  points to the first input matrixstructure   
[in]    *pSrcB  points to the second input matrixstructure   
[out]  *pDst   points to output matrix structure   
return              Thefunction returns either
注意事项:
1.    两个1.31格式的数据相乘产生2.62格式的数据,最终结果要做偏移和饱和运算产生1.31格式数据。
2.    两个矩阵M x N和N x P相乘的结果是M x P.(必须保证一个矩形的列数等于另一个矩阵的行数)。

20.2.3  arm_mat_mult_q15
函数定义如下:
arm_statusarm_mat_mult_q15(
  const arm_matrix_instance_q15 * pSrcA,
  const arm_matrix_instance_q15 * pSrcB,
  arm_matrix_instance_q15 * pDst,
  q15_t * pState CMSIS_UNUSED)
参数定义:
[in]     *pSrcA   points to the first input matrixstructure   
[in]     *pSrcB  points to the second input matrixstructure   
[out]   *pDst    points to output matrix structure   
[in]        *pState  points to the array for storing intermediateresults   
return               The function returns either   
注意事项:
1.    两个1.15格式数据相乘是2.30格式,函数的内部使用了64位的累加器,那个就是34.30格式,最终结果将低15位截取掉并作饱和处理为1.15格式。
2.    两个矩阵M x N和N x P相乘的结果是M x P.(必须保证一个矩形的列数等于另一个矩阵的行数)。

20.2.4  arm_mat_mult_fast_q31
函数定义如下:
arm_statusarm_mat_mult_fast_q31(
  const arm_matrix_instance_q31 * pSrcA,
  const arm_matrix_instance_q31 * pSrcB,
  arm_matrix_instance_q31 * pDst)
参数定义:
[in]    *pSrcA  points to the first input matrixstructure   
[in]    *pSrcB  points to the second input matrixstructure   
[out]  *pDst   points to output matrix structure   
return              Thefunction returns either
注意事项:
1.    两个1.31格式的数据相乘产生2.62格式的数据,最终结果要做偏移和饱和运算产生1.31格式数据。
2.    两个矩阵M x N和N x P相乘的结果是M x P.(必须保证一个矩形的列数等于另一个矩阵的行数)。
3.    函数arm_mat_mult_fast_q31是arm_mat_mult_q31的快速算法。

20.2.5  arm_mat_mult_fast_q15
函数定义如下:
arm_statusarm_mat_mult_fast_q15(
  const arm_matrix_instance_q15 * pSrcA,
  const arm_matrix_instance_q15 * pSrcB,
  arm_matrix_instance_q15 * pDst,
  q15_t * pState)
参数定义:
[in]     *pSrcA   points to the first input matrixstructure   
[in]     *pSrcB  points to the second input matrixstructure   
[out]   *pDst    points to output matrix structure   
[in]        *pState  points to the array for storing intermediateresults   
return               The function returns either   
注意事项:
1.    两个1.15格式数据相乘是2.30格式,函数的内部使用了64位的累加器,那个就是34.30格式,最终结果将低15位截取掉并作饱和处理为1.15格式。
2.    两个矩阵M x N和N x P相乘的结果是M x P.(必须保证一个矩形的列数等于另一个矩阵的行数)。
3.    函数arm_mat_mult_fast_q15是arm_mat_mult_q15的快速算法。

20.2.6  实例讲解
实验目的:
1. 学习MatrixFunctions中矩阵乘法
实验内容:
       1. 按下按键K2, 串口打印函数DSP_MatMult的输出结果
实验现象:
       通过窗口上位机软件SecureCRT(V5光盘里面有此软件)查看打印信息现象如下:
20.5.jpg
程序设计:
  1. /*
  2. *********************************************************************************************************
  3. *    函 数 名: DSP_MatMult
  4. *    功能说明: 矩阵乘法
  5. *    形    参:无
  6. *    返 回 值: 无
  7. *********************************************************************************************************
  8. */
  9. static void DSP_MatMult(void)
  10. {
  11.      uint8_t i;
  12.    
  13.      /****浮点数数组******************************************************************/
  14.      float32_t pDataA[9] = {1.1f, 1.1f, 2.1f, 2.1f, 3.1f, 3.1f, 4.1f, 4.1f, 5.1f};
  15.      float32_t pDataB[9] = {1.1f, 1.1f, 2.1f, 2.1f, 3.1f, 3.1f, 4.1f, 4.1f, 5.1f};
  16.      float32_t pDataDst[9];
  17.    
  18.      arm_matrix_instance_f32 pSrcA; //3行3列数据
  19.      arm_matrix_instance_f32 pSrcB; //3行3列数据
  20.      arm_matrix_instance_f32 pDst;
  21.    
  22.      /****定点数Q31数组******************************************************************/
  23.      q31_t pDataA1[9] = {1, 1, 2, 2, 3, 3, 4, 4, 5};
  24.      q31_t pDataB1[9] = {1, 1, 2, 2, 3, 3, 4, 4, 5};
  25.      q31_t pDataDst1[9];
  26.    
  27.      arm_matrix_instance_q31 pSrcA1; //3行3列数据
  28.      arm_matrix_instance_q31 pSrcB1; //3行3列数据
  29.      arm_matrix_instance_q31 pDst1;
  30.    
  31.      /****定点数Q15数组******************************************************************/
  32.      q15_t pDataA2[9] = {1, 1, 2, 2, 3, 3, 4, 4, 5};
  33.      q15_t pDataB2[9] = {1, 1, 2, 2, 3, 3, 4, 4, 5};
  34.      q15_t pDataDst2[9];
  35.    
  36.      arm_matrix_instance_q15 pSrcA2; //3行3列数据
  37.      arm_matrix_instance_q15 pSrcB2; //3行3列数据
  38.      arm_matrix_instance_q15 pDst2;
  39.      q15_t pState;
  40.    
  41.      /****浮点数***********************************************************************/
  42.      pSrcA.numCols = 3;
  43.      pSrcA.numRows = 3;
  44.      pSrcA.pData = pDataA;
  45.    
  46.      pSrcB.numCols = 3;
  47.      pSrcB.numRows = 3;
  48.      pSrcB.pData = pDataB;
  49.    
  50.      pDst.numCols = 3;
  51.      pDst.numRows = 3;
  52.      pDst.pData = pDataDst;
  53.    
  54.      printf("****浮点数******************************************rn");
  55.      arm_mat_mult_f32(&pSrcA, &pSrcB, &pDst);
  56.      for(i = 0; i < 9; i++)
  57.      {
  58.          printf("pDataDst[%d] = %frn", i, pDataDst[i]);
  59.      }
  60.    
  61.      /****定点数Q31***********************************************************************/
  62.      pSrcA1.numCols = 3;
  63.      pSrcA1.numRows = 3;
  64.      pSrcA1.pData = pDataA1;
  65.    
  66.      pSrcB1.numCols = 3;
  67.      pSrcB1.numRows = 3;
  68.      pSrcB1.pData = pDataB1;
  69.    
  70.      pDst1.numCols = 3;
  71.      pDst1.numRows = 3;
  72.      pDst1.pData = pDataDst1;
  73.    
  74.      printf("****定点数Q31******************************************rn");
  75.      arm_mat_mult_q31(&pSrcA1, &pSrcB1, &pDst1);
  76.      arm_mat_mult_fast_q31(&pSrcA1, &pSrcB1, &pDst1);
  77.      for(i = 0; i < 9; i++)
  78.      {
  79.          printf("pDataDst1[%d] = %drn", i, pDataDst1[i]);
  80.      }
  81.    
  82.      /****定点数Q15***********************************************************************/
  83.      pSrcA2.numCols = 3;
  84.      pSrcA2.numRows = 3;
  85.      pSrcA2.pData = pDataA2;
  86.    
  87.      pSrcB2.numCols = 3;
  88.      pSrcB2.numRows = 3;
  89.      pSrcB2.pData = pDataB2;
  90.    
  91.      pDst2.numCols = 3;
  92.      pDst2.numRows = 3;
  93.      pDst2.pData = pDataDst2;
  94.    
  95.      printf("****定点数Q15******************************************rn");
  96.      arm_mat_mult_q15(&pSrcA2, &pSrcB2, &pDst2, &pState);
  97.      arm_mat_mult_fast_q15(&pSrcA2, &pSrcB2, &pDst2, &pState);
  98.      for(i = 0; i < 9; i++)
  99.      {
  100.          printf("pDataDst2[%d] = %drn", i, pDataDst2[i]);
  101.      }
  102.    
  103. }
1.    下面通过matlab实现矩阵的乘法:
20.6.png

举报

硬汉Eric2013

2015-6-22 14:28:58
20.3  转置矩阵 MatTrans
20.3.1  arm_mat_trans_f32
公式描述:
20.7.png
                              
函数定义如下:
arm_statusarm_mat_trans_f32(
  const arm_matrix_instance_f32 * pSrc,
  arm_matrix_instance_f32 * pDst)
参数定义:
[in]   *pSrc  points to the input matrix   
[out]  *pDst  pointsto the output matrix   
return   The function returns either ARM_MATH_SIZE_MISMATCH  
注意事项:
1.    矩阵M x N转置后是N x M。

20.3.2  arm_mat_trans_q31
函数定义如下:
arm_statusarm_mat_trans_q31(
  const arm_matrix_instance_q31 * pSrc,
  arm_matrix_instance_q31 * pDst)
参数定义:
[in]  *pSrc points to the input matrix   
[out] *pDst points to the output matrix   
return  Thefunction returns either ARM_MATH_SIZE_MISMATCH  
注意事项:
1.    矩阵M x N转置后是N x M。

20.3.3  arm_mat_trans_q15
函数定义如下:
arm_statusarm_mat_trans_q15(
  const arm_matrix_instance_q15 * pSrc,
  arm_matrix_instance_q15 * pDst)
参数定义:
[in]  *pSrc points to the input matrix   
[out] *pDst points to the output matrix   
return  Thefunction returns either ARM_MATH_SIZE_MISMATCH  
注意事项:
1.    矩阵M x N转置后是N x M。

20.3.4  实例讲解
实验目的:
1. 学习MatrixFunctions中的转置矩阵
实验内容:
       1. 按下按键K3, 串口打印函数DSP_MatTrans的输出结果
实验现象:
       通过窗口上位机软件SecureCRT(V5光盘里面有此软件)查看打印信息现象如下:
20.8.jpg
程序设计:
  1. /*
  2. *********************************************************************************************************
  3. *    函 数 名: DSP_MatTrans
  4. *    功能说明: 求逆矩阵
  5. *    形    参:无
  6. *    返 回 值: 无
  7. *********************************************************************************************************
  8. */
  9. static void DSP_MatTrans(void)
  10. {
  11.      uint8_t i;
  12.    
  13.      /****浮点数数组******************************************************************/
  14.      float32_t pDataA[9] = {1.1f, 1.1f, 2.1f, 2.1f, 3.1f, 3.1f, 4.1f, 4.1f, 5.1f};
  15.      float32_t pDataDst[9];
  16.    
  17.      arm_matrix_instance_f32 pSrcA; //3行3列数据
  18.      arm_matrix_instance_f32 pDst;
  19.    
  20.      /****定点数Q31数组******************************************************************/
  21.      q31_t pDataA1[9] = {1, 1, 2, 2, 3, 3, 4, 4, 5};
  22.      q31_t pDataDst1[9];
  23.    
  24.      arm_matrix_instance_q31 pSrcA1; //3行3列数据
  25.      arm_matrix_instance_q31 pDst1;
  26.    
  27.      /****定点数Q15数组******************************************************************/
  28.      q15_t pDataA2[9] = {1, 1, 2, 2, 3, 3, 4, 4, 5};
  29.      q15_t pDataDst2[9];
  30.    
  31.      arm_matrix_instance_q15 pSrcA2; //3行3列数据
  32.      arm_matrix_instance_q15 pDst2;
  33.    
  34.      /****浮点数***********************************************************************/
  35.      pSrcA.numCols = 3;
  36.      pSrcA.numRows = 3;
  37.      pSrcA.pData = pDataA;

  38.      pDst.numCols = 3;
  39.      pDst.numRows = 3;
  40.      pDst.pData = pDataDst;
  41.    
  42.      printf("****浮点数******************************************rn");
  43.      status = arm_mat_trans_f32(&pSrcA, &pDst);
  44.      for(i = 0; i < 9; i++)
  45.      {
  46.          printf("pDataDst[%d] = %frn", i, pDataDst[i]);
  47.      }
  48.    
  49.      /****定点数Q31***********************************************************************/
  50.      pSrcA1.numCols = 3;
  51.      pSrcA1.numRows = 3;
  52.      pSrcA1.pData = pDataA1;
  53.    
  54.      pDst1.numCols = 3;
  55.      pDst1.numRows = 3;
  56.      pDst1.pData = pDataDst1;
  57.    
  58.      printf("****定点数Q31******************************************rn");
  59.      status = arm_mat_trans_q31(&pSrcA1, &pDst1);
  60.      for(i = 0; i < 9; i++)
  61.      {
  62.          printf("pDataDst1[%d] = %drn", i, pDataDst1[i]);
  63.      }
  64.    
  65.      /****定点数Q15***********************************************************************/
  66.      pSrcA2.numCols = 3;
  67.      pSrcA2.numRows = 3;
  68.      pSrcA2.pData = pDataA2;
  69.    
  70.      pDst2.numCols = 3;
  71.      pDst2.numRows = 3;
  72.      pDst2.pData = pDataDst2;
  73.    
  74.      printf("****定点数Q15******************************************rn");
  75.      status = arm_mat_trans_q15(&pSrcA2, &pDst2);
  76.      for(i = 0; i < 9; i++)
  77.      {
  78.          printf("pDataDst2[%d] = %drn", i, pDataDst2[i]);
  79.      }
  80.    
  81. }
1.    下面通过matlab实现矩阵的转置:
20.9.png

举报

硬汉Eric2013

2015-6-22 14:29:23
20.4        总结
本期教程就跟大家讲这么多,有兴趣的可以深入研究下算法的具体实现。
举报

更多回帖

发帖
×
20
完善资料,
赚取积分