Skip to content

MagnitudeTensorFlow

MagnitudePhaseTensorFlow

A class for computing magnitudes using TensorFlow.

Source code in src/libsegmenter/transforms/magnitude_phase/MagnitudePhaseTensorFlow.py
27
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 MagnitudePhaseTensorFlow:
    """A class for computing magnitudes using TensorFlow."""

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

    def forward(self, x: tf.Tensor) -> Tuple[tf.Tensor, tf.Tensor]:
        """
        Converts segments into a magnitude.

        Args:
            x (tf.Tensor): Segments as generated by a Segmenter object.

        Returns:
            tf.Tensor: MagnitudePhase representation.

        """
        tensor = self._spectrogram.forward(x)
        return tf.abs(tensor), tf.math.angle(tensor)  # pyright: ignore

    def inverse(self, magnitude: tf.Tensor, phase: tf.Tensor) -> tf.Tensor:
        """
        Converts magnitude / phase spectrogram into segments.

        Args:
            magnitude (Tensor): MagnitudePhase spectrogram resulting from a `forward`
                pass.
            phase (Tensor): Phase spectrogram resulting from a `forward` pass.

        """
        magnitude_complex = tf.cast(magnitude, dtype=tf.complex64)  # pyright: ignore
        phase_complex = tf.cast(phase, dtype=tf.complex64)  # pyright: ignore
        j = tf.complex(0.0, 1.0)  # pyright: ignore
        complex_exp = tf.exp(j * phase_complex)  # pyright: ignore

        return self._spectrogram.inverse(
            magnitude_complex * complex_exp  # pyright: ignore
        )

__init__(*args, **kwargs)

Initializes the MagnitudePhaseTensorFlow instance.

Source code in src/libsegmenter/transforms/magnitude_phase/MagnitudePhaseTensorFlow.py
30
31
32
def __init__(self, *args: Any, **kwargs: Any) -> None:
    """Initializes the MagnitudePhaseTensorFlow instance."""
    self._spectrogram = SpectrogramTensorFlow(*args, **kwargs)

forward(x)

Converts segments into a magnitude.

Parameters:

Name Type Description Default
x Tensor

Segments as generated by a Segmenter object.

required

Returns:

Type Description
Tuple[Tensor, Tensor]

tf.Tensor: MagnitudePhase representation.

Source code in src/libsegmenter/transforms/magnitude_phase/MagnitudePhaseTensorFlow.py
34
35
36
37
38
39
40
41
42
43
44
45
46
def forward(self, x: tf.Tensor) -> Tuple[tf.Tensor, tf.Tensor]:
    """
    Converts segments into a magnitude.

    Args:
        x (tf.Tensor): Segments as generated by a Segmenter object.

    Returns:
        tf.Tensor: MagnitudePhase representation.

    """
    tensor = self._spectrogram.forward(x)
    return tf.abs(tensor), tf.math.angle(tensor)  # pyright: ignore

inverse(magnitude, phase)

Converts magnitude / phase spectrogram into segments.

Parameters:

Name Type Description Default
magnitude Tensor

MagnitudePhase spectrogram resulting from a forward pass.

required
phase Tensor

Phase spectrogram resulting from a forward pass.

required
Source code in src/libsegmenter/transforms/magnitude_phase/MagnitudePhaseTensorFlow.py
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
def inverse(self, magnitude: tf.Tensor, phase: tf.Tensor) -> tf.Tensor:
    """
    Converts magnitude / phase spectrogram into segments.

    Args:
        magnitude (Tensor): MagnitudePhase spectrogram resulting from a `forward`
            pass.
        phase (Tensor): Phase spectrogram resulting from a `forward` pass.

    """
    magnitude_complex = tf.cast(magnitude, dtype=tf.complex64)  # pyright: ignore
    phase_complex = tf.cast(phase, dtype=tf.complex64)  # pyright: ignore
    j = tf.complex(0.0, 1.0)  # pyright: ignore
    complex_exp = tf.exp(j * phase_complex)  # pyright: ignore

    return self._spectrogram.inverse(
        magnitude_complex * complex_exp  # pyright: ignore
    )