# Re: [music-dsp] Time-variant 2nd-order sinusoidal resonator

```
�
this is just a guess at what the C code might look like to calculate one
sample.� this uses Olli's 7th-order with continuous derivative.� f0 is the
oscillator frequency which can vary but it must be non-negative.� 1/T is the
sample rate.
�
float
sine_osc(float f0, float T)
{

� �static float phase = 1.0;� � // this is the only state.� -2.0 <= phase < +2.0

� �phase += 4.0*f0*T;

� �if (phase >= 2.0)

� �{

� � � phase -= 4.0;

� �}

� �if (phase < 0.0)

� �{

� � � triangle = -phase - 1.0;

� �}

� �else

� �{

� � � triangle = phase - 1.0;

� �}

� �x2 = triangle*triangle;

� �return triangle*(1.570781972 - x2*(0.6458482979 - x2*(0.07935067784 -
x2*0.004284352588)));

}
i haven't run this code nor checked it for syntax.� but it's conceptually so
simple that i'll bet it works.

r b-j

---------------------------- Original Message ----------------------------

Subject: Re: [music-dsp] Time-variant 2nd-order sinusoidal resonator

From: "Olli Niemitalo" <o...@iki.fi>

Date: Thu, February 21, 2019 11:58 pm

To: "A discussion list for music-related DSP" <music-dsp@music.columbia.edu>

--------------------------------------------------------------------------

> On Fri, Feb 22, 2019 at 9:08 AM robert bristow-johnson <

> r...@audioimagination.com> wrote:

>

>> i just got in touch with Olli, and this "triangle wave to sine wave"

>> shaper polynomial is discussed at this Stack Exchange:

>>

>>

>>

>> https://dsp.stackexchange.com/questions/46629/finding-polynomial-approximations-of-a-sine-wave/46761#46761

>>

> I'll just summarize the results here. The polynomials f(x) approximate

> sin(pi/2*x) for x=-1..1 and are solutions with minimum peak harmonic

> distortion compared to the fundamental frequency. Both solutions with

> continuous and discontinuous derivative are given. In summary:

>

> Shared polynomial approximation properties and constraints:

> x = -1..1, f(-1) = -1, f(0) = 0, f(1) = 1, and f(-x) = -f(x).

>

> If continuous derivative:

> f'(-1) = 0 and f'(1) = 0 for the anti-periodic extension f(x + 2) = -f(x).

>

> 5th order, continuous derivative, -78.99 dB peak harmonic distortion:

> f(x) = 1.569778813*x - 0.6395576276*x^3 + 0.06977881382*x^5

>

> 5th order, discontinuous derivative, -91.52 dB peak harmonic distortion:

> f(x) = 1.570034357*x - 0.6425216143*x^3 + 0.07248725712*x^5

>

> 7th order, continuous derivative, -123.8368 dB peak harmonic distortion:

> f(x) = 1.570781972*x - 0.6458482979*x^3 + 0.07935067784*x^5

> - 0.004284352588*x^7

>

> 7th order, discontinuous derivative, -133.627 dB peak harmonic distortion:

> f(x) = 1.5707953785726114835*x -

> 0.64590724797262922190*x^3 + 0.079473610232926783079*x^5

> - 0.0043617408329090447344*x^7

>

> Also the exact coefficients that are rational functions of pi are given in

> the answer, in case anyone needs more precision.

>

> -olli

> _______________________________________________

> dupswapdrop: music-dsp mailing list

> music-dsp@music.columbia.edu

> https://lists.columbia.edu/mailman/listinfo/music-dsp
�
�
�

--

r b-j� � � � � � � � � � � � �r...@audioimagination.com

"Imagination is more important than knowledge."

�
�
�
�
```
```_______________________________________________
dupswapdrop: music-dsp mailing list
music-dsp@music.columbia.edu
https://lists.columbia.edu/mailman/listinfo/music-dsp```