# linear vs logarithmic

i’m not sure i understand what ur after, but this patch might help if u want to be able to control the curve (log factor):

j

#P window setfont "Sans Serif" 9.;

#P window linecount 1;

#P comment 95 173 51 196617 pow base;

#P comment 668 273 149 196617 MIDI pitch log curve;

#P button 465 151 15 0;

#P user multiSlider 499 268 147 101 0. 13000. 128 2681 15 0 0 2 0 0 0;

#M frgb 0 0 0;

#M brgb 255 255 255;

#M rgb2 127 127 127;

#M rgb3 0 0 0;

#M rgb4 37 52 91;

#M rgb5 74 105 182;

#M rgb6 112 158 18;

#M rgb7 149 211 110;

#M rgb8 187 9 201;

#M rgb9 224 62 37;

#M rgb10 7 114 128;

#P newex 499 213 67 196617 zl group 128;

#P newex 465 169 44 196617 uzi 128;

#P newex 499 192 31 196617 mtof;

#P comment 535 194 149 196617 scale 0-127 to frequency;

#P comment 256 107 149 196617 pow exponent (1= linear);

#P comment 250 274 149 196617 check out the curve;

#P newex 220 128 27 196617 t b i;

#P number 220 108 35 9 1 0 1 3 0 0 0 221 221 221 222 222 222 0 0 0;

#P button 47 152 15 0;

#P user multiSlider 81 269 147 101 0. 100. 128 2681 15 0 0 2 0 0 0;

#M frgb 0 0 0;

#M brgb 255 255 255;

#M rgb2 127 127 127;

#M rgb3 0 0 0;

#M rgb4 37 52 91;

#M rgb5 74 105 182;

#M rgb6 112 158 18;

#M rgb7 149 211 110;

#M rgb8 187 9 201;

#M rgb9 224 62 37;

#M rgb10 7 114 128;

#P newex 81 214 67 196617 zl group 128;

#P newex 47 170 44 196617 uzi 128;

#P newex 81 193 166 196617 expr pow(($i1/127.)\,$i2) *100.;

#P comment 252 193 149 196617 scale 0-127 to 0.-100.;

#P connect 12 2 11 0;

#P connect 11 0 13 0;

#P connect 13 0 14 0;

#P connect 15 0 12 0;

#P connect 7 1 1 1;

#P fasten 7 0 5 0 225 148 52 148;

#P connect 6 0 7 0;

#P connect 2 2 1 0;

#P connect 1 0 3 0;

#P connect 5 0 2 0;

#P connect 3 0 4 0;

#P window clipboard copycount 18;

Yes, this is helpful. So what you have done here is take (x/127) to the y power and multiply by 100.

So,

