Normalizing from [0.5 - 1] to [0 - 1]

前端 未结 6 1545
自闭症患者
自闭症患者 2021-01-30 02:45

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 a

相关标签:
6条回答
  • 2021-01-30 03:00

    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.

    0 讨论(0)
  • 2021-01-30 03:00

    × 2 − 1

    should do the trick

    0 讨论(0)
  • 2021-01-30 03:12

    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.

    0 讨论(0)
  • 2021-01-30 03:15

    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.

    0 讨论(0)
  • 2021-01-30 03:15

    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)
    
    0 讨论(0)
  • 2021-01-30 03:24

    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;
    }
    
    0 讨论(0)
提交回复
热议问题