HOME

TheInfoList



OR:

Smoothstep is a family of sigmoid-like
interpolation In the mathematical field of numerical analysis, interpolation is a type of estimation, a method of constructing (finding) new data points based on the range of a discrete set of known data points. In engineering and science, one often has a n ...
and
clamping A wheel clamp, also known as wheel boot, parking boot, or Denver boot, is a device that is designed to prevent motor vehicles from being moved. In its most common form, it consists of a clamp that surrounds a vehicle wheel, designed to preven ...
functions commonly used in
computer graphics Computer graphics deals with generating images with the aid of computers. Today, computer graphics is a core technology in digital photography, film, video games, cell phone and computer displays, and many specialized applications. A great de ...
,
video game engines Video is an electronic medium for the recording, copying, playback, broadcasting, and display of moving visual media. Video was first developed for mechanical television systems, which were quickly replaced by cathode-ray tube (CRT) syste ...
, and
machine learning Machine learning (ML) is a field of inquiry devoted to understanding and building methods that 'learn', that is, methods that leverage data to improve performance on some set of tasks. It is seen as a part of artificial intelligence. Machine ...
. The function depends on three parameters, the input ''x'', the "left edge" and the "right edge", with the left edge being assumed smaller than the right edge. The function receives a real number ''x'' as an argument and returns 0 if ''x'' is less than or equal to the left edge, 1 if x is greater than or equal to the right edge, and smoothly interpolates, using a
Hermite polynomial In mathematics, the Hermite polynomials are a classical orthogonal polynomial sequence. The polynomials arise in: * signal processing as Hermitian wavelets for wavelet transform analysis * probability, such as the Edgeworth series, as well as ...
, between 0 and 1 otherwise. The gradient of the smoothstep function is zero at both edges. This is convenient for creating a sequence of transitions using smoothstep to interpolate each segment as an alternative to using more sophisticated or expensive interpolation techniques. In
HLSL The High-Level Shader Language or High-Level Shading Language (HLSL) is a proprietary shading language developed by Microsoft for the Direct3D 9 API to augment the shader assembly language, and went on to become the required shading language f ...
and
GLSL OpenGL Shading Language (GLSL) is a high-level shading language with a syntax based on the C programming language. It was created by the OpenGL ARB (OpenGL Architecture Review Board) to give developers more direct control of the graphics pipeli ...
, smoothstep implements the \operatorname_1(x), the cubic
Hermite interpolation In numerical analysis, Hermite interpolation, named after Charles Hermite, is a method of polynomial interpolation, which generalizes Lagrange interpolation. Lagrange interpolation allows computing a polynomial of degree less than that takes the s ...
after
clamping A wheel clamp, also known as wheel boot, parking boot, or Denver boot, is a device that is designed to prevent motor vehicles from being moved. In its most common form, it consists of a clamp that surrounds a vehicle wheel, designed to preven ...
: : \operatorname(x) = S_1(x) = \begin 0 & x \le 0 \\ 3x^2 - 2x^3 & 0 \le x \le 1 \\ 1 & 1 \le x \\ \end Assuming that the left edge is 0, the right edge is 1, with the transition between edges taking place where 0 ≤ ''x'' ≤ 1. A C/C++ example implementation provided by AMD follows. float smoothstep (float edge0, float edge1, float x) The general form for ''smoothstep'', again assuming the left edge is 0 and right edge is 1, is : \operatorname_n(x) = \begin 0 & \text x \le 0 \\ x^ \sum_^ \binom \binom (-x)^ & \text 0 \le x \le 1 \\ 1 & \text 1 \le x \\ \end \operatorname_0(x) is identical to the clamping function: : \operatorname_0(x) = \begin 0 & \text x \le 0 \\ x & \text 0 \le x \le 1 \\ 1 & \text 1 \le x \\ \end The characteristic "S"-shaped sigmoid curve is obtained with \operatorname_n(x) only for integers ''n'' ≥ 1. The order of the polynomial in the general smoothstep is 2''n'' + 1. With ''n'' = 1, the slopes or first derivatives of the ''smoothstep'' are equal to zero at the left and right edge (''x'' = 0 and ''x'' = 1), where the curve is appended to the constant or
saturated Saturation, saturated, unsaturation or unsaturated may refer to: Chemistry * Saturation, a property of organic compounds referring to carbon-carbon bonds ** Saturated and unsaturated compounds **Degree of unsaturation ** Saturated fat or fatty ac ...
levels. With higher integer ''n'', the second and higher derivatives are zero at the edges, making the polynomial functions as flat as possible and the splice to the limit values of 0 or 1 more seamless.