((x/(xn-1))^y

would be a useful equation to use, in general?

I’m also interested in applying this to cycle~, so that cycle~ could output waves logarithmically. How would one go about this?

nevermind

#P window setfont "Sans Serif" 9.;

#P window linecount 1;

#P newex 140 329 27 9109513 * 2.;

#P flonum 140 304 35 9 0 0 0 139 0 0 0 221 221 221 222 222 222 0 0 0;

#P flonum 176 304 35 9 0 0 0 139 0 0 0 221 221 221 222 222 222 0 0 0;

#P user scope~ 140 381 893 535 256 3 128 -1. 1. 0 0. 0 0. 102 255 51 135 135 135 0;

#P newex 140 357 46 9109513 pow~;

#P newex 176 328 36 9109513 cycle~;

#P connect 4 0 5 0;

#P connect 5 0 1 0;

#P connect 0 0 1 1;

#P connect 3 0 0 0;

#P connect 1 0 2 0;

#P window clipboard copycount 6;

Quote: laotze wrote on Thu, 31 July 2008 16:28

—————————————————-

> I’m also interested in applying this to cycle~, so that cycle~ could output waves logarithmically. How would one go about this?

—————————————————-

i’m sorry i dont understand what u mean by outputting waves logarithmically? what would be the purpose, or try to describe what ur trying to achieve?

j

Jay Bodley schrieb:

> I am wondering how Max computes on a logarithmic scale.

There are a lot of ways to do that, the most universal is expr, but for

musical purposes, there are mtof, ftom, atodb and dbtoa which transform

common human perception curves (loudness/amplitude and pitch/frequency)

Stefan

–

Stefan Tiedje————x——-

–_____———–|————–

–(_|_ —-|—–|—–()——-

– _|_)—-|—–()————–

———-()——–www.ccmix.com

Using [expr] you can use natural log and log base 10.

However you can use maths laws to translate these to work in any base you want. For example this is helpful if you want to play microtonal music using something other than 12 equal divisions of the octave (12 EDO or 12 TET).

Here are some two [expr]s which show how MIDI pitch number and frequency are related logarithmically. Basically they are equivalent to [mtof] and [ftom] but you can see (and hopefully understand) all the maths going on inside.

There is also a mathematical expression I have worked out that transforms 0-127 linear range into the [filtergraph~] logarithmic range of 20-20000. You might find this helpful for any filter GUIs as you will find that using a standard [num] or [float] box you move through low frequencies quickly and high frequencies really slowly!

I hope this helps in some way.

lh

max v2;

#N vpatcher 210 44 1030 503;

#P window setfont "Sans Serif" 9.;

#P number 450 322 35 9 0 0 0 3 0 0 0 221 221 221 222 222 222 0 0 0;

#P flonum 540 229 71 9 0 0 0 3 0 0 0 221 221 221 222 222 222 0 0 0;

#P number 411 322 35 9 0 0 0 3 0 0 0 221 221 221 222 222 222 0 0 0;

#P flonum 411 283 71 9 0 0 0 3 0 0 0 221 221 221 222 222 222 0 0 0;

#P window linecount 1;

#P newex 411 303 31 196617 ftom;

#P flonum 465 229 71 9 0 0 0 3 0 0 0 221 221 221 222 222 222 0 0 0;

#P newex 465 210 31 196617 mtof;

#P number 465 190 35 9 0 0 0 3 0 0 0 221 221 221 222 222 222 0 0 0;

#P flonum 221 125 71 9 0 0 0 3 0 0 0 221 221 221 222 222 222 0 0 0;

#P newex 221 106 343 196617 expr (pow(2.\,((($i1/127.*119.589417+15.48682)-69.)/12.))) * 440.;

#P number 221 89 35 9 0 0 0 3 0 0 0 221 221 221 222 222 222 0 0 0;

#P user hslider 221 69 18 128 128 1 0 0;

#P newex 450 303 265 196617 expr (((log10 (($f1 / 440.))) / (log10 (2))) * 12.) + 69;

#P newex 540 210 194 196617 expr (pow(2.\,(($f1-69.)/12.))) * 440.;

#P window setfont Helvetica 9.;

#P user filtergraph~ 46 150 256 128 21 9 20. 20000. 0.0625 16. 0 1 1 0 0 1 1;

#X frgb 170 170 170;

#X brgb 209 209 209;

#X rgb2 0 0 0;

#X rgb3 130 130 130;

#X rgb4 0 0 0;

#X rgb5 76 108 172;

#X rgb6 210 74 54;

#X rgb7 255 22 22;

#X linmarkers 20. 50. 100. 250. 500. 1000. 2500. 5000. 10000. 20000.;

#X logmarkers 20. 50. 100. 250. 500. 1000. 2500. 5000. 10000. 20000.;

#X nfilters 1;

#X setfilter 0 5 1 0 0 20. 1.048069 2.5 0. 0. 0. 0. 0. 0.;

#X done;

#P connect 3 0 4 0;

#P connect 4 0 5 0;

#P connect 5 0 6 0;

#P connect 6 0 0 5;

#P connect 11 0 10 0;

#P connect 10 0 12 0;

#P fasten 11 0 2 0 416 300 455 300;

#P connect 2 0 14 0;

#P connect 7 0 8 0;

#P connect 8 0 9 0;

#P fasten 7 0 1 0 470 207 545 207;

#P connect 1 0 13 0;

#P pop;

thanks for your examples! for the sake of deeper understanding – would

you mind telling us what the two values 119.589417 and 15.48682 stand

for in your equation?

p

> There is also a mathematical expression I have worked out that transforms 0-127 linear range into the [filtergraph~] logarithmic range of 20-20000. You might find this helpful for any filter GUIs as you will find that using a standard [num] or [float] box you move through low frequencies quickly and high frequencies really slowly!

>

> I hope this helps in some way.

>

> lh

>

>

>

Of course! Sorry for not explaining it in the original post.

Those numbers are simply there to scale the output of the [mtof] equation to fit the boundaries 20-20000Hz. If you run the MIDI range 0-127 straight into [mtof] then the range is something like 8.2-12553Hz which isn’t ideal. You could simply use a [scale] object mapping the values you get straight to the audible audio frequency range. Perhaps try:

[scale 8.1758 12543.85 20. 20000.].

However I decided to do it using maths. To get these numbers I sent 20 and 20000 to my [ftom] equation. In this case using the [ftom] object doesn’t work because it only returns integers. The results were:

20Hz = 15.48682

20000Hz = 135.076233

difference = 119.589417

Now to use these in the [mtof] equation we need to take the original input ($i1) and replace it with a new equation. First we scale the 0-127 range to 0-1. Then we multiply this by the difference between our desired range. Finally we add on the minimum value.

$i1/127.*119.589417+15.48682

Substituting this equation in to the original [mtof] gives us the final result. I hope this has helped you understand, I have tried to explain it as clearly as possible. If you’d like to see the equations for [atodb] and [dbtoa] then let me know. They were much simpler to work out.

lh

=)))

