Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a simple way to compute a "smooth" function with the following characteristics in C/C++?

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:

  1. 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.
  2. 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?

alt textalt textalt textalt text

like image 427
st-h Avatar asked Dec 16 '10 23:12

st-h


People also ask

What is a smooth function example?

For example, a smooth function of class C2 has both a first derivative and a second derivative. If all derivatives exist, the function is called infinitely smooth or infinitely differentiable. The derivatives are continuous. In other words, its derivative is a continuous function.

How do you determine if a function is smooth?

Smooth functions have a unique defined first derivative (slope or gradient) at every point. Graphically, a smooth function of a single variable can be plotted as a single continuous line with no abrupt bends or breaks.

How do you determine the smoothness of a curve?

Continuity. The terms parametric continuity (Ck) and geometric continuity (Gn) were introduced by Brian Barsky, to show that the smoothness of a curve could be measured by removing restrictions on the speed, with which the parameter traces out the curve.

What does it mean if a function is smooth?

A smooth function is a function that has continuous derivatives up to some desired order over some domain.


2 Answers

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 have D*(n-2) parameters if each segment has D parameters. You have four end-point constraints f(start)=y_0, f(end)=y_n, f'(start) = f'(end) = 0), and some set of match constraints between the segments:

f_n(between n and n+1) = f_n+1(between n and n+1)
f'_n(between n and n+1) = f'_n+1(between n and n+1)
...

plus each segment is constrained by it's relationship to the control point (usually either f(point n) = y_n or f'(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.

like image 159
dmckee --- ex-moderator kitten Avatar answered Sep 25 '22 20:09

dmckee --- ex-moderator kitten


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!

like image 24
SEngstrom Avatar answered Sep 22 '22 20:09

SEngstrom