Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Scale numbers/values

Tags:

math

I am working with a piece of hardware that I'm communicating with VIA serial control using a proprietary programming language that looks like a very dumbed down version of C.

The device reports it's current volume when queried. The range is -60 to + 20. How do I scale that to a 0-255 range which goes up in increments of 3?

Can you also provide an example of another value and other scale i.e. -15 to 15 scaled to 0-165, etc

like image 474
inbinder Avatar asked Oct 18 '12 16:10

inbinder


1 Answers

To scale a range x0..x1 to a new range y0..y1:

    y = y0 + (y1 - y0) * (x - x0) / (x1 - x0)

So for your first example above, x0 = -60, x1 = 20, y0 = 0, y1 = 255:

    y = 0 + (255 - 0) * (x - -60) / (20 - -60)

=>  y = 255 * (x + 60) / 80
like image 129
Paul R Avatar answered Nov 14 '22 07:11

Paul R