What would be a fast way to implent this osscilation function.
A signature would look like this:
public static double Calculate(UInt64 currentCounter, uint duration, uint inDuration, uint outDuration)
And the result should be a double that as currentCounter advances, ossciates between 0 and 1. The osscialtion speed is defines by the duration parameter (the number of ticks for a single osccilation). Similarily the ascent and descent speed is defines via inDUration and outDuration (inDUration + outDuration).
alt text http://img252.imageshack.us/img252/9457/graphuf.jpg
The x-Axis of this graph would of course be currentCounter.
EDIT – Here’s a new function that includes staying at 1.0 between
outDurationand the the nextinDuration. Note that I’ve changed your function signature – the input parameters are nowinDuration,holdDuration, andoutDuration. The function stays at 0 betweeninDurationandoutDurationforholdDurationsamples, then stays at 1.0 afteroutDurationfor anotherholdDurationsamples. The ramps are half-Hann functions again, you can change them as desired.This has the same periodicity features as the previous version.
Here’s a graph showing two cycles of the function. The test loop was
alt text http://img16.imageshack.us/img16/4443/oscfnxn2.png
First version
I’m a big fan of the Hann function for stuff like that. It’s continuous and differentiable, if that’s a concern. Here’s a simple implementation:
Here’s a sample graph. This was generated with the loop
Graph with duration = 10000, in = 2500, out = 3000 http://img269.imageshack.us/img269/2969/oscfnxn.png
The function descends from 1.0 to 0 from index
0toinDuration, stays at 0 until indexduration-outDuration, then ascends to 1.0 at indexduration, so it is exactly periodic in ‘duration’ samples.I didn’t understand your comment “Between out and in it is 1 for some time.” Don’t you need another parameter to specify the hold time?