Skip to content

SpectrogramTorch

SpectrogramTorch

A class for computing spectrograms using PyTorch.

The normalization for the Fourier transform is backward by default.

Source code in src/libsegmenter/transforms/spectrogram/SpectrogramTorch.py
23
24
25
26
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 SpectrogramTorch:
    """
    A class for computing spectrograms using PyTorch.

    The normalization for the Fourier transform is `backward` by default.
    """

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

    def forward(self, x: torch.Tensor) -> torch.Tensor:
        """
        Converts segments into a spectrogram.

        Args:
            x (torch.Tensor): Input segments.

        Returns:
            torch.Tensor: Spectrogram representation.

        """
        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 torch.fft.rfft(x, dim=-1, norm="backward")  # pyright: ignore

    def inverse(self, y: torch.Tensor) -> torch.Tensor:
        """
        Converts spectrogram into segments.

        Args:
            y (torch.Tensor): Spectrogram from a `forward` pass.

        Returns:
            torch.Tensor: Reconstructed segments.

        """
        return torch.fft.irfft(y, dim=-1, norm="backward")  # pyright: ignore

__init__()

Initializes the SpectrogramTorch instance.

Source code in src/libsegmenter/transforms/spectrogram/SpectrogramTorch.py
30
31
32
def __init__(self) -> None:
    """Initializes the SpectrogramTorch instance."""
    return

forward(x)

Converts segments into a spectrogram.

Parameters:

Name Type Description Default
x Tensor

Input segments.

required

Returns:

Type Description
Tensor

torch.Tensor: Spectrogram representation.

Source code in src/libsegmenter/transforms/spectrogram/SpectrogramTorch.py
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
def forward(self, x: torch.Tensor) -> torch.Tensor:
    """
    Converts segments into a spectrogram.

    Args:
        x (torch.Tensor): Input segments.

    Returns:
        torch.Tensor: Spectrogram representation.

    """
    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 torch.fft.rfft(x, dim=-1, norm="backward")  # pyright: ignore

inverse(y)

Converts spectrogram into segments.

Parameters:

Name Type Description Default
y Tensor

Spectrogram from a forward pass.

required

Returns:

Type Description
Tensor

torch.Tensor: Reconstructed segments.

Source code in src/libsegmenter/transforms/spectrogram/SpectrogramTorch.py
52
53
54
55
56
57
58
59
60
61
62
63
def inverse(self, y: torch.Tensor) -> torch.Tensor:
    """
    Converts spectrogram into segments.

    Args:
        y (torch.Tensor): Spectrogram from a `forward` pass.

    Returns:
        torch.Tensor: Reconstructed segments.

    """
    return torch.fft.irfft(y, dim=-1, norm="backward")  # pyright: ignore