Variations

Ken Perlin Kenneth H. Perlin is a professor in the Department of Computer Science at New York University, founding director of the Media Research Lab at NYU, director of the Future Reality Lab at NYU, and the Director of the Games for Learning Institute. He ...
suggested an improved version of the commonly used first-order smoothstep function, equivalent to the second order of its general form. It has zero 1st- and 2nd-order
derivative In mathematics, the derivative of a function of a real variable measures the sensitivity to change of the function value (output value) with respect to a change in its argument (input value). Derivatives are a fundamental tool of calculus. F ...
s at ''x'' = 0 and ''x'' = 1: : \operatorname(x) = S_2(x) = \begin 0 & x \le 0 \\ 6x^5 - 15x^4 + 10x^3 & 0 \le x \le 1 \\ 1 & 1 \le x \\ \end C/C++ reference implementation: float smootherstep(float edge0, float edge1, float x) float clamp(float x, float lowerlimit, float upperlimit)


Origin


3rd-order equation

Starting with a generic third-order
polynomial In mathematics, a polynomial is an expression consisting of indeterminates (also called variables) and coefficients, that involves only the operations of addition, subtraction, multiplication, and positive-integer powers of variables. An exa ...
function and its first
derivative In mathematics, the derivative of a function of a real variable measures the sensitivity to change of the function value (output value) with respect to a change in its argument (input value). Derivatives are a fundamental tool of calculus. F ...
: :\begin \operatorname_1(x) &&\; = \;&& a_3 x^3 &&\; + \;&& a_2 x^2 &&\; + \;&& a_1 x &&\; + \;&& a_0, & \\ \operatorname_1'(x) &&\; = \;&& 3 a_3 x^2 &&\; + \;&& 2 a_2 x &&\; + \;&& a_1. & \end Applying the desired values for the function at both endpoints: :\begin \operatorname_1(0) &&\; = \;&& 0 \quad&& \Rightarrow &&\quad 0 \;&& + &&\; 0 \;&& + &&\; 0 \;&& + &&\; a_0 &&\; = \;&& 0, & \\ \operatorname_1(1) &&\; = \;&& 1 \quad&& \Rightarrow &&\quad a_3 \;&& + &&\; a_2 \;&& + &&\; a_1 \;&& + &&\; a_0 &&\; = \;&& 1. & \end Applying the desired values for the first derivative of the function at both endpoints: :\begin \operatorname_1'(0) &&\; = \;&& 0 \quad&& \Rightarrow &&\quad 0 \;&& + &&\; 0 \;&& + &&\; a_1 \;&& = \;&& 0, & \\ \operatorname_1'(1) &&\; = \;&& 0 \quad&& \Rightarrow &&\quad 3 a_3 \;&& + &&\; 2 a_2 \;&& + &&\; a_1 \;&& = \;&& 0. & \end Solving the system of 4 unknowns formed by the last 4 equations result in the values of the polynomial coefficients: :a_0 = 0, \quad a_1 = 0, \quad a_2 = 3, \quad a_3 = -2. This results in the third-order ''"smoothstep"'' function: :\operatorname_1(x) = -2x^3 + 3x^2.


5th-order equation

