Create sine lookup table in C++

后端 未结 6 1338
庸人自扰
庸人自扰 2020-12-13 15:27

How can I rewrite the following pseudocode in C++?

real array sine_table[-1000..1000]
    for x from -1000 to 1000
        sine_table[x] := sine(pi * x / 100         


        
6条回答
  •  时光取名叫无心
    2020-12-13 15:58

    another approximation from a book or something

    streamin ramp;
    streamout sine;
    
    float x,rect,k,i,j;
    
    x = ramp -0.5;
    
    rect = x * (1 - x < 0 & 2);
    k = (rect + 0.42493299) *(rect -0.5) * (rect - 0.92493302) ;
    i = 0.436501 + (rect * (rect + 1.05802));
    j = 1.21551 + (rect * (rect - 2.0580201));
    sine = i*j*k*60.252201*x;
    

    full discussion here: http://synthmaker.co.uk/forum/viewtopic.php?f=4&t=6457&st=0&sk=t&sd=a

    I presume that you know, that using a division is a lot slower than multiplying by decimal number, /5 is always slower than *0.2

    it's just an approximation.

    also:

    streamin ramp;
    streamin x;  // 1.5 = Saw   3.142 = Sin   4.5 = SawSin
    streamout sine;
    float saw,saw2;
    saw = (ramp * 2 - 1) * x;
    saw2 = saw * saw;
    
    sine = -0.166667 + saw2 * (0.00833333 + saw2 * (-0.000198409 + saw2 * (2.7526e-006+saw2 * -2.39e-008)));
    sine = saw * (1+ saw2 * sine);
    

提交回复
热议问题