问题
I'm writing a c function to generate trapezoidal wave. Does any one know a mathematical equation to generate trapezoidal wave? Very similar idea as y=A*sin(B*x) generates a sin wave for different values of x.
回答1:
A way of sending a single pulse of trapezoidal wave includes using the Heaviside Step Function http://en.wikipedia.org/wiki/Heaviside_step_function
Use it if you want the "pure" mathematical way of representing this kind of function. Just build your function "piece by piece", multiply the first piece by a heaviside that "activates" when x = the beginning of your pulse. For the next piece, first subtract the last function, then add the new piece mathematical function, multiply it by the adequate heaviside function and so on. It should end something like this (if you don't get it, go through the wikipedia article):
H(n) := (x >= n)?1:0;
y := H(0)*(x) + H(1)*(-x + 1) + H(2)*(-(-x + 1) + (3-x));
However, for code simplicity and efficiency, lets use an if statement. Consider a 45-degree trapezoidal wave, with constant unitary velocity.
float trapezoidalWave(float x, float t) {
float y;
if ( x <= t + 1 ) {
// 45 degree ascending line
y = x - t;
} else if ( x <= t + 2) {
// horizontal line
y = 1;
} else if (x <= t + 3) {
// 45 degree descending line
y = t + 3 - x;
} else {
y = 0;
}
return y;
}
If you want a "long wave" and not just one pulse, work with modules (%), if you don't need the time variable, just replace it by 0.
回答2:
There's an equation you can use instead of restrictions.
a/pi(arcsin(sin((pi/m)x+l))+arccos(cos((pi/m)x+l)))-a/2+c
a
is the amplitudem
is the periodl
is the horizontal transitionc
is the vertical transition
Plus this is a direct trigonometric function, even though it could be longer,and a little more complicated.
来源:https://stackoverflow.com/questions/11041498/equation-for-trapezoidal-wave-equation