硬汉嵌入式论坛

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

[DSP] DSP库中的arm_sin_q31函数是查表实现,精度略低,大概到11到12个bit

[复制链接]

1万

主题

6万

回帖

10万

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
106978
QQ
发表于 2021-3-13 09:35:00 | 显示全部楼层 |阅读模式


原始数据:
0x00000000, 0x04000000, 0x08000000, 0x0C000000,
  0x10000000, 0x14000000, 0x18000000, 0x1C000000,
  0x20000000, 0x24000000,


理论结果:
0x00000000, 0x0C8BD35E, 0x18F8B83C, 0x25280C5D,
  0x30FBC54D, 0x3C56BA70, 0x471CECE6, 0x5133CC94,
  0x5A827999, 0x62F201AC,


实际求得结果:
1.png
回复

使用道具 举报

1万

主题

6万

回帖

10万

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
106978
QQ
 楼主| 发表于 2021-3-13 09:36:02 | 显示全部楼层
实现代码:

  1. /* ----------------------------------------------------------------------
  2. * Project:      CMSIS DSP Library
  3. * Title:        arm_sin_q31.c
  4. * Description:  Fast sine calculation for Q31 values
  5. *
  6. * $Date:        18. March 2019
  7. * $Revision:    V1.6.0
  8. *
  9. * Target Processor: Cortex-M cores
  10. * -------------------------------------------------------------------- */
  11. /*
  12. * Copyright (C) 2010-2019 ARM Limited or its affiliates. All rights reserved.
  13. *
  14. * SPDX-License-Identifier: Apache-2.0
  15. *
  16. * Licensed under the Apache License, Version 2.0 (the License); you may
  17. * not use this file except in compliance with the License.
  18. * You may obtain a copy of the License at
  19. *
  20. * www.apache.org/licenses/LICENSE-2.0
  21. *
  22. * Unless required by applicable law or agreed to in writing, software
  23. * distributed under the License is distributed on an AS IS BASIS, WITHOUT
  24. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  25. * See the License for the specific language governing permissions and
  26. * limitations under the License.
  27. */

  28. #include "arm_math.h"
  29. #include "arm_common_tables.h"

  30. /**
  31.   @ingroup groupFastMath
  32. */

  33. /**
  34.   @addtogroup sin
  35.   @{
  36. */

  37. /**
  38.   @brief         Fast approximation to the trigonometric sine function for Q31 data.
  39.   @param[in]     x  Scaled input value in radians
  40.   @return        sin(x)

  41.   The Q31 input value is in the range [0 +0.9999] and is mapped to a radian value in the range [0 2*PI).
  42. */

  43. q31_t arm_sin_q31(
  44.   q31_t x)
  45. {
  46.   q31_t sinVal;                                  /* Temporary variables for input, output */
  47.   int32_t index;                                 /* Index variable */
  48.   q31_t a, b;                                    /* Two nearest output values */
  49.   q31_t fract;                                   /* Temporary values for fractional values */

  50.   if (x < 0)
  51.   { /* convert negative numbers to corresponding positive ones */
  52.     x = (uint32_t)x + 0x80000000;
  53.   }

  54.   /* Calculate the nearest index */
  55.   index = (uint32_t)x >> FAST_MATH_Q31_SHIFT;

  56.   /* Calculation of fractional value */
  57.   fract = (x - (index << FAST_MATH_Q31_SHIFT)) << 9;

  58.   /* Read two nearest values of input value from the sin table */
  59.   a = sinTable_q31[index];
  60.   b = sinTable_q31[index+1];

  61.   /* Linear interpolation process */
  62.   sinVal = (q63_t) (0x80000000 - fract) * a >> 32;
  63.   sinVal = (q31_t) ((((q63_t) sinVal << 32) + ((q63_t) fract * b)) >> 32);

  64.   /* Return output value */
  65.   return (sinVal << 1);
  66. }

  67. /**
  68.   @} end of sin group
  69. */
复制代码


回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-5-13 04:33 , Processed in 0.156104 second(s), 28 queries .

Powered by Discuz! X3.4 Licensed

Copyright © 2001-2023, Tencent Cloud.

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