有单线函数产生三角波吗?

时间:2021-12-17 23:59:17

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:

生成一个序列0 . .9、3次,看起来是这样的:

有单线函数产生三角波吗?

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

在这种情况下,一行代码是x = i++ % m


What I want is this:

我想要的是:

有单线函数产生三角波吗?

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

如果你知道其他波形的一行程序(sin, square),那就更好了。

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:

在三角波函数中加入什么可以使曲线的斜率像这样

有单线函数产生三角波吗?

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.

谢谢大家,你们各种各样的回答帮助我从更大的角度看问题。特别感谢Noldorin将方程扩展为二次曲线。

8 个解决方案

#1


22  

x = m - abs(i % (2*m) - m)

#2


84  

Triangular Wave

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

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

这是周期为6的三角波,在3和0之间振荡。

Square Wave

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

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

这就给出了周期为6的正方波,在3和0之间震荡。

Sine Wave

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

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

得到周期为20的正弦波,在3和-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):

凹曲线形状(即x ^ 2):

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

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

凹曲线(即sqrt(x)):

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.

或者使用pow函数,您可以简单地定义一个平方函数,并在数学中使用sqrt函数。h,这可能会稍微提高性能。

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

在所有这些情况下,您应该能够轻松地调整常数,并在正确的位置添加缩放因子,以给出给定波形的变化(不同的周期、ampltiudes、不对称等等)。

#3


21  

Expanding on Eric Bainville's answer:

扩展Eric Bainville的回答:

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

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

其中x是一个正在运行的整数,y是三角波的输出。A是波的振幅,P是半周期。例如,A=5会产生一个从0到5的波;P=10将产生周期为20的波。x=0时,波从y=0开始。

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

注意y是浮点数,除非P是a的因数,对数学纯粹主义者来说,a是波的振幅的两倍,但是看看下面的图你就明白我的意思了。

Visualised:

视觉效果:

有单线函数产生三角波吗?

#4


5  

y = abs( amplitude - x % (2*amplitude) )

Changing the wavelength just needs a factor for x.

改变波长只需要x的因数。

Edit: What I call amplitude is actually not the amplitude, but the maximum value (i.e. 5 if the curve oscillates betwen 0 and 5). The amplitude in the mathematical sense is half of that. But you get the point.

编辑:我所称的振幅实际上不是振幅,而是最大值(即如果曲线在0和5之间振荡)。在数学意义上的振幅是它的一半。但你懂的。

#5


5  

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

我知道这是一个古老的帖子,但对于任何正在寻找类似的东西的人,我推荐你看看。http://en.wikipedia.org/wiki/Triangle_wave

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

最后的公式y(x)=(2 /π)arcsin罪(((2π/ p)* x))

or.

或。

(2 * amplitudeConstant / Math.PI) * Math.Asin(Math.Sin(2 * (Math.PI / periodConstant) * Convert.ToDouble(variableX)))

#6


0  

Try this:

试试这个:

x = m - abs(m - 2*(i++ % m))

#7


0  

Here is a periodic function that looks like a distant sine approximation; essentially it's a Sinuating paraboloid, using X squared:

这是一个周期函数它看起来像一个远正弦逼近;本质上它是一个旋转的抛物面,用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;

}

#8


-1  

I've tested it with a simple loop and you guys haven't answered the man's question at all. Cut and pasting won't help people. No wonder so many mass media hoaxes have so much success. With a folks that repeats mistakes of others that cannot be any surprise is it. And people even give positive reputation rates for these wrong answers?! Unbelievable! But again, this parallels with my previous remark.

我用一个简单的循环测试过,你们根本没有回答这个人的问题。剪切和粘贴不会帮助人们。难怪如此多的大众媒体骗局如此成功。对于一个重复别人错误的人来说,这一点也不奇怪。人们甚至对这些错误的答案给出了积极的评价?难以置信!再一次,这与我之前的评论有相似之处。

So first off we are going to explain to our folks what a TRIANGLE wave IS. Well it is a wave which has a period consisting of TWO equal sloped ramps. A ramp sloped UP and a ramp equally as the first but sloped DOWN contrary to a SAWTOOTH which has a ramp sloped UP or a ramp sloped DOWN repeated after a reversed talud.

首先我们要向大家解释三角波是什么。它是一个波,它的周期由两个相等的斜坡坡度组成。斜坡向上倾斜,斜坡与第一个斜坡一样向下倾斜,与锯齿相反,锯齿是斜坡向上倾斜的,斜坡向下倾斜,或者斜坡向下倾斜。

PS: The last one who gave "y(x)=(2a/π)arcsin(sin((2π/p)*x))" is too complicated, we are looking for a fast c++ routine so trigonometry is absolutely out of the question.

PS:最后一个谁给“y(x)=(2 /π)arcsin罪(((2π/ p)* x))“太复杂,我们正在寻找一个快速c++程序所以三角学是绝对不可能的。

Test routine:

测试程序:

(...)

(…)

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 which tells exactly what the answer is in mathematical notation: http://mathworld.wolfram.com/TriangleWave.html

=>我找到了一个用数学符号精确地告诉答案的来源:http://mathworld.wolfram.com/TriangleWave.html

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

我在一个名为KmPlot的Linux程序上测试了这个公式。Linux用户可以通过根终端输入ap -get install kmplot获得kmplot,如果不行,请尝试使用常规终端并键入sudo ap -get install kmplot,如果不行,请观看youtube视频,了解关于安装Linux程序的一般说明http://www.youtube.com/watchv= ikhcwxc0oug

