Skip to content

SpectrogramNumpy

SpectrogramNumpy

A class for computing spectrograms.

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

Source code in src/libsegmenter/transforms/spectrogram/SpectrogramNumpy.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
class SpectrogramNumpy:
    """
    A class for computing spectrograms.

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

    """

    def __init__(self) -> None:
        """Initializes the SpectrogramNumpy instance."""
        return

    def forward(self, x: NDArray[T]) -> NDArray[np.complex128]:
        """
        Converts segments into a spectrogram.

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

        """
        if x.shape[-1] % 2 != 0:
            raise ValueError(
                "Input segment size is expected to be even for a consistent definition "
                + "of the inverse real-valued FFT."
            )
        return np.fft.rfft(x, axis=-1, norm="backward")

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

        Args:
            y (NDArray[np.complex128]): Spectrogram resulting from a `forward` pass.

        """
        return np.fft.irfft(y, axis=-1, norm="backward")

__init__()

Initializes the SpectrogramNumpy instance.

Source code in src/libsegmenter/transforms/spectrogram/SpectrogramNumpy.py
36
37
38
def __init__(self) -> None:
    """Initializes the SpectrogramNumpy instance."""
    return

forward(x)

Converts segments into a spectrogram.

Parameters:

Name Type Description Default
x NDArray[T]

Segments as generated by a Segmenter object.

required
Source code in src/libsegmenter/transforms/spectrogram/SpectrogramNumpy.py
40
41
42
43
44
45
46
47
48
49
50
51
52
53
def forward(self, x: NDArray[T]) -> NDArray[np.complex128]:
    """
    Converts segments into a spectrogram.

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

    """
    if x.shape[-1] % 2 != 0:
        raise ValueError(
            "Input segment size is expected to be even for a consistent definition "
            + "of the inverse real-valued FFT."
        )
    return np.fft.rfft(x, axis=-1, norm="backward")

inverse(y)

Converts spectrogram into segments.

Parameters:

Name Type Description Default
y NDArray[complex128]

Spectrogram resulting from a forward pass.

required
Source code in src/libsegmenter/transforms/spectrogram/SpectrogramNumpy.py
55
56
57
58
59
60
61
62
63
def inverse(self, y: NDArray[np.complex128]) -> NDArray[np.float64]:
    """
    Converts spectrogram into segments.

    Args:
        y (NDArray[np.complex128]): Spectrogram resulting from a `forward` pass.

    """
    return np.fft.irfft(y, axis=-1, norm="backward")