Generate sine signal in C without using the standard function

后端 未结 11 743
情歌与酒
情歌与酒 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:48

    Since you try to generate a signal, i think use a differential equation should not be a bad idea ! it give something like that

    #include 
    #include 
    
    #define DT (0.01f) //1/s
    #define W0 (3)     //rad/s
    
    int main(void) {
        float a = 0.0f;
        float b = DT * W0;
        float tmp;
    
        for (int i = 0; i < 400; i++) {
            tmp = (1 / (1 + (DT * DT * W0 * W0))) * (2 * a - b);
            b = a;
            a = tmp;
    
            printf("%f\n", tmp);
        }
    }
    

    Still set the amplitude and frequency of the signal is a pain in the neck :/

提交回复
热议问题