席萌0209 发表于 2015-3-31 10:53:55

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

特别说明:完整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)
参数定义:
    *S       points to an instance of the floating-point matrix structure.   
       nRows    number of rows in the matrix.   
       nColumns number of columns in the matrix.   
       *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)
参数定义:
    *S       points to an instance of the floating-point matrix structure.   
       nRows    number of rows in the matrix.   
       nColumns number of columns in the matrix.   
       *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)
参数定义:
    *S       points to an instance of the floating-point matrix structure.   
       nRows    number of rows in the matrix.   
       nColumns number of columns in the matrix.   
       *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光盘里面有此软件)查看打印信息现象如下:
程序设计:
/*
*********************************************************************************************************
*    函 数 名: DSP_MatInit
*    功能说明: 矩阵数据初始化
*    形    参:无
*    返 回 值: 无
*********************************************************************************************************
*/
static void DSP_MatInit(void)
{
uint8_t i;

/****浮点数数组******************************************************************/
float32_t pDataA = {1.1f, 1.1f, 2.1f, 2.1f, 3.1f, 3.1f, 4.1f, 4.1f, 5.1f};
arm_matrix_instance_f32 pSrcA; //3行3列数据
arm_matrix_instance_f32 pDst;
/****定点数Q31数组******************************************************************/
q31_t pDataA1 = {1, 1, 2, 2, 3, 3, 4, 4, 5};
arm_matrix_instance_q31 pSrcA1; //3行3列数据
arm_matrix_instance_q31 pDst1;
/****定点数Q15数组******************************************************************/
q15_t pDataA2 = {1, 1, 2, 2, 3, 3, 4, 4, 5};
arm_matrix_instance_q15 pSrcA2; //3行3列数据
arm_matrix_instance_q15 pDst2;
/****浮点数***********************************************************************/
printf("****浮点数******************************************\r\n");
arm_mat_init_f32(&pSrcA, 3,3, pDataA);
for(i = 0; i < 9; i++)
{
printf("pDataA[%d] = %f\r\n", i, pDataA);
}
/****定点数Q31***********************************************************************/
printf("****浮点数******************************************\r\n");
arm_mat_init_q31(&pSrcA1, 3,3, pDataA1);
for(i = 0; i < 9; i++)
{
printf("pDataA1[%d] = %d\r\n", i, pDataA1);
}
/****定点数Q15***********************************************************************/
printf("****浮点数******************************************\r\n");
arm_mat_init_q15(&pSrcA2, 3,3, pDataA2);
for(i = 0; i < 9; i++)
{
printf("pDataA2[%d] = %d\r\n", i, pDataA2);
}
}

席萌0209 发表于 2015-3-31 11:05:25

19.2 矩阵加法 MatAdd

19.2.1 arm_mat_add_f32

公式描述(以3*3矩阵为例进行说明):

函数定义如下:
    arm_status arm_mat_add_f32(
      const arm_matrix_instance_f32 * pSrcA,
      const arm_matrix_instance_f32 * pSrcB,
      arm_matrix_instance_f32 * pDst)
参数定义:
        *pSrcA points to the first input matrix structure      
        *pSrcB points to the second input matrix structure      
       *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)
参数定义:
        *pSrcA points to the first input matrix structure      
        *pSrcB points to the second input matrix structure      
       *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)
参数定义:
        *pSrcA points to the first input matrix structure      
        *pSrcB points to the second input matrix structure      
       *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光盘里面有此软件)查看打印信息现象如下:
