你的浏览器版本过低,可能导致网站不能正常访问!
为了你能正常使用网站功能,请使用这些浏览器。

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

[复制链接]
baiyongbin2009 发布时间:2015-3-31 11:25
特别说明:完整45期数字信号处理教程,原创高性能示波器代码全开源地址:链接
第19章 MatrixFunctions的使用(一)

    本期教程主要讲解矩阵运算中的初始化,加法,逆矩阵和减法。
    19.1 矩阵初始化 MatInit
    19.2 矩阵加法 MatAdd
    19.3 逆矩阵 MatInverse
    19.4 矩阵减法 MatSub
    19.5 总结

19.1 矩阵初始化 MatInit19.1.1 arm_mat_init_f32
函数定义如下:
    void arm_mat_init_f32(
        arm_matrix_instance_f32 * S,
        uint16_t nRows,
        uint16_t nColumns,
        float32_t * pData)
参数定义:
    [in,out] *S         points to an instance of the floating-point matrix structure.   
    [in]     nRows    number of rows in the matrix.   
    [in]     nColumns number of columns in the matrix.   
    [in]     *pData           points to the matrix data array.  
注意事项:
    1. arm_matrix_instance_f32的结构体定义如下(在文件arm_math.h文件里面):
      typedef struct
          {
              uint16_t numRows;     // number of rows of the matrix.
              uint16_t numCols;      // number of columns of the matrix.
              float32_t *pData;       // points to the data of the matrix.
          } arm_matrix_instance_f32;

19.1.2 arm_mat_init_q31
函数定义如下:
    void arm_mat_init_q31(
        arm_matrix_instance_q31 * S,
        uint16_t nRows,
        uint16_t nColumns,
        q31_t * pData)
参数定义:
    [in,out] *S         points to an instance of the floating-point matrix structure.   
    [in]     nRows    number of rows in the matrix.   
    [in]     nColumns number of columns in the matrix.   
    [in]     *pData           points to the matrix data array.  
注意事项:
    1. arm_matrix_instance_q31的结构体定义如下(在文件arm_math.h文件里面):
      typedef struct
           {
              uint16_t numRows;     // number of rows of the matrix.
              uint16_t numCols;      // number of columns of the matrix.
              q31_t  *pData;        // points to the data of the matrix.
           } arm_matrix_instance_q31;

19.1.3 arm_mat_init_q15
函数定义如下:
    void arm_mat_init_q15(
        arm_matrix_instance_q15 * S,
        uint16_t nRows,
        uint16_t nColumns,
        q15_t * pData)
参数定义:
    [in,out] *S         points to an instance of the floating-point matrix structure.   
    [in]     nRows    number of rows in the matrix.   
    [in]     nColumns number of columns in the matrix.   
    [in]     *pData           points to the matrix data array.  
注意事项:
    1. arm_matrix_instance_q15的结构体定义如下(在文件arm_math.h文件里面):
      typedef struct
          {
              uint16_t numRows;     // number of rows of the matrix.
              uint16_t numCols;      // number of columns of the matrix.
              q15_t  *pData;        // points to the data of the matrix.
          } arm_matrix_instance_q15;

19.1.4 实例讲解
实验目的:
    1. 学习MatrixFunctions中矩阵的初始化
实验内容:
    1. 按下按键K1, 串口打印函数DSP_MatInit的输出结果
实验现象:
     通过窗口上位机软件SecureCRT(V5光盘里面有此软件)查看打印信息现象如下:
