28
$\begingroup$

I was wondering how can I shift my data that fall between a range lets say [0, 125] to another range like [-128, 128].

Thanks for any help

$\endgroup$

3 Answers 3

63
$\begingroup$

The function $$f(t) = c + \left(\frac{d-c}{b-a}\right)(t-a)$$ will map the interval $[a,b]$ onto the interval $[c,d]$ in that order (meaning $f(a)=c$ and $f(b)=d$), assuming $a\neq b$.

Addendum: It's worth checking out how one can derive this instead of just memorizing it. It's the composition of the following maps.

(1) First, apply the map $t\mapsto t-a$ so the left endpoint shifts to the origin. The image interval is $[0,b-a]$.

(2) Next scale the interval to unit length by dividing by its length (this is the only place the requirement $a\neq b$ is needed). The map that does this is $t\mapsto \frac{1}{b-a}\cdot t$. The image interval is $[0,1]$.

Then reverse the process to get at the other interval as follows.

(3) Scale up by the desired length $d-c$ using the map $t\mapsto (d-c )\cdot t$, and the image is $[0,d-c]$.

(4) Finally shift the left endpoint to $c$ by the map. $t\mapsto c+t$. The image is $[c,d]$.

Composing these maps in order gives the map I indicated at the top.

$\endgroup$
3
  • $\begingroup$ It's beautiful, Does it have a name? $\endgroup$ Commented Sep 11, 2022 at 21:00
  • 1
    $\begingroup$ At its heart, this is really just linear interpolation, @TimothyL.J.Stewart $\endgroup$
    – MPW
    Commented Sep 12, 2022 at 17:43
  • $\begingroup$ precise function! $\endgroup$ Commented Sep 25, 2023 at 14:13
10
$\begingroup$

You want to transform $x\in[0,125]$ to $y\in[-128,128]$.

Assuming scales are linear, you need to do the following:

(1) "Stretch" the $x$-range of $125$ to the desired $y$-range of $256$: $$y=\frac {256}{125}x$$

(2) Set the zero offset (i.e. value of $y$ when $x$ is zero) to $-128$: $$y=\frac {256}{125}x-128\qquad \blacksquare$$

Check:

$$x=0 \Rightarrow y=-128\\ x=125 \Rightarrow y=128$$

as required.

NB: the same method can be applied to convert Celsius to Fahrenheit.

$\endgroup$
4
$\begingroup$

To map [a,b] onto [c,d], just seek the equation of the line through the points (a,c) and (b,d). A point M(x,y) will lie on this line iff det(AM, AB)=0. i.e., (x-a)(d-c)=(y-c)(b-a). Then proceed to solve this equation for y in terms of x. Straightforward algebra yields y = c + ((d-c)/(b-a))(x-a). It is therefore very easy to see that, indeed, a is mapped onto c, b to d and anything in between a and b onto something in [c,d].

$\endgroup$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .