File size: 1,117 Bytes
2b7bf83
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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
#!/usr/bin/env python3

# Copyright 2021 Tomoki Hayashi
#  MIT License (https://opensource.org/licenses/MIT)

"""Test code for Mel-spectrogram loss modules."""


import numpy as np
import torch

from parallel_wavegan.bin.preprocess import logmelfilterbank
from parallel_wavegan.losses import MelSpectrogram


def test_mel_spectrogram_is_equal():
    x = np.random.randn(22050)
    x = np.abs(x) / np.max(np.abs(x))
    mel_npy = logmelfilterbank(
        x,
        22050,
        fft_size=1024,
        hop_size=256,
        win_length=None,
        window="hann",
        num_mels=80,
        fmin=80,
        fmax=7600,
        eps=1e-10,
    )
    mel_spectrogram = MelSpectrogram(
        fs=22050,
        fft_size=1024,
        hop_size=256,
        win_length=None,
        window="hann",
        num_mels=80,
        fmin=80,
        fmax=7600,
        eps=1e-10,
    ).to(dtype=torch.double)
    mel_torch = mel_spectrogram(torch.from_numpy(x).unsqueeze(0))
    np.testing.assert_array_almost_equal(
        mel_npy.transpose(1, 0).astype(np.float32),
        mel_torch[0].numpy().astype(np.float32),
    )