processing.py 1.08 KB
Newer Older
1
2
3
4
5
6
7
8
import torch
import torch.nn as nn


class NormalizeDB(nn.Module):
    r"""Normalize the spectrogram with a minimum db value
    """

9
    def __init__(self, min_level_db, normalization):
10
11
        super().__init__()
        self.min_level_db = min_level_db
12
        self.normalization = normalization
13
14

    def forward(self, specgram):
15
        specgram = torch.log10(torch.clamp(specgram.squeeze(0), min=1e-5))
16
17
18
19
20
        if self.normalization:
            return torch.clamp(
                (self.min_level_db - 20 * specgram) / self.min_level_db, min=0, max=1
            )
        return specgram
21
22


23
def normalized_waveform_to_bits(waveform: torch.Tensor, bits: int) -> torch.Tensor:
24
25
26
27
28
29
30
31
    r"""Transform waveform [-1, 1] to label [0, 2 ** bits - 1]
    """

    assert abs(waveform).max() <= 1.0
    waveform = (waveform + 1.0) * (2 ** bits - 1) / 2
    return torch.clamp(waveform, 0, 2 ** bits - 1).int()


32
def bits_to_normalized_waveform(label: torch.Tensor, bits: int) -> torch.Tensor:
33
34
35
36
    r"""Transform label [0, 2 ** bits - 1] to waveform [-1, 1]
    """

    return 2 * label / (2 ** bits - 1.0) - 1.0