librosa.magphase

librosa.magphase(D, *, power=1)[source]

Separate a complex-valued spectrogram D into its magnitude (S) and phase (P) components, so that D = S * P.

Parameters:
Dnp.ndarray [shape=(…, d, t), dtype=complex]

complex-valued spectrogram

powerfloat > 0

Exponent for the magnitude spectrogram, e.g., 1 for energy, 2 for power, etc.

Returns:
D_magnp.ndarray [shape=(…, d, t), dtype=real]

magnitude of D, raised to power

D_phasenp.ndarray [shape=(…, d, t), dtype=complex]

exp(1.j * phi) where phi is the phase of D

Examples

>>> y, sr = librosa.load(librosa.ex('trumpet'))
>>> D = librosa.stft(y)
>>> magnitude, phase = librosa.magphase(D)
>>> magnitude
array([[5.395e-03, 3.332e-03, ..., 9.862e-07, 1.201e-05],
       [3.244e-03, 2.690e-03, ..., 9.536e-07, 1.201e-05],
       ...,
       [7.523e-05, 3.722e-05, ..., 1.188e-04, 1.031e-03],
       [7.640e-05, 3.944e-05, ..., 5.180e-04, 1.346e-03]],
      dtype=float32)
>>> phase
array([[ 1.   +0.000e+00j,  1.   +0.000e+00j, ...,
        -1.   -8.742e-08j, -1.   -8.742e-08j],
       [-1.   -8.742e-08j, -0.775-6.317e-01j, ...,
        -0.885-4.648e-01j,  0.472-8.815e-01j],
       ...,
       [ 1.   -4.342e-12j,  0.028-9.996e-01j, ...,
        -0.222-9.751e-01j, -0.75 -6.610e-01j],
       [-1.   -8.742e-08j, -1.   -8.742e-08j, ...,
         1.   +0.000e+00j,  1.   +0.000e+00j]], dtype=complex64)

Or get the phase angle (in radians)

>>> np.angle(phase)
array([[ 0.000e+00,  0.000e+00, ..., -3.142e+00, -3.142e+00],
       [-3.142e+00, -2.458e+00, ..., -2.658e+00, -1.079e+00],
       ...,
       [-4.342e-12, -1.543e+00, ..., -1.794e+00, -2.419e+00],
       [-3.142e+00, -3.142e+00, ...,  0.000e+00,  0.000e+00]],
      dtype=float32)