Spherical harmonic basis functions can be defined in various different ways depending on your derivation or normalisation requirements. This page defines the real-valued set I use, along with algorithms and code snippets for irradiance estimation and run-time evaluation.
Real-valued spherical harmonics are defined in terms of their complex analogues. This derivation produces the signless real spherical harmonics, which seem to be the most common basis in use. It can be succinctly written down as:
The Legendre polynomials are defined recursively using:
Using this definition, the first 9 basis functions are:
Note that these basis functions do not agree with those given in . The functions for evaluation of spherical harmonics listed in Ramamoorthi et al’s paper have the convolution of incoming to outgoing radiance baked in. We will cover this separately later.
The basis functions defined above are orthonormal when integrated over the unit sphere. This can be written down as:
Note that the sine term is only necessary due to our parameterisation of the sphere. Completing the basis up to the first 25 basis functions (i.e. order 4 spherical harmonics) requires these functions:
Since I don’t have access to any algebraic manipulation software, I wrote a small application that algebraically generates basis functions using the derivation above and outputs them in TeX format. The source can be downloaded here:
To prove orthogonality, I wrote another small application that numerically integrates basis functions against each other pairwise. This application implements the basis functions above up to order 4. The source can be downloaded here:
Both source files are released under the terms and conditions of the MIT license. See the copyright notice at the top of each file for details.
Incoming Radiance to Outgoing Radiance
Summarising the result from  and , outgoing radiance from a lambertian surface can be efficiently approximated using only the first few terms of the spherical harmonic representation of the incoming radiance. The coefficients of this conversion are:
Using this notation, the first few coefficients evaluate to:
Since all terms past order 2 are considered to be small, the function is usually approximated using the first 9 spherical harmonics only. This approximation is shown in  to have worst-case average error of 3% and worst-case sample error of 9%, however the average error for typical radiance maps is around 1%.
Spherical Harmonic Rotations
This can be efficiently implemented using . There are unfortunately some errors in the later equations of this paper. Replacing equations 8.8 through 8.14 with the following equations gives the required results:
This is a direct evaluation of R = W D W* (where W* is the hermitian conjugate of W). I will endeavour to free up some example code that implements these equations in the near future.
 Blanco, Florez and Bermejo – Evaluation of the Rotation Matrices in the Basis of Real Spherical Harmonics.
 Ramamoorthi and Hanrahan – An Efficient Representation for Irradiance Environment Maps.
 Ramamoorthi and Hanrahan – On The Relationship Between Radiance and Irradiance.
 Choi, Ivanic, Gordon and Ruedenberg – Rapid and stable determination of rotation matrices between spherical harmonics by direct recursion. Direct download here.