19.1.png
程序设计:
  1. /*
  2. *********************************************************************************************************
  3. *        函 数 名: DSP_MatInit
  4. *        功能说明: 矩阵数据初始化
  5. *        形    参:无
  6. *        返 回 值: 无
  7. *********************************************************************************************************
  8. */
  9. static void DSP_MatInit(void)
  10. {
  11. uint8_t i;

  12. /****浮点数数组******************************************************************/
  13. float32_t pDataA[9] = {1.1f, 1.1f, 2.1f, 2.1f, 3.1f, 3.1f, 4.1f, 4.1f, 5.1f};
  14. arm_matrix_instance_f32 pSrcA; //3行3列数据
  15. arm_matrix_instance_f32 pDst;
  16. /****定点数Q31数组******************************************************************/
  17. q31_t pDataA1[9] = {1, 1, 2, 2, 3, 3, 4, 4, 5};
  18. arm_matrix_instance_q31 pSrcA1; //3行3列数据
  19. arm_matrix_instance_q31 pDst1;
  20. /****定点数Q15数组******************************************************************/
  21. q15_t pDataA2[9] = {1, 1, 2, 2, 3, 3, 4, 4, 5};
  22. arm_matrix_instance_q15 pSrcA2; //3行3列数据
  23. arm_matrix_instance_q15 pDst2;
  24. /****浮点数***********************************************************************/
  25. printf("****浮点数******************************************\r\n");
  26. arm_mat_init_f32(&pSrcA, 3,3, pDataA);
  27. for(i = 0; i < 9; i++)
  28. {
  29. printf("pDataA[%d] = %f\r\n", i, pDataA[i]);
  30. }
  31. /****定点数Q31***********************************************************************/
  32. printf("****浮点数******************************************\r\n");
  33. arm_mat_init_q31(&pSrcA1, 3,3, pDataA1);
  34. for(i = 0; i < 9; i++)
  35. {
  36. printf("pDataA1[%d] = %d\r\n", i, pDataA1[i]);
  37. }
  38. /****定点数Q15***********************************************************************/
  39. printf("****浮点数******************************************\r\n");
  40. arm_mat_init_q15(&pSrcA2, 3,3, pDataA2);
  41. for(i = 0; i < 9; i++)
  42. {
  43. printf("pDataA2[%d] = %d\r\n", i, pDataA2[i]);
  44. }
  45. }
复制代码

收藏 评论5 发布时间:2015-3-31 11:25

举报

5个回答
baiyongbin2009 回答时间:2015-3-31 11:28:59
19.2 矩阵加法 MatAdd

19.2.1 arm_mat_add_f32
公式描述(以3*3矩阵为例进行说明):
19.2.png
函数定义如下:
    arm_status arm_mat_add_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 matrix structure        
    [in]    *pSrcB   points to the second input matrix structure        
    [out]   *pDst   points to output matrix structure        
    return                     The function returns either     
注意事项:
    1. pSrcA,pSrcB,pDst的行数和列数必须是相同的,要不没有办法使用加法运行。
    2. 矩阵在数组中的存储是从左到右,再从上到下。

19.2.2 arm_mat_add_q31
函数定义如下:
    arm_status arm_mat_add_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 matrix structure        
    [in]    *pSrcB   points to the second input matrix structure        
    [out]   *pDst   points to output matrix structure        
    return                     The function returns either     
注意事项:
    1. pSrcA,pSrcB,pDst的行数和列数必须是相同的,要不没有办法使用加法运行。
    2. 矩阵在数组中的存储是从左到右,再从上到下。

19.2.3 arm_mat_add_q15
函数定义如下:
    arm_status arm_mat_add_q15(
        const arm_matrix_instance_q15 * pSrcA,
        const arm_matrix_instance_q15 * pSrcB,
        arm_matrix_instance_q15 * pDst)
参数定义:
    [in]    *pSrcA   points to the first input matrix structure        
    [in]    *pSrcB   points to the second input matrix structure        
    [out]   *pDst   points to output matrix structure        
    return                     The function returns either     
注意事项:
    1. pSrcA,pSrcB,pDst的行数和列数必须是相同的,要不没有办法使用加法运行。
    2. 矩阵在数组中的存储是从左到右,再从上到下。

19.2.4 实例讲解
实验目的:
    1. 学习MatrixFunctions中矩阵的加法
实验内容:
    1. 按下按键K2, 串口打印函数DSP_MatAdd的输出结果
实验现象:
    通过窗口上位机软件SecureCRT(V5光盘里面有此软件)查看打印信息现象如下:
