Is there a single line function that generates a triangular wave?

Similarly, that modulo generates a sawtooth wave. It does not 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 of 0..9, three times, which looks like this:

sawtooth wave graph

note that the slope to the right of the peak is only a graphic artifact

Single-line in this case x = i ++% m




I want this:

triangle wave graph

If you know single-line symbols for other waveforms (sine, square), this is also good to know.

Update : all answers were very helpful, and I have the following question.

What will be added to the wave function of the triangle to make the slope of the curve lines anyway:

bulging waveforms

Thanks to everyone , your varied answers helped me see the problem from a larger perspective. Special thanks to Noldorin for being extended to quadratic curves.

+53
c
Jul 02 '09 at 10:23
source share
8 answers
 x = m - abs(i % (2*m) - m) 
+18
Jul 02 '09 at 10:28
source share

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 a period of 20 pi , oscillating between 3 and -3.




Update:

Solid triangular wave

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

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

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

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

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

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

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




In all these cases, you should easily adjust the constants and add scaling factors to the desired places to give variations to the waveform data (different periods, amplitudes, asymmetries, etc.).

+78
Jul 02 '09 at 10:28
source share

Turning to Eric Bainville, answer:

 y = (A/P) * (P - abs(x % (2*P) - P) ) 

Where x is a running integer and y is the output of a triangular wave. A is the amplitude of the wave, and P is the half-period. For example, A = 5 will produce a wave that goes from 0 to 5; P = 10 will generate a wave with a period of 20. The wave starts at y = 0 at x = 0.

Note that y will be a floating point if P is not a coefficient of A. And, yes, for mathematical purists: A is technically twice the wave amplitude, but look at the picture below and you will understand what I mean .

Visualization:

+18
Mar 14 '14 at 9:24
source share
 y = abs( amplitude - x % (2*amplitude) ) 

Changing the wavelength simply requires a factor for x .

Edit: what I call amplitude is not really amplitude, but the maximum value (i.e. 5 if the curve oscillates between 0 and 5). Amplitude in the mathematical sense is half this. But you understand.

+5
Jul 02 '09 at 10:30
source share

I know this is an old post, but for those who are looking for something similar that I recommend looking at. http://en.wikipedia.org/wiki/Triangle_wave

The last formula is y (x) = (2a / π) arcsin (sin ((2π / p) * x))

or.

 (2 * amplitudeConstant / Math.PI) * Math.Asin(Math.Sin(2 * (Math.PI / periodConstant) * Convert.ToDouble(variableX))) 
+3
Oct. 15 '13 at 6:27
source share

Try the following:

 x = m - abs(m - 2*(i++ % m)) 
0
Jul 02 '09 at 10:47
source share

Here is a periodic function that looks like a distant sinusoidal approximation; essentially it's a bluing paraboloid using square X:

 function xs ( xx : float ): float{ var xd =Mathf.Abs((Mathf.Abs(xx) % 2.4) - 1.2); if ( Mathf.Abs(Mathf.Abs(xx) % 4.8) > 2.4){ xd=(-xd*xd)+2.88; }else{ xd = xd*xd; } return xd; } 
0
Mar 30 '13 at 11:33
source share

I tested it with a simple loop, and you guys didn't answer the question about the person at all. Cutting and gluing will not help people. No wonder so many hoaxes in the media are so successful. With people who repeat the mistakes of others that cannot be unexpected. And people even give a positive reputation for these wrong answers ?! Unbelievable! But then again, this is consistent with my previous point.

So, firstly, we are going to explain to our people what the TRIANGLE wave is. Well, this is a wave that has a period consisting of two identical inclined ramps. The ramp is inclined up and ramp, as well as the first, but inclined down, in contrast to SAWTOOTH, in which the inclined sloping inclination or slope is inclined down, after repeated talud.

PS: The last one that gave "y (x) = (2a / π) arcsin (sin ((2π / p) * x))" is too complicated, we are looking for a fast C ++ subroutine, so trigonometry absolutely cannot be and speech.

Testing Procedure:

(...)

 for (byte V=0; V<255; V++) { unsigned int x = evenramp(V); plotRGB(0,0,x,x,x); delay(100); // make sure you have your own delay function declared of course /* use your own graphic function !!! plotRGB(row,column,R,G,B) */ /* the light intensity will give you the change of value V in time */ /* all functions suggested as answer give SAWTOOTH and NOT TRIANGLE */ /* it is a TRIANGLE the man wants */ } float triangleattempt(unsigned int x) // place any answered formula after '255 *' behind return. { return 255 * (255 - abs(255 - 2*(x % 255))); // this will show a SAWTOOTH } //All given answers up to now excluding "function xs ( xx : float ): float" (this is not the requested one-liner, sorry) that are not a symmetrical triangle wave // m - abs(i % (2*m) - m); (this is still a SAWTOOTH wave and not a TRIANGLE wave) // abs((x++ % 6) - 3); (this is still a SAWTOOTH wave and not a TRIANGLE wave) // abs(amplitude - x % (2*amplitude)); (this is still a SAWTOOTH wave and not a TRIANGLE wave) 

=> I found a source that determines exactly what the answer is in mathematical notation: http://mathworld.wolfram.com/TriangleWave.html

I tested the formula in a Linux program called KmPlot. Linux users can get kmplot through the root terminal by typing apt-get install kmplot, and if that doesn't work, try using a regular terminal and type sudo apt-get install kmplot, and if that doesn't work, watch this YouTube video for general installation instructions Linux software http://www.youtube.com/watch?v=IkhcwxC0oUg

SO A CORRECT ANSWER on the thread question is an example of declaring a symmetric triangular function in C ++ form, shown below:

(...)

 int symetrictriangle(float x) { unsigned int period = 30; // number of increases of x before a new cycle begins unsigned int amplitude = 100; // top to bottom value while the bottom value is always zero return amplitude * 2 * abs(round(x/period)-(x/period)); } 

(...)

Cheerz!

-one
Jan 17 '14 at 23:23
source share



All Articles