Shop OBEX P1 Docs P2 Docs Learn Events
Error in ROM Sin table???.....No there is no error....it is my mistake — Parallax Forums

Error in ROM Sin table???.....No there is no error....it is my mistake

SamMishalSamMishal Posts: 468
edited 2009-09-14 10:23 in Propeller 1
Hi All,

I downloaded the Sin table values from the ROM and put them into an excel spread sheet and compared the values to calculated values.

I am sure I am wrong....but...I found that in many places there is a LARGE error......where am I going wrong?

Here is an excerpt from the spread sheet

[img]http://forums.parallax.com/attachment.php?attachmentid=74234[/img]
[img]http://forums.parallax.com/attachment.php?attachmentid=74235[/img]


I noticed the errors get larger towards the middle of the table. At the beginning and the end there was only an error of 1 or 2.
See the attached zip file that contains an .XLS file for a full table.

If you are wondering how I got the values, I used a RobotBASIC program to read the data from the Propeller running a SPIN program.
See the attached programs below.

The RobotBASIC program even plots the values and the Plot is perfect looking. Also the RB program puts the values on the Clipboard
which I then used to paste into the excel sheet in the "Spin Table Sin()" column as you see above.

In the·spread sheet·the "Calculated Sin()" uses the formula
·· · =ROUND(SIN(Angle)*65535,0)
this gets the normal Sin() which of course ranges from 0 to 1 (for angles 0 to 90) and then multiplies it by 65535 and rounds it to
get an integer value ranging from 0 to 65535 (i.e. 16-bit integer) as is given in the ROM table.

(I could have used RobotBASIC to do all these calcs but I wanted people to see the data without having
·to run or download RB and most people have Excel)


So my questions are:
·· - Why are the values from the ROM so far off from the calculated values?
········· I know 18 is not much but that happens to be 0.04% in some places....not much really
········· But why couldn't the ROM data have been made more accurate?
·· - Am I missing something obvious?




Regards

Samuel




Post Edited (SamMishal) : 9/14/2009 10:25:54 AM GMT

Comments

  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2009-09-14 08:45
    I checked the sine table. It has no errors. Attached is the Perl program I used, along with the data dumped from the Prop using this program:

    [b]CON[/b]
    
      [b]_clkmode[/b]      = [b]xtal1[/b] + [b]pll16x[/b]
      [b]_xinfreq[/b]      = 5_000_000
    
      SINE          = $e000
    
    
    [b]OBJ[/b]
    
      sio : "FullDuplexSerial"
    
    [b]PUB[/b] start | i
    
      sio.start(31, 30, 0, 9600)
      [b]repeat[/b] i [b]from[/b] 0 to $7ff
        sio.hex(sin(i), 4)
        [b]if[/b] (i & 15 == 15)
          sio.tx(13)
        [b]else[/b]
          sio.tx(" ")  
    
    [b]PUB[/b] sin(x) : value
    
    '' Sine of the angle x: 0 to 360 degrees == $0000 to $2000
    
      [b]if[/b] (x & $fff == $800)
        value := $1_0000
      [b]elseif[/b] (x & $800)
        value := [b]word[/b][noparse][[/noparse]*SINE][noparse][[/noparse]*-x & $7ff]
      [b]else[/b]
        value := [b]word[/b][noparse][[/noparse]*SINE][noparse][[/noparse]*x & $7ff]
      [b]if[/b] (x & $1000)
        value := -value
    
    
    


    I did, however, discover that the table is normalized to 65535 rather than 65536. I had assumed the latter (and it still makes more sense to me).

    -Phil

    Post Edited (Phil Pilgrim (PhiPi)) : 9/14/2009 8:52:55 AM GMT
  • SamMishalSamMishal Posts: 468
    edited 2009-09-14 10:23
    Hi Phil,

    I have discovered the error.......it is because I am using excel.....had I used RB to calculate the
    values I would not have had the error.....I did the calculations using RB and there was no error.

    I used the formula

    for i=0 to 2048
       N = round(sin(pi(i)/4096)*65535)
       if N != a[noparse][[/noparse]i] then print a[noparse][[/noparse]i];N;abs(x-a[noparse][[/noparse]i])
    next
    

    and that printed no lines at all which means that all calculated N were equal to a[noparse][[/noparse]i] (data from table).

    So·I went through the Excel formulas and found that the problem is in the way I am generating
    the data in the Angles column....which actually makes sense once I thought about it....

    I am generating each angle in each row as the sum of the angle in the row above it and Pi()/4096 (seems logical...no?).

    This looks like it is OK initially....but that means each error in the row above is accumulated in the row below
    and thus the problem......

    When I changed the Angles column to calculate the angle from a counter as I did using RB then it was
    OK and there was no errors too.

    I knew there was something wrong......and it is due to my inability to use Excel properly.....blush.gif

    I should have stuck to RobotBASIC in the first place.........tongue.gif

    But it is interesting how what seemed as a logical thing to do is actually quite wrong when considered in details...

    Anyway this was an interesting exercise....I was trying to understand the ROM sin and log tables
    on my way to figure out how to use them to generate DTMF tones using PWM techniques.

    Has anyone done this....is there an OBEX object for a summing-multichannel sinewave generator?
    I looked and I cannot see any???


    Regards

    Samuel

    ·
Sign In or Register to comment.