[PD] convert range (0 to 1) to (-4 to 4)

Frank Barknecht fbar at footils.org
Tue Nov 20 14:03:34 CET 2007


Hallo,
bernardo amorim hat gesagt: // bernardo amorim wrote:

> i want to build a simple patch that converts the range 0 to 1 to -4 to 4
> where 0.5 equals 0 in the second range. can anyone help?

Now that you've already got some solutions for this case, it's maybe
time to go for the more general case as well? 

What you want to do is called scaling or mapping or interpolating one
range to another range. First you need to adapt the lengths of your
ranges: 0-1 has length 1, while -4 to 4 has length  4-(-4) = 4 + 4 =
8. So you first multiply by 8 to scale the length. 

This will give you an intermediate mapping from 0-1 to 0-8. This then
needs to be shifted to the starting point, -4 in the example, by
adding the starting point's value: + (-4) or -4. 

Now replace -4 by a variable, lets say, x1, and 4 by y1, to calculate
the general formula for interpolation (and extrapolation, too):

 scale_to_x1_y1(x) = x * (y1 - x1) + x1 

You can put this into an expr-fomula: [expr $f1 * ($f3-$f2) - $f2] 
where x -> $f1, x1 -> $f2 and y1 -> $f3.

This also will work with "inverse" mappings, where the second number
is smaller than the first, like mapping 0,1 to 3,-2 (needed when
scaling gemmouse coordinates to gemwin coordinates): 

 scale_to_3_minus2(x) = x * (-2 - 3) + 3 
                      = -5*x + 3 

Note that the "length" of the ouput range is negative here: -5 This
will make your scale be turned upside down as needed (in
gemmouse->gemwin you'd scale the x-axis this way, it has 4 on top and
-4 on bottom, while gemmouse has 0 on top, and 1 on bottom, so the
mapping would be 0,1 -> 4,-4)

Lets test the borders: 

left:   scale_to_3_minus2(0) = 3 (good)
right:  scale_to_3_minus2(1) = -5 + 3 = -2 (good, too).

Scaling from arbitrary input ranges (x0,y0) is left as an excercise.
Hint: First scale to (0,1).

Ciao
-- 
 Frank Barknecht                                     _ ______footils.org__




More information about the Pd-list mailing list