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
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
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
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