<<
 
>>
 
 
justin = {main feed , music , code , askjf , pubkey };
 
stumbling in code
October 1, 2008
So while playing with some code to model a resonating spring (F=-k*pos), I discovered that the model I was using produced a very good sine wave approximation. This isn't normally something terribly interesting, as you can approximate sine/cosines very easily, but it was actually quite low complexity -- an iterative approximation with only 2 multiplies and 3 adds per sample. It can also generate the cos() (well, a 90 degree shifted signal) for each point for just one additional multiply.

The error is pretty low for the first few cycles, though after a bit it does drift in relation to the correct wave. I'm not going to spend too much more time on this, but if anybody wants to see if there's some way to correct it, go for it (it may just be rounding error, though, of course).

Here is the code:

Ta-da! If anybody wants to go and do some fancy pants math to show why this works, too, I'd love to hear it... :)


3 Comments:

Posted by Justin on Wed 01 Oct 2008 at 13:02 from 64.81.54.x

ahh, just found this: musicdsp.org/showArchiveComment.ph...

Less operations, pretty much same output..

Posted by Tim on Thu 09 Oct 2008 at 08:54 from 128.193.152.x

Har har...

as I read the first line and saw F=-k*pos I thought you were setting up a rant that was going to end in.."Fucking Piece of Shit"....

Posted by Nolan Eakins on Fri 10 Oct 2008 at 18:02 from 98.222.193.x

Just loop one cycle, perfect cos.

Add comment:

Name:
Human?: (no or yes, patented anti crap stuff here)
Comment:
search : rss : recent comments : Copyright © 2025 Justin Frankel