[sdiy] formula for exponential decay

Magnus Danielson cfmd at swipnet.se
Thu Feb 15 21:40:40 CET 2001


From: Kenneth Martinez <kmartinez at bency.com>
Subject: [sdiy] formula for exponential decay
Date: Thu, 15 Feb 2001 10:54:47 -0700

Hi!

> Just wanted to start out by thanking everyone on this list for being a
> great source of information, and for inspiring me to begin building my
> own analog synth.  I tried lots of virtual analogs and software synths,
> but to me they just don't quite capture the sound of real analog
> hardware.  Of course, being a software guy, I just *had* to go for a
> computer-controlled analog ;-)  After several months, there's finally
> some visible and audible progress...

I guess we have to take that as a good sign after all... ;)

> Currently I'm making software envelopes to control my
> still-on-the-breadboard hardware (I may add hardware envelopes later,
> but I'm starting with software ones - reduces the initial hardware
> complexity too).  I started simple by using linear attack and decay.  To
> my ears, linear sounds fine for attack, but for decay I'd like to
> include the traditional and more pleasant-sounding exponential decay.
> Checked several circuits and saw that a discharging capacitor generates
> this; checked a textbook which says the formula for an RC discharge is:
> 
>                                  -t / RC
>     V  =  V          *   e
>                initial
> 
> My formula for linear decay is just:
> 
>    V  =  V          -  ( t  *  decay slope)
>                initial

Should really be

                     t
V = V        (1 - ------)
     initial      t
                   slope

where 0 <= t <= tslope, those, tslope is the length of the slope.

> Now, the program is designed to calculate the next envelope level by
> taking the current level and subtracting a constant which was computed
> from the user-selected decay setting and the fixed refresh time period
> (since the above formula produces a straight line graph, and the first
> derivative of that is a constant).  If the decay setting changes, the
> periodic level change constant is recomputed and the envelope continues
> from its current point with the new decay rate.  This method easily
> allows retrigger of the attack phase during decay, or the attack phase
> to be cut short and jump into the decay phase.

Right.

> It is at the point where I attempt to do the same thing for exponential
> decay that my atrophied math skills fail me...so...first, I'd like
> confirmation that the RC formula above does in fact describe the decay
> portion of  hardware-generated exponential envelopes; or if not, what is
> the correct formula?

The formula is correct.

> Then, if someone would be so kind as to show a
> formula that could be applied to calculate the next envelope level given
> the current level & time & RC constant (decay rate), that would be most
> appreciated.  A reasonable approximation would be fine (particularly one
> that's not terribly computation-intensive); I'm using floating-point
> math now, but of course an integer-based solution could also be used.

OK. This is the deal:

First off, you don't want to calculate with the actual time t as a
basis, but rather suffice with a method to acheive the same thing
incrementally per sample, that is, every ts.

For exponential equations this is fortunately easy. You just multiply
instead of add!

For the linear decay you would do

Prepare:
C = - ts/tslope
X(0) = 1

Repeat:
X(n+1) = X(n) + C

For the exponential decay you would do

Prepare:
C = exp(-ts/tslope)
X(0) = 1

Repeat:
X(n+1) = X(n) * C

> Once I get this formula, I can also apply it for exponential portamento,
> etc.  It's not holding up progress, but needs to be done at some point.

Have fun, and most importantly, let us know how it goes!

Cheers,
Magnus




More information about the Synth-diy mailing list