1
$\begingroup$

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

  • 0
    Can you use a spline?2017-02-22
  • 0
    I would prefer not to since it would require me to duplicate the logical components for each function.2017-02-23

1 Answers 1

1

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!

  • 0
    Thanks! I've actually experimented with this function already. Two concerns though: 1. Using large exponents makes the function more computationally expensive than I'd like (less than 20 operations would be ideal). 2. Is there any way to apply the slope/angle component to this equation?2017-02-23
  • 0
    I'm not sure what you mean by slope/angle. Slope only applies to linear functions.... I took your original question to mean you wanted to vary the curvature of the function as a transition from triangular to square. The only way to vary the curvature is to increase the degree.2017-02-23
  • 0
    There was a second part to my original question. Think of how a sawtooth wave differs from a triangle wave. I need another variable that can do this transformation. Clearly this transformation would occur inside the (x-1) since it's just shifting the 12017-02-24
  • 0
    Also to do the transformation from triangle to sawtooth, the negative compliment of your function would need to be transformed as well. If we assume the negative compliment of your function occurs at -22017-02-26