Zephyr Scientific Library (zscilib)
Functions
Functions

Functions

int zsl_interp_lerp (zsl_real_t v0, zsl_real_t v1, zsl_real_t t, zsl_real_t *v)
 Calculates a number between two numbers using linear interpolation, where 't' is the interpolation factor between 0.0f and 1.0f. More...
 
int zsl_interp_find_x (struct zsl_interp_xy xy[], size_t n, zsl_real_t x, int *idx)
 Uses bisection to search the zsl_interp_xy array for the closest array position to 'x', assigning the matching index value to the 'idx' pointer. The array must be monotonic, but can be either increasing or decreasing between xy[0] and xy[n]. More...
 
int zsl_interp_nn (struct zsl_interp_xy *xy1, struct zsl_interp_xy *xy3, zsl_real_t x2, zsl_real_t *y2)
 Nearest neighbour (AKA 'piecewise constant') interpolation based on zsl_real_ts. More...
 
int zsl_interp_nn_arr (struct zsl_interp_xy xy[], size_t n, zsl_real_t x, zsl_real_t *y)
 Nearest neighbour (AKA 'piecewise constant') interpolation based on an array of zsl_real_ts. More...
 
int zsl_interp_lin_y (struct zsl_interp_xy *xy1, struct zsl_interp_xy *xy3, zsl_real_t x2, zsl_real_t *y2)
 Linear (AKA 'piecewise linear') interpolation for Y between two points, based on zsl_real_ts. More...
 
int zsl_interp_lin_y_arr (struct zsl_interp_xy xy[], size_t n, zsl_real_t x, zsl_real_t *y)
 Linear (AKA 'piecewise linear') interpolation for Y between two points, based on zsl_real_ts. More...
 
int zsl_interp_lin_x (struct zsl_interp_xy *xy1, struct zsl_interp_xy *xy3, zsl_real_t y2, zsl_real_t *x2)
 Linear (AKA 'piecewise linear') interpolation for X between two points, based on zsl_real_ts. More...
 
int zsl_interp_cubic_calc (struct zsl_interp_xyc xyc[], size_t n, zsl_real_t yp1, zsl_real_t ypn)
 Calculates xyc[n].y2 for natural cubic spline interpolation, based on the assigned xyc[n].x and xyc[n].y values. More...
 
int zsl_interp_cubic_arr (struct zsl_interp_xyc xyc[], size_t n, zsl_real_t x, zsl_real_t *y)
 Natural cubic spline interpolation between two points, based on zsl_real_ts. More...
 

Detailed Description

Helper functions to interpolate between known values.

Function Documentation

◆ zsl_interp_cubic_arr()

int zsl_interp_cubic_arr ( struct zsl_interp_xyc  xyc[],
size_t  n,
zsl_real_t  x,
zsl_real_t y 
)

Natural cubic spline interpolation between two points, based on zsl_real_ts.

Parameters
xycThe array of X,Y,Y2 values to use when interpolating (min four!).
nThe number of elements in the X,Y,Y2 array.
xThe X value to interpolate for (x >= xyc[1].x, <= xyc[n-3].x).
yPointer to the placeholder for the interpolated Y value.
Returns
0 on success, error code on error.

Definition at line 322 of file interp.c.

◆ zsl_interp_cubic_calc()

int zsl_interp_cubic_calc ( struct zsl_interp_xyc  xyc[],
size_t  n,
zsl_real_t  yp1,
zsl_real_t  ypn 
)

Calculates xyc[n].y2 for natural cubic spline interpolation, based on the assigned xyc[n].x and xyc[n].y values.

Parameters
xycThe array of X,Y,Y2 values to use when interpolating (min four!).
nThe number of elements in the X,Y,Y2 array.
yp11st derivative at 1. Set to >= 1e30 for natural spline.
ypn1st derivative at n'th point. Set to >= 1e30 for natural spline.

NOTE: This function must be called BEFORE using zsl_interp_cubic_arr.

Definition at line 254 of file interp.c.

◆ zsl_interp_find_x()

int zsl_interp_find_x ( struct zsl_interp_xy  xy[],
size_t  n,
zsl_real_t  x,
int *  idx 
)

Uses bisection to search the zsl_interp_xy array for the closest array position to 'x', assigning the matching index value to the 'idx' pointer. The array must be monotonic, but can be either increasing or decreasing between xy[0] and xy[n].

Parameters
xyThe array of zsl_real_t-based X,Y values to search.
nThe number of elements in the X,Y array.
xThe x value to search for.
idxPointer to the placeholder for the position of x in the X,Y array.
Returns
0 on success, error code on error.

Definition at line 33 of file interp.c.

Referenced by zsl_interp_lin_y_arr(), and zsl_interp_nn_arr().

◆ zsl_interp_lerp()

int zsl_interp_lerp ( zsl_real_t  v0,
zsl_real_t  v1,
zsl_real_t  t,
zsl_real_t v 
)

Calculates a number between two numbers using linear interpolation, where 't' is the interpolation factor between 0.0f and 1.0f.

Parameters
v0The lower value used when interpolating.
v1The upper value used when interpolating.
tThe interpolation factor between 0.0f and 1.0f.
vPointer to the placeholder for the interpolated value.
Returns
0 on success, error code on error.

Notes

This can be used for alpha-blending or easing of motion, amongst other things.

For example, you can use zsl_interp_lerp to move an object between two points in 5% increments, creating a smooth animation that slows down as you approach the second point. For each 'step' between the two points you can calculate the new position as follows:

x = y = 0.0;
// Increment x 5% towards 1.0 (new value = 0.05)
zsl_interp_lerp(x, 1.0, 0.05, &x);
// Increment y 5% towards 1.5 (new value = 0.075)
izsl_nterp_lerp(y, 1.5, 0.05, &y);

Definition at line 14 of file interp.c.

◆ zsl_interp_lin_x()

int zsl_interp_lin_x ( struct zsl_interp_xy xy1,
struct zsl_interp_xy xy3,
zsl_real_t  y2,
zsl_real_t x2 
)

Linear (AKA 'piecewise linear') interpolation for X between two points, based on zsl_real_ts.

Parameters
xy1The lower XY pair used when interpolating.
xy3The upper XY pair used when interpolating.
y2The Y value to interpolate for (>= y1 && <= y3).
x2Pointer to the placeholder for the interpolated X value.
Returns
0 on success, error code on error.

Definition at line 216 of file interp.c.

◆ zsl_interp_lin_y()

int zsl_interp_lin_y ( struct zsl_interp_xy xy1,
struct zsl_interp_xy xy3,
zsl_real_t  x2,
zsl_real_t y2 
)

Linear (AKA 'piecewise linear') interpolation for Y between two points, based on zsl_real_ts.

Parameters
xy1The lower XY pair used when interpolating.
xy3The upper XY pair used when interpolating.
x2The X value to interpolate for (>= x1 && <= x3).
y2Pointer to the placeholder for the interpolated Y value.
Returns
0 on success, error code on error.

Definition at line 155 of file interp.c.

Referenced by zsl_interp_lin_y_arr().

◆ zsl_interp_lin_y_arr()

int zsl_interp_lin_y_arr ( struct zsl_interp_xy  xy[],
size_t  n,
zsl_real_t  x,
zsl_real_t y 
)

Linear (AKA 'piecewise linear') interpolation for Y between two points, based on zsl_real_ts.

Parameters
xyThe array of XY pairs tp use when interpolating (min two!).
nThe number of elements in the XY array.
xThe X value to interpolate for (x >= xy[0].x, <= xy[n-1].x).
yPointer to the placeholder for the interpolated Y value.
Returns
0 on success, error code on error.

Definition at line 190 of file interp.c.

◆ zsl_interp_nn()

int zsl_interp_nn ( struct zsl_interp_xy xy1,
struct zsl_interp_xy xy3,
zsl_real_t  x2,
zsl_real_t y2 
)

Nearest neighbour (AKA 'piecewise constant') interpolation based on zsl_real_ts.

Parameters
xy1The lower XY pair used when interpolating.
xy3The upper XY pair used when interpolating.
x2The X value to interpolate for (>= x1 && <= x3).
y2Pointer to the placeholder for the interpolated Y value.
Returns
0 on success, error code on error.

Definition at line 100 of file interp.c.

Referenced by zsl_interp_nn_arr().

◆ zsl_interp_nn_arr()

int zsl_interp_nn_arr ( struct zsl_interp_xy  xy[],
size_t  n,
zsl_real_t  x,
zsl_real_t y 
)

Nearest neighbour (AKA 'piecewise constant') interpolation based on an array of zsl_real_ts.

Parameters
xyThe array of XY pairs tp use when interpolating (min two!).
nThe number of elements in the XY array.
xThe X value to interpolate for (x >= xy[0].x, <= xy[n-1].x).
yPointer to the placeholder for the interpolated Y value.
Returns
0 on success, error code on error.

Definition at line 129 of file interp.c.

zsl_interp_lerp
int zsl_interp_lerp(zsl_real_t v0, zsl_real_t v1, zsl_real_t t, zsl_real_t *v)
Calculates a number between two numbers using linear interpolation, where 't' is the interpolation fa...
Definition: interp.c:14
zsl_real_t
double zsl_real_t
Definition: zsl.h:51