Skip to content

MagnitudePhaseNumpy

MagnitudePhaseNumpy

A class for computing magnitude and phase spectra.

Currently, the normalization for the fourier transform cannot be controlled and is thus backward by default.

Source code in src/libsegmenter/transforms/magnitude_phase/MagnitudePhaseNumpy.py
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
class MagnitudePhaseNumpy:
    """
    A class for computing magnitude and phase spectra.

    Currently, the normalization for the fourier transform cannot be controlled and is
    thus `backward` by default.

    """

    def __init__(self, *args: Any, **kwargs: Any) -> None:
        """Initializes the MagnitudePhaseNumpy instance."""
        self._spectrogram = SpectrogramNumpy(*args, **kwargs)

    def forward(self, x: NDArray[T]) -> Tuple[NDArray[Any], NDArray[Any]]:
        """
        Converts segments into a magnitude and phase spectrogram.

        Args:
            x (NDArray[T]): Segments as generated by a Segmenter object.

        """
        tensor = self._spectrogram.forward(x)
        return np.abs(tensor), np.angle(tensor)

    def inverse(
        self, magnitude: NDArray[np.complex128], phase: NDArray[np.complex128]
    ) -> NDArray[np.float64]:
        """
        Converts magnitude / phase spectrogram into segments.

        Args:
            magnitude (NDArray[np.complex128]): MagnitudePhase spectrogram resulting
                from a `forward` pass.
            phase (NDArray[np.complex128]): Phase spectrogram resulting from a
                `forward` pass.

        """
        return self._spectrogram.inverse(np.multiply(magnitude, np.exp(1j * phase)))

__init__(*args, **kwargs)

Initializes the MagnitudePhaseNumpy instance.

Source code in src/libsegmenter/transforms/magnitude_phase/MagnitudePhaseNumpy.py
37
38
39
def __init__(self, *args: Any, **kwargs: Any) -> None:
    """Initializes the MagnitudePhaseNumpy instance."""
    self._spectrogram = SpectrogramNumpy(*args, **kwargs)

forward(x)

Converts segments into a magnitude and phase spectrogram.

Parameters:

Name Type Description Default
x NDArray[T]

Segments as generated by a Segmenter object.

required
Source code in src/libsegmenter/transforms/magnitude_phase/MagnitudePhaseNumpy.py
41
42
43
44
45
46
47
48
49
50
def forward(self, x: NDArray[T]) -> Tuple[NDArray[Any], NDArray[Any]]:
    """
    Converts segments into a magnitude and phase spectrogram.

    Args:
        x (NDArray[T]): Segments as generated by a Segmenter object.

    """
    tensor = self._spectrogram.forward(x)
    return np.abs(tensor), np.angle(tensor)

inverse(magnitude, phase)

Converts magnitude / phase spectrogram into segments.

Parameters:

Name Type Description Default
magnitude NDArray[complex128]

MagnitudePhase spectrogram resulting from a forward pass.

required
phase NDArray[complex128]

Phase spectrogram resulting from a forward pass.

required
Source code in src/libsegmenter/transforms/magnitude_phase/MagnitudePhaseNumpy.py
52
53
54
55
56
57
58
59
60
61
62
63
64
65
def inverse(
    self, magnitude: NDArray[np.complex128], phase: NDArray[np.complex128]
) -> NDArray[np.float64]:
    """
    Converts magnitude / phase spectrogram into segments.

    Args:
        magnitude (NDArray[np.complex128]): MagnitudePhase spectrogram resulting
            from a `forward` pass.
        phase (NDArray[np.complex128]): Phase spectrogram resulting from a
            `forward` pass.

    """
    return self._spectrogram.inverse(np.multiply(magnitude, np.exp(1j * phase)))