Note that there are some explanatory texts on larger screens.

plurals
  1. POFast equivalent to sin() for DSP referenced in STK
    primarykey
    data
    text
    <p>I'm using bits of Perry Cook's Synthesis Toolkit (STK) to generate saw and square waves. STK includes this BLIT-based sawtooth oscillator:</p> <pre><code>inline STKFloat BlitSaw::tick( void ) { StkFloat tmp, denominator = sin( phase_ ); if ( fabs(denominator) &lt;= std::numeric_limits&lt;StkFloat&gt;::epsilon() ) tmp = a_; else { tmp = sin( m_ * phase_ ); tmp /= p_ * denominator; } tmp += state_ - C2_; state_ = tmp * 0.995; phase_ += rate_; if ( phase_ &gt;= PI ) phase_ -= PI; lastFrame_[0] = tmp; return lastFrame_[0]; } </code></pre> <p>The square wave oscillator is broadly similar. At the top, there's this comment:</p> <pre><code>// A fully optimized version of this code would replace the two sin // calls with a pair of fast sin oscillators, for which stable fast // two-multiply algorithms are well known. </code></pre> <p>I don't know where to start looking for these "fast two-multiply algorithms" and I'd appreciate some pointers. I could use a lookup table instead, but I'm keen to learn what these 'fast sin oscillators' are. I could also use an abbreviated Taylor series, but thats way more than two multiplies. Searching hasn't turned up anything much, although I did find this approximation:</p> <pre><code>#define AD_SIN(n) (n*(2.f- fabs(n))) </code></pre> <p>Plotting it out shows that it's not really a close approximation outside the range of -1 to 1, so I don't think I can use it when <code>phase_</code> is in the range -pi to pi:</p> <p><img src="https://i.stack.imgur.com/28scP.png" alt="Plot of Sine vs. approximation"></p> <p>Here, Sine is the blue line and the purple line is the approximation.</p> <p>Profiling my code reveals that the calls to <code>sin()</code> are far and away the most time-consuming calls, so I really would like to optimise this piece.</p> <p>Thanks</p> <p><strong>EDIT</strong> Thanks for the detailed and varied answers. I will explore these and accept one at the weekend.</p> <p><strong>EDIT 2</strong> Would the anonymous close voter please kindly explain their vote in the comments? Thank you.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload