Down scale

For general discussion related FlowStone
Post Reply
billv
Posts: 1165
Joined: Tue Aug 31, 2010 3:34 pm
Location: Australia
Contact:

Down scale

Post by billv »

eg:0-255 int scale down to 0-1 float.
Always had trouble with this sort of thing, but
recently came up with a solution that seems to work ok.
Just want to ask...is there a proper way to do this via math?
tulamide
Posts: 2714
Joined: Sat Jun 21, 2014 2:48 pm
Location: Germany

Re: Down scale

Post by tulamide »

As long as the result can be floats you can map any range to any other range.

source_min, source max, source_number, dest_min, dest_max

f = (dest_max - dest_min) / (source_max - source_min)
v = (source_number - source_min) * f + dest_min


Example
source: 0-255, dest: 0-1

f = (1 - 0) / (255 - 0) = 0.00392 (abbr.)

for each number 0-255 (example is 255 here)
v = (255 - 0) * f + 0 = 1
"There lies the dog buried" (German saying translated literally)
KG_is_back
Posts: 1196
Joined: Tue Oct 22, 2013 5:43 pm
Location: Slovakia

Re: Down scale

Post by KG_is_back »

yes... simply divide the int 0-255 input with 255 (make sure the math is done in float format). Or preferably pre-compute the value of 1/255 and multiply by that.
ruby code would look like this:

Code: Select all

value01=value255.to_f * 0.00392156862745098 #0.00392156862745098 is 1/255


To make sure you do the math in float format right click one of the input on divide prim (or multiply prim if you use that) and set the input to float... or connect the float number first.
billv
Posts: 1165
Joined: Tue Aug 31, 2010 3:34 pm
Location: Australia
Contact:

Re: Down scale

Post by billv »

thanks guys....look forward to trying it when I get home..
billv
Posts: 1165
Joined: Tue Aug 31, 2010 3:34 pm
Location: Australia
Contact:

Re: Down scale

Post by billv »

KG_is_back wrote: simply divide the int 0-255 input with 255

Wow..that was simple.... :o
just for laughs...here's my dumb approach...
Create ruby version of green loop prim...set to 255 ticks with value 0.001
Feed into back of knob. set knob min/max 0-25, also create int output * 255
place "changed" prim on into output... on change push float value into a new array.
Hit ruby loop and get an array of 255 float values ranging from 0-1.
RJHollins
Posts: 1573
Joined: Thu Mar 08, 2012 7:58 pm

Re: Down scale

Post by RJHollins »

talk about 'two ways to 'skin-na-kat' :shock:

:lol:

BTW ... I have some concoction of schematic designs that turned so evil ... I actually heard some form of growling every time I opened the schematic :?

Now I can't even figure out what I was trying to do ... scary ... just real scary ...
billv
Posts: 1165
Joined: Tue Aug 31, 2010 3:34 pm
Location: Australia
Contact:

Re: Down scale

Post by billv »

RJHollins wrote:scary

:D
Scary is seeing a solution and not been able to break it down to
"simply divide the int 0-255 input with 255"
:lol:
tulamide
Posts: 2714
Joined: Sat Jun 21, 2014 2:48 pm
Location: Germany

Re: Down scale

Post by tulamide »

billv wrote:
RJHollins wrote:scary

:D
Scary is seeing a solution and not been able to break it down to
"simply divide the int 0-255 input with 255"
:lol:

That's a bit unfair :'(
You said "eg:", so I answered with a universal valid formula that you can use for each task (like 42-51 -> -1-0), not just 0-255 -> 0-1. But if, and it seems so, this was all you're interested in, here's a Ruby version of your array approach:

Code: Select all

# my_array filled with values 0-255, for example Array.new(256) {|index| index}
my_array.map {|item| item.to_f / 255}
#my_array filled with values 0-1
"There lies the dog buried" (German saying translated literally)
User avatar
Nubeat7
Posts: 1347
Joined: Sat Apr 14, 2012 9:59 am
Location: Vienna
Contact:

Re: Down scale

Post by Nubeat7 »

i normally use this methode:

Code: Select all

   def scale_value(value,oldMin,oldMax,newMin=0.0,newMax=1.0)     
     scale = (newMax - newMin) / (oldMax - oldMin)
     offset = newMin - oldMin * scale
     return value * scale + offset
   end


while Tulamides version is a bit smarter and needs 1 multiply less ;)

depending on the need its pretty comfortable to use default values for the methode variables, the example above would fit well for billv's example and would look like this:

Code: Select all

   def scale_value(value,oldMin,oldMax,newMin=0.0,newMax=1.0)     
          scale = (newMax - newMin) / (oldMax - oldMin)
          return (value - oldMin) * scale + newMin  #tulamide's version
   end
   
def event i,v
    output scale_value(@value,0,255)
end

this would scale from 0..255 to 0..1

if you need rescaling for arrays you find it also in my array class expansion here:
http://flowstone.guru/downloads/array-c ... expansion/
billv
Posts: 1165
Joined: Tue Aug 31, 2010 3:34 pm
Location: Australia
Contact:

Re: Down scale

Post by billv »

@tulamide
Im still trying to understand your code., and KG's
believe it or not. So please take no offense.
The comment 'simply divide by 25', gave me a way
in , so I could actually see what I was doing with my
solution. Compared to my solution, it is a more
'proper' way..
Yes, your code shows a more effective way,
but I havnt been able to modify it yet.
Edit..
Thanks nubeat7...just saw your post after I posted
Post Reply