IT TIP

삼각파를 생성하는 단선 함수가 있습니까?

itqueen 2020. 11. 29. 12:47
반응형

삼각파를 생성하는 단선 함수가 있습니까?


유사한 방식으로 모듈로 가 톱니파를 생성합니다. 연속적 일 필요는 없습니다.

내가 의미하는 바는 다음과 같습니다.

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

다음과 같이 세 번 시퀀스 0..9를 생성합니다.

톱니파 그래프

피크 오른쪽의 기울기는 그래프 아티팩트 일뿐입니다.

이 경우 한 줄짜리는 x = i ++ % m입니다.


내가 원하는 것은 다음과 같습니다.

삼각파 그래프

다른 파형 (사인, 정사각형)에 대해 한 줄짜리를 알고 있다면 그것도 알아두면 좋을 것입니다.

업데이트 : 모든 분들의 답변이 매우 도움이되었고 후속 질문이 있습니다.

선의 기울기를 다음과 같이 곡선으로 만들기 위해 삼각파 함수에 추가되는 것은 무엇입니까?

불룩한 파형

감사 합니다. 다양한 답변을 통해 더 큰 관점에서 문제를 볼 수있었습니다. 방정식을 2 차 곡선으로 확장 한 Noldorin에게 특별히 감사드립니다.


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

삼각파

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

이것은 3과 0 사이에서 진동하는주기 6의 삼각파를 제공합니다.

네모 난 파동

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

이것은주기 6의 규칙적인 구형파를 제공하며 3과 0 사이에서 진동합니다.

사인파

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

이것은 20 pi3과 -3 사이에서 진동 하는주기의 사인파를 제공합니다 .


최신 정보:

매력적인 삼각파

직선이 아닌 곡선을 가진 삼각파의 변형을 얻으려면 방정식에 지수를 도입하여 2 차로 만들면됩니다.

오목한 곡선 (예 : x^2모양) :

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

오목한 곡선 (예 : sqrt(x)모양) :

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

대안으로 사용하는 pow기능을, 당신은 간단하게 정의 할 수 있습니다 square기능과 사용 sqrt의 기능 math.h아마 성능을 약간 개선 할 것입니다.

또한 곡선을 더 가파르거나 얕게 만들고 싶다면 인덱스를 변경해보십시오.


이러한 모든 경우에 상수를 쉽게 조정하고 적절한 위치에 스케일링 계수를 추가하여 주어진 파형 (다른주기, 증폭, 비대칭 등)의 변형을 제공 할 수 있어야합니다.


Eric Bainville의 답변 확장 :

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

여기서 x는 실행중인 정수이고 y는 삼각파 출력입니다. A는 파동의 진폭이고 P는 반주기입니다. 예를 들어, A = 5는 0에서 5까지의 파동을 생성합니다. P = 10은주기가 20 인 파동을 생성합니다. 파동은 x = 0 인 경우 y = 0에서 시작합니다.

P가 A의 인수가 아닌 경우 y는 부동 소수점 숫자입니다. 그리고 예, 수학적 순수 주의자의 경우 : A는 기술적으로 파동 진폭의 두 배이지만 아래 그림을 보면 제가 무엇을 이해할 수 있는지 이해할 수 있습니다. 평균.

시각화 :


나는 이것이 오래된 게시물이라는 것을 알고 있지만 비슷한 것을 검색하는 사람에게는 내가 보는 것이 좋습니다. http://en.wikipedia.org/wiki/Triangle_wave

마지막 공식 y (x) = (2a / π) arcsin (sin ((2π / p) * x))

또는.

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

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

파장을 변경하려면 x.

편집 : 내가 진폭이라고 부르는 것은 실제로 진폭이 아니라 최대 값 (즉, 곡선이 0과 5 사이에서 진동하는 경우 5)입니다. 수학적 의미에서 진폭은 그 절반입니다. 그러나 당신은 요점을 얻습니다.


이 시도:

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

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

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;

}

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.

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

KmPlot이라는 Linux 프로그램에서 공식을 테스트했습니다. Linux 사용자는 루트 터미널을 통해 apt-get install kmplot을 입력하여 kmplot을 얻을 수 있으며 작동하지 않는 경우 일반 터미널을 사용하여 sudo apt-get install kmplot을 입력하고 작동하지 않으면이 youtube 비디오에서 일반적인 지침을 확인하십시오. Linux 프로그램 설치 http://www.youtube.com/watch?v=IkhcwxC0oUg

그래서 스레드 질문에 대한 올바른 대답은 아래에 표시된 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));
}

(...)

치어 즈!

참고 URL : https://stackoverflow.com/questions/1073606/is-there-a-one-line-function-that-generates-a-triangle-wave

반응형