ok, i thought its some magic numbers like pi or 1/sqrt(2) or something.

so you simply stretched mtof to 20-20000?

thanks for the demystification!

p

Luke wrote:

> Of course! Sorry for not explaining it in the original post.

>

> Those numbers are simply there to scale the output of the [mtof] equation to fit the boundaries 20-20000Hz. If you run the MIDI range 0-127 straight into [mtof] then the range is something like 8.2-12553Hz which isn’t ideal. You could simply use a [scale] object mapping the values you get straight to the audible audio frequency range. Perhaps try:

>

> [scale 8.1758 12543.85 20. 20000.].

>

> However I decided to do it using maths. To get these numbers I sent 20 and 20000 to my [ftom] equation. In this case using the [ftom] object doesn’t work because it only returns integers. The results were:

>

> 20Hz = 15.48682

> 20000Hz = 135.076233

> difference = 119.589417

>

> Now to use these in the [mtof] equation we need to take the original input ($i1) and replace it with a new equation. First we scale the 0-127 range to 0-1. Then we multiply this by the difference between our desired range. Finally we add on the minimum value.

>

> $i1/127.*119.589417+15.48682

>

> Substituting this equation in to the original [mtof] gives us the final result. I hope this has helped you understand, I have tried to explain it as clearly as possible. If you’d like to see the equations for [atodb] and [dbtoa] then let me know. They were much simpler to work out.

>

> lh

>

>

>

Luke schrieb:

> In this case using the [ftom] object doesn’t work because it only

> returns integers. The results were:

give ftom a float argument and it will spit out floats…

Stefan

–

Stefan Tiedje————x——-

–_____———–|————–

–(_|_ —-|—–|—–()——-

– _|_)—-|—–()————–

———-()——–www.ccmix.com

Luke (thereishopeforus, for some reason I think your name is luke, am I correct?)

Any chance you could explain your ftom equation? I need to make it work to output numbers from 1-256.

I need to have a logathmic slider, like the one in your example, but the user needs to be able to type in frequency values in the numeric box and have it update the slider position correctly, by using a [float] object and some set $1 messages.

It would be greatly appreciated!

Sure.

The log() bit switches from a linear to logarithmic scale, to be specific using the twelfth root of two which is the equation used to work out frequencies in 12 tone equal temperament (equal divisions of the octave).

The $f1/440 and +69 bits on there are to work this out relative to concert A = 440Hz. This would give you midi note values.

The -15/119*127 bit then scales the midi note value output between 0 and 127 for the pitches 20-20000Hz. I’ve explained how I got these numbers in a previous post on this topic. Assuming you want to still use this frequency range then all you need to do to get the output value 0-255 is replace the final "127" with "255".

lh (the l is for luke)

max v2;

#N vpatcher 10 59 610 459;

#P button 95 123 15 0;

#P window setfont "Sans Serif" 9.;

#P window linecount 1;

#P message 95 207 39 196617 set $1;

#P newex 95 188 27 196617 f;

#P flonum 112 123 71 9 20. 20000. 3 3 0 0 0 221 221 221 222 222 222 0 0 0;

#P user hslider 95 224 18 128 256 1 0 0;

#P flonum 112 171 71 9 0 0 0 3 0 0 0 221 221 221 222 222 222 0 0 0;

#P window linecount 2;

#P newex 112 140 205 196617 expr (((((log10(($f1/440.)))/(log10(2)))*12.)+69)-15.48682)/119.589417*255.;

#P connect 6 0 4 0;

#P connect 4 0 5 0;

#P connect 5 0 2 0;

#P connect 3 0 0 0;

#P connect 0 0 1 0;

#P connect 1 0 4 1;

#P pop;