The idea for this post was written after some people asked what my previous post and spherical harmonics were all about. This is my attempt at explaining them in a visually interesting, intuitive fashion.
Functions
Let’s start with functions. Functions map values from a domain to a range. Let’s see some examples:
 The typical 1D function covered in high school algebra goes from realvalued scalars to realvalued scalars: . Visualizing these functions is done as a line on a 2D Cartesian plane, with domain on the xaxis and range on the yaxis.
 Another type of 1D function are those defined as polar functions: . Visualizing these functions is done as a line on a polar plot, where the domain is the angle relative to the horizontal axis and the range is the distance from the origin.

A typical 2D function, can be analogously visualized as a surface in a 3D Cartesian space, with domain on the x and yaxes, and range on the zaxis.

However, we can also represent a 2D function in two dimensions, by using x and yaxes as domain and intensity as range. In other words, a grayscale image is a 2D function. One caveat: negative function values obviously can’t be represented as a physical intensity, so to deal with negative values in 2D functions, we need to e.g. show negative values in a different hue than positive values.
 Our image representation naturally extends itself to a higher dimensional range: a (nonnegative) function can be visualized as a color image, with the red, green, and blue components being the first, second, and third coordinates of the range.
 A more interesting set of functions are those defined on the spherical domain, . Spherical functions are to 2D functions as polar functions are to 1D functions.
Function Approximation
General functions are hard to deal with for practical purposes, since they can be defined arbitrarily. You might have a nice polynomial function , or it might use some less wellknown function like , or it could just be an infinite lookup table of values. For real world applications, there is often no way to write down the function precisely  the price of a stock as a function of time, the temperature as a function of location on the earth, and so on.
So when modelling realworld phenomena, we approximate functions by putting them into some more natural representation. There are many ways of doing this: you can fit a function to it, use a deep neural network, a support vector machine, and so on. Here, we’re going to represent functions as linear combinations of basis functions. What does this mean? Basis functions are just a set (possibly infinite) of predefined functions that are defined on the same domain as the function we want to represent. We form a linear combination of these functions by multiplying each of them by a different coefficient and then add them all up. So the representation of our original function becomes simply the set of scale factors (coefficients) of our basis functions .
See the figure below for some visual examples.
Linear Bases
Here we’ll go over some common types of basis functions. These function types generalize to many different domains; here we’ll show examples for approximating a 1D function and a 2D function.
Basis Function  1D basis functions  1D reconstruction 

Constant Basis  
Radial Basis  
Harmonic Basis  
Wavelet Basis  
Polynomial Basis 
Basis Function  2D basis functions  2D reconstruction 

Constant Basis  
Radial Basis  
Harmonic Basis  
Wavelet Basis  
Polynomial Basis 

The constant basis is a very simple one. You split the domain into discrete sections; each basis function is 1 within the section and 0 everywhere else. Pixelwise representation of images are constant basis representations, as are digital audio signals. The constant basis is very general for finite domains, and is easy to compute (each coefficient only depends on the function’s values within each section, i.e. it is locally supported). It usually results in significant aliasing (jaggy edges) if used naively.

Closely related to the constant basis is the radial basis. In radial basis functions, you pick a set of “centers” in your domain; each basis function has a peak at the center and smoothly falls to zero not too far from the center. Usually, Gaussian functions are used as the basis functions. RBF reconstructions of functions are smoother than constant basis representations, but otherwise have similar benefits and drawbacks.

The harmonic basis (such as the 1D Fourier basis) is a more theoretically motivated set of basis functions. In 1D, the basis functions are simply . Technically, the Fourier basis isn’t quite a linear basis, since the coefficients may be complex (the imaginary component encodes the phase); the Discrete Cosine (or Sine) Transform is the simpler version in real numbers only. This basis has the very nice property that using only the first few basis functions usually results in a pretty good approximation of the function; in signalprocessing terms, they encode the low frequencies of the function. Harmonic bases are nonlocal, in that changing the function values in one place changes all the coefficients.

A wavelet basis is similar to the Fourier basis in that it captures different signal frequencies in different basis functions; however in a wavelet basis, higher order basis functions are more localized over the domain. Haar wavelets are the most common example of wavelets.

