back to list

Csound expseg function

🔗monz <monz@tonalsoft.com>

12/30/2005 2:00:12 PM

Can anyone explain to me exactly what exponential
function Csound uses for the expseg opcode?

That is, if i want to graph the function, say on
an Excel spreadsheet, how do it get the y-value
for each x-value?

I've looked around in everything i have on Csound
and don't see this explained in detail anywhere.

-monz
http://tonalsoft.com
Tonescape microtonal music software

🔗monz <monz@tonalsoft.com>

12/30/2005 2:04:51 PM

--- In tuning@yahoogroups.com, "monz" <monz@t...> wrote:
>
>
> Can anyone explain to me exactly what exponential
> function Csound uses for the expseg opcode?
>
> That is, if i want to graph the function, say on
> an Excel spreadsheet, how do it get the y-value
> for each x-value?

Of course, i meant to specify that the x-value is time
and the y-value is amplitude.

-monz
http://tonalsoft.com
Tonescape microtonal music software

🔗monz <monz@tonalsoft.com>

12/30/2005 5:57:02 PM

--- In tuning@yahoogroups.com, "monz" <monz@t...> wrote:
>
> --- In tuning@yahoogroups.com, "monz" <monz@t...> wrote:
> >
> >
> > Can anyone explain to me exactly what exponential
> > function Csound uses for the expseg opcode?
> >
> > That is, if i want to graph the function, say on
> > an Excel spreadsheet, how do it get the y-value
> > for each x-value?
>
>
> Of course, i meant to specify that the x-value is time
> and the y-value is amplitude.

OK, i guess i can make this a little easier by supplying
some data.

The scale of the values is arbitrary ... i set up a
Csound expseg envelope which makes the amplitude decay
exponentially from a maximum of 1.94 at 0.1 second
to a minimum close to zero at 4.9 seconds.

After rendering the Csound file into a .wav file,
i did an analysis of it by taking:

* 0.1-second snapshots up from 0.1-0.2 to 1.2-1.3 seconds,

* a 0.2-second snapshot from 1.3-1.5 seconds,

* 0.5-second snapshots for 1.5-2.0, 2.0-2.5, and 2.5-3.0 seconds,

* 1-second snapshots for 3.0-4.0 and 4.0-5.0 seconds.

The only reason i didn't keep the snapshot size consistent
was because it's a lot of work doing 0.1-second snapshots,
and (because of the exponential function) the decay levels
off quite a lot by the time the signal reaches ~2 seconds.

So here's my data:

time .. value
0.1 .. 1.940
0.2 .. 1.757
0.3 .. 1.589
0.4 .. 1.432
0.5 .. 1.291
0.6 .. 1.164
0.7 .. 1.054
0.8 .. 0.950
0.9 .. 0.856
1.0 .. 0.772
1.1 .. 0.699
1.2 .. 0.630

1.3 .. 0.545

1.5 .. 0.389
2.0 .. 0.233
2.5 .. 0.139

3.0 .. 0.068
4.0 .. 0.024

What is the exponential function which produces that
kind of curve?

-monz
http://tonalsoft.com
Tonescape microtonal music software

🔗Graham Breed <gbreed@gmail.com>

12/31/2005 6:08:59 AM

On 12/31/05, monz <monz@tonalsoft.com> wrote:

> The scale of the values is arbitrary ... i set up a
> Csound expseg envelope which makes the amplitude decay
> exponentially from a maximum of 1.94 at 0.1 second
> to a minimum close to zero at 4.9 seconds.

I'll need to know how close to zero to answer this. An exponential
decay should be

y = exp(-xt)

where t is time and x is the decay constant. In this case,

y = exp(-x(t-0.1))

because you start at 0.1. x can be guessed as

log(1.94/0.024)/3.9=1.126253697874212...

with natural logarithms, but would ideally be

log(1.94/something)/4.8

where "something" is the "close to zero" you set 4.9 seconds to

> The only reason i didn't keep the snapshot size consistent
> was because it's a lot of work doing 0.1-second snapshots,
> and (because of the exponential function) the decay levels
> off quite a lot by the time the signal reaches ~2 seconds.

This isn't a plain exponential, although it's close to start with. I
can't work out what's happening from the source. It seems to guess
the decay per time slice and do repeated multiplication. That should
still be more consistent to an exponential decay than what you've
given. Perhaps it tries to round of the segment boundaries. If you
really want to know what's happening you'll have to bring it up on the
Csound list.

Graham

🔗monz <monz@tonalsoft.com>

12/31/2005 7:03:26 AM

Hi Graham,

--- In tuning@yahoogroups.com, Graham Breed <gbreed@g...> wrote:
>
> On 12/31/05, monz <monz@t...> wrote:
>
> > The scale of the values is arbitrary ... i set up a
> > Csound expseg envelope which makes the amplitude decay
> > exponentially from a maximum of 1.94 at 0.1 second
> > to a minimum close to zero at 4.9 seconds.
>
> I'll need to know how close to zero to answer this.
> An exponential decay should be
>
> y = exp(-xt)
>
> where t is time and x is the decay constant. In this case,
>
> y = exp(-x(t-0.1))
>
> because you start at 0.1. x can be guessed as

0.1 is the time of the peak value ... everything before
that is "attack", so it's less than the peak.

> log(1.94/0.024)/3.9=1.126253697874212...
>
> with natural logarithms, but would ideally be
>
> log(1.94/something)/4.8
>
> where "something" is the "close to zero" you set 4.9 seconds to

Ah, thanks!

My "something" was 0.00003, but i'll probably switch
to 0.00001 if it works OK.

Where does the 1.94 come from? Is it some function of e?

> > The only reason i didn't keep the snapshot size consistent
> > was because it's a lot of work doing 0.1-second snapshots,
> > and (because of the exponential function) the decay levels
> > off quite a lot by the time the signal reaches ~2 seconds.
>
> This isn't a plain exponential, although it's close to
> start with. I can't work out what's happening from the
> source. It seems to guess the decay per time slice and
> do repeated multiplication. That should still be more
> consistent to an exponential decay than what you've
> given. Perhaps it tries to round of the segment boundaries.

My data is quantized to 0.1 seconds or even more ...
i'm sure that on a per-sample basis you'd get a much
better picture of what's happening.

> If you really want to know what's happening you'll have
> to bring it up on the Csound list.

I know ... i've been trying to avoid joining yet another list.

-monz
http://tonalsoft.com
Tonescape microtonal music software

🔗Graham Breed <gbreed@gmail.com>

12/31/2005 11:13:02 AM

> My "something" was 0.00003, but i'll probably switch
> to 0.00001 if it works OK.
>
> Where does the 1.94 come from? Is it some function of e?

It's your peak.

> My data is quantized to 0.1 seconds or even more ...
> i'm sure that on a per-sample basis you'd get a much
> better picture of what's happening.

If it's a true exponential decay, it should be possible to tell what's
happening from 3 data points. And the whole thing should look like
this:

0.1 1.940000
0.2 1.540207
0.3 1.222804
0.4 0.970810
0.5 0.770747
0.6 0.611912
0.7 0.485810
0.8 0.385695
0.9 0.306212
1.0 0.243108
1.1 0.193009
1.2 0.153234
1.3 0.121655
1.4 0.096585
1.5 0.076681
1.6 0.060879
1.7 0.048333
1.8 0.038372
1.9 0.030465
2.0 0.024187
2.1 0.019202
2.2 0.015245
2.3 0.012103
2.4 0.009609
2.5 0.007629
2.6 0.006057
2.7 0.004809
2.8 0.003818
2.9 0.003031
3.0 0.002406
3.1 0.001910
3.2 0.001517
3.3 0.001204
3.4 0.000956
3.5 0.000759
3.6 0.000603
3.7 0.000478
3.8 0.000380
3.9 0.000302
4.0 0.000239
4.1 0.000190
4.2 0.000151
4.3 0.000120
4.4 0.000095
4.5 0.000076
4.6 0.000060
4.7 0.000048
4.8 0.000038
4.9 0.000030

Graham

🔗monz <monz@tonalsoft.com>

