Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a one-line function that generates a triangle wave?

Tags:

c

In a similar way that modulo generates a sawtooth wave. It doesn't have to be continuous.

here is what i mean:

int m = 10; int x = 0; int i = 0; while (i < m*3) {     printf("%d ", x);     x++;     x = x % m;     i++; } 

generates a sequence 0..9, three times which looks like this:

sawtooth wave graph

note that the slope on the right side of the peak is just a graphing artifact

The one-liner in this case is x = i++ % m


What I want is this:

triangle wave graph

If you know one-liners for the other wave forms (sine, square), that would be good to know as well.

Update: everyone's answers have been very helpful and I have a follow-up question.

What would be added to the triangle wave function to make the slope of the lines curve in or out like this:

bulging waveforms

Thanks everyone, your varied answers helped me see the problem from a larger perspective. Special thanks to Noldorin for his take on extending the equation to quadratic curves.

like image 858
willc2 Avatar asked Jul 02 '09 10:07

willc2


People also ask

How do you make a waveform of a triangle?

➢ Triangular waveform can also be generated by integrating square wave from an astable multivibrator. ➢ The cycle from the square wave to the next operational amplifier repeats and generates a triangular waveform. ➢ Triangular waveform can also be generated by integrating square wave from an astable multivibrator.

What causes a triangle waveform?

Triangular waves are a form of electronic waveform where the voltage level ramps up linearly and falls away linearly at the same rate for both ramps. Triangular waves or waveforms are often found within electronics and are used for a variety of purposes.

Is a triangle wave an even function?

Symmetry Trigonometric Series and Symmetry In other words, if you shift the function by half of a period, then the resulting function is the opposite the original function. The triangle wave has half-wave symmetry. See below for clarification. without being either even or odd.


1 Answers

Triangular Wave

y = abs((x++ % 6) - 3); 

This gives a triangular wave of period 6, oscillating between 3 and 0.

Square Wave

y = (x++ % 6) < 3 ? 3 : 0; 

This gives a regular square wave of period 6, oscillating between 3 and 0.

Sine Wave

y = 3 * sin((float)x / 10); 

This gives a sine wave of period 20 pi, oscillating between 3 and -3.


Update:

Curvy Triangular Wave

To get a variation of the triangular wave that has curves rather than straight lines, you just need to introduce an exponent into the equation to make it quadratic.

Concave curves (i.e. x^2 shape):

y = pow(abs((x++ % 6) - 3), 2.0); 

Concave curves (i.e. sqrt(x) shape):

y = pow(abs((x++ % 6) - 3), 0.5); 

Alternatively to using the pow function, you could simply define a square function and use the sqrt function in math.h, which would probably improve performance a bit.

Also, if you want to make the curves steeper/shallower, just try changing the indices.


In all of these cases you should easily be able to adjust constants and add scaling factors in the right places to give variations of the given waveforms (different periods, ampltiudes, asymmetries, etc.).

like image 186
Noldorin Avatar answered Oct 21 '22 16:10

Noldorin