打印
[DemoCode下载]

DSP计算卷积

[复制链接]
1150|19
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
新唐很多系列MCU具备DSP内核,可以很方便的进行高级计算。
简介
展示使用 CMSIS DSP 函式库进行卷积运算(Convolution),用户可以直接使用这些函式,来实现自己的数学方程式运算。程序内比较了有无使用 DSP 计算时间的差异,并计算效率提升比率。
原理



使用特权

评论回复
沙发
jiekou001|  楼主 | 2022-12-22 16:15 | 显示全部楼层
/*************************************************************************//**
* [url=home.php?mod=space&uid=288409]@file[/url]     main.c
* [url=home.php?mod=space&uid=247401]@brief[/url]    Display how to use DSP Convolution function
*           and compare with calcultion without DSP
*
* @note
* [url=home.php?mod=space&uid=17282]@CopyRight[/url] (C) 2019 Nuvoton Technology Corp. All rights reserved.
*****************************************************************************/
#include <stdio.h>
#include "NuMicro.h"
#include "arm_math.h"
/*---------------------------------------------------------------------------*/
/* Define                                                                    */
/*---------------------------------------------------------------------------*/
#define PLL_CLOCK    192000000
#define TEST_LENGTH_SAMPLES 320
#define BLOCK_SIZE          32
#define NUM_TAPS            29

extern float32_t testInput_f32_1kHz_15kHz[TEST_LENGTH_SAMPLES];
/*---------------------------------------------------------------------------*/
/* Global variables                                                          */
/*---------------------------------------------------------------------------*/
float32_t firCoeffs32[NUM_TAPS] = {
    -0.0018225230f, -0.0015879294f, +0.0000000000f, +0.0036977508f, +0.0080754303f, +0.0085302217f, -0.0000000000f, -0.0173976984f,
        -0.0341458607f, -0.0333591565f, +0.0000000000f, +0.0676308395f, +0.1522061835f, +0.2229246956f, +0.2504960933f, +0.2229246956f,
        +0.1522061835f, +0.0676308395f, +0.0000000000f, -0.0333591565f, -0.0341458607f, -0.0173976984f, -0.0000000000f, +0.0085302217f,
        +0.0080754303f, +0.0036977508f, +0.0000000000f, -0.0015879294f, -0.0018225230f
    };
float32_t conoutput[400], CONoutput[320], CONLOutput, DSPCalTime, CalTime;
uint32_t numBlocks = TEST_LENGTH_SAMPLES / BLOCK_SIZE, i = 0, j = 0, k = 0;

/*---------------------------------------------------------------------------*/
/* Functions                                                                 */
/*---------------------------------------------------------------------------*/
/* Convolution (length of the first input vector, length of the second input vector) */
void Convolution(int n, int m)
{
    CONLOutput = TEST_LENGTH_SAMPLES + NUM_TAPS - 1;

    for (i = 0; i < CONLOutput; ++i) {
        CONoutput[i] = 0;
    }

    for (i = 0; i < n; ++i) {
        for (j = 0; j < m; ++j) {
            CONoutput[i + j] += testInput_f32_1kHz_15kHz[i] * firCoeffs32[j];
        }
    }
}

void SYS_Init(void)
{
    /*---------------------------------------------------------------------------------------------------------*/
    /* Init System Clock                                                                                       */
    /*---------------------------------------------------------------------------------------------------------*/
    /* Unlock protected registers */
    SYS_UnlockReg();

    /* Set XT1_OUT(PF.2) and XT1_IN(PF.3) to input mode */
    PF->MODE &= ~(GPIO_MODE_MODE2_Msk | GPIO_MODE_MODE3_Msk);

    /* Enable External XTAL (4~24 MHz) */
    CLK_EnableXtalRC(CLK_PWRCTL_HXTEN_Msk);

    /* Waiting for 12MHz clock ready */
    CLK_WaitClockReady(CLK_STATUS_HXTSTB_Msk);

    /* Set core clock as PLL_CLOCK from PLL */
    CLK_SetCoreClock(PLL_CLOCK);
    /* Set PCLK0/PCLK1 to HCLK/2 */
    CLK->PCLKDIV = (CLK_PCLKDIV_PCLK0DIV2 | CLK_PCLKDIV_PCLK1DIV2);

    /* Enable UART clock */
    CLK_EnableModuleClock(UART0_MODULE);
    CLK_EnableModuleClock(TMR0_MODULE);

    /* Select UART clock source from HXT */
    CLK_SetModuleClock(UART0_MODULE, CLK_CLKSEL1_UART0SEL_HXT, CLK_CLKDIV0_UART0(1));
    CLK_SetModuleClock(TMR0_MODULE, CLK_CLKSEL1_TMR0SEL_HXT, 0);

    /* Update System Core Clock */
    /* User can use SystemCoreClockUpdate() to calculate SystemCoreClock. */
    SystemCoreClockUpdate();

    /* Lock protected registers */
    SYS_LockReg();
}
void UART_Init(void)
{
    /* Set GPB multi-function pins for UART0 RXD and TXD */
    SYS->GPB_MFPH &= ~(SYS_GPB_MFPH_PB12MFP_Msk | SYS_GPB_MFPH_PB13MFP_Msk);
    SYS->GPB_MFPH |= (SYS_GPB_MFPH_PB12MFP_UART0_RXD | SYS_GPB_MFPH_PB13MFP_UART0_TXD);

    /* Reset UART module */
    SYS_ResetModule(UART0_RST);

    /* Configure UART0 and set UART0 Baudrate */
    UART_Open(UART0, 115200);
}
/*---------------------------------------------------------------------------------------------------------*/
/*  Main Function                                                                                          */
/*---------------------------------------------------------------------------------------------------------*/
int main()
{
    /* Init System, peripheral clock and multi-function I/O */
    SYS_Init();
    /* Init UART for printf */
    UART_Init();

    printf("+-----------------------------------------+\n");
    printf("|       DSP Convolution Sample Code       |\n");
    printf("+-----------------------------------------+\n\n");

    /* Init Timer */
    TIMER_Open(TIMER0, TIMER_CONTINUOUS_MODE, 1);
    /* Let TIMER0 start to count */
    TIMER_Start(TIMER0);

    /* Calculate convoltion with M4 DSP instruction */
    arm_conv_f32(testInput_f32_1kHz_15kHz, TEST_LENGTH_SAMPLES, firCoeffs32, NUM_TAPS, conoutput);

    /* Read Timer counter */
    DSPCalTime = TIMER_GetCounter(TIMER0);
    /* Reset Timer counter */
    TIMER_ResetCounter(TIMER0);

    /* Calculate convoltion */
    Convolution(TEST_LENGTH_SAMPLES, NUM_TAPS);

    TIMER_Close(TIMER0);
    /* Read Timer counter */
    CalTime = TIMER_GetCounter(TIMER0);

    /* Calculate the time, timer clock source is 12M, unit is ms */
    DSPCalTime = (DSPCalTime / 12000000) * 1000;
    CalTime = (CalTime / 12000000) * 1000;

    printf("Calculating time with DSP instruction is %f ms\n\n", DSPCalTime);
    printf("Calculating time without DSP instruction is %f ms\n\n", CalTime);
    printf("Efficiency increase rate is %.2f \n", CalTime / DSPCalTime);

    while (1);

}

使用特权

评论回复
板凳
jiekou001|  楼主 | 2023-1-12 14:34 | 显示全部楼层
小夏天的大西瓜 发表于 2023-1-12 13:34
卷积学习一下,新手没有应用这个卷积定理

高等数学上的概念。

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

146

主题

1476

帖子

2

粉丝