log2

nickpic's icon

Hi,
I just can't figure out how I can evaluate log base 2.
With [expr log($f1)], the log seems natural. log2($f1) is refused.

Thanks for your help!

NP

Roman Thilenius's icon

expr log($f1,2)

you give the base as second value, the values are separated by a
comma, and commas must be "escaped" by a backslash.

Luke Hall's icon

In the [expr] object it seems that log() and ln() bother calculate the natural logarithm. When I try putting two arguments into log() I get an error saying it only understands one argument.

What you want to do is use the base changing rule of logarithms which states loga(x) = (logb(x)/logb(a)) where the emphasised letters represent the base of the logarithm. So if you want to be in base two use something like:

[expr log10($f1)/log10(2)]

lh

jvkr's icon

Hey, in my version of expr that doesn't work. That's why I always do it like this: expr log10($f1)/log10(2).

Roman Thilenius's icon

let see first if my version of expr really can do what i suggested. :P

kjg's icon

expr log($f1)/log(2)

or

expr log10($f1)/log10(2)

or

expr ln($f1)/ln(2)

nickpic's icon

Thank you for that precious help!

loga(x) = logb(x)/logb(a)

This is fascinating...

NP

xidance's icon

thx to luke hall also from me.

metamax's icon

Is it ever too late for thank you? I think not. Thanks for the math lesson Luke.

Laurent Pigeolet's icon

Hi,

In the same order, how could I write "2 e (x/64)" with "expr" object?
"2exp($f1/64)" is refused...

Thanks,

Laurent.

Jean-Francois Charles's icon

Something like this? Check out [expr] Help File, functions tab.

Max Patch
Copy patch and select New From Clipboard in Max.

Jean-Francois Charles's icon

Or maybe that's the one:

Max Patch
Copy patch and select New From Clipboard in Max.

Laurent Pigeolet's icon

Bonjour Jean-François,

Je dois simplement transformer une échelle linéaire de 0 à 128 (=x) (ou 127) en une échelle exponentielle de 0,5 à 2 (= y). La valeur 64 de la première échelle doit correspondre à la valeur 1 dans la seconde (voir schéma en pièce jointe). Puisque chaque fois qu"on ajoute 64 dans l' échelle de x cela double les valeurs dans l'échelle de y, il me semble que l'équation de base est la suivante: y = 2 exposant ( x / 64 ) 
Mais les valeurs obtenues par 2*exp($f1/64.) ne sont pas les chiffres escomptés.
2 exp 1= 2, non? Mais avec cette formule où $1 = 64., j'obtiens 5,437. Comment écrire simplement "2 exposant x?
Puis je dois faire correspondre les deux échelles: 0 de x à 0,5 de y, 64 de x à 1 de y, et 128 de x à 2 de y...
Je ne m'y retrouve pas...
Merci de votre aide.

Laurent.


Roald Baudoux's icon

Don't know who is Jean-François, but what you need is expr pow(2, $f1/64.)

Salutations.

Laurent Pigeolet's icon

Of course, Laurent! pow = power. So the good solution is finally "pow(2, $f1/64.)/2" and it makes the good draw like on the graphic. Thanks a lot my friend Roald! What would I do without you in my life? :D

Jean-Francois Charles's icon

En effet, exp() est la fonction e à la puissance... (e = 2.71...)
If you wanted 127 to correspond to 2., you could use something like this:

Max Patch
Copy patch and select New From Clipboard in Max.

Laurent Pigeolet's icon

Merci! Je ne connaissais pas l'objet "split".

Greg Debicki's icon

im not sure if this is directly related to the question but, i was hoping to use this ln($f1)/ln(2) trick to scale a frequency control, but im unclear on what my input is supposed to be to get 20, to 20000 (scaled correctly) as the output? also, is there a standard for doing this? ive been poking around but it kinda seems like people just do whatever to make the scaling sort of curve in the right way, is there a "correct" way to do this?

Roman Thilenius's icon


"but im unclear on what my input is supposed to be to get 20, to 20000 (scaled correctly) as the output?"

by doing the reverse function to find out which note value corresponds to 20 and 20k Hz - and then use the "normal" function which already exists (on the form of mtof and at least one other method) on this range.

mtof uses pow().

around here note/cent without range limits is the standard way to represent "linear", where in flowstone they use 0.-1. clipped - i dont think there is a right and wrong for this kind of thing, but imho, in our context midi note numbers make the most sense.


Roald Baudoux's icon

I usually send two values in Hz to a expr log($f1)/log(2.) then I got the extremes of the range I want to control. Then after the control I have a expr pow(2., $f1).

Max Patch
Copy patch and select New From Clipboard in Max.