Support

If you have a problem or need to report a bug please email : support@dsprobotics.com

There are 3 sections to this support area:

DOWNLOADS: access to product manuals, support files and drivers

HELP & INFORMATION: tutorials and example files for learning or finding pre-made modules for your projects

USER FORUMS: meet with other users and exchange ideas, you can also get help and assistance here

NEW REGISTRATIONS - please contact us if you wish to register on the forum

Users are reminded of the forum rules they sign up to which prohibits any activity that violates any laws including posting material covered by copyright

Down scale

For general discussion related FlowStone

Down scale

Postby billv » Sun Nov 30, 2014 12:52 am

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?
billv
 
Posts: 1157
Joined: Tue Aug 31, 2010 3:34 pm
Location: Australia

Re: Down scale

Postby tulamide » Sun Nov 30, 2014 1:28 am

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)
tulamide
 
Posts: 2714
Joined: Sat Jun 21, 2014 2:48 pm
Location: Germany

Re: Down scale

Postby KG_is_back » Sun Nov 30, 2014 1:32 am

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.
KG_is_back
 
Posts: 1196
Joined: Tue Oct 22, 2013 5:43 pm
Location: Slovakia

Re: Down scale

Postby billv » Sun Nov 30, 2014 2:16 am

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

Re: Down scale

Postby billv » Sun Nov 30, 2014 5:13 am

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.
billv
 
Posts: 1157
Joined: Tue Aug 31, 2010 3:34 pm
Location: Australia

Re: Down scale

Postby RJHollins » Sun Nov 30, 2014 5:21 am

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 ...
RJHollins
 
Posts: 1571
Joined: Thu Mar 08, 2012 7:58 pm

Re: Down scale

Postby billv » Sun Nov 30, 2014 6:15 am

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:
billv
 
Posts: 1157
Joined: Tue Aug 31, 2010 3:34 pm
Location: Australia

Re: Down scale

Postby tulamide » Sun Nov 30, 2014 9:06 am

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)
tulamide
 
Posts: 2714
Joined: Sat Jun 21, 2014 2:48 pm
Location: Germany

Re: Down scale

Postby Nubeat7 » Sun Nov 30, 2014 11:18 am

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/
User avatar
Nubeat7
 
Posts: 1347
Joined: Sat Apr 14, 2012 9:59 am
Location: Vienna

Re: Down scale

Postby billv » Sun Nov 30, 2014 11:41 am

@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
billv
 
Posts: 1157
Joined: Tue Aug 31, 2010 3:34 pm
Location: Australia


Return to General

Who is online

Users browsing this forum: No registered users and 62 guests