Starting with a generic fifth-order
polynomial In mathematics, a polynomial is an expression consisting of indeterminates (also called variables) and coefficients, that involves only the operations of addition, subtraction, multiplication, and positive-integer powers of variables. An exa ...
function, its first derivative and its second derivative: :\begin \operatorname_2(x) &&\; = \;&& a_5 x^5 &&\; + \;&& a_4 x^4 &&\; + \;&& a_3 x^3 &&\; + \;&& a_2 x^2 &&\; + \;&& a_1 x &&\; + \;&& a_0, & \\ \operatorname_2'(x) &&\; = \;&& 5 a_5 x^4 &&\; + \;&& 4 a_4 x^3 &&\; + \;&& 3 a_3 x^2 &&\; + \;&& 2 a_2 x &&\; + \;&& a_1, & \\ \operatorname_2''(x) &&\; = \;&& 20 a_5 x^3 &&\; + \;&& 12 a_4 x^2 &&\; + \;&& 6 a_3 x &&\; + \;&& 2 a_2. & \end Applying the desired values for the function at both endpoints: :\begin \operatorname_2(0) &&\; = \;&& 0 \;\;\;\;\;&& \Rightarrow &&\;\;\;\;\; 0 \;&& + &&\; 0 \;&& + &&\; 0 \;&& + &&\; 0 \;&& + &&\; 0 \;&& + &&\; a_0 &&\; = \;&& 0, & \\ \operatorname_2(1) &&\; = \;&& 1 \;\;\;\;\;&& \Rightarrow &&\;\;\;\;\; a_5 \;&& + &&\; a_4 \;&& + &&\; a_3 \;&& + &&\; a_2 \;&& + &&\; a_1 \;&& + &&\; a_0 &&\; = \;&& 1. & \end Applying the desired values for the first derivative of the function at both endpoints: :\begin \operatorname_2'(0) &&\; = \;&& 0 \;\;\;\;&& \Rightarrow &&\;\;\;\; 0 \;&& + &&\; 0 \;&& + &&\; 0 \;&& + &&\; 0 \;&& + &&\; a_1 \;&& = \;&& 0, & \\ \operatorname_2'(1) &&\; = \;&& 0 \;\;\;\;&& \Rightarrow &&\;\;\;\; 5 a_5 \;&& + &&\; 4 a_4 \;&& + &&\; 3 a_3 \;&& + &&\; 2 a_2 \;&& + &&\; a_1 \;&& = \;&& 0. & \end Applying the desired values for the second derivative of the function at both endpoints: :\begin \operatorname_2''(0) &&\; = \;&& 0 \;\;\;\;&& \Rightarrow &&\;\;\;\; 0 \;&& + &&\; 0 \;&& + &&\; 0 \;&& + &&\; 2 a_2 \;&& = \;&& 0, & \\ \operatorname_2''(1) &&\; = \;&& 0 \;\;\;\;&& \Rightarrow &&\;\;\;\; 20 a_5 \;&& + &&\; 12 a_4 \;&& + &&\; 6 a_3 \;&& + &&\; 2 a_2 \;&& = \;&& 0. & \end Solving the system of 6 unknowns formed by the last 6 equations result in the values of the polynomial coefficients: :a_0 = 0, \quad a_1 = 0 , \quad a_2 = 0, \quad a_3 = 10, \quad a_4 = -15, \quad a_5 = 6. This results in the fifth-order ''"smootherstep"'' function: :\operatorname_2(x) = 6x^5 - 15x^4 + 10x^3.


7th-order equation

Applying similar techniques, the 7th-order equation is found to be: :\operatorname_3(x) = -20x^7 + 70x^6 - 84x^5 + 35x^4.


Generalization to higher-order equations

