@@ -107,3 +107,79 @@ def db(x, power=False):
107
107
"""
108
108
with np .errstate (divide = 'ignore' ):
109
109
return 10 if power else 20 * np .log10 (np .abs (x ))
110
+
111
+
112
+ def sph2cart (alpha , beta , r ):
113
+ r"""Spherical to cartesian coordinate transform.
114
+
115
+ taken from https://github.com/sfstoolbox/sfs-python commit: efdda38
116
+
117
+ .. math::
118
+
119
+ x = r \cos \alpha \sin \beta \\
120
+ y = r \sin \alpha \sin \beta \\
121
+ z = r \cos \beta
122
+
123
+ with :math:`\alpha \in [0, 2\pi), \beta \in [0, \pi], r \geq 0`
124
+
125
+ Parameters
126
+ ----------
127
+ alpha : float or array_like
128
+ Azimuth angle in radiants
129
+ beta : float or array_like
130
+ Colatitude angle in radiants (with 0 denoting North pole)
131
+ r : float or array_like
132
+ Radius
133
+
134
+ Returns
135
+ -------
136
+ x : float or `numpy.ndarray`
137
+ x-component of Cartesian coordinates
138
+ y : float or `numpy.ndarray`
139
+ y-component of Cartesian coordinates
140
+ z : float or `numpy.ndarray`
141
+ z-component of Cartesian coordinates
142
+
143
+ """
144
+ x = r * np .cos (alpha ) * np .sin (beta )
145
+ y = r * np .sin (alpha ) * np .sin (beta )
146
+ z = r * np .cos (beta )
147
+ return x , y , z
148
+
149
+
150
+ def cart2sph (x , y , z ):
151
+ r"""Cartesian to spherical coordinate transform.
152
+
153
+ taken from https://github.com/sfstoolbox/sfs-python commit: efdda38
154
+
155
+ .. math::
156
+
157
+ \alpha = \arctan \left( \frac{y}{x} \right) \\
158
+ \beta = \arccos \left( \frac{z}{r} \right) \\
159
+ r = \sqrt{x^2 + y^2 + z^2}
160
+
161
+ with :math:`\alpha \in [-pi, pi], \beta \in [0, \pi], r \geq 0`
162
+
163
+ Parameters
164
+ ----------
165
+ x : float or array_like
166
+ x-component of Cartesian coordinates
167
+ y : float or array_like
168
+ y-component of Cartesian coordinates
169
+ z : float or array_like
170
+ z-component of Cartesian coordinates
171
+
172
+ Returns
173
+ -------
174
+ alpha : float or `numpy.ndarray`
175
+ Azimuth angle in radiants
176
+ beta : float or `numpy.ndarray`
177
+ Colatitude angle in radiants (with 0 denoting North pole)
178
+ r : float or `numpy.ndarray`
179
+ Radius
180
+
181
+ """
182
+ r = np .sqrt (x ** 2 + y ** 2 + z ** 2 )
183
+ alpha = np .arctan2 (y , x )
184
+ beta = np .arccos (z / r )
185
+ return alpha , beta , r
0 commit comments