硬汉嵌入式论坛

 找回密码
 立即注册
查看: 3736|回复: 3
收起左侧

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

[复制链接]

740

主题

1326

回帖

3546

积分

管理员

春暖花开

Rank: 9Rank: 9Rank: 9

积分
3546
QQ
发表于 2015-3-31 10:53:55 | 显示全部楼层 |阅读模式
特别说明:完整45期数字信号处理教程,原创高性能示波器代码全开源地址:链接

第19章 MatrixFunctions的使用(一)

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

19.1 矩阵初始化 MatInit

19.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光盘里面有此软件)查看打印信息现象如下:

程序设计:
  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. }
复制代码
19.1.png
努力打造安富莱高质量微信公众号:点击扫描图片关注
回复

使用道具 举报

740

主题

1326

回帖

3546

积分

管理员

春暖花开

Rank: 9Rank: 9Rank: 9

积分
3546
QQ
 楼主| 发表于 2015-3-31 11:05:25 | 显示全部楼层
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("****浮点数******************************************rn");
  44. arm_mat_add_f32(&pSrcA, &pSrcB, &pDst);
  45. for(i = 0; i < 9; i++)
  46. {
  47. printf("pDataDst[%d] = %frn", 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******************************************rn");
  60. arm_mat_add_q31(&pSrcA1, &pSrcB1, &pDst1);
  61. for(i = 0; i < 9; i++)
  62. {
  63. printf("pDataDst1[%d] = %drn", 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******************************************rn");
  76. arm_mat_add_q15(&pSrcA2, &pSrcB2, &pDst2);
  77. for(i = 0; i < 9; i++)
  78. {
  79. printf("pDataDst2[%d] = %drn", i, pDataDst2[i]);
  80. }
  81. }
复制代码
1. 矩阵的加法从C语言的实现上来看,比较的容易,下面通过Matlab来求解矩阵和(在命令窗口输入)。
19.4.png
努力打造安富莱高质量微信公众号:点击扫描图片关注
回复

使用道具 举报

740

主题

1326

回帖

3546

积分

管理员

春暖花开

Rank: 9Rank: 9Rank: 9

积分
3546
QQ
 楼主| 发表于 2015-3-31 11:08:56 | 显示全部楼层
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] = %frn", i, pDataB[i]);
  28. }
  29. }
复制代码
1. 用C语言实现逆矩阵要稍麻烦些,下面我们通过Matlab来实现求逆矩阵(数据和上面代码中的程序一样
19.7.png
    可以看出求得结果跟上面的C函数求得结果基本一致。
努力打造安富莱高质量微信公众号:点击扫描图片关注
回复

使用道具 举报

740

主题

1326

回帖

3546

积分

管理员

春暖花开

Rank: 9Rank: 9Rank: 9

积分
3546
QQ
 楼主| 发表于 2015-3-31 11:12:57 | 显示全部楼层
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("****浮点数******************************************rn");
  44. arm_mat_sub_f32(&pSrcA, &pSrcB, &pDst);
  45. for(i = 0; i < 9; i++)
  46. {
  47. printf("pDataDst[%d] = %frn", 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******************************************rn");
  60. arm_mat_sub_q31(&pSrcA1, &pSrcB1, &pDst1);
  61. for(i = 0; i < 9; i++)
  62. {
  63. printf("pDataDst1[%d] = %drn", 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******************************************rn");
  76. arm_mat_sub_q15(&pSrcA2, &pSrcB2, &pDst2);
  77. for(i = 0; i < 9; i++)
  78. {
  79. printf("pDataDst2[%d] = %drn", i, pDataDst2[i]);
  80. }
  81. }
复制代码
1. 矩阵的减法从C语言的实现上来看,比较的容易,下面通过Matlab来求解矩阵和(在命令窗口输入)。
19.10.png

19.5 总结
    本期教程就跟大家讲这么多,有兴趣的可以深入研究下算法的具体实现。
努力打造安富莱高质量微信公众号:点击扫描图片关注
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|小黑屋|Archiver|手机版|硬汉嵌入式论坛

GMT+8, 2024-5-4 18:17 , Processed in 0.304003 second(s), 28 queries .

Powered by Discuz! X3.4 Licensed

Copyright © 2001-2023, Tencent Cloud.

快速回复 返回顶部 返回列表