程序设计:
/*
*********************************************************************************************************
*    函 数 名: DSP_MatAdd
*    功能说明: 矩阵求和
*    形    参:无
*    返 回 值: 无
*********************************************************************************************************
*/
static void DSP_MatAdd(void)
{
uint8_t i;
/****浮点数数组******************************************************************/
float32_t pDataA = {1.1f, 1.1f, 2.1f, 2.1f, 3.1f, 3.1f, 4.1f, 4.1f, 5.1f};
float32_t pDataB = {1.1f, 1.1f, 2.1f, 2.1f, 3.1f, 3.1f, 4.1f, 4.1f, 5.1f};
float32_t pDataDst;
arm_matrix_instance_f32 pSrcA; //3行3列数据
arm_matrix_instance_f32 pSrcB; //3行3列数据
arm_matrix_instance_f32 pDst;
/****定点数Q31数组******************************************************************/
q31_t pDataA1 = {1, 1, 2, 2, 3, 3, 4, 4, 5};
q31_t pDataB1 = {1, 1, 2, 2, 3, 3, 4, 4, 5};
q31_t pDataDst1;
arm_matrix_instance_q31 pSrcA1; //3行3列数据
arm_matrix_instance_q31 pSrcB1; //3行3列数据
arm_matrix_instance_q31 pDst1;
/****定点数Q15数组******************************************************************/
q15_t pDataA2 = {1, 1, 2, 2, 3, 3, 4, 4, 5};
q15_t pDataB2 = {1, 1, 2, 2, 3, 3, 4, 4, 5};
q15_t pDataDst2;
arm_matrix_instance_q15 pSrcA2; //3行3列数据
arm_matrix_instance_q15 pSrcB2; //3行3列数据
arm_matrix_instance_q15 pDst2;
/****浮点数***********************************************************************/
pSrcA.numCols = 3;
pSrcA.numRows = 3;
pSrcA.pData = pDataA;
pSrcB.numCols = 3;
pSrcB.numRows = 3;
pSrcB.pData = pDataB;
pDst.numCols = 3;
pDst.numRows = 3;
pDst.pData = pDataDst;
printf("****浮点数******************************************rn");
arm_mat_add_f32(&pSrcA, &pSrcB, &pDst);
for(i = 0; i < 9; i++)
{
printf("pDataDst[%d] = %frn", i, pDataDst);
}
/****定点数Q31***********************************************************************/
pSrcA1.numCols = 3;
pSrcA1.numRows = 3;
pSrcA1.pData = pDataA1;
pSrcB1.numCols = 3;
pSrcB1.numRows = 3;
pSrcB1.pData = pDataB1;
pDst1.numCols = 3;
pDst1.numRows = 3;
pDst1.pData = pDataDst1;
printf("****定点数Q31******************************************rn");
arm_mat_add_q31(&pSrcA1, &pSrcB1, &pDst1);
for(i = 0; i < 9; i++)
{
printf("pDataDst1[%d] = %drn", i, pDataDst1);
}
/****定点数Q15***********************************************************************/
pSrcA2.numCols = 3;
pSrcA2.numRows = 3;
pSrcA2.pData = pDataA2;
pSrcB2.numCols = 3;
pSrcB2.numRows = 3;
pSrcB2.pData = pDataB2;
pDst2.numCols = 3;
pDst2.numRows = 3;
pDst2.pData = pDataDst2;
printf("****定点数Q15******************************************rn");
arm_mat_add_q15(&pSrcA2, &pSrcB2, &pDst2);
for(i = 0; i < 9; i++)
{
printf("pDataDst2[%d] = %drn", i, pDataDst2);
}
}
1. 矩阵的加法从C语言的实现上来看,比较的容易,下面通过Matlab来求解矩阵和(在命令窗口输入)。

席萌0209 发表于 2015-3-31 11:08:56

19.3 逆矩阵 MatInverse

19.3.1 arm_mat_inverse_f32

公式描述(Gauss-Jordan法求逆矩阵):

函数定义如下:
    arm_status arm_mat_inverse_f32(
      const arm_matrix_instance_f32 * pSrc,
      arm_matrix_instance_f32 * pDst)
参数定义:
    *pSrc points to input matrix structure   
    *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光盘里面有此软件)查看打印信息现象如下:
程序设计:
/*
*********************************************************************************************************
*    函 数 名: DSP_MatInverse
*    功能说明: 求逆矩阵
*    形    参:无
*    返 回 值: 无
*********************************************************************************************************
*/
static void DSP_MatInverse(void)
{
uint8_t i;

/****浮点数数组******************************************************************/
float32_t pDataA = {1.1f, 1.1f, 2.1f, 2.1f, 3.1f, 3.1f, 4.1f, 4.1f, 5.1f};
float32_t pDataB;
arm_matrix_instance_f32 pSrcA; //3行3列数据
arm_matrix_instance_f32 pSrcB; //3行3列数据;

/****浮点数***********************************************************************/
pSrcA.numCols = 3;
pSrcA.numRows = 3;
pSrcA.pData = pDataA;
pSrcB.numCols = 3;
pSrcB.numRows = 3;
pSrcB.pData = pDataB;
arm_mat_inverse_f32(&pSrcA, &pSrcB);
for(i = 0; i < 9; i++)
{
printf("pDataB[%d] = %frn", i, pDataB);
}
}
1. 用C语言实现逆矩阵要稍麻烦些,下面我们通过Matlab来实现求逆矩阵(数据和上面代码中的程序一样)

    可以看出求得结果跟上面的C函数求得结果基本一致。

席萌0209 发表于 2015-3-31 11:12:57

19.4 矩阵减法 MatSub

19.4.1 arm_mat_sub_f32

公式描述(以3*3矩阵为例进行说明):

