This maps a single coordinate to a 2L-dimensional vector. Higher frequencies capture fine details while lower frequencies capture smooth variations.
For 3D points, we apply this independently to x, y, z coordinates.
Example
Input
positional_encoding(0.5, 2)
Output
[1.0, 0.0, 0.0, -1.0]
Reasoning
Inputs:value=0.5 is one scalar coordinate (say the x of a 3D point) and L=2 is the number of frequency bands, so the output has 2L=4 entries: one sine and one cosine per band.
Band 0 has angular frequency 20π=π. The angle is π⋅0.5=π/2, giving sin2π=1 and cos2π=0.
Band 1 doubles the frequency to 21π=2π. The angle is 2π⋅0.5=π, giving sinπ=0 and cosπ=−1.
Output: the pairs in band order, sine before cosine: [1.0, 0.0, 0.0, -1.0]. In floating point the zeros are tiny values near 10−16, which show as 0.0 once rounded to 4 decimal places.
Notice that each band doubles the angle, so higher bands swing faster as value changes; that is what lets the network capture fine detail. With value=0 every angle is 0, giving [0.0, 1.0, 0.0, 1.0].
NeRF positional encoding maps low-dimensional coordinates into a higher-dimensional space using sinusoidal functions at multiple frequencies. This “Fourier feature” mapping lets a simple MLP represent high-frequency details (sharp edges, fine textures) that would otherwise be hard to learn from raw coordinates, which tend to bias networks toward overly smooth functions. In NeRF, both 3D positions and viewing directions are run through such encodings before being fed into the network, enabling detailed geometry and view-dependent appearance.
Mathematically, for a scalar p and a chosen number of frequency bands L, you build γ(p) by stacking sin and cos at exponentially increasing frequencies: 20\pi,21\pi,…,2L−1π. For a 3D point (x,y,z), you apply this encoding to each coordinate independently and concatenate the results. The network then operates on this large feature vector rather than the raw (x,y,z), effectively giving it a multiscale “basis” to approximate complex signals.
1. Background Knowledge
Why positional encoding in NeRF?
Standard MLPs with ReLU or similar activations are biased toward low-frequency (smooth) solutions; they struggle to represent highly detailed signals when fed raw coordinates. NeRF addresses this by first mapping coordinates into a high-dimensional space of sinusoids at multiple frequencies, so the MLP can express both low- and high-frequency variations via linear combinations of these basis functions.
Fourier feature idea:
The encoding is similar to using a truncated Fourier series: by including sin(2k\pip) and cos(2k\pip) for multiple k, you provide the network with fixed basis functions across scales. Lower k (low frequency) capture coarse structure; higher k (high frequency) capture fine details like edges or texture. The parameter L controls how many frequency bands you include.
Extending to 3D:
Coordinates in NeRF are 3D positions along a camera ray. Since the scalar encoding is defined, the 3D encoding is simply the concatenation of encodings for each coordinate: γ(x), γ(y), γ(z). This yields a vector of dimension 3×2L=6L for positions (ignoring any raw coordinates you might also concatenate).
2. Algorithm / Approach
For this type of coding problem, the pattern is:
Implement scalar positional encoding based on the given formula for γ(p) and parameter L.
Apply this encoding independently to each coordinate (x, y, z) of a 3D point.
Concatenate the results to form the final encoded vector for that 3D point.
Ensure your implementation is:
Deterministic (always same order: [sin, cos, sin, cos,...]).
Vectorized if the input is a batch of points.
Dimensionally correct (output shape matches what the rest of the pipeline expects).
This is essentially a pure function from (x,y,z) (and L) to an encoded feature vector.