test_online_mfcc.py (1216B)
1 #!/usr/bin/env python3 2 3 import sys 4 5 try: 6 import kaldifeat 7 except: 8 print("Please install kaldifeat first") 9 sys.exit(0) 10 11 import kaldi_native_fbank as knf 12 import torch 13 14 15 def main(): 16 sampling_rate = 16000 17 samples = torch.randn(sampling_rate * 10) 18 19 opts = kaldifeat.MfccOptions() 20 opts.frame_opts.dither = 0 21 opts.num_ceps = 40 22 opts.mel_opts.num_bins = 40 23 opts.mel_opts.high_freq = -200 24 opts.frame_opts.snip_edges = False 25 online_mfcc = kaldifeat.OnlineMfcc(opts) 26 27 online_mfcc.accept_waveform(sampling_rate, samples) 28 29 opts = knf.MfccOptions() 30 opts.frame_opts.dither = 0 31 opts.num_ceps = 40 32 opts.mel_opts.num_bins = 40 33 opts.mel_opts.high_freq = -200 34 opts.frame_opts.snip_edges = False 35 36 mfcc = knf.OnlineMfcc(opts) 37 mfcc.accept_waveform(sampling_rate, samples.tolist()) 38 39 assert online_mfcc.num_frames_ready == mfcc.num_frames_ready 40 for i in range(mfcc.num_frames_ready): 41 f1 = online_mfcc.get_frame(i) 42 f2 = torch.from_numpy(mfcc.get_frame(i)) 43 assert torch.allclose(f1, f2, atol=1e-3), (i, (f1 - f2).abs().max(), f1, f2) 44 45 46 if __name__ == "__main__": 47 torch.manual_seed(20240603) 48 main() 49 print("success")