1/1/2006 11:31:42 AM

--- In tuning@yahoogroups.com, Graham Breed <gbreed@g...> wrote:
>
> > My "something" was 0.00003, but i'll probably switch
> > to 0.00001 if it works OK.
> >
> > Where does the 1.94 come from? Is it some function of e?
>
> It's your peak.

Oops! ... OK.

> > My data is quantized to 0.1 seconds or even more ...
> > i'm sure that on a per-sample basis you'd get a much
> > better picture of what's happening.
>
> If it's a true exponential decay, it should be possible
> to tell what's happening from 3 data points. And the
> whole thing should look like this:
>
> 0.1 1.940000
> 0.2 1.540207
> 0.3 1.222804
> <etc. - rest of data snipped>

OK, thanks.

So how does your formula:

y = exp(-x(t-0.1))

translate into something that would work in an Excel
spreadsheet?

IOW, to me "exp" is a black-box, and i want to know
how it works.

-monz
http://tonalsoft.com
Tonescape microtonal music software

🔗Yahya Abdal-Aziz <yahya@melbpc.org.au>

1/1/2006 7:52:51 PM

Hi monz,

On Sat, 31 Dec 2005, you wrote:
> > > Can anyone explain to me exactly what exponential
> > > function Csound uses for the expseg opcode?
> > >
> > > That is, if i want to graph the function, say on
> > > an Excel spreadsheet, how do it get the y-value
> > > for each x-value?
> >
> > Of course, i meant to specify that the x-value is time
> > and the y-value is amplitude.
>
> OK, i guess i can make this a little easier by supplying
> some data.

You bet! :-)

> The scale of the values is arbitrary ... i set up a
> Csound expseg envelope which makes the amplitude decay
> exponentially from a maximum of 1.94 at 0.1 second
> to a minimum close to zero at 4.9 seconds.

> After rendering the Csound file into a .wav file,
> i did an analysis of it by taking:
> * 0.1-second snapshots up from 0.1-0.2 to 1.2-1.3 seconds,
> * a 0.2-second snapshot from 1.3-1.5 seconds,
> * 0.5-second snapshots for 1.5-2.0, 2.0-2.5, and 2.5-3.0 seconds,
> * 1-second snapshots for 3.0-4.0 and 4.0-5.0 seconds.

> The only reason i didn't keep the snapshot size consistent
> was because it's a lot of work doing 0.1-second snapshots,
> and (because of the exponential function) the decay levels
> off quite a lot by the time the signal reaches ~2 seconds.

> So here's my data:

> time .. value
> 0.1 .. 1.940
> 0.2 .. 1.757
> 0.3 .. 1.589
> 0.4 .. 1.432
> 0.5 .. 1.291
> 0.6 .. 1.164
> 0.7 .. 1.054
> 0.8 .. 0.950
> 0.9 .. 0.856
> 1.0 .. 0.772
> 1.1 .. 0.699
> 1.2 .. 0.630
> 1.3 .. 0.545
> 1.5 .. 0.389
> 2.0 .. 0.233
> 2.5 .. 0.139
> 3.0 .. 0.068
> 4.0 .. 0.024
>
> What is the exponential function which produces that
> kind of curve?

Any exponential time decay function will have the general
form:

y = a exp(-bt)

Here a and b should be positive constants.

With data like yours, the usual approach if one suspects
that it follows an exponential function is to simply take
logs of the y values, then plot them against the t values.
The points should fall approximately in a straight line
with slope -b, if your hypothesis is true - see (*) below.
The graph also gives a visual indication of how far from
accurate the exp relationship is, by the amount of
scatter of the data points about the line. You can then
define a multiplicative error function by:

err(t) = y/a exp(-bt)

then graph it (or its log) to see what form it takes.

That's the experimental approach for you!

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

(*) from y = a exp(-bt), taking logs we get:
log y = log a - bt, which is the equation of a
straight line with slope -b and with y=a when
t=0.

-------------------------------------------------------------
MATHOPHOBES! Close your eyes and skip to
the CONCLUSION below (Did you open your
eyes in time?)

Here's the experiment:

