Generate sine signal in C without using the standard function

后端 未结 11 749
情歌与酒
情歌与酒 2021-02-02 09:41

I want to generate a sine signal in C without using the standard function sin() in order to trigger sine shaped changes in the brightness of a LED. My basic idea was to use a lo

11条回答
  •  独厮守ぢ
    2021-02-02 09:52

    It would help if you explain why you don't want the built-in function, but as others have said, the Taylor series is one way to estimate the value. However, the other answers seem to actually be using the Maclaurin series, not Taylor. You should have a lookup table of both sine and cosine. Then find x0, the closest x value in your lookup table to the x you want, and find d = x-x0. Then

    sin(x) =sin(x0)+cos(x0)*d-sin(x0)*d2/2-cos(x0)*d3/6 + ...

    If your lookup table is such that d<.01, then you'll be getting more than two digits of precision per term.

    Another method is to use the fact that if x = x0+d, then

    sin(x) = sin(x0)*cos(d)+cos(x0)*sin(d)

    You can use a lookup table to get sin(x0) and cos(x0), and then use Maclaurin series to get cos(d) and sin(d).

提交回复
热议问题