Another basis that is commonly used is the polynomial basis. When you do a polynomial fit in Matlab, you are finding a representation in the polynomial basis. When you do a Taylor expansion of a function, you’re also getting a polynomial basis representation. The basis functions are the polynomials; for 1D they are while for 2D they are (the constant factors are included for illustrative purposes, but not actually used). For most applications, we only take the linear or quadratic approximation since it’s much easier to analyze and work with. Like the harmonic basis, the polynomial basis is nonlocal.
Back to Spherical Harmonics
Armed with this knowledge, spherical harmonics are now easy to define. They are simply the set of harmonic basis functions over the spherical domain. This is what the spherical harmonic basis functions look like:
The spherical harmonic functions are referenced by two indices, usually . is referred to as the band, while is sometimes called the index. corresponds to the frequency of the signal (e.g. band 0 is the bias, band 1 is very low frequency, and adding more bands adds higher frequencies). ranges from to in each band, so that using an band spherical harmonic approximation results in basis functions.
Spherical harmonics in computer graphics and vision are commonly used to represent incident lighting: if you consider a point on a surface, the intensity of the light hitting it is a function of direction. It has been shown that, for diffuse surfaces, three bands (i.e. nine coefficients) of incident lighting spherical harmonics are indistinguishable from higher frequency representations of the incident light. The diffuseness of the surface essentially blurs out the higher frequencies.
If you’ve ever seen diagrams of electron orbitals, they happen to have the exact same shape as the spherical harmonics! More precisely, the solution to the Schrodinger Equation for a hydrogen atom are multiples of the spherical harmonics (see Wikipedia).
I won’t go into any of the mathematical details behind the functions; if you do need more depth, a list of references is at the end of this page.
Working with Linear Bases
There are many things you can do using linear bases; “function approximation” is a very broad term. What we did above can be seen as compression: we took a dense sampling of a function, and then represented it with a much smaller number of coefficients. In the sawtooth case, we took 512 samples of a sawtooth function and compressed it into 32 or fewer numbers; in the image case we took pixels and represented them with 225 numbers or fewer.
We can also use linear bases for interpolation: if we only had a sparse sampling of function values, and we want to know the function value for somewhere we don’t have a value for, we can just use the linear basis approximation.
In both of these cases, we want to use a sampling of function values, and project the function into the linear basis. The most general way to do this is, unsurprisingly, linear least squares. Given a list of observations , you can form the equations
You then want to minimize the squared differences between your observed values and your fitted values .
You can just plug in your values for and evaluate the (since you know your basis functions), so that your only unknowns are the .
Sample code for using linear least squares for function approximation (including code used to generate the above images) is available at https://github.com/kyzyx/linearbasis. Note that, while linear least squares will work for projecting into any linear basis, it is not always the most effective way to do so. For example, when using the harmonic bases, you usually want to use a Fourier transform instead; various types of wavelet transforms also exist for different types of wavelet bases.
You can do much more than just interpolation and compression with function approximation! What if you can’t actually directly get samples of your function? For example, in my research, I want to determine some unknown lighting distribution , but I have no direct observations of . Instead, I only indirectly see the effects of the lighting by what the surfaces in the scene look like, e.g. , where the only things I know are and maybe . By writing as a linear combination of basis functions, we can optimize directly for the coefficients of in the linear basis. This lets us get an approximation for the full function without actually having seen any values for the function!
Conclusion
Linear bases are an extremely simple but powerful tool for function approximation. There are all sorts of theoretical pros and cons to the bases presented here that I haven’t gotten into (e.g. orthogonality, nonnegativity), but hopefully this gives you an intuitive sense as to how they work. If you were here just to figure out spherical harmonics, this might have been a rather roundabout and lowlevel way to explain them. If you want to dive more deeply into them, hopefully I’ve provided enough of the first principles to make the references below less confusing.
Spherical Harmonics References
Here are a collection of classic references for spherical harmonics, usually in the context of lighting.
 Spherical Harmonic Lighting: The Gritty Details: the authoritative reference for spherical harmonics in computer graphics.
 Stupid Spherical Harmonics (SH) Tricks: another commonly cited reference for spherical harmonics in computer graphics. Contains useful appendices, including polynomial approximations and code for up to 5 bands.
 InDepth: Spherical Harmonic Lighting: a concise reference for SH lighting
 An Efficient Representation for Irradiance Environment Maps: the original paper by Ramamoorthi describing the use of spherical harmonics for diffuse environment maps
 On the relationship between radiance and irradiance: Mathheavy theoretical analysis by Ramamoorthi on diffuse SH lighting