函数定义如下:
    arm_status arm_mat_sub_f32(
      const arm_matrix_instance_f32 * pSrcA,
      const arm_matrix_instance_f32 * pSrcB,
      arm_matrix_instance_f32 * pDst)
参数定义:
        *pSrcA points to the first input matrix structure      
        *pSrcB points to the second input matrix structure      
       *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)
参数定义:
        *pSrcA points to the first input matrix structure      
        *pSrcB points to the second input matrix structure      
       *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)
参数定义:
        *pSrcA points to the first input matrix structure      
        *pSrcB points to the second input matrix structure      
       *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光盘里面有此软件)查看打印信息现象如下:
程序设计:
/*
*********************************************************************************************************
*    函 数 名: DSP_MatSub
*    功能说明: 矩阵减法
*    形    参:无
*    返 回 值: 无
*********************************************************************************************************
*/
static void DSP_MatSub(void)
{
uint8_t i;
/****浮点数数组******************************************************************/
float32_t pDataA = {1.1f, 1.1f, 2.1f, 2.1f, 3.1f, 3.1f, 4.1f, 4.1f, 5.1f};
float32_t pDataB = {1.1f, 1.1f, 2.1f, 2.1f, 3.1f, 3.1f, 4.1f, 4.1f, 5.1f};
float32_t pDataDst;
arm_matrix_instance_f32 pSrcA; //3行3列数据
arm_matrix_instance_f32 pSrcB; //3行3列数据
arm_matrix_instance_f32 pDst;
/****定点数Q31数组******************************************************************/
q31_t pDataA1 = {1, 1, 2, 2, 3, 3, 4, 4, 5};
q31_t pDataB1 = {2, 2, 2, 2, 2, 2, 2, 2, 2};
q31_t pDataDst1;
arm_matrix_instance_q31 pSrcA1; //3行3列数据
arm_matrix_instance_q31 pSrcB1; //3行3列数据
arm_matrix_instance_q31 pDst1;
/****定点数Q15数组******************************************************************/
q15_t pDataA2 = {1, 1, 2, 2, 3, 3, 4, 4, 5};
q15_t pDataB2 = {2, 2, 2, 2, 23, 2, 2, 2, 2};
q15_t pDataDst2;
arm_matrix_instance_q15 pSrcA2; //3行3列数据
arm_matrix_instance_q15 pSrcB2; //3行3列数据
arm_matrix_instance_q15 pDst2;
/****浮点数***********************************************************************/
pSrcA.numCols = 3;
pSrcA.numRows = 3;
pSrcA.pData = pDataA;
pSrcB.numCols = 3;
pSrcB.numRows = 3;
pSrcB.pData = pDataB;
pDst.numCols = 3;
pDst.numRows = 3;
pDst.pData = pDataDst;
printf("****浮点数******************************************rn");
arm_mat_sub_f32(&pSrcA, &pSrcB, &pDst);
for(i = 0; i < 9; i++)
{
printf("pDataDst[%d] = %frn", i, pDataDst);
}
/****定点数Q31***********************************************************************/
pSrcA1.numCols = 3;
pSrcA1.numRows = 3;
pSrcA1.pData = pDataA1;
pSrcB1.numCols = 3;
pSrcB1.numRows = 3;
pSrcB1.pData = pDataB1;
pDst1.numCols = 3;
pDst1.numRows = 3;
pDst1.pData = pDataDst1;
printf("****定点数Q31******************************************rn");
arm_mat_sub_q31(&pSrcA1, &pSrcB1, &pDst1);
for(i = 0; i < 9; i++)
{
printf("pDataDst1[%d] = %drn", i, pDataDst1);
}
/****定点数Q15***********************************************************************/
pSrcA2.numCols = 3;
pSrcA2.numRows = 3;
pSrcA2.pData = pDataA2;
pSrcB2.numCols = 3;
pSrcB2.numRows = 3;
pSrcB2.pData = pDataB2;
pDst2.numCols = 3;
pDst2.numRows = 3;
pDst2.pData = pDataDst2;
printf("****定点数Q15******************************************rn");
arm_mat_sub_q15(&pSrcA2, &pSrcB2, &pDst2);
for(i = 0; i < 9; i++)
{
printf("pDataDst2[%d] = %drn", i, pDataDst2);
}
}
1. 矩阵的减法从C语言的实现上来看,比较的容易,下面通过Matlab来求解矩阵和(在命令窗口输入)。


19.5 总结
    本期教程就跟大家讲这么多,有兴趣的可以深入研究下算法的具体实现。
页: [1]
查看完整版本: 【安富莱DSP教程】第19章 MatrixFunctions的使用(一)