Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 486051
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T01:27:50+00:00 2026-05-13T01:27:50+00:00

What would be a fast way to implent this osscilation function. A signature would

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T01:27:50+00:00Added an answer on May 13, 2026 at 1:27 am

    EDIT – Here’s a new function that includes staying at 1.0 between outDuration and the the next inDuration. Note that I’ve changed your function signature – the input parameters are now inDuration, holdDuration, and outDuration. The function stays at 0 between inDuration and outDuration for holdDuration samples, then stays at 1.0 after outDuration for another holdDuration samples. The ramps are half-Hann functions again, you can change them as desired.

    public static double Calculate(UInt64 currentCounter, uint inDuration, uint holdDuration, uint outDuration)
    {
        UInt64 curTime;
        double ret;
    
        curTime = currentCounter % (inDuration + 2*holdDuration + outDuration); //this wrapping should really be handled by the caller
    
        if (curTime < inDuration)
        {
            ret = 0.5 * (1.0 - Math.Cos(2.0 * Math.PI * (inDuration - curTime) / (2.0 * inDuration)));
        }
        else if (curTime < inDuration + holdDuration)
        {
            ret = 0.0;
        }
        else if (curTime < inDuration + holdDuration + outDuration)
        {
            ret = 0.5 * (1.0 - Math.Cos(2.0 * Math.PI * (curTime - inDuration - holdDuration) / (2.0 * outDuration)));
        }
        else
        {
            ret = 1.0;
        }
    
        return ret;
    }
    

    This has the same periodicity features as the previous version.

    Here’s a graph showing two cycles of the function. The test loop was

    for (ctr = 0; ctr < 20000; ctr++)
        Calculate(ctr, 2500, 2250, 3000);
    

    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:

    public static double Calculate(UInt64 currentCounter, uint duration, uint inDuration, uint outDuration)
    {
        UInt64 curTime;
        double ret;
    
        //should check that inDuration + outDuration <= duration
        curTime = currentCounter % duration; //this wrapping should really be handled by the caller
    
        if (curTime < inDuration)
        {
            ret = 0.5 * (1.0 - Math.Cos(2.0 * Math.PI * (inDuration - curTime) / (2.0 * inDuration)));
        }
        else if (curTime >= (duration - outDuration))
        {
            ret = 0.5 * (1.0 - Math.Cos(2.0 * Math.PI * (outDuration + duration - curTime) / (2.0 * outDuration)));
        }
        else
        {
            ret = 1.0;
        }
    
        return ret;
    }
    

    Here’s a sample graph. This was generated with the loop

    for (ctr = 0; ctr < 10000; ctr++)
        Calculate(ctr, 10000, 2500, 3000);
    

    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 0 to inDuration, stays at 0 until index duration-outDuration, then ascends to 1.0 at index duration, 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?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to find a fast way to convert a Data Contract to
I would like to know if there is any simple/fast way to create a
I'm using sqlite3 and would like to know, if there is a fast way
I would like to implement a fast group by like feature in Java. I
I would like to find a clean and clever way (in python) to find
I would like to speed up this short piece of code max_x=array([max(x[(id==dummy)]) for dummy
What would be a very fast way to determine if your connectionstring lets you
Is there an easy and fast way to calculate the rank of a field
How fast in frames per second can an iphone 4s take screenshots? What would
Would like to parse IPv4 address from exit-addresses . Format of the file: ExitNode

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.