Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Waveform Generator?

  1. #1
    Member
    Join Date
    Feb 2013
    Posts
    78
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Waveform Generator?

    Hey guys. So, I am wondering.. I need to implement this interface for some of the common waveforms such as sine:
    public interface SignalFunction
    {
    	public double f(int freq, int time);
    }
    And.. im not exactly sure how... I mean for sine, would it simply be this:
    public double f(int freq, int time)
    {
            return Math.sin(time);
    }
    ?
    As for sawtooth, triangle and square, im not sure. If anyone can help me implement these, I would really appreciate it. Thanks!

    EDIT:

    okay... so... I am currently working on making a triangle wave. I have this:
    Math.abs((((2 * t) / Math.PI - 1) % 4) - 2) - 1
    It almost works... but its not QUITE a triangle.... what am I doing wrong?


  2. #2
    Member
    Join Date
    Jun 2012
    Location
    Left Coast, USA
    Posts
    451
    My Mood
    Mellow
    Thanks
    1
    Thanked 97 Times in 88 Posts

    Default Re: Waveform Generator?

    Quote Originally Posted by sci4me View Post
    I mean for sine, would it simply be this:
    public double f(int freq, int time)
    {
            return Math.sin(time);
    }
    No, it wouldn't. The argument of the Math.sin method is in radians, so if you have a sinusoidal function with a peak amplitude of 1 and a given frequency in Hz, the value of the function at a given time, t is
        f = Math.sin(2.0*Math.PI*frequency*t);

    Instead of frequency in Hz, you could specify the period of the sine wave as T seconds, where T = 1.0/frequency.

    Then the value of the function at time t is given by
        f = 2.0*Math.PI/T * t;

    Now, let's compute a value of a sawtooth taking on values of 0 through 1 and having period T seconds.

    First, compute the value for t on the interval 0 <= t <= T:

    It's a straight line going through points (0, 0) and (T, 1), so the value for time t is given by
        f = t/T;

    Now, unlike the sine function, which is periodic by definition, our sawtooth waveform will be periodic if (and only if) we define it to be the periodic extension of the above. If the value of t is greater than T (or less than zero) then use some kind of modulus operation to get a value of t that is in the interval 0 through T and is an integer number of periods away from the given time value. Then you can use the above expression. For example if the period is 1 millisecond and we want the value of the function at 1.75 milliseconds, the value will be the same as at 0.75 milliseconds. Stuff like that.


    Now for your triangle function.

    Suppose it has a period of T seconds and its value goes from 0 through 1. Then the function for 0 <= t <= T is actually two straight lines. (It will be a continuous piecewise-linear function.)

    One line goes (uphill) through points (0,0) and (T/2, 1). The other goes (downhill) from point (T/2, 1) to (T, 0).

    The value over the interval (0, T) would be computed as
        if (0 <= t && t < T/2) {
            // f = the value on the first line I defined above
        }
        else {
            // f = the value on the second line I defined above.
        }

    As in the case of the sawtooth, this only holds for values of t from 0 through T. Anything else takes a little calculation to get the value of the periodic extension of this function, as it did with the sawtooth.


    Cheers!

    Z

Similar Threads

  1. Help with Schedule Generator
    By popnfresh in forum What's Wrong With My Code?
    Replies: 3
    Last Post: April 9th, 2013, 01:08 PM
  2. Sales Generator
    By zbailey in forum What's Wrong With My Code?
    Replies: 6
    Last Post: January 21st, 2013, 04:06 PM
  3. Barcode Generator
    By StarRocks in forum JDBC & Databases
    Replies: 4
    Last Post: August 27th, 2012, 12:35 AM
  4. Matrix Generator
    By kakb9091 in forum What's Wrong With My Code?
    Replies: 29
    Last Post: May 10th, 2012, 12:00 PM
  5. Replies: 0
    Last Post: January 25th, 2011, 01:24 AM