Smoothstep polynomials are generalized, with 0 ≤ ''x'' ≤ 1 as :\begin \operatorname_N(x) &= x^ \sum_^ \binom \binom (-x)^ \qquad N \in \mathbb\\ &= \sum_^ (-1)^n \binom \binom x^ \\ &= \sum_^ \binom \binom x^, \\ \end where ''N'' determines the order of the resulting polynomial function, which is 2''N'' + 1. The first seven smoothstep polynomials, with 0 ≤ ''x'' ≤ 1, are :\begin \operatorname_0(x) &= x, \\ \operatorname_1(x) &= -2x^3 + 3x^2, \\ \operatorname_2(x) &= 6x^5 - 15x^4 + 10x^3, \\ \operatorname_3(x) &= -20x^7 + 70x^6 - 84x^5 + 35x^4, \\ \operatorname_4(x) &= 70x^9 - 315x^8 + 540x^7 - 420x^6 + 126x^5, \\ \operatorname_5(x) &= -252x^ + 1386x^ - 3080x^9 + 3465x^8 - 1980x^7 + 462x^6, \\ \operatorname_6(x) &= 924x^ - 6006x^ + 16380x^ - 24024x^ + 20020x^9 - 9009x^8 + 1716x^7. \\ \end The differential of \operatorname_N(x) is :\begin \operatorname_N(x) &= (2N+1)\binom (x-x^2)^N. \\ \end It can be shown that the smoothstep polynomials \operatorname_N(x) that transition from 0 to 1 when ''x'' transitions from 0 to 1 can be simply mapped to odd-symmetry polynomials : \operatorname_N(x) = \left(\int_^ \big(1 - u^2 \big)^N \,du \right)^ \int_^ \big(1 - u^2 \big)^N \,du, where :\operatorname_N(x) = \tfrac12 \operatorname_N(2x-1) + \tfrac12 and : \operatorname_N(-x) = -\operatorname_N(x). The argument of R''N''(''x'') is −1 ≤ ''x'' ≤ 1 and is appended to the constant −1 on the left and +1 at the right. An implementation of \operatorname_N(x) in Javascript:General smoothstep equation
// Generalized smoothstep function generalSmoothStep(N, x) // Returns binomial coefficient without explicit use of factorials, // which can't be used with negative integers function pascalTriangle(a, b) function clamp(x, lowerlimit, upperlimit)


Inverse Smoothstep

The inverse of smoothstep() can be useful when doing certain operations in computer graphics when its effect needs to be reversed or compensated for. In the case of the 3rd-order equation there exists an analytical solution for the inverse, which is: :\operatorname_1(x) = 1/2 - \sin(\operatorname(1-2x)/3) This arises as the inverse of \operatorname(x) = 1/2 - \sin(3 \operatorname(0.5-x))/2, whose
Maclaurin series Maclaurin or MacLaurin is a surname. Notable people with the surname include: * Colin Maclaurin (1698–1746), Scottish mathematician * Normand MacLaurin (1835–1914), Australian politician and university administrator * Henry Normand MacLaurin ( ...
terminates at 3x^2-2x^3=S_1(x), meaning \operatorname and \operatorname_1 express the same function. The series expansion of the inverse, on the other hand, does not terminate. In GLSL: float inverse_smoothstep(float x)


References

{{reflist


External links


Using smoothstep
(in the
RenderMan Shading Language Renderman Shading Language (abbreviated RSL) is a component of the RenderMan Interface Specification, and is used to define shaders. The language syntax is C-like. A shader written in RSL can be used without changes on any RenderMan-compliant rend ...
) by Prof. Malcolm Kesson.
Interpolation tricks
by Jari Komppa
Swift Interpolation Playground
demonstrates ''smoothStep()'', ''smootherStep()'' and ''smoothestStep()'' in a
Swift Swift or SWIFT most commonly refers to: * SWIFT, an international organization facilitating transactions between banks ** SWIFT code * Swift (programming language) * Swift (bird), a family of birds It may also refer to: Organizations * SWIFT, ...
playground by Simon Gladman
Inverse smoothstep
by Inigo Quilez Computer graphics algorithms