POV-Ray : Newsgroups : povray.off-topic : Adding adjustable randomness to a sin wave.. Server Time
2 Nov 2024 01:20:23 EDT (-0400)
  Adding adjustable randomness to a sin wave.. (Message 1 to 2 of 2)  
From: Patrick Elliott
Subject: Adding adjustable randomness to a sin wave..
Date: 5 Jan 2011 21:06:39
Message: <4d2523af$1@news.povray.org>
Ok, here is what I have thought of, so far, I keep reworking it, but 
haven't tested it at all, and I suspect I may have the code wrong...

level = 1; // Max light amount.
frequency = 0.1; // Rate of effect. (Assumes fractions of a second.)
randomness = 0.5; // How much it wobbles.
min = 0; // Minimum light level.
mid = (level - min) / 2;
squaring = 1;

a = Rand(1 - randomness);
b = Rand(1 - randomness);

loop
   loop
     x = x + 0.01;
     newe = Interpolate(a, b, x);
     tlevel = (level - min) * (1 + squaring);
     newf = Interpolate(0, tlevel, x) + mid;
     if (newf > level)
       newf = level;
     if (newf < min)
       newf = min;
     SetLightLevel(newe * f);
     wait(frequency);
   until x = 1;
   x = 0;
   a = b;
   b = Rand (1 - randomness);
until script_end;

function Iterpolate(a, b, x)
   c = x * PI;
   d = (1 - cos(c)) * .5;
   return a*(1-d) + b*d;
end function

In fact, I am 90% sure it *is* wrong.. lol

So, here is what I am attempting to actually do. I want to set a maximum 
light level, ranging from 0-1, for current purposes. However, it is open 
ended, since this may change in the future. Same with the "minimum", 
which currently assumes 0, but since its using GPU, could at some point 
allow "negative" light. I need to be able to do:

1. Have it stay at the max.
2. Turn on/off sin wave.
3. Set frequency. I.e., duration between trough and peak.
4. Turn on/off a "capping" for either top or bottom (preferred), or as 
above, where it doubles the result with the used settings, caps both. 
I.e., produces a semi-square wave. I would like to be able to actually 
force the derived points to be either closer/farther apart, so that the 
result produced ramps up/down quicker. A 100% ramp up/down would produce 
a full square wave, anything between would exaggerate how much 
"flatness" the top and bottom had, while still being curved. I.e., 
something that dims fast, and stays dim for a long time, then brightens 
fast, and stays, etc., but doesn't just blink on, stay that way, when 
off, instantly. But, I have no damn clue how to even try that with this 
method, and even less idea what would work better.

5. Turn on/off randomness.
6. Set how random that is.
7. Do this relatively simply.

Nearest I have come up with, above, is to run one cosin interpolated 
wave as a value between 0-1, and the other as a value between min-level, 
then multiply them, since the result should be a percentage of what ever 
the "usable" range is for the "random" pattern. A version where the 
randomness is = 1 would produce a flat line (i.e., midline all the way), 
a value of 0, as random as it can get. The problem is how effective, my 
"clip to square" effect would be, if I am doing any of this right at 
all, and, more to the point, since I am just fumbling here, if there is 
a better way to handle it. :p

I would like to, if I got something that worked, to submit the code as a 
new option to the thing I plan to use it in, so it doesn't have to be 
done in script, you could just set the parameters and let it run. For 
now, I just want something I can adjust to do this sort of thing. Which 
is a single way to do anything from simulating flickering candles, to a 
blinking tail light, to something that slowly lights, then dims, without 
different code for every single case.

-- 
void main () {

     if version = "Vista" {
       call slow_by_half();
       call DRM_everything();
     }
     call functional_code();
   }
   else
     call crash_windows();
}

<A HREF='http://www.daz3d.com/index.php?refid=16130551'>Get 3D Models, 
3D Content, and 3D Software at DAZ3D!</A>


Post a reply to this message

From: Patrick Elliott
Subject: Re: Adding adjustable randomness to a sin wave..
Date: 14 Jan 2011 01:01:03
Message: <4d2fe69f@news.povray.org>
OK. Rethought this a bit.. Here is what I have, pseudo-coded:

//Settings.
noise = 0.5; // (percentage of drift possible)
top = 1; // Top and bottom same = constant level.
bottom = 0;
interval = 0.01;

x = 0;
lval = bottom;
do
   if (noise) rval = rand(1 - noise);
   else rval = 1;

   d = (1 - cos(x * 2 * PI)) * .5; // Get curve point.
   nval = d * rval;
   cval = lval*(1-d) + nval*d;
   //:1: - Other code.
   cval = cval * (top - bottom)) + bottom; // Move to range.
   lval = nval;
   x = x * (x < 1) + interval * (x < 1); // Should cycle back to 0, when 
over 1.
loop (until script closes)

This means that, in principle, I could add in something at :1: that 
could transform the resulting curve into a step, saw tooth, square wave, 
or some mix, in theory, if certain things are set right. For example, 
"noise = 0" would always produce the sin wave, so would be easy to 
"clip/adjust" into something else, in principle. Still thinking on how 
that might be possible...

And finding formulas online to handle it... Gah!

-- 
void main () {

     if version = "Vista" {
       call slow_by_half();
       call DRM_everything();
     }
     call functional_code();
   }
   else
     call crash_windows();
}

<A HREF='http://www.daz3d.com/index.php?refid=16130551'>Get 3D Models, 
3D Content, and 3D Software at DAZ3D!</A>


Post a reply to this message

Copyright 2003-2023 Persistence of Vision Raytracer Pty. Ltd.