[DemoCode下载] 新唐M4系列DSP实现快速数**算示例

[复制链接]
7854|21
 楼主| 幸福小强 发表于 2021-7-10 17:29 | 显示全部楼层 |阅读模式
展示使用CMSIS DSP函式库快速数**算,其中为三角函数运算包含
1. 正弦(输入为弧度)
2. 余弦(输入为弧度)
3. 正弦和余弦(输入为角度)
用户可以直接使用这些函式,来实现自己的数学方程式运算。
程序内比较了有无使用DSP计算时间的差异,并计算效率提升比率。
4954060e96876c8e59.png

 楼主| 幸福小强 发表于 2021-7-10 17:30 | 显示全部楼层
EC_M480_DSP_FastMath_V1.00.zip (6.54 MB, 下载次数: 6)
  1. /*************************************************************************//**
  2. * [url=home.php?mod=space&uid=288409]@file[/url]     main.c
  3. * [url=home.php?mod=space&uid=247401]@brief[/url]    Display how to use DSP Fast math functions(sine, cosine)
  4. *           and compare with calcultion without DSP
  5. *
  6. * @note
  7. * [url=home.php?mod=space&uid=17282]@CopyRight[/url] (C) 2019 Nuvoton Technology Corp. All rights reserved.
  8. *****************************************************************************/
  9. #include <stdio.h>
  10. #include "NuMicro.h"
  11. #include "arm_math.h"
  12. /*---------------------------------------------------------------------------*/
  13. /* Define                                                                    */
  14. /*---------------------------------------------------------------------------*/
  15. #define PLL_CLOCK    192000000
  16. #define blockSize    32

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

  26. float32_t  cosOutput[blockSize], cosOutput1[blockSize];
  27. float32_t  sinOutput[blockSize], sinOutput1[blockSize];
  28. float cosine[blockSize], cosine1[blockSize], sine[blockSize], sine1[blockSize];
  29. float DSPCalTime, CalTime;
  30. uint32_t i = 0, j = 0;

  31. /*---------------------------------------------------------------------------*/
  32. /* Functions                                                                 */
  33. /*---------------------------------------------------------------------------*/
  34. void sin_cos(float theta)
  35. {
  36.     if (j < blockSize) {
  37.         sine1[j] = sin(theta);
  38.         cosine1[j] = cos(theta);
  39.         j++;
  40.     }
  41. }

  42. void SYS_Init(void)
  43. {
  44.     /*---------------------------------------------------------------------------------------------------------*/
  45.     /* Init System Clock                                                                                       */
  46.     /*---------------------------------------------------------------------------------------------------------*/
  47.     /* Unlock protected registers */
  48.     SYS_UnlockReg();

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

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

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

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

  59.     /* Enable UART clock */
  60.     CLK_EnableModuleClock(UART0_MODULE);
  61.     CLK_EnableModuleClock(TMR0_MODULE);

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

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

  68.     /* Lock protected registers */
  69.     SYS_LockReg();
  70. }
  71. void UART_Init(void)
  72. {
  73.     /* Set GPB multi-function pins for UART0 RXD and TXD */
  74.     SYS->GPB_MFPH &= ~(SYS_GPB_MFPH_PB12MFP_Msk | SYS_GPB_MFPH_PB13MFP_Msk);
  75.     SYS->GPB_MFPH |= (SYS_GPB_MFPH_PB12MFP_UART0_RXD | SYS_GPB_MFPH_PB13MFP_UART0_TXD);

  76.     /* Reset UART module */
  77.     SYS_ResetModule(UART0_RST);

  78.     /* Configure UART0 and set UART0 Baudrate */
  79.     UART_Open(UART0, 115200);
  80. }
  81. /*---------------------------------------------------------------------------------------------------------*/
  82. /*  Main Function                                                                                          */
  83. /*---------------------------------------------------------------------------------------------------------*/
  84. int main()
  85. {
  86.     /* Init System, peripheral clock and multi-function I/O */
  87.     SYS_Init();
  88.     /* Init UART for printf */
  89.     UART_Init();

  90.     printf("+-----------------------------------------+\n");
  91.     printf("|   DSP Fast math functions Sample Code   |\n");
  92.     printf("+-----------------------------------------+\n\n");

  93.     /* Init Timer */
  94.     TIMER_Open(TIMER0, TIMER_CONTINUOUS_MODE, 1);
  95.     /* Let TIMER0 start to count */
  96.     TIMER_Start(TIMER0);

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

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

  105.     /* Read Timer counter */
  106.     DSPCalTime = TIMER_GetCounter(TIMER0);
  107.     /* Reset Timer counter */
  108.     TIMER_ResetCounter(TIMER0);

  109.     /* Calculate sine and cosine */
  110.     for (i = 0; i < blockSize; i++) {
  111.         /* input is radian */
  112.         cosine[i] = cos(testInput_f32[i]);
  113.         sine[i] = sin(testInput_f32[i]);
  114.         /* input is radian */
  115.         sin_cos(testInput_f32[i]);
  116.     }

  117.     TIMER_Close(TIMER0);
  118.     /* Read Timer counter */
  119.     CalTime = TIMER_GetCounter(TIMER0);

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

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

  126.     while (1);

  127. }

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


huahuagg 发表于 2021-7-10 21:57 | 显示全部楼层
看起来很好用,先掌握一下这个技能,如果配合DAC是不是就可以不用查表就能输出正弦波信号了。
foxsbig 发表于 2021-7-12 09:55 | 显示全部楼层
额,库函数里还有这个,不知道哎
学习了
twjiang 发表于 2021-7-12 10:03 | 显示全部楼层
阿波罗飞般实现的三角函数

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

评论

666  发表于 2021-9-24 08:25
cyclefly 发表于 2021-7-15 13:18 | 显示全部楼层
感谢分享,有机会试试
nawu 发表于 2021-8-13 18:07 | 显示全部楼层
可以不用查表就能输出正弦波信号  是吗
tfqi 发表于 2021-8-13 18:08 | 显示全部楼层
应该是很好用的
wiba 发表于 2021-8-13 18:10 | 显示全部楼层
需要对三角函数有所了解吧
zljiu 发表于 2021-8-13 18:11 | 显示全部楼层
直接调用就可以的吧
coshi 发表于 2021-8-13 18:12 | 显示全部楼层
有成熟的库函数吗
elsaflower 发表于 2021-10-6 16:42 | 显示全部楼层
比math快多少呢?   
ulystronglll 发表于 2021-10-6 16:42 | 显示全部楼层
DSP库在哪里下载呢   
fengm 发表于 2021-10-6 16:42 | 显示全部楼层
三角函数运算浪费时间   
sdCAD 发表于 2021-10-6 16:42 | 显示全部楼层
能用查表的方法吗   
backlugin 发表于 2021-10-6 16:42 | 显示全部楼层
CMSIS DSP是keil的吗   
sanxingnote7 发表于 2021-10-6 16:42 | 显示全部楼层
dzfansman 发表于 2021-10-6 16:43 | 显示全部楼层
iamaiqiyi 发表于 2021-10-6 16:43 | 显示全部楼层
twjiang 发表于 2021-7-12 10:03
阿波罗飞般实现的三角函数

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

计算的效果怎么样   
jstgotodo 发表于 2021-10-6 16:43 | 显示全部楼层
DSP计算时间的差异对比有吗   
您需要登录后才可以回帖 登录 | 注册

本版积分规则

143

主题

1720

帖子

2

粉丝
快速回复 在线客服 返回列表 返回顶部