DSP使用三角函数问题

问题

当浮点数很大时,作为三角函数的入参,计算出来的结果会让人大跌眼镜!

X64 Windows计算器计算的sin(110000),上面那个数值明显是不对的

分析

查看sinf函数原型,是通过查表法来实现的,也就说芯片内部不可能维护一个巨大的表格,入参很大会有误差

C++ 复制代码
//! \brief Single-Precision Floating-Point Sine (radians)
//! \param[in] X single precision floating point argument
//! \return the sine of a floating-point argument X (in radians) using table
//! look-up and Taylor series expansion between the look-up table entries.
//!
//! \note 
//! -# This is a standard C math function and requires "math.h" to be 
//! included
//! -# For COFF executables float and double are both single precision
//! 32-bit floating point types, therefore, the double and float variant of 
//! this function may be used interchangeably.
//!
//! <table>
//! <caption id="multi_row">Performance Data</caption>
//! <tr><th>Cycles <th> Comment 
//! <tr><td> 38 <td> Cycle count includes the call and return
//! </table>
//
float32_t sinf (float32_t X);

解决

需要将角度值限制在±2π范围内(理论上说这个范围内的精度是最好的)

Plain 复制代码
// 将角度归一化到[-2π, 2π]范围内
float normalize_angle(float angle)
{
    const float two_pi = 2.0f * PI;
    float normalized = fmodf(angle, two_pi);
    if(normalized < -PI) normalized += two_pi;
    if(normalized > PI) normalized -= two_pi;
    return normalized;
}

调试看归一化后的函数值

相关推荐
冷凝雨7 天前
复数乘法(C & Simulink)
c语言·开发语言·信号处理·simulink·dsp
老歌老听老掉牙10 天前
复杂表达式的三角平方和化简及其几何意义
三角函数·化简
昔时扬尘处20 天前
【C2000系列DSP的不掉电升级】C2000 不掉电升级(LFU)方案详解(含流程、代码与官方方案适配)
网络·dsp·c2000·德州仪器·实时控制mcu·lfu不掉电升级·后台升级
撬动未来的支点23 天前
【AI邪修·嵌入式】入门DSP28335
dsp
jzwalliser1 个月前
三角函数公式全归纳
数学·三角函数·高考
太爱学习了1 个月前
XILINX SRIOIP核详解、FPGA实现及仿真全流程(Serial RapidIO Gen2 Endpoint v4.1)
fpga开发·srio·dsp
課代表1 个月前
正弦函数与椭圆的关系
数学·算法·几何·三角函数·椭圆·正弦·周长
ltqshs1 个月前
TI DSP-TMS32F28335开发
dsp·28335
硬汉嵌入式2 个月前
H7-TOOL集成DSP数字信号量处理库,FFT注册添加成功
数字信号处理·dsp·fft·h7-tool
硬汉嵌入式2 个月前
Cortex-M55/M85的单片机DSP,AI技术Helium权威指南电子书
dsp·cortex-m85