有没有一种简单的方法来计算“平滑” C/C++ 中具有以下特征的函数?
为了首先指定一些事情:用户应该能够通过在 2D 字段上指定 3 到 5 个点来创建图形。 第一个和最后一个点始终位于该字段的边界处(它们的位置只能在 y 方向上更改,而不能在 x 方向上更改)。这些位置处的图的导数应为 0。 第3点以后的点的位置可以自由指定。 应该对图表进行插值,该图表遍历所有点。然而,该图应该尽可能平滑和平坦。 (请为数学上不正确而道歉)
重要的是:我需要随后对该图的值进行采样并将它们应用于离散信号。第二件事:在 x 轴范围内,函数的值不应超过 y 轴上的边界。在我的图片中,y 轴上为 0 和 1。 我创建了一些图片来用 3 个点来说明我正在谈论的内容。
我的一些想法:
- 使用(三次?)样条曲线:可以应用它们的特性来形成此类曲线,而不会出现太多问题。然而,据我所知,它们与全局 x 轴无关。它们是通过一个通常称为 (s) 的参数与下一个点相关地指定的。因此,很难对与 x 轴相关的图表的值进行采样。当我错了的时候请纠正我。
- 创建一个矩阵,其中包含点和这些点的导数,并使用 LU 分解或等效方法求解该矩阵。
到目前为止,我对这些技术还没有深入的了解,所以我可能会错过一些我还不知道的很棒的技术或算法。
还有一件事,如果能够做到的话那就太好了:能够通过改变一个或几个参数来调整曲线的陡度。我在一些图片中使用红色和黑色图表来说明这一点。 有什么想法或提示如何有效地解决这个问题吗?
In order to specify some things first: The user should be able to create a graph by specifying 3 to 5 points on a 2D field.
The first and the last points are always at the bounds of that field (their position may only be changed in y direction - not x). The derivation of the graph at these positions should be 0.
The position of the 3rd and following points may be specified freely.
A graph should be interpolated, which goes through all the points. However, this graph should be as smooth and flat as possible. (please apologize for not being mathematically correct)
The important thing: I need to sample values of that graph afterwards and apply them to a discrete signal. Second thing: Within the range of the x-Axis the values of the function should not exceed the boundaries on the y-Axis.. In my pics that would be 0 and 1 on the y-Axis.
I created some pics to illustrate what I am talking about using 3 points.
Some thoughts I had:
- Use (cubic?) splines: their characteristics could be applied to form such curves without too many problems. However, as far as I know, they don't relate to a global x-Axis. They are specified in relation to the next point, through a parameter usually called (s). Therefore it will be difficult to sample the values of the graph related to the x-Axis. Please correct me when I am wrong.
- create a matrix, which contains the points and the derivations at those points and solve that matrix using LU decomposition or something equivalent.
So far, I don't have in depth understanding of these techniques, so I might miss some great technique or algorithm I haven't known about yet.
There is one more thing, that would be great to be able to do: Being able to adjust the steepness of the curve via the change of one or a few parameters. I illustrated this by using a red and a black graph in some of my pictures.
Any ideas or hints how to solve that efficiently?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您了解样条曲线是如何得出的吗?
进行样条曲线的总结
您根据控制点将范围分成几部分(在控制点处分割或在它们之间放置中断),并将一些参数化函数放入每个子范围中,然后通过控制点人为地约束函数引入了端点约束和段间约束。
如果您正确计算了自由度和约束,您将得到一个可解的方程组,它会告诉您控制点方面的正确参数,然后您就可以开始了。
结果是分段函数的一组参数。通常是分段连续且可微的函数,否则还有什么意义。
在这种情况下如何使用它
因此,请考虑将每个内部点作为线段的中心,该线段将被类峰函数(可能是线性背景上的高斯函数)占据,并使用端点作为约束。
对于
n
总点,如果每个段都有D
参数,则您将拥有D*(n-2)
参数。您有四个端点约束f(start)=y_0
、f(end)=y_n
、f'(start) = f'(end) = 0)
,以及段之间的一些匹配约束:加上每个段都受到其与控制点的关系的约束(通常是
f(point n) = y_n
或f'(point n) = 0 或两者(但您可以决定)
有多少个匹配约束取决于自由度的数量(约束总数必须等于自由度总数,对吧) 。 ?)。您必须以
f''(start) = 0
的形式引入一些额外的端点约束...才能得到正确的结果。繁琐的代数如何将其转化为一个大的线性方程组,您可以通过矩阵求逆来求解该方程组,
大多数数值方法书籍都会涵盖这些内容。
Do you understand how splines are arrived at?
Summary of doing splines
You break the range into pieces based on the control points (splitting at the control points or putting the breaks between them), and plop some parameterized function into each sub-range, then constrain the functions by the control points, artificially introduced end-point constraints, and inter-segment constrains.
If you've counted your degrees of freedom and constraints right, you get a solvable system of equations which tells you the right parameters in terms of the control points and away you go.
The result is a set of parameters for a piecewise function. Generally a piecewise continuous and differentiable function, because what would be the point otherwise.
How you can use that in this case
So consider making each interior point the center of a segment which will be occupied by a peak-like function (Gaussian on a linear background, maybe) and use the end points as constraints.
For
n
total points you'd haveD*(n-2)
parameters if each segment hasD
parameters. You have four end-point constraintsf(start)=y_0
,f(end)=y_n
,f'(start) = f'(end) = 0)
, and some set of match constraints between the segments:plus each segment is constrained by it's relationship to the control point (usually either
f(point n) = y_n
orf'(point n) = 0
or both (but you get to decide).How many matching constraints you can have depends on the number of degrees of freedom (total number of constraints must equal total number of DoF, right?). You have have to introduce some extra endpoint constraints in the form
f''(start) = 0
... to get it right.At that point you're just looking at a lot of tedious algebra to earn how to translate this into a big system of linear equation which you can solve with a matrix inversion.
Most numeric methods books will cover this stuff.
没有理由不能使用样条曲线。如果您有一个处理增量的公式/库,只需使用所需的点数从图形的起点到终点即可。
如果您想要更好地控制导数在控制点处的行为,您可以编写自己的分段多项式来满足函数及其导数的连续性条件。这些条件最终形成线性方程组的方程,然后您将按照您的指示使用矩阵方法求解该方程。这是将端点的导数钳位为零的一种方法。
允许您扩大或缩小插值函数中的峰值的自由度更加模糊。一种可能性是通过其在高阶导数中的属性或通过其与一阶(线性)插值器的偏差来对一组其他未确定的方程进行排序。
祝你好运!
There is no reason you cannot use splines. If you have a formulation/library for that that deals with increments, just step from the starting point to the end point of your graph with the desired number of points.
If you want more control over the behavior of the derivative at the control points, you can compose your own piece-wise polynomial that satisfy the conditions of continuity in the function and its derivative. These conditions end up as equations for a linear system of equations that you will then solve with matrix methods as you indicate. This is one way to clamp the derivative at zero for the endpoints.
The degree of freedom that allows you to broaden or narrow the peaks in the interpolated function are more ambiguous. One possibility is to order an otherwise under-determined set of equations by its properties in higher order derivatives, or by its deviation from the first order (linear) interpolator.
Good luck!
为什么不使用众所周知的解决方案来解决您的问题呢?
了解线性回归和多项式回归。
这些是您正在寻找的算法。
更新:多项式一将符合您的兴趣
Why not use well-known solutions to your problem?
Read about linear regression and polynomial regression.
These are the algorithms you're looking for.
UPDATE: polynomial one would be of your interests