Explicit formulas for High Order Ambisonics

The goal of this page is to provide an univoque, explicit set of formulas describing the amplitude of the Ambisonics polar patterns as function of the azimuth and elevation angles, and also as a function of the three cartesian coordinates of the unit vector pointing towards the sound source.
Unfortunately in the years many different Ambisonics formulations have been employed, differing in naming, channel order, and gain normalization.
Furthermore, the Cartesian and Spherical coordinate systems have also been messed up, again with wrong naming, polarity inversion, and so on.
So this page also serves for providing the correct information about all these possible causes of errors.
The Cartesian reference must be conforming to ISO standards (for example, ISO2631), as in the following picture:
The spherical coordinate system is coherent with this Cartesian reference system: the azimuth angle (a) lies in the XY plane, starting form the X axis and running towards the Y axis, so an azimut of 0 degrees means "Front", 90 degrees means "Left", 180 degrees means "Back" and 90 degrees (which also equates to 270 degrees) means "Right". Elevation angle (e) is measured from the horizontal plane, with positive elevations going up to the sky, so e = 90 degrees means Zenith (or North pole) and e=90 degrees means Nadir (or South pole).
The usage of other symbols for azimuth and elevation must be discouraged. Many people use θ (theta) and j (phi) for naming azimuth and elevation, but these are often swapped (there is no univoque correspondance establishing if theta is azimuth or elevation), and in some cases the value of elevation is not starting from the horizontal plane, but instead from the North pole and going down, as shown, for example, on the Wikipedia page https://en.wikipedia.org/wiki/Spherical_coordinate_system, where the two possible naming conventions are shown, but elevation is always wrong.
Also the sign of azimuth is sometimes reversed: for example, in FB360 Spatial Workstation the elevation angle is correct, but the azimut is reversed, with positive values going right instead of going left, as it should be. The same problem occurs in Audio Ease and Ambix.
Luckily enough, most other software used for creating and manipulating High Order Ambisonics signals are usually perfectly coherent with the ISO standard described here, for example O3A, Noise Makers, Wigware, Ambdec, York University, Ambisonic Studio, Gerzonic, etc.
I hope that the information above serves for avoiding errors... And perhaps the authors of software employing wrong conventions cited above will decide to fix their crappy angular systems...
And now let's define the spherical harmonics functions, which provide the directiondependent gain of each of the Ambisonics signals.
These functions are defined in a series, with 2 more functions for each order, providing an increasing spatial resolution. The following figure shows these directiondependent gain functions up to 4th order (I did not find a nice picture showing also 5th order harmonics):
The meaning of the colors is as following:
BLUE means positive
GREEN means negative
And finally here we present the explicit formulas giving these gain functions, either in polar and Cartesian coordinates, worked out from the formulas given here, and corresponding to current standard Ambix format (that is, channel ordering is ACN and gain normalization is SN3D  any other convention is nowadays obsolete and should be completely avoided):
ACN 
ACN 
Order 
Azimuth/Elevation Representation 
Cartesian Representation 
0 
0 
0 
1 
1 
1 
1 
1 
sin(a)*cos(e) 
y 
2 
2 
1 
sin(e) 
z 
3 
3 
1 
cos(a)*cos(e) 
x 
4 
4 
2 
sqrt(3/4)*sin(2*a)*(cos(e))^2 
sqrt(3)*x*y 
5 
5 
2 
sqrt(3/4)*sin(a)*sin(2*e) 
sqrt(3)*y*z 
6 
6 
2 
(1/2)*(3*(sin(e))^21) 
(1/2)*(3*z^21) 
7 
7 
2 
sqrt(3/4)*cos(a)*sin(2*e) 
sqrt(3)*x*z 
8 
8 
2 
sqrt(3/4)*cos(2*a)*(cos(e))^2 
sqrt(3/4)*(x^2y^2) 
9 
9 
3 
sqrt(5/8)*sin(3*a)*(cos(e))^3 
sqrt(5/8)*y*(3*x^2y^2) 
10 
10 
3 
sqrt(15/4)*sin(2*a)*sin(e)*(cos(e))^2 
sqrt(15)*x*y*z 
11 
11 
3 
sqrt(3/8)*sin(a)*cos(e)*(5*(sin(e))^21) 
sqrt(3/8)*y*(5*z^21) 
12 
12 
3 
(1/2)*sin(e)*(5*(sin(e))^23) 
(1/2)*z*(5*z^23) 
13 
13 
3 
sqrt(3/8)*cos(a)*cos(e)*(5*(sin(e))^21) 
sqrt(3/8)*x*(5*z^21) 
14 
14 
3 
sqrt(15/4)*cos(2*a)*sin(e)*(cos(e))^2 
sqrt(15/4)*z*(x^2y^2) 
15 
15 
3 
sqrt(5/8)*cos(3*a)*(cos(e))^3 
sqrt(5/8)*x*(x^23*y^2) 
16 
16 
4 
sqrt(35/64)*sin(4*a)*(cos(e))^4 
sqrt(35/4)*x*y*(x^2y^2) 
17 
17 
4 
sqrt(35/8)*sin(3*a)*sin(e)*(cos(e))^3 
sqrt(35/8)*y*z*(3*x^2y^2) 
18 
18 
4 
sqrt(5/16)*sin(2*a)*(cos(e))^2*(7*(sin(e))^21) 
sqrt(5/4)*x*y*(7*z^21) 
19 
19 
4 
sqrt(5/32)*sin(a)*sin(2*e)*(7*(sin(e))^23) 
sqrt(5/8)*y*z*(7*z^23) 
20 
20 
4 
1/8*(35*sin(e)^430*sin(e)^2+3) 
1/8*(35*z^430*z^2+3) 
21 
21 
4 
sqrt(5/32)*cos(a)*sin(2*e)*(7*(sin(e))^23) 
sqrt(5/8)*x*z*(7*z^23) 
22 
22 
4 
sqrt(5/16)*cos(2*a)*(cos(e))^2*(7*(sin(e))^21) 
sqrt(5/16)*(x^2y^2)*(7*z^21) 
23 
23 
4 
sqrt(35/8)*cos(3*a)*sin(e)*(cos(e))^3 
sqrt(35/8)*x*z*(x^23*y^2) 
24 
24 
4 
sqrt(35/64)*cos(4*a)*(cos(e))^4 
sqrt(35/64)*(x^46*x^2*y^2+y^4) 
25 
25 
5 
sqrt(63/128)*sin(5*a)*(cos(e))^5 
sqrt(63/128)*y*(5*x^410*x^2*y^2+y^4) 
26 
26 
5 
sqrt(315/64)*sin(4*a)*sin(e)*(cos(e))^4 
sqrt(315/4)*x*y*z*(x^2y^2) 
27 
27 
5 
sqrt(35/128)*sin(3*a)*(cos(e))^3*(9*(sin(e))^21) 
sqrt(35/128)*y*(y^42*x^2*y^23*x^48*y^2*z^2+24*x^2*z^2) 
28 
28 
5 
sqrt(105/16)*sin(2*a)*sin(e)*(cos(e))^2*(3*(sin(e))^21) 
sqrt(105/4)*x*y*z*(2*z^2x^2y^2) 
29 
29 
5 
sqrt(15/64)*sin(a)*cos(e)*(21*(sin(e))^414*(sin(e))^2+1) 
sqrt(15/64)*y*(x^4+2*x^2*y^2+y^412*x^2*z^212*y^2*z^2+8*z^4) 
30 
30 
5 
1/8*(63*(sin(e))^570*sin(e)^3+15*sin(e)) 
1/8*z*(63*z^470*z^2+15) 
31 
31 
5 
sqrt(15/64)*cos(a)*cos(e)*(21*(sin(e))^414*(sin(e))^2+1) 
sqrt(15/64)*x*(x^4+2*x^2*y^2+y^412*x^2*z^212*y^2*z^2+8*z^4) 
32 
32 
5 
sqrt(105/16)*cos(2*a)*sin(e)*(cos(e))^2*(3*(sin(e))^21) 
sqrt(105/16)*z*(2*x^2*z^22*y^2*z^2x^4+y^4) 
33 
33 
5 
sqrt(35/128)*cos(3*a)*(cos(e))^3*(9*(sin(e))^21) 
sqrt(35/128)*x*(2*x^2*y^2+8*x^2*z^224*y^2*z^2x^4+3*y^4) 
34 
34 
5 
sqrt(315/64)*cos(4*a)*sin(e)*(cos(e))^4 
sqrt(315/64)*z*(x^46*x^2*y^2+y^4) 
35 
35 
5 
sqrt(63/128)*cos(5*a)*(cos(e))^5 
sqrt(63/128)*x*(x^410*x^2*y^2+5*y^4) 
If you find errors in these formulas, please point them out to the author of this page...
Angelo Farina, August 2017