Phase wrapping with floats
Sigh. I just cannot get my head around this!
I want to take a single varying value (0.-1.), and produce 2 additional out-of-phase streams.
0. ............0.5 .............. 1. .............. 0.5 ................ 0.
0.5 .......... 1. ............... 0.5 ............. 0. ............... 0.5
1. .......... 0.5 ................ 0. .............. 0.5 ............... 1.
The 3rd one is easy - I can just use [scale 0. 1. 1. 0.] or equivalent. (Though that may not be the best way of going about this)
It's the middle one that's doing my head in. (I tried using the 3PO [180wrap], but couldn't get anything meaningful with that either)
What I'm trying to do is to control the colour of an LED lamp with a single controller. Colour-wise, it's pretty much the effect you get if you drag the [swatch] cursor from left to right, vertically half way.
I got something acceptable using Nathan Wolek's [phasor.shift~] (with 3 outlets), (which suggests to me that just phase shifting a single number stream should produce a similar effect). I've been trying different approaches for an hour or so now, but d**ned if i can figure out how to phase shift floats!
Help!
Hmm. Here's one way to do it. There's almost certainly a better way though!
Sigh, I just cannot WRAP my head around this ;)
Is this what you need?
Brendan
Not so fast there Brendan, I see some folding . . . . .
Yeah, scrap that, it needs a closer look. Apologies for the noise.
hmmmmm
Ok, any better? Such a n00b trying to perform modulo 1 on a cosine. HTH
Brendan
ps I'm loving the PD-vibe from the Consolas font!
. . . sorry David, I'm stumped. : (
Hate my weak maths. Hnnnngh
Wow. If you're stumped I haven't a cat in hell's chance!
Slightly modified to show the differences between the 2 approaches. And the results _are different. It may well be that sending 1/3 out of phase signals isn't actually the correct way to navigate through a colour space. But I have no idea what a correct way would be!
The good news is that you can use the hsl message to swatch to get the values you want without having to work out the maths.
And just for fun here's what the phases actually should look like:
Ha! I knew there had to be an easier way (though I would never have discovered that one)
thanks John!
HSL
ffs, it's right there in the helpfile! Nice one John, just saved me an hour or two of brain-melt.
Brendan
ps - and your second patch shows the maths very concisely
alternatively you could first add the offset(s) - and then wrap and fold all three versions. :)
@Roman. Well, yes. That was my first thought, but I couldn't figure out the wrapping/folding bit - The middle curve was never simply an out of phase sine wave. Also, the only thing it seems I can do with a vanilla max object is to wrap. I tried with scampf, which at least wraps/folds/reflects, but even with that I couldn't get my head around it.. As the [source control value plus (eg 0.5)] goes above 1., the [phased value (being reflected)] starts to decrease from 1. - so far so good. But once the [source value plus] drops down below 1., the [phased value] jumps up to join it.
Maybe one needs to break the [source value plus] into ranges using [split], and perform different functions on each range? Not sure if I tried that. (Though I may have done so before, as "phasing" a float stream is something I've attempted at several times in the past. ).
Since you're interested in navigating colorspace, @leafcutter's example is the best solution.
In general, for stepping through a function with different phase relationships, as @roman intimates, you can add a phase offset to your indexed place in the function, and wrap that with %. That's what the MSP cycle~ object does, and is the generally accepted way. Viz. the formula f(n)=Asin(2πƒn/R+ø).
In the "Did you know?" category: Did you know that the buffer~ object in combination with peek~ is a great way to make a non-MSP lookup table for floats in Max? In this example, I've filled the buffer~ with a sine function, but you can fill it with whatever you want using peek~, then look up the stored values according to their sample index in the buffer~.
@christopherdobrian. Nice. So what the "number + offset -> %" bit is doing is to create a ("delayed") ramp which then steps through a stored wavetable (as a phasor~ connected to the right inlet of cycle~ does in the msp domain). I thought that something on the lines of a "ramp stepping through a wavetable" bit that was the general solution, but couldn't quite figure out how to make it work. (I'd been trying with [table], but was missing the % bit. And using buffer~/peek~ like this is neater than trying to use [table]). Thanks for the help in clearing my mental fog!
And for this particular application, you're right that @leafcutter's solution is the simplest solution.