File size: 1,248 Bytes
38c1604
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Checks quran_asr.features against torchaudio's Kaldi-compliance fbank (the reference recipe)."""
import os
import sys

import torch

sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
from quran_asr.features import fbank  # noqa: E402


def main():
    import torchaudio.compliance.kaldi as kaldi

    torch.manual_seed(0)
    worst = 0.0
    for n in (16000, 16000 * 3 + 123, 48000 + 77, 5000):
        wave = (torch.randn(n) * 0.1).clamp(-1, 1)
        ours = fbank(wave)
        ref = kaldi.fbank(
            wave[None, :], num_mel_bins=80, frame_length=25.0, frame_shift=10.0, dither=0.0,
            preemphasis_coefficient=0.97, remove_dc_offset=True, window_type="povey",
            round_to_power_of_two=True, low_freq=20.0, high_freq=-400.0, snip_edges=False,
            use_energy=False, use_log_fbank=True, use_power=True, sample_frequency=16000.0,
        )
        assert ours.shape == ref.shape, (ours.shape, ref.shape)
        diff = (ours - ref).abs().max().item()
        worst = max(worst, diff)
        print(f"{n} samples: {ours.shape[0]} frames, max |diff| = {diff:.2e}")
    assert worst < 1e-3, worst
    print("OK: features match the Kaldi recipe")


if __name__ == "__main__":
    main()