foresight_smooth
foresight_smooth, gnuplot
smoothfilters of a data series.
The filters gnuplot computes from the data alone, without a fit: each run of placeable points (a block of the data file, broken by undefined points and by points not placeable on a log axis) is sorted by x, stably, and its points of equal x are merged into one:
unique: the mean of their y;frequency: the sum of their y;fnormaldivided by the sum of y over every run;cumulative: the sum of y up to that x within the run;cnormaldivided by the sum of y over every run.
Runs stay separate, joined by an undefined (NaN) point that breaks the line, as gnuplot draws them.
The curves (smooth_curve) are sampled at set samples points: the natural cubic spline (csplines), the smoothing spline of Reinsch (acsplines, weights from a third column), the Bezier curve of the points (bezier, in the file order; sbezier sorted and merged), the Gaussian kernel density estimate (kdensity) and the monotone cubic spline of Fritsch and Carlson (mcsplines, at the points and between them), as gnuplot 6.0.
The bins (bin_values, gnuplot's bins filter) are the sums, or the means, of y over equal-width intervals of x.
Source: src/lib/foresight_smooth.F90
Dependencies
Contents
- bin_values
- smooth
- smooth_curve
- merge_equal
- path_spline
- spline_curve3d
- fill_coefficients
- natural_spline
- approximation_spline
- placeable
- sorted
Variables
| Name | Type | Attributes | Description |
|---|---|---|---|
SMOOTH_MODES | character(len=*) | parameter | Filters of smooth. |
CURVE_MODES | character(len=*) | parameter | Filters of smooth_curve. |
Subroutines
bin_values
Bin the series (x, y) as gnuplot's bins filter: nbins bins of equal width over [lo, hi] (the data's x extremes if lo equals hi), lo and hi the centres of the first and last bins; with a width > 0 instead, the fewest bins of that width that span the range, centred on it. A point falls in bin floor(n (x - xmin) / (xmax - xmin)), xmin and xmax the outer edges: on a boundary, in the upper bin; beyond them, in none. Each bin is one point at its centre: the sum of its y (average: their mean, for two or more). Points not placeable (NaN, or not positive on a log axis) are skipped. edges are xmin and xmax, which the x autoscale includes; message is empty on success.
Attributes: pure
subroutine bin_values(x, y, nbins, lo, hi, width, average, xlog, ylog, xs, ys, edges, message)Arguments
| Name | Type | Intent | Attributes | Description |
|---|---|---|---|---|
x | real(kind=R8P) | in | Abscissae. | |
y | real(kind=R8P) | in | Ordinates, the value each point adds to its bin. | |
nbins | integer(kind=I4P) | in | Number of bins, if width is not positive. | |
lo | real(kind=R8P) | in | Centre of the first bin (equal to hi: from the data). | |
hi | real(kind=R8P) | in | Centre of the last bin. | |
width | real(kind=R8P) | in | Bin width, if positive. | |
average | logical | in | The mean of y per bin, not the sum. | |
xlog | logical | in | Log x axis. | |
ylog | logical | in | Log y axis. | |
xs | real(kind=R8P) | out | allocatable | Bin centres. |
ys | real(kind=R8P) | out | allocatable | Bin values. |
edges | real(kind=R8P) | out | Outer edges of the bins. | |
message | character(len=:) | out | allocatable | Problem, empty if none. |
Call graph
smooth
Filter the series (x, y) with the smooth mode (one of SMOOTH_MODES) into (xs, ys); xlog, ylog tell the log axes, on which non-positive values are not placeable.
subroutine smooth(mode, x, y, xlog, ylog, xs, ys)Arguments
| Name | Type | Intent | Attributes | Description |
|---|---|---|---|---|
mode | character(len=*) | in | Filter. | |
x | real(kind=R8P) | in | Abscissae. | |
y | real(kind=R8P) | in | Ordinates. | |
xlog | logical | in | Log x axis. | |
ylog | logical | in | Log y axis. | |
xs | real(kind=R8P) | out | allocatable | Filtered abscissae. |
ys | real(kind=R8P) | out | allocatable | Filtered ordinates. |
Call graph
smooth_curve
Smooth the series (x, y) with the mode of CURVE_MODES, samples points per run, as gnuplot:
csplines: the natural cubic spline through the points sorted by x, those of equal x merged into their mean;acsplines: the smoothing (approximation) spline of Reinsch, the weightsw(larger: closer to the points);bezier: the Bezier curve with the points as control points, in the file order;sbezierafter sorting and merging them;kdensity: the Gaussian kernel density estimate of the x values weighted by y, over [lo,hi], ofbandwidth(gnuplot's default, (4/3n)^(1/5) times the standard deviation of x, if not positive);mcsplines: the monotone cubic spline of Fritsch and Carlson through the points sorted and merged, of the first run only (gnuplot 6.0 drops the others), sampled at the points and at max(samples, 2 n) points within [lo,hi] (seemcs_samples).
The splines are sampled over the run, within [lo, hi]; runs are joined by an undefined point. message is empty on success.
subroutine smooth_curve(mode, x, y, w, samples, lo, hi, bandwidth, xlog, ylog, xs, ys, message)Arguments
| Name | Type | Intent | Attributes | Description |
|---|---|---|---|---|
mode | character(len=*) | in | Filter. | |
x | real(kind=R8P) | in | Abscissae. | |
y | real(kind=R8P) | in | Ordinates. | |
w | real(kind=R8P) | in | Weights (acsplines). | |
samples | integer(kind=I4P) | in | Points per run. | |
lo | real(kind=R8P) | in | Smallest abscissa sampled. | |
hi | real(kind=R8P) | in | Largest abscissa sampled. | |
bandwidth | real(kind=R8P) | in | kdensity bandwidth, default if not positive. | |
xlog | logical | in | Log x axis. | |
ylog | logical | in | Log y axis. | |
xs | real(kind=R8P) | out | allocatable | Smoothed abscissae. |
ys | real(kind=R8P) | out | allocatable | Smoothed ordinates. |
message | character(len=:) | out | allocatable | Problem, empty if none. |
Call graph
merge_equal
Sort the points by x, stably, and merge those of equal x into one, the mean of their y and weights (gnuplot's preparation of the splines and of sbezier).
Attributes: pure
subroutine merge_equal(x, y, w)Arguments
| Name | Type | Intent | Attributes | Description |
|---|---|---|---|---|
x | real(kind=R8P) | inout | allocatable | Abscissae. |
y | real(kind=R8P) | inout | allocatable | Ordinates. |
w | real(kind=R8P) | inout | allocatable | Weights. |
Call graph
path_spline
gnuplot 6.0's smooth path (filters.c gen_2d_path_splines): each set of points (apart at undefined ones) a natural cubic spline of x and of y against the path length (scaled by xrange, yrange, normalized to 0..1), sampled samples times over the whole path and kept along the points, in their order; a set whose ends meet is closed (the spline wraps around), and closed (filledcurves closed) closes every set. Undefined points and repeated ones are dropped, a set of fewer than 3 points too; the sets apart at undefined points in xs, ys.
Attributes: pure
subroutine path_spline(x, y, samples, xrange, yrange, closed, xs, ys)Arguments
| Name | Type | Intent | Attributes | Description |
|---|---|---|---|---|
x | real(kind=R8P) | in | Abscissae. | |
y | real(kind=R8P) | in | Ordinates. | |
samples | integer(kind=I4P) | in | Samples of a whole path. | |
xrange | real(kind=R8P) | in | x scale of the path length. | |
yrange | real(kind=R8P) | in | y scale of the path length. | |
closed | logical | in | Close every set. | |
xs | real(kind=R8P) | out | allocatable | Curve abscissae. |
ys | real(kind=R8P) | out | allocatable | Curve ordinates. |
Call graph
spline_curve3d
A 3D curve smoothed as gnuplot 6.0's do_3d_cubic (smooth csplines|acsplines of splot): its unusable points (NaN) and repeated ones dropped; with more than 3 left, a curve in a plane of constant x is z of y (samples points from its first y to its last), of constant y z of x, of constant z y of x; else x, y and z each a spline of the path length (the steps over the axis ranges, normalized to 1). csplines natural cubic splines, acsplines the smoothing splines weighted by w. With 3 points or fewer the curve is kept as it is.
Attributes: pure
subroutine spline_curve3d(mode, x, y, z, w, samples, ranges, xs, ys, zs)Arguments
| Name | Type | Intent | Attributes | Description |
|---|---|---|---|---|
mode | character(len=*) | in | csplines or acsplines. | |
x | real(kind=R8P) | in | Abscissae. | |
y | real(kind=R8P) | in | Ordinates. | |
z | real(kind=R8P) | in | Heights. | |
w | real(kind=R8P) | in | Weights (acsplines). | |
samples | integer(kind=I4P) | in | Points of the smoothed curve. | |
ranges | real(kind=R8P) | in | x, y and z axis lengths (the path's scale). | |
xs | real(kind=R8P) | out | allocatable | Smoothed abscissae. |
ys | real(kind=R8P) | out | allocatable | Smoothed ordinates. |
zs | real(kind=R8P) | out | allocatable | Smoothed heights. |
Call graph
fill_coefficients
The first and third order coefficients of a cubic spline from its knot values v and half second derivatives c(2, 😃: c(1, i) the slope, c(3, i) a third of the change of c(2, 😃 per unit, on each interval.
Attributes: pure
subroutine fill_coefficients(x, v, c)Arguments
| Name | Type | Intent | Attributes | Description |
|---|---|---|---|---|
x | real(kind=R8P) | in | Knots. | |
v | real(kind=R8P) | in | Knot values. | |
c | real(kind=R8P) | inout | Coefficients. |
Call graph
Functions
natural_spline
Coefficients c(0:3, i) of the natural cubic spline through (x, y) (x increasing, at least 3 points) on each interval i, in powers of x - x(i): second derivative zero at both ends.
Attributes: pure
Returns: real(kind=R8P)
function natural_spline(x, y) result(c)Arguments
| Name | Type | Intent | Attributes | Description |
|---|---|---|---|---|
x | real(kind=R8P) | in | Knots. | |
y | real(kind=R8P) | in | Values. |
Call graph
approximation_spline
Coefficients c(0:3, i) of the smoothing spline of Reinsch through (x, y) with weights w (x increasing, at least 4 points), as gnuplot's acsplines: natural, its values at the knots c(0, i) drawn towards y(i) by w(i).
Attributes: pure
Returns: real(kind=R8P)
function approximation_spline(x, y, w) result(c)Arguments
| Name | Type | Intent | Attributes | Description |
|---|---|---|---|---|
x | real(kind=R8P) | in | Knots. | |
y | real(kind=R8P) | in | Values. | |
w | real(kind=R8P) | in | Weights, positive. |
Call graph
placeable
Whether v is placeable on an axis: finite, and positive on a log axis (as foresight_axis%accepts).
Attributes: elemental
Returns: logical
function placeable(v, log) result(ok)Arguments
| Name | Type | Intent | Attributes | Description |
|---|---|---|---|---|
v | real(kind=R8P) | in | Value. | |
log | logical | in | Log axis. |
Call graph
sorted
Permutation sorting keys ascending, stable (equal keys keep their order): bottom-up merge sort.
Attributes: pure
Returns: integer(kind=I4P)
function sorted(keys) result(order)Arguments
| Name | Type | Intent | Attributes | Description |
|---|---|---|---|---|
keys | real(kind=R8P) | in | Keys, no NaN. |
Call graph