librosa.util.phasor(angles, *, mag=None)[source]

Construct a complex phasor representation from angles.

When mag is not provided, this is equivalent to:

z = np.cos(angles) + 1j * np.sin(angles)

or by Euler’s formula:

z = np.exp(1j * angles)

When mag is provided, this is equivalent to:

z = mag * np.exp(1j * angles)

This function should be more efficient (in time and memory) than the equivalent’ formulations above, but produce numerically identical results.

anglesnp.ndarray or scalar, real-valued

Angle(s), measured in radians

magnp.ndarray or scalar, optional

If provided, phasor(s) will be scaled by mag.

If not provided (default), phasors will have unit magnitude.

mag must be of compatible shape to multiply with angles.

znp.ndarray or scalar, complex-valued

Complex number(s) z corresponding to the given angle(s) and optional magnitude(s).


Construct unit phasors at angles 0, pi/2, and pi:

>>> librosa.util.phasor([0, np.pi/2, np.pi])
array([ 1.000e+00+0.000e+00j,  6.123e-17+1.000e+00j,

Construct a phasor with magnitude 1/2:

>>> librosa.util.phasor(np.pi/2, mag=0.5)

Or arrays of angles and magnitudes:

>>> librosa.util.phasor(np.array([0, np.pi/2]), mag=np.array([0.5, 1.5]))
array([5.000e-01+0.j , 9.185e-17+1.5j])