I want to approximate a sine signal with a parabola, which is given by the function:
$$f(x) = 0.4053 \cdot x \cdot (3.1415 - x) \space\text{for}\space 0 \le x \le 3.1415$$
In a first approach, I wrote the following code which works fine:
float sin1(int phase)
{
if(phase % 1000 <= 500)
{
return(0.4053*2*3.1415*(phase % 1000)/1000*(3.1415 - 2*3.1415*(phase % 1000)/1000));
}
else
{
return(-0.4053*2*3.1415*(phase % 500)/1000*(3.1415 - 2*3.1415*(phase % 500)/1000));
}
}
As I want to use this function on a microcontroller, I'm interested in how to optimize this code regarding memory consumption and performance.
phase
is given as thousandths of a radian? \$\endgroup\$ – 200_success Dec 22 '17 at 20:21phase
guaranteed to be positive? \$\endgroup\$ – Snowbody Dec 22 '17 at 22:27