single equation that can approximate sine, triangle, and square waves by changing a single variable

1.2k Views Asked by At

I'm trying to develop a function that can be used to approximate a sine wave, a triangle wave, and a square wave by adjusting one variable in the equation.

This function will be used as the main oscillator in a soft synthesizer and I would prefer computational simplicity over accuracy. So a parabolic approximation of sine would be much better than actually using a sine function.

The transition between waveforms would be Triangle -> Sine -> Square as the constant increases from 0 to 1 (or any similar number range).

The design of the synthesizer assumes that the input (x) of this function will oscillate from -1 to 1 linearly. But this could be changed to any range if needed.

Also, I would also like to adjust the slope/angle of the wave with a separate variable (think of the transition between a sawtooth wave and a triangle wave).

1

There are 1 best solutions below

4
On BEST ANSWER

I would consider only mapping the function from 0 to 1, and then reflecting across the $x$-axis for the range 0 to -1.

In that context, one way to approximate the Triangle Wave would be an absolute value function. An approximation of Sine would be a parabola (your suggestion here started my entire train of thought, so thank you). Then, an approximation of a Square wave could be a power function with a large exponent that has tiny curves at the corners. Consider this screen shot (taken at desmos.com):

enter image description here

The function in red is $f(x)=-\left|x-1\right|+1$.
The function in blue is $f(x)=-\left(x-1\right)^2+1$.
The function in green is $f(x)=-\left(x-1\right)^{100}+1$.

This suggests that your variable could run from 0 to 100, where the variable is the degree (exponent) of the base function (note that 0 represents the absolute value function, a form of a linear function, which is actually degree 1). Make sure only even values of the variable are allowed.

If this works, there may be an area where we could improve it. There isn't much difference in shape between a function of degree 50 and a function of degree 100. That suggests the ear may not hear much of a difference, either. If that is the case, calculate the logarithm of the variable, and use the logarithm as the degree of the function.

Hope this helps!