19.3.png
程序设计:
  1. /*
  2. *********************************************************************************************************
  3. *        函 数 名: DSP_MatAdd
  4. *        功能说明: 矩阵求和
  5. *        形    参:无
  6. *        返 回 值: 无
  7. *********************************************************************************************************
  8. */
  9. static void DSP_MatAdd(void)
  10. {
  11. uint8_t i;
  12. /****浮点数数组******************************************************************/
  13. float32_t pDataA[9] = {1.1f, 1.1f, 2.1f, 2.1f, 3.1f, 3.1f, 4.1f, 4.1f, 5.1f};
  14. float32_t pDataB[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. arm_matrix_instance_f32 pSrcA; //3行3列数据
  17. arm_matrix_instance_f32 pSrcB; //3行3列数据
  18. arm_matrix_instance_f32 pDst;
  19. /****定点数Q31数组******************************************************************/
  20. q31_t pDataA1[9] = {1, 1, 2, 2, 3, 3, 4, 4, 5};
  21. q31_t pDataB1[9] = {1, 1, 2, 2, 3, 3, 4, 4, 5};
  22. q31_t pDataDst1[9];
  23. arm_matrix_instance_q31 pSrcA1; //3行3列数据
  24. arm_matrix_instance_q31 pSrcB1; //3行3列数据
  25. arm_matrix_instance_q31 pDst1;
  26. /****定点数Q15数组******************************************************************/
  27. q15_t pDataA2[9] = {1, 1, 2, 2, 3, 3, 4, 4, 5};
  28. q15_t pDataB2[9] = {1, 1, 2, 2, 3, 3, 4, 4, 5};
  29. q15_t pDataDst2[9];
  30. arm_matrix_instance_q15 pSrcA2; //3行3列数据
  31. arm_matrix_instance_q15 pSrcB2; //3行3列数据
  32. arm_matrix_instance_q15 pDst2;
  33. /****浮点数***********************************************************************/
  34. pSrcA.numCols = 3;
  35. pSrcA.numRows = 3;
  36. pSrcA.pData = pDataA;
  37. pSrcB.numCols = 3;
  38. pSrcB.numRows = 3;
  39. pSrcB.pData = pDataB;
  40. pDst.numCols = 3;
  41. pDst.numRows = 3;
  42. pDst.pData = pDataDst;
  43. printf("****浮点数******************************************\r\n");
  44. arm_mat_add_f32(&pSrcA, &pSrcB, &pDst);
  45. for(i = 0; i < 9; i++)
  46. {
  47. printf("pDataDst[%d] = %f\r\n", i, pDataDst[i]);
  48. }
  49. /****定点数Q31***********************************************************************/
  50. pSrcA1.numCols = 3;
  51. pSrcA1.numRows = 3;
  52. pSrcA1.pData = pDataA1;
  53. pSrcB1.numCols = 3;
  54. pSrcB1.numRows = 3;
  55. pSrcB1.pData = pDataB1;
  56. pDst1.numCols = 3;
  57. pDst1.numRows = 3;
  58. pDst1.pData = pDataDst1;
  59. printf("****定点数Q31******************************************\r\n");
  60. arm_mat_add_q31(&pSrcA1, &pSrcB1, &pDst1);
  61. for(i = 0; i < 9; i++)
  62. {
  63. printf("pDataDst1[%d] = %d\r\n", i, pDataDst1[i]);
  64. }
  65. /****定点数Q15***********************************************************************/
  66. pSrcA2.numCols = 3;
  67. pSrcA2.numRows = 3;
  68. pSrcA2.pData = pDataA2;
  69. pSrcB2.numCols = 3;
  70. pSrcB2.numRows = 3;
  71. pSrcB2.pData = pDataB2;
  72. pDst2.numCols = 3;
  73. pDst2.numRows = 3;
  74. pDst2.pData = pDataDst2;
  75. printf("****定点数Q15******************************************\r\n");
  76. arm_mat_add_q15(&pSrcA2, &pSrcB2, &pDst2);
  77. for(i = 0; i < 9; i++)
  78. {
  79. printf("pDataDst2[%d] = %d\r\n", i, pDataDst2[i]);
  80. }
  81. }
复制代码
1. 矩阵的加法从C语言的实现上来看,比较的容易,下面通过Matlab来求解矩阵和(在命令窗口输入)。
19.4.png


baiyongbin2009 回答时间:2015-3-31 11:31:31
19.3 逆矩阵 MatInverse

19.3.1 arm_mat_inverse_f32
公式描述(Gauss-Jordan法求逆矩阵):
19.5.png
函数定义如下:
    arm_status arm_mat_inverse_f32(
        const arm_matrix_instance_f32 * pSrc,
        arm_matrix_instance_f32 * pDst)
参数定义:
    [in]   *pSrc  points to input matrix structure   
    [out]  *pDst  points to output matrix structure      
注意事项:
    1. pSrc必须得是方阵(行数和列数相同)。
    2. pSrc和pDst必须是相同的方阵。
    3. 输入的矩阵可逆,函数会返回ARM_MATH_SUCCESS,如果不可逆,返回ARM_MATH_SINGULAR。
    4. ARM官方库只提供了浮点数矩阵求逆矩阵。
19.3.2 实例讲解
实验目的:
    1. 学习MatrixFunctions中逆矩阵的求解
实验内容:
     1. 按下按键K3, 串口打印函DSP_MatInverse的输出结果
实验现象:
    通过窗口上位机软件SecureCRT(V5光盘里面有此软件)查看打印信息现象如下:
19.6.png
程序设计:
  1. /*
  2. *********************************************************************************************************
  3. *        函 数 名: DSP_MatInverse
  4. *        功能说明: 求逆矩阵
  5. *        形    参:无
  6. *        返 回 值: 无
  7. *********************************************************************************************************
  8. */
  9. static void DSP_MatInverse(void)
  10. {
  11. uint8_t i;

  12. /****浮点数数组******************************************************************/
  13. float32_t pDataA[9] = {1.1f, 1.1f, 2.1f, 2.1f, 3.1f, 3.1f, 4.1f, 4.1f, 5.1f};
  14. float32_t pDataB[9];
  15. arm_matrix_instance_f32 pSrcA; //3行3列数据
  16. arm_matrix_instance_f32 pSrcB; //3行3列数据;

  17. /****浮点数***********************************************************************/
  18. pSrcA.numCols = 3;
  19. pSrcA.numRows = 3;
  20. pSrcA.pData = pDataA;
  21. pSrcB.numCols = 3;
  22. pSrcB.numRows = 3;
  23. pSrcB.pData = pDataB;
  24. arm_mat_inverse_f32(&pSrcA, &pSrcB);
  25. for(i = 0; i < 9; i++)
  26. {
  27. printf("pDataB[%d] = %f\r\n", i, pDataB[i]);
  28. }
  29. }
复制代码
1. 用C语言实现逆矩阵要稍麻烦些,下面我们通过Matlab来实现求逆矩阵(数据和上面代码中的程序一样
19.7.png
    可以看出求得结果跟上面的C函数求得结果基本一致。


baiyongbin2009 回答时间:2015-3-31 11:35:03
19.4 矩阵减法 MatSub

19.4.1 arm_mat_sub_f32
公式描述(以3*3矩阵为例进行说明):
19.8.png
函数定义如下:
    arm_status arm_mat_sub_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 matrix structure        
    [in]    *pSrcB   points to the second input matrix structure        
    [out]   *pDst   points to output matrix structure        
    return                     The function returns either     
注意事项:
    1. pSrcA,pSrcB,pDst的行数和列数必须是相同的,要不没有办法使用加法运行。
    2. 矩阵在数组中的存储是从左到右,再从上到下。


19.4.2
arm_mat_add_q31
函数定义如下:
    arm_status arm_mat_add_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 matrix structure        
    [in]    *pSrcB   points to the second input matrix structure        
    [out]   *pDst   points to output matrix structure        
    return                     The function returns either     
注意事项:
    1. pSrcA,pSrcB,pDst的行数和列数必须是相同的,要不没有办法使用加法运行。
    2. 矩阵在数组中的存储是从左到右,再从上到下。


19.4.3
arm_mat_add_q15
函数定义如下:
    arm_status arm_mat_add_q15(
        const arm_matrix_instance_q15 * pSrcA,
        const arm_matrix_instance_q15 * pSrcB,
        arm_matrix_instance_q15 * pDst)
参数定义:
    [in]    *pSrcA   points to the first input matrix structure        
    [in]    *pSrcB   points to the second input matrix structure        
    [out]   *pDst   points to output matrix structure        
    return                     The function returns either     
注意事项:
    1. pSrcA,pSrcB,pDst的行数和列数必须是相同的,要不没有办法使用加法运行。
    2. 矩阵在数组中的存储是从左到右,再从上到下。


19.4.4
实例讲解
实验目的:
    1. 学习MatrixFunctions中矩阵的加法
实验内容:
    1. 按下按键K2, 串口打印函数DSP_MatAdd的输出结果
实验现象:
    通过窗口上位机软件SecureCRT(V5光盘里面有此软件)查看打印信息现象如下:
19.9.png
程序设计:
  1. /*
  2. *********************************************************************************************************
  3. *        函 数 名: DSP_MatSub
  4. *        功能说明: 矩阵减法
  5. *        形    参:无
  6. *        返 回 值: 无
  7. *********************************************************************************************************
  8. */
  9. static void DSP_MatSub(void)
  10. {
  11. uint8_t i;
  12. /****浮点数数组******************************************************************/
  13. float32_t pDataA[9] = {1.1f, 1.1f, 2.1f, 2.1f, 3.1f, 3.1f, 4.1f, 4.1f, 5.1f};
  14. float32_t pDataB[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. arm_matrix_instance_f32 pSrcA; //3行3列数据
  17. arm_matrix_instance_f32 pSrcB; //3行3列数据
  18. arm_matrix_instance_f32 pDst;
  19. /****定点数Q31数组******************************************************************/
  20. q31_t pDataA1[9] = {1, 1, 2, 2, 3, 3, 4, 4, 5};
  21. q31_t pDataB1[9] = {2, 2, 2, 2, 2, 2, 2, 2, 2};
  22. q31_t pDataDst1[9];
  23. arm_matrix_instance_q31 pSrcA1; //3行3列数据
  24. arm_matrix_instance_q31 pSrcB1; //3行3列数据
  25. arm_matrix_instance_q31 pDst1;
  26. /****定点数Q15数组******************************************************************/
  27. q15_t pDataA2[9] = {1, 1, 2, 2, 3, 3, 4, 4, 5};
  28. q15_t pDataB2[9] = {2, 2, 2, 2, 23, 2, 2, 2, 2};
  29. q15_t pDataDst2[9];
  30. arm_matrix_instance_q15 pSrcA2; //3行3列数据
  31. arm_matrix_instance_q15 pSrcB2; //3行3列数据
  32. arm_matrix_instance_q15 pDst2;
  33. /****浮点数***********************************************************************/
  34. pSrcA.numCols = 3;
  35. pSrcA.numRows = 3;
  36. pSrcA.pData = pDataA;
  37. pSrcB.numCols = 3;
  38. pSrcB.numRows = 3;
  39. pSrcB.pData = pDataB;
  40. pDst.numCols = 3;
  41. pDst.numRows = 3;
  42. pDst.pData = pDataDst;
  43. printf("****浮点数******************************************\r\n");
  44. arm_mat_sub_f32(&pSrcA, &pSrcB, &pDst);
  45. for(i = 0; i < 9; i++)
  46. {
  47. printf("pDataDst[%d] = %f\r\n", i, pDataDst[i]);
  48. }
  49. /****定点数Q31***********************************************************************/
  50. pSrcA1.numCols = 3;
  51. pSrcA1.numRows = 3;
  52. pSrcA1.pData = pDataA1;
  53. pSrcB1.numCols = 3;
  54. pSrcB1.numRows = 3;
  55. pSrcB1.pData = pDataB1;
  56. pDst1.numCols = 3;
  57. pDst1.numRows = 3;
  58. pDst1.pData = pDataDst1;
  59. printf("****定点数Q31******************************************\r\n");
  60. arm_mat_sub_q31(&pSrcA1, &pSrcB1, &pDst1);
  61. for(i = 0; i < 9; i++)
  62. {
  63. printf("pDataDst1[%d] = %d\r\n", i, pDataDst1[i]);
  64. }
  65. /****定点数Q15***********************************************************************/
  66. pSrcA2.numCols = 3;
  67. pSrcA2.numRows = 3;
  68. pSrcA2.pData = pDataA2;
  69. pSrcB2.numCols = 3;
  70. pSrcB2.numRows = 3;
  71. pSrcB2.pData = pDataB2;
  72. pDst2.numCols = 3;
  73. pDst2.numRows = 3;
  74. pDst2.pData = pDataDst2;
  75. printf("****定点数Q15******************************************\r\n");
  76. arm_mat_sub_q15(&pSrcA2, &pSrcB2, &pDst2);
  77. for(i = 0; i < 9; i++)
  78. {
  79. printf("pDataDst2[%d] = %d\r\n", i, pDataDst2[i]);
  80. }
  81. }
复制代码
1. 矩阵的减法从C语言的实现上来看,比较的容易,下面通过Matlab来求解矩阵和(在命令窗口输入)。
19.10.png
19.5 总结
    本期教程就跟大家讲这么多,有兴趣的可以深入研究下算法的具体实现。


jackzhouly 回答时间:2015-3-31 19:53:09
谢谢分享
jackzhouly 回答时间:2015-3-31 19:53:47

谢谢分享~
说起来这些板子都挺贵orz

所属标签

关于
我们是谁
投资者关系
意法半导体可持续发展举措
创新与技术
意法半导体官网
联系我们
联系ST分支机构
寻找销售人员和分销渠道
社区
媒体中心
活动与培训
隐私策略
隐私策略
Cookies管理
行使您的权利
官方最新发布
STM32N6 AI生态系统
STM32MCU,MPU高性能GUI
ST ACEPACK电源模块
意法半导体生物传感器
STM32Cube扩展软件包
关注我们
st-img 微信公众号
st-img 手机版