Normalizing from [0.5 - 1] to [0 - 1]
Asked Answered
E

6

27

I'm kind of stuck here, I guess it's a bit of a brain teaser. If I have numbers in the range between 0.5 to 1 how can I normalize it to be between 0 to 1?

Thanks for any help, maybe I'm just a bit slow since I've been working for the past 24 hours straight O_O

Electromechanical answered 24/9, 2009 at 12:27 Comment(1)
I assume the + votes were directed exclusively at "been working for the past 24 hours straight" :)Singlefoot
P
66

Others have provided you the formula, but not the work. Here's how you approach a problem like this. You might find this far more valuable than just knowning the answer.

To map [0.5, 1] to [0, 1] we will seek a linear map of the form x -> ax + b. We will require that endpoints are mapped to endpoints and that order is preserved.

Method one: The requirement that endpoints are mapped to endpoints and that order is preserved implies that 0.5 is mapped to 0 and 1 is mapped to 1

a * (0.5) + b = 0 (1)
a * 1 + b = 1     (2)

This is a simultaneous system of linear equations and can be solved by multiplying equation (1) by -2 and adding equation (1) to equation (2). Upon doing this we obtain b = -1 and substituting this back into equation (2) we obtain that a = 2. Thus the map x -> 2x - 1 will do the trick.

Method two: The slope of a line passing through two points (x1, y1) and (x2, y2) is

(y2 - y1) / (x2 - x1).

Here we will use the points (0.5, 0) and (1, 1) to meet the requirement that endpoints are mapped to endpoints and that the map is order-preserving. Therefore the slope is

m = (1 - 0) / (1 - 0.5) = 1 / 0.5 = 2.

We have that (1, 1) is a point on the line and therefore by the point-slope form of an equation of a line we have that

y - 1 = 2 * (x - 1) = 2x - 2

so that

y = 2x - 1.

Once again we see that x -> 2x - 1 is a map that will do the trick.

Protanopia answered 24/9, 2009 at 13:24 Comment(2)
In method one, to solve, why do you multiply equation (1) by -2 ?Epimorphosis
@RobertL: To solve a linear system of two equations with two unknowns, the easiest method is to convert the system to an equivalent one where the coefficients on one of the unknowns are equal in both equations. So here we have the two equations a * (0.5) + b = 0 and a * 1 + b = 1. After multiplying the first equation by 2 we arrive at the two equations a + 2 * b = 0 and a * 1 + b = 1. Since the coefficients on a are equal in both equations we can subtract the first equation from the second to obtain -b = 1.Protanopia
M
31

Subtract 0.5 (giving you a new range of 0 - 0.5) then multiply by 2.

double normalize( double x )
{
    // I'll leave range validation up to you
    return (x - 0.5) * 2;
}
Motherland answered 24/9, 2009 at 12:30 Comment(0)
P
24

To add another generic answer.

If you want to map the linear range [A..B] to [C..D], you can apply the following steps:

Shift the range so the lower bound is 0. (subract A from both bounds:

[A..B] -> [0..B-A]

Scale the range so it is [0..1]. (divide by the upper bound):

[0..B-A] -> [0..1]

Scale the range so it has the length of the new range which is D-C. (multiply with D-C):

[0..1] ->  [0..D-C]

Shift the range so the lower bound is C. (add C to the bounds):

[0..D-C] -> [C..D]

Combining this to a single formula, we get:

       (D-C)*(X-A)
X' =   -----------  + C
          (B-A)

In your case, A=0.5, B=1, C=0, D=1 you get:

       (X-0.5)
X' =   ------- = 2X-1
        (0.5)

Note, if you have to convert a lot of X to X', you can change the formula to:

       (D-C)         C*B - A*D
X' =   ----- * X  +  ---------  
       (B-A)           (B-A)

It is also interesting to take a look at non linear ranges. You can take the same steps, but you need an extra step to transform the linear range to a nonlinear range.

Pleurisy answered 25/9, 2009 at 13:23 Comment(1)
What would be the thing you need to do in order to accommodate a non linear range? I want to convert a range of -1..1 to -.25..1 where -.25..0..1 = -1..0..1 (so -0.25..0 is the entire range of -1..0).Salade
H
16

Lazyweb answer: To convert a value x from [minimum..maximum] to [floor..ceil]:

General case:

normalized_x = ((ceil - floor) * (x - minimum))/(maximum - minimum) + floor

To normalize to [0..255]:

normalized_x = (255 * (x - minimum))/(maximum - minimum)

To normalize to [0..1]:

normalized_x = (x - minimum)/(maximum - minimum)
Harrie answered 10/6, 2013 at 17:41 Comment(1)
I like the way you generalized your answer. I used your syntax to do this in Python. +1Gasper
L
15

× 2 − 1

should do the trick

Lagrange answered 24/9, 2009 at 12:29 Comment(1)
It was the math part of the solution I was solving not the language related partLagrange
A
0

You could always use clamp or saturate within your math to make sure your final value is between 0-1. Some saturate at the end, but I've seen it done during a computation, too.

Alberik answered 13/1, 2014 at 23:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.