Reversing floats and integer values

Aug 20, 2011 at 11:46am

Reversing floats and integer values

This will have a simple solution and it may sound stupid to ask after everything else that I’ve built and found solutions too but I havent been sleeping because of this project properly for months and my pragmatic mind is deteriorating!

Does anyone know the simplest way to reverse the incoming float number range..

For example, I have a mean number coming in from the camera between 0 – 255, but counting down from 255 – 0..I need to reverse this so that I get 0 – 255 instead. How can I do this??

Peas

#58560
Aug 20, 2011 at 11:55am

[!- 255] or [!- 255.] ??

Steve

#210437
Aug 20, 2011 at 12:15pm

255.

#210438
Aug 20, 2011 at 2:37pm

110.

#210439
Aug 20, 2011 at 2:42pm

@Roman ?

@mr_mapes Its a float so its 0. – 255.

Any ideas?

#210440
Aug 20, 2011 at 2:55pm

The [scale] object is what you need.
Feed to the incoming data stream into [scale] with the following four arguments – 255. 0. 0. 255.

#210441
Aug 20, 2011 at 2:59pm

mr.mapes had it:

[!- 255.]

it’s the reverse-subtraction object, note the !. there’s one for division too, which gives the reciprocal if the argument is 1:

[!/ 1.]

because addition and multiplication are commutative (order doesn’t matter), there are no corresponding [!+] or [!*] objects.

and yes, [scale] will do it too, it’s a more general-purpose object for expanding/shrinking/reversing ranges. very handy for things like input sensor data turning into effect sizes, mouse motions to frequencies, etc… you probably don’t want 0-127 MIDI CC mapping to 0-20000 Hz, most of the output would sound really high, so you can scale to a more human-tolerable range like 50-2000 or something.

#210442
Aug 21, 2011 at 1:35am

I like using [scale] when I want to change ranges and scales dynamically. Call me old-fashioned but I prefer to y=mx+c with a [* ] and [+ ] as often as possible. Something in my head tells me it’s more eco to do it that way…

#210443
Aug 21, 2011 at 1:38am

@seejayjames – why use [scale] to convert MIDI cc to frequency in such a linear fashion when [mtof] does exactly what it says on the box?

#210444
Aug 21, 2011 at 12:11pm

I think using [scale] to map to/from frequencies is horrible, that dreaded fifth argument! I’d always recommend sticking with [mtof] or [expr] in these situations.

#210445
Aug 21, 2011 at 1:18pm

My old lecturer used to encourage converting one’s scale to 0-1, then cube it, then scale and offset the result to get nice log behaviour

#210446
Aug 21, 2011 at 3:58pm

@mr_mapes
your alternative approaches to the age-old conundrum of parsing/scaling are insightful and vital, but unfortunately a little vague:
“then scale and offset the result to get nice log behaviour”; the OP and others may not know how to implement y=mx+c for example, particularly as the OP is asking about something as basic as inversion; furthermore, I believe our regular and eminent contributor seejay said:
“you probably don’t want 0-127 MIDI CC mapping to 0-20000 Hz….”
thereby suggesting that linear mapping of frequency is not recommended; and Luke underlined this. We often (myself included) forget that every post is a valuable archived resource for those on their way through the edges of the enchanted forest called MaxMSP.

I played an open air festival y’day so forgive the hippy ethos

Brendan

#210447
Aug 21, 2011 at 5:18pm

Humble apologies for drifting off topic…

#210448
Aug 21, 2011 at 5:41pm

Apologies?? For what? I and other n00bs would merely love to see an implementation of your formulae!

:)

Brendan

#210449
Aug 21, 2011 at 5:58pm

So I could use scale to convert the number range im receiving from a camera in a totally different range to control say the volume of a track?

Does anyone know what the maths would be for this using scale?

To convert 255 > 0 to -20 > 0

??

#210450
Aug 21, 2011 at 6:07pm

Hi
to invert your input, the first thing is ‘remainder subtract’, as mr_mapes states: [!- 255] (float or integer);
then [/ 12.5] approx, then [- 20].
Take care with your float or integer preference to ensure it works.

Brendan

#210451
Aug 21, 2011 at 6:37pm

I just don’t like tones much above 2000 Hz :) and yes, linear doesn’t really “sound right”, it was just a simple thought to get started. The scale exponent argument takes some experimentation to get used to, or you can just go with [expr], which gives you total control.

you can also use [scale] to simply convert to 0-127 (or maybe 0-158) and then use that to control a gain~, if your intent is to scale audio.

Here are some other options to make curve outputs from linear inputs. You can also just draw in whatever curve you want:

– Pasted Max Patch, click to expand. –
#210452
Aug 21, 2011 at 10:30pm

My old lecturer used to encourage converting one’s scale to 0-1, then cube it, then scale and offset the result to get nice log behaviour

That’s not logarithmic behavior, that’s cubic behavior. Depending on your needs, the latter may, for certain values, be close enough to exponential to be a cheap substitute. But it ain’t the same thing. Not never nohow.

And it certainly isn’t logarithmic.

#210453

You must be logged in to reply to this topic.