time .. y ........ log y
0.1 .. 1.940 .. 0.663
0.2 .. 1.757 .. 0.564
0.3 .. 1.589 .. 0.463
0.4 .. 1.432 .. 0.359
0.5 .. 1.291 .. 0.255
0.6 .. 1.164 .. 0.152
0.7 .. 1.054 .. 0.053
0.8 .. 0.950 .. -0.051
0.9 .. 0.856 .. -0.155
1.0 .. 0.772 .. -0.259
1.1 .. 0.699 .. -0.358
1.2 .. 0.630 .. -0.462
1.3 .. 0.545 .. -0.607
1.5 .. 0.389 .. -0.944
2.0 .. 0.233 .. -1.457
2.5 .. 0.139 .. -1.973
3.0 .. 0.068 .. -2.688
4.0 .. 0.024 .. -3.730

I get for your equation:
y_est = 2.083 * exp(-0.873 * t)

What is the error in this formula?
Write y = y_est * err, or more formally
y(t) = y_est(t) * err(t),
Then err(t) = y(t) / y_est(t)
Tabulate and graph err(t) against t:

t .. err(t)
0.1 .. 1.016
0.2 .. 1.004
0.3 .. 0.991
0.4 .. 0.975
0.5 .. 0.959
0.6 .. 0.944
0.7 .. 0.932
0.8 .. 0.917
0.9 .. 0.902
1.0 .. 0.887
1.1 .. 0.877
1.2 .. 0.862
1.3 .. 0.814
1.5 .. 0.692
2.0 .. 0.642
2.5 .. 0.592
3.0 .. 0.448
4.0 .. 0.379

As you can see, there is a systematic error
in the data, which follows the approximate
straight trend line:
err(t) = 1.0472 - 0.1017 * t
(with R^2 = 0.9709).

Repeating the above process to uncover the
residual error, it is approximately cubic, with
trend equation:
err2(t) = 0.0176t^3 - 0.1036t^2 + 0.0362t + 0.975
(with R^2 = 0.9551).

To this level of approximation, your data can
be fitted by:

y_est = 2.083 * exp(-0.873 * t)
* (1.0472 - 0.1017 * t)
* (0.0176t^3 - 0.1036t^2 + 0.0362t + 0.975)

-------------------------------------------------------------
CONCLUSION:
It's a long way from being a straight exponential
function!

My guess is that your long samples caused significant
error in the values you got for t=2.0 and greater.
Can you try using shorter (0.1s) samples for a couple
of those values, and see whether they work better?
Even shorter samples would be preferable, say 0.05s
or even 0.02s samples at about 5 points spread across
the total range (0 to 5 seconds).

Regards,
Yahya

--
No virus found in this outgoing message.
Checked by AVG Free Edition.
Version: 7.1.371 / Virus Database: 267.14.9/217 - Release Date: 30/12/05

🔗monz <monz@tonalsoft.com>

1/2/2006 12:33:26 AM

Hi Yayha,

--- In tuning@yahoogroups.com, "Yahya Abdal-Aziz" <yahya@m...> wrote:
>
>
> Hi monz,
>
> On Sat, 31 Dec 2005, you wrote:
> > > > Can anyone explain to me exactly what exponential
> > > > function Csound uses for the expseg opcode?
>
> <big snip>
>
> -------------------------------------------------------------
> CONCLUSION:
> It's a long way from being a straight exponential
> function!
>
> My guess is that your long samples caused significant
> error in the values you got for t=2.0 and greater.
> Can you try using shorter (0.1s) samples for a couple
> of those values, and see whether they work better?
> Even shorter samples would be preferable, say 0.05s
> or even 0.02s samples at about 5 points spread across
> the total range (0 to 5 seconds).

Thanks for all the explanation of this.

The problem with doing shorter samples is that the
shorter the sample becomes, the lower the resolution.
There simply isn't enough time for the FFT do calculate
the spectrum accurately.

Even at the 0.1-second sample length, my software can
only quantize the frequency spectrum into 4096 divisions,
which is far from accurate enough to get a good plot
of certain partials.

-monz
http://tonalsoft.com
Tonescape microtonal music software