Spherical Harmonic Basis Functions
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.
Derivation
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:

Spherical harmonics defined in terms of legendre polynomials.
The Legendre polynomials are defined recursively using:

Legendre polynomials defined recursively.
The Basis
Using this definition, the first 9 basis functions are:

The first 9 basis functions using the previous definitions.
Note that these basis functions do not agree with those given in [2]. 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:
Spherical harmonics orthogonality.
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:

More spherical harmonics up to order 4.
Proof
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 [2] and [3], 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:

Computing outgoing radiance using incoming radiance.
Using this notation, the first few coefficients evaluate to:

Outgoing diffuse radiance coefficients.
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 [2] 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 [4]. 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:

Fixed rotation matrices for real spherical harmonics.
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.
References
[1] Blanco, Florez and Bermejo – Evaluation of the Rotation Matrices in the Basis of Real Spherical Harmonics.
[2] Ramamoorthi and Hanrahan – An Efficient Representation for Irradiance Environment Maps.
[3] Ramamoorthi and Hanrahan – On The Relationship Between Radiance and Irradiance.
[4] Choi, Ivanic, Gordon and Ruedenberg – Rapid and stable determination of rotation matrices between spherical harmonics by direct recursion. Direct download here.
hi,
Thank you.
could you help me to arrive at an expression for the basis function for l=50 m=50
prabhakar
14 Nov 11 at 12:17 pm
I think at some point you probably want to use the recursive formulation directly, I’d expect the expression for that harmonic to be extremely long if written out in full.
Simon Brown
15 Nov 11 at 6:07 pm
I think that : Y11 should be equal :
1/2(sq(3/pi)*sin(theta)*cos(phi).
Please confirm that to me.
Saddek
16 May 12 at 6:58 am
The definition of Theta_lm(theta) function in the first box uses a theta argument for P^m_l. From my rather bumpy adventures with Maxima, I think you should have used a cos(theta) argument instead.
Apart from that, a very neat sum-up! Thanks for the work
Dominik Witczak
25 Nov 12 at 10:39 pm