It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center。
6年前关闭。
我试图用仅整数和不使用表来逼近正弦函数。
为每个步骤调用函数。我已经成功地在Matlab中实现了它,但是在C代码中有些问题。由于某种原因,我得到了错误的频率读数,而该功能并不适用于所有频率。
也许我把它复杂化了。
6年前关闭。
我试图用仅整数和不使用表来逼近正弦函数。
为每个步骤调用函数。我已经成功地在Matlab中实现了它,但是在C代码中有些问题。由于某种原因,我得到了错误的频率读数,而该功能并不适用于所有频率。
typedef volatile struct tone_s{
int32_t impulse;
int32_t acceleration;
int32_t rollover;
int32_t velocity;
int32_t phase;
int32_t counter;
int32_t position_acc;
int32_t velocity_acc;
}tone_t;
static void Osc_Init(tone_t *osc, uint32_t frequency, uint32_t sample_rate){
int32_t max_int = (1UL << 16);
frequency *= 4UL;
osc->impulse = (max_int * frequency);
osc->acceleration = max_int/sample_rate * frequency * frequency;
osc->rollover = (sample_rate * 2UL / frequency);
osc->velocity = osc->impulse - (osc->acceleration / 2UL);
osc->velocity_acc = osc->velocity;
osc->phase = -1UL;
osc->counter = 0;
osc->position_acc = 0;
}
#include <stdio.h>
static int16_t Osc_GenSample(tone_t *osc){
if(osc->counter == osc->rollover){
osc->velocity_acc = osc->velocity;
osc->position_acc = 0;
osc->phase = -osc->phase;
osc->counter = 0;
}
int32_t sample = (osc->position_acc / 4194304UL) * osc->phase;
osc->position_acc += osc->velocity_acc;
osc->velocity_acc -= osc->acceleration;
osc->counter++;
//fprintf(stdout, "%d - %d %d %d %d %d %d %d\n", sample, osc->impulse, osc->acceleration, osc->velocity, osc->rollover, osc->position_acc, osc->velocity_acc, osc->counter);
return sample;
}
也许我把它复杂化了。
最佳答案
请参见Hakmem items 149 to 152以了解如何用很少的算法递增地绘制圆(当然这会给出正弦值)。
关于c - 正弦近似,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/17200717/
10-17 02:47