打印
[DemoCode下载]

新唐M4系列DSP实现快速数**算示例

[复制链接]
6719|21
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
幸福小强|  楼主 | 2021-7-10 17:29 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
展示使用CMSIS DSP函式库快速数**算,其中为三角函数运算包含
1. 正弦(输入为弧度)
2. 余弦(输入为弧度)
3. 正弦和余弦(输入为角度)
用户可以直接使用这些函式,来实现自己的数学方程式运算。
程序内比较了有无使用DSP计算时间的差异,并计算效率提升比率。


使用特权

评论回复
沙发
幸福小强|  楼主 | 2021-7-10 17:30 | 只看该作者
EC_M480_DSP_FastMath_V1.00.zip (6.54 MB)
/*************************************************************************//**
* [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 Fast math functions(sine, cosine)
*           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 blockSize    32

/*---------------------------------------------------------------------------*/
/* Global variables                                                          */
/*---------------------------------------------------------------------------*/
const float32_t testInput_f32[blockSize] = {
    -1.244916875853235400,  -4.793533929171324800,  0.360705030233248850,   0.827929644170887320,   -3.299532218312426900,  3.427441903227623800,   3.422401784294607700,   -0.108308165334010680,
        0.941943896490312180,   0.502609575000365850,   -0.537345278736373500,  2.088817392965764500,   -1.693168684143455700,  6.283185307179590700,   -0.392545884746175080,  0.327893095115825040,
        3.070147440456292300,   0.170611405884662230,   -0.275275082396073010,  -2.395492805446796300,  0.847311163536506600,   -3.845517018083148800,  2.055818378415868300,   4.672594161978930800,
        -1.990923030266425800,  2.469305197656249500,   3.609002606064021000,   -4.586736582331667500,  -4.147080139136136300,  1.643756718868359500,   -1.150866392366494800,  1.985805026477433800
    };

float32_t  cosOutput[blockSize], cosOutput1[blockSize];
float32_t  sinOutput[blockSize], sinOutput1[blockSize];
float cosine[blockSize], cosine1[blockSize], sine[blockSize], sine1[blockSize];
float DSPCalTime, CalTime;
uint32_t i = 0, j = 0;

/*---------------------------------------------------------------------------*/
/* Functions                                                                 */
/*---------------------------------------------------------------------------*/
void sin_cos(float theta)
{
    if (j < blockSize) {
        sine1[j] = sin(theta);
        cosine1[j] = cos(theta);
        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 Fast math functions Sample Code   |\n");
    printf("+-----------------------------------------+\n\n");

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

    /* Calculate sine and cosine with M4 DSP instruction */
    for (i = 0; i < blockSize; i++) {
        /* input is radian */
        cosOutput[i] = arm_cos_f32(testInput_f32[i]);
        sinOutput[i] = arm_sin_f32(testInput_f32[i]);

        /* input is degree */
        arm_sin_cos_f32(testInput_f32[i], &sinOutput1[i], &cosOutput1[i]);
    }

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

    /* Calculate sine and cosine */
    for (i = 0; i < blockSize; i++) {
        /* input is radian */
        cosine[i] = cos(testInput_f32[i]);
        sine[i] = sin(testInput_f32[i]);
        /* input is radian */
        sin_cos(testInput_f32[i]);
    }

    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);

}

/*** (C) COPYRIGHT 2019 Nuvoton Technology Corp. ***/


使用特权

评论回复
板凳
huahuagg| | 2021-7-10 21:57 | 只看该作者
看起来很好用,先掌握一下这个技能,如果配合DAC是不是就可以不用查表就能输出正弦波信号了。

使用特权

评论回复
地板
foxsbig| | 2021-7-12 09:55 | 只看该作者
额,库函数里还有这个,不知道哎
学习了

使用特权

评论回复
5
twjiang| | 2021-7-12 10:03 | 只看该作者
阿波罗飞般实现的三角函数

https://fermatslibrary.com/s/apollo-11-implementation-of-trigonometric-functions

使用特权

评论回复
评论
幸福小强 2021-9-24 08:25 回复TA
666 
6
cyclefly| | 2021-7-15 13:18 | 只看该作者
感谢分享,有机会试试

使用特权

评论回复
7
nawu| | 2021-8-13 18:07 | 只看该作者
可以不用查表就能输出正弦波信号  是吗

使用特权

评论回复
8
tfqi| | 2021-8-13 18:08 | 只看该作者
应该是很好用的

使用特权

评论回复
9
wiba| | 2021-8-13 18:10 | 只看该作者
需要对三角函数有所了解吧

使用特权

评论回复
10
zljiu| | 2021-8-13 18:11 | 只看该作者
直接调用就可以的吧

使用特权

评论回复
11
coshi| | 2021-8-13 18:12 | 只看该作者
有成熟的库函数吗

使用特权

评论回复
12
elsaflower| | 2021-10-6 16:42 | 只看该作者
比math快多少呢?   

使用特权

评论回复
13
ulystronglll| | 2021-10-6 16:42 | 只看该作者
DSP库在哪里下载呢   

使用特权

评论回复
14
fengm| | 2021-10-6 16:42 | 只看该作者
三角函数运算浪费时间   

使用特权

评论回复
15
sdCAD| | 2021-10-6 16:42 | 只看该作者
能用查表的方法吗   

使用特权

评论回复
16
backlugin| | 2021-10-6 16:42 | 只看该作者
CMSIS DSP是keil的吗   

使用特权

评论回复
17
sanxingnote7| | 2021-10-6 16:42 | 只看该作者

使用特权

评论回复
18
dzfansman| | 2021-10-6 16:43 | 只看该作者

用了查表的方法吗   

使用特权

评论回复
19
iamaiqiyi| | 2021-10-6 16:43 | 只看该作者
twjiang 发表于 2021-7-12 10:03
阿波罗飞般实现的三角函数

https://fermatslibrary.com/s/apollo-11-implementation-of-trigonometric-fun ...

计算的效果怎么样   

使用特权

评论回复
20
jstgotodo| | 2021-10-6 16:43 | 只看该作者
DSP计算时间的差异对比有吗   

使用特权

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

本版积分规则

107

主题

1415

帖子

2

粉丝