SO A CORRECT ANSWER to the thread question is an example of a symmetrical triangle function declaration in c++ form shown below:

因此,正确的线程问题的答案是一个对称三角形函数声明的例子,用c++形式表示如下:

(...)

(…)

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!

Cheerz !

#1


22  

x = m - abs(i % (2*m) - m)

#2


84  

Triangular Wave

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

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

这是周期为6的三角波,在3和0之间振荡。

Square Wave

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

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

这就给出了周期为6的正方波,在3和0之间震荡。

Sine Wave

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

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

得到周期为20的正弦波,在3和-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):

凹曲线形状(即x ^ 2):

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

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

凹曲线(即sqrt(x)):

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.

或者使用pow函数,您可以简单地定义一个平方函数,并在数学中使用sqrt函数。h,这可能会稍微提高性能。

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

在所有这些情况下,您应该能够轻松地调整常数,并在正确的位置添加缩放因子,以给出给定波形的变化(不同的周期、ampltiudes、不对称等等)。

#3


21  

Expanding on Eric Bainville's answer:

扩展Eric Bainville的回答:

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

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

其中x是一个正在运行的整数,y是三角波的输出。A是波的振幅,P是半周期。例如,A=5会产生一个从0到5的波;P=10将产生周期为20的波。x=0时,波从y=0开始。

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

注意y是浮点数,除非P是a的因数,对数学纯粹主义者来说,a是波的振幅的两倍,但是看看下面的图你就明白我的意思了。

Visualised:

视觉效果:

有单线函数产生三角波吗?

#4


5  

y = abs( amplitude - x % (2*amplitude) )

Changing the wavelength just needs a factor for x.

改变波长只需要x的因数。

Edit: What I call amplitude is actually not the amplitude, but the maximum value (i.e. 5 if the curve oscillates betwen 0 and 5). The amplitude in the mathematical sense is half of that. But you get the point.

编辑:我所称的振幅实际上不是振幅,而是最大值(即如果曲线在0和5之间振荡)。在数学意义上的振幅是它的一半。但你懂的。

#5


5  

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

我知道这是一个古老的帖子,但对于任何正在寻找类似的东西的人,我推荐你看看。http://en.wikipedia.org/wiki/Triangle_wave

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

最后的公式y(x)=(2 /π)arcsin罪(((2π/ p)* x))

or.

或。

(2 * amplitudeConstant / Math.PI) * Math.Asin(Math.Sin(2 * (Math.PI / periodConstant) * Convert.ToDouble(variableX)))

#6


0  

Try this:

试试这个:

x = m - abs(m - 2*(i++ % m))

#7


0  

Here is a periodic function that looks like a distant sine approximation; essentially it's a Sinuating paraboloid, using X squared:

这是一个周期函数它看起来像一个远正弦逼近;本质上它是一个旋转的抛物面,用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;

}

#8


-1  

I've tested it with a simple loop and you guys haven't answered the man's question at all. Cut and pasting won't help people. No wonder so many mass media hoaxes have so much success. With a folks that repeats mistakes of others that cannot be any surprise is it. And people even give positive reputation rates for these wrong answers?! Unbelievable! But again, this parallels with my previous remark.

我用一个简单的循环测试过,你们根本没有回答这个人的问题。剪切和粘贴不会帮助人们。难怪如此多的大众媒体骗局如此成功。对于一个重复别人错误的人来说,这一点也不奇怪。人们甚至对这些错误的答案给出了积极的评价?难以置信!再一次,这与我之前的评论有相似之处。

So first off we are going to explain to our folks what a TRIANGLE wave IS. Well it is a wave which has a period consisting of TWO equal sloped ramps. A ramp sloped UP and a ramp equally as the first but sloped DOWN contrary to a SAWTOOTH which has a ramp sloped UP or a ramp sloped DOWN repeated after a reversed talud.

首先我们要向大家解释三角波是什么。它是一个波,它的周期由两个相等的斜坡坡度组成。斜坡向上倾斜,斜坡与第一个斜坡一样向下倾斜,与锯齿相反,锯齿是斜坡向上倾斜的,斜坡向下倾斜,或者斜坡向下倾斜。

PS: The last one who gave "y(x)=(2a/π)arcsin(sin((2π/p)*x))" is too complicated, we are looking for a fast c++ routine so trigonometry is absolutely out of the question.

PS:最后一个谁给“y(x)=(2 /π)arcsin罪(((2π/ p)* x))“太复杂,我们正在寻找一个快速c++程序所以三角学是绝对不可能的。

Test routine:

测试程序:

(...)

(…)

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 which tells exactly what the answer is in mathematical notation: http://mathworld.wolfram.com/TriangleWave.html

=>我找到了一个用数学符号精确地告诉答案的来源:http://mathworld.wolfram.com/TriangleWave.html

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

我在一个名为KmPlot的Linux程序上测试了这个公式。Linux用户可以通过根终端输入ap -get install kmplot获得kmplot,如果不行,请尝试使用常规终端并键入sudo ap -get install kmplot,如果不行,请观看youtube视频,了解关于安装Linux程序的一般说明http://www.youtube.com/watchv= ikhcwxc0oug

SO A CORRECT ANSWER to the thread question is an example of a symmetrical triangle function declaration in c++ form shown below:

因此,正确的线程问题的答案是一个对称三角形函数声明的例子,用c++形式表示如下:

(...)

(…)

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!

Cheerz !