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.
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.
How much better is it than the parabola-based generator?
FreeFull: i have no idea.
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. ;)
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. ;)
This is mine: while(x<256)*s++=sin(a*x);
(OK, you need to increment x as well. I'm sure you can also extend it to cos() if you think for a bit.)
Quote:
this is clearly impossible.I'm sure you can also extend it to cos() if you think for a bit.
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
I think I'm going to stick with http://codebase64.org/doku.php?id=base:generating_approximate_sines_in_assembly
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.
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
That, as always, is completely irrelevant and worthless here.