pouët.net

Go to bottom

sin/cos table generator

category: code [glöplog]
 
this is my sine table generator:
for(;x<256;){a+=b;b+=c;*s++=0xf-((a+t)>>3);x++==127?c=-c:0;}

it is harmonic oscillator based on simple physics.
a and b are initial seeds.

this one will work on 8-bit with a little offset-tweaking,
between the region[0..59] where x+=95 and y+=22.
the drawback is its amplitude will be something like -16 -> 16.
tweaking might be required. or an additional byte for bigger values.

i found some interesting areas while running through all possible states for b and c regions (total 65536 states) but none of them where perfect.
added on the 2012-11-17 19:38:24 by rudi rudi
How much better is it than the parabola-based generator?
added on the 2012-11-17 20:01:06 by FreeFull FreeFull
FreeFull: i have no idea.
added on the 2012-11-17 20:17:05 by rudi rudi
This IS a parabola generator, since the accelleration (b delta) is constant for each half. To generate a sine shape, the update of b must be b-=(a*f)>>g for some appropriate f and g.

There might be some problems with skewed updating, though, since the new value of a is used. If the table evaluation points fall in between the points that would hit 0 exactly, it fits. But that might not be the desirable result. ;)
added on the 2012-11-17 20:50:48 by Blueberry Blueberry
This is mine: while(x<256)*s++=sin(a*x);
added on the 2012-11-17 21:52:13 by Sesse Sesse
(OK, you need to increment x as well. I'm sure you can also extend it to cos() if you think for a bit.)
added on the 2012-11-17 21:52:59 by Sesse Sesse
Quote:
I'm sure you can also extend it to cos() if you think for a bit.
this is clearly impossible.
added on the 2012-11-17 22:28:24 by ferris ferris
Blueberry: ok. the points does not hit 0 exactly. thats the problem. if it might be because all variables are bytes, and the integers does not fit in the "oscillator space" for the particle. i dont know? a better approximation if one use words ive tested that. but i wanted it for a 8-bit instruction set, so this is how i came up with this so far. :s
added on the 2012-11-17 22:43:27 by rudi rudi
Yes, I think the oscillator/parabola way is the easiest/dirtiest way to make something that looks like a sine without using higher math. The 20 byte sine generator I used in my x86 intros (and borrowed from baze under his permission) I have heard is an oscillator, I also wrote something with the similar concept to gain some bytes in my last CPC intros, where I originally just used a 64byte table that mirrored 3 more times.
added on the 2012-11-18 16:34:32 by Optimus Optimus
There was an article about sine generation in an old issue of Hugi: http://www.hugi.scene.org/online/coding/hugi%2016%20-%20cosine.htm
added on the 2012-11-18 16:37:27 by Adok Adok
That, as always, is completely irrelevant and worthless here.
added on the 2012-11-18 16:40:23 by Preacher Preacher

login

Go to top