"normalizing" a value
Denis McMahon
denismfmcmahon at gmail.com
Wed Jul 1 21:06:23 EDT 2015
On Wed, 01 Jul 2015 17:12:36 -0700, bvdp wrote:
> Not sure what this is called (and I'm sure it's not normalize). Perhaps
> "scaling"?
>
> Anyway, I need to convert various values ranging from around -50 to 50
> to an 0 to 12 range (this is part of a MIDI music program). I have a
> number of places where I do:
>
> while x < 0: x += 12 while x >= 12: x -= 12
>
> Okay, that works. Just wondering if there is an easier (or faster) way
> to accomplish this.
Are you sure it works? Do you simply want to reduce it to the arbitrary
range, or do you want to also retain the relationship between different x
values such that if x1 < x2, f(x1) < f(x2)?
Consider the following x values:
-11, -12, -13, 11, 12, 13
-11 -> 1
-12 -> 0
-13 -> 11
11 -> 11
12 -> 0
13 -> 1
So -13 gives the same output value as 11 in your current code. Is this
what you want?
You could try the following:
# step 1, limit x to the range -50 .. 50
if x < -50:
x = -50.0
ix x >= 50:
x = 50.0
# step 2, scale x to the range 0 .. 12
x = x * 0.12 + 6.0
If you want an integer value, you need to determine which method is most
relevant. I would suggest rounding.
--
Denis McMahon, denismfmcmahon at gmail.com
More information about the Python-list
mailing list