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

1. ## 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. ## Re: Waveform Generator?

Originally Posted by sci4me
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.

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