• Pins used are for an Espruino board.

    //Using a rotation matrix to generate sin and cos values
    //output them on Pin A4 and input them on Pin A1 as analog values.
    //Pin A4 is wired to Pin A1 for this test
    
    A1.mode('analog');
    A4.mode('analog');
    //console.log(A1.getMode());
    //console.log(A4.getMode());
    
    //for angles <5 degrees sin(a)=a, where a is in radians
    var s=6.28/360;
    //cos^2 = 1-sin^2
    var b=1.0-s*s;
    // the cos
    var c=Math.sqrt(b);
    // a unit vector point along positive x axis
    var ss=0.0;
    var cc=1.0;
    var sss=ss;
    var ccc=cc;
    
    for(i=0;i<361;i+=1){
    //sin values
     analogWrite(A4,0.5+ss/2.0);
     console.log(i,' ',0.5+ss/2.0,' ',analogRead(A1));
    //cos values
    // analogWrite(A4,0.5+cc/2.0);
    // console.log(i,' ',0.5+cc/2.0,' ',analogRead(A1));
    
    //Matrix multiply the unit vector times the rotation matrix
    //                       [c,-s]
    // [ccc,sss] = [cc,ss] * [s, c]
    //
     ccc=c*cc-s*ss;
     sss=s*cc+c*ss;
     ss=sss;
     cc=ccc;
    }//next i
    

    And some of the output:

    >echo(0);
    0   0.5   0.50098420691
    1   0.50872222222   0.50928511482
    2   0.51744178999   0.51783016708
    3   0.52615604967   0.52661936369
    4   0.53486234924   0.53565270466
    5   0.54355803908   0.54468604562
    6   0.55224047282   0.55298695353
    7   0.56090700813   0.56226443884
    8   0.56955500750   0.57032120241
    9   0.57818183907   0.57935454337
    10   0.58678487742   0.58814373998
    11   0.59536150437   0.59693293659
    12   0.60390910979   0.60547798886
    13   0.61242509236   0.61329060807
    14   0.62090686040   0.62232394903
    15   0.62935183263   0.63062485694
    16   0.63775743899   0.63868162050
    17   0.64612112138   0.64747081712
    18   0.65444033447   0.65577172503
    19   0.66271254646   0.66407263294
    20   0.67093523984   0.67237354085
    21   0.67910591219   0.68018616006
    22   0.68722207692   0.68848706797
    23   0.69528126402   0.69703212024
    24   0.70328102082   0.70460059510
    25   0.71121891274   0.71314564736
    26   0.71909252403   0.72193484397
    27   0.72689945850   0.72828259708
    28   0.73463734025   0.73658350499
    29   0.74230381440   0.74366369115
    30   0.74989654779   0.75123216601
    31   0.75741322972   0.75904478522
    32   0.76485157262   0.76661326009
    
  • Nice - thanks for posting up!

    I guess dividing by sqrt(ss*ss+cc*cc) would ensure that even after lots of iterations, you still kept the magnitude of the 2 numbers the same?

  • Numerical errors do build up because computer math is quantized. Makes one wonder if the world is really a computer simulation with Plank's constant being the bit size.

  • Post a reply
    • Bold
    • Italics
    • Link
    • Image
    • List
    • Quote
    • code
    • Preview
About

A different way to generate sines and cosines using a rotation matrix (sounds scary but it's not)

Posted by Avatar for ClearMemory041063 @ClearMemory041063

Actions