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

```Thanks Robert, much appreciated for adding this to my code :)

With double precision I've noticed that even when undergoing drastic
frequency modulation it all works pretty accurately still, so any
correction term could probably be applied with a very long time constant
and linear interpolation to reduce the cpu load something along the lines
of (have not compiled or tested so could contain errors):```
```
pre loop:
new_gain = 1 + adaptationspeed*(1 - c*c - s*s)
gain_inc = (new_gain - gain)/blocklength

loop blocklength:
t0 = g0*c - g1*s
t1 = g1*c + g0*s
c = gain*t0
s = gain*t1
gain += gain_inc

On Thu, 21 Feb 2019 at 16:06, robert bristow-johnson <
r...@audioimagination.com> wrote:

>
>
> ---------------------------- Original Message ----------------------------
> Subject: Re: [music-dsp] Time-variant 2nd-order sinusoidal resonator
> From: "Andrew Simper" <a...@cytomic.com>
> Date: Wed, February 20, 2019 9:20 pm
> To: "Robert Bristow-Johnson" <r...@audioimagination.com>
> "A discussion list for music-related DSP" <music-dsp@music.columbia.edu>
> --------------------------------------------------------------------------
>
> > This looks pretty good to me, and I like the amplitude adjustment g[n]
> term
> > :)
>
> well, then let's integrate that into your code.
>
>
> >
> > Depending on the situation you may want to modulate the frequency of the
> > oscillator pretty fast, so it can help to use a tan approximation
> function
> > and then a division and a few other operations to get your cos (w) and
> sin
> > (w) rotation terms from that single approximation. I've called the
> rotation
> > terms g0 and g1, and c and s are the output cos and sin quadrature
> > oscillator values
> >
> > init:
> > c = cos(2*pi*startphase)
> > s = sin(2*pi*startphase)
>    gain = 1
>    adaptationspeed = 0.5     // this could be made smaller, but must be
> positive
> >
> > set frequency:
> > g0 = cos(2*pi*frequency/samplerate)
> > g1 = sin(2*pi*frequency/samplerate)
> >
> > or
> >
> > g = tan(pi*frequency/samplerate);
> > gg = 2/(1 + g*g)
> > g0 = gg-1
> > g1 = g*gg
> >
> > tick:
> > t0 = g0*c - g1*s
> > t1 = g1*c + g0*s
>   c = gain*t0
>   s = gain*t1
>   gain = 1 + adaptationspeed*(1 - c*c - s*s)
>
>
>
> --
>
> 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
```
```_______________________________________________
dupswapdrop: music-dsp mailing list
music-dsp@music.columbia.edu
https://lists.columbia.edu/mailman/listinfo/music-dsp```