Question About Sine Table

The sine table in main ROM goes from $E000 to $F001. There are 2,049 16-bit samples in that memory block. I guess that means there are 4,098, or 4097, 32-bit registers. Does that mean sine samples repeat every two longs?


  • localrogerlocalroger Posts: 3,430
    edited 2020-11-23 18:16
    Addresses in Hub RAM are byte addresses, so the range $E000 to $F000 is 4 kilobytes ($1000 or 4096). The samples are words which are read from even byte offsets ($E000, $E002, $E004 etc.) and there are 2049 of them with a single word sample overlap at $F000-1, which simplifies some wraparound algorithms.
  • @localroger Thank you. I've been trying for several days to figure that out. I've seen "$F001-1" before and it had me scratching my head.
    I wrote some test Spin code that displayed 4 digit integer sine values from 0° to 90°. The results were close enough but a lot still wasn't clear to me.
    I'll order a DAC and see if I can produce a sine wave.
  • Duane DegnDuane Degn Posts: 10,405
    edited 2020-11-23 20:34
    You mentioned "MathTestSine" in a note. I just want to make sure you're also aware of this forum thread.

    I thought you found "MathTestSine" in that thread but I suppose it's also possible to have found it on my GitHub.

    BTW, I'm glad you found the program helpful.
  • Tracy AllenTracy Allen Posts: 6,545
    edited 2020-11-24 00:40
    You can use the Duty mode of the counter as a DAQ in order to produce your sine wave. It is pulse density encoded and can be easily smoothed with an RC circuit.

    The attached tutorial program in Spin produces a 1Hz sine and suggests outputting it to an LED see the pulsations.

    Below is the table lookup method. You can see that a full circle is 13 bits, and the top two point to quadrants 0--3. The lower 11 bits get shifted one place left in order to point to a word address in the sine table. The address at $F001 helps to tie the ends together as it transitions into quadrants 1 and 3.
    PUB Fullsine(x) | q                                  ' x is from counter, 13 msbs 0--8192 represents 0--360 degrees
      q := x >> 11                                          ' quadrant, 0 to 3
      x := (x & $7ff) << 1                                  ' 0 to 90- degrees, adjust to 2048 word addresses.
      case q                                                ' by quadrant
        0 : result := word[$E000 + x]                       ' 
        1 : result := word[$F001 - x]
        2 : result := -word[$E000 + x]
        3 : result := -word[$F001 - x]

  • @"Duane Degn" I found your post on the forum. The value @E801 is 46340 and I could not figure how to convert that to sine 45° 0.7071. You pointed out that the key was to divide the raw table values by '1' as a 16-bit value or 65535. I tested it for 30°, 45° and 60°. It worked and I exhaled. I didn't want to bump up a post from 2015 just to say thank you so I left it on your message board instead.
    @"Tracy Allen" All four quadrants using duty mode with an RC circuit sounds like an excellent test. That's what I'll do. Thank you.
    @localroger so hub ram is byte addressed and cog ram is long addressed. Got it.👍
  • Tracy AllenTracy Allen Posts: 6,545
    edited 2020-11-24 00:44
    I forgot to attach the tutorial program to my previous post; now it's there. Oh, edited again, to default to a standard 5MHz crystal and pin numbers for LEDs on the old Parallax demo board.
  • @"Tracy Allen" This is a wonderful sight. 🙂 Thank you.
    2448 x 3264 - 2M
  • I'm posting a zip for those who search the subject in the future. I hope they can benefit from what I've learned.
Sign In or Register to comment.