当前位置:网站首页>Compare the audio librosa library with the Mel spectrogram in the torchaudio library
Compare the audio librosa library with the Mel spectrogram in the torchaudio library
2022-06-30 18:52:00 【mingqian_ chu】
Four sections describe code generation
import torch
import torchaudio
import librosa
import numpy as np
filename = "./sample.wav"
waveform, sample_rate = torchaudio.load(filename)
a = waveform.squeeze()
n_fft = 20
win_length = 20
hop_length = 10
# -------- part 1. torch spectrogram --------------
# pytorch spectrogram
# numpy --> tensor() , b = torch.from_numpy(a)
spec = torchaudio.transforms.Spectrogram(n_fft, win_length, hop_length)
spec_pytorch = spec(torch.Tensor(a))
print(" 1. torchaudio way spectrogram: ", spec_pytorch.shape)
# ---------- part 2. librosa spectrogram --------------
# librosa spectrogram
def Spectrogram(input, n_fft, win_length, hop_length):
stft_librosa = librosa.stft(y=input,
hop_length=hop_length,
n_fft=n_fft)
spec_librosa = pow(np.abs(stft_librosa),2)
return spec_librosa
a = a.numpy()
spec_librosa = Spectrogram(a, n_fft, win_length, hop_length)
print(" 2. librosa way spectrogram: ", spec_librosa.shape)
# --------------part 3. torchaudio Mel spectrogram -----------------
n_fft = 20
win_length = 20
hop_length = 10
sample_rate = 16000
mel_len = 12
mel_spec = torchaudio.transforms.MelSpectrogram(sample_rate, n_fft, win_length, hop_length, n_mels=mel_len)
mel_out = mel_spec(torch.tensor(a).to(torch.float))
print(" 3.1 torchaudio way Mel spectrogram: ", mel_out.shape)
# Use torchaudio Decomposition implementation , melspectrogram;
from torchaudio import functional as F
n_fft = 20
win_length = 20
hop_length = 10
sample_rate = 16000
mel_len = 12
stft_len = n_fft//2+1
spec = torchaudio.transforms.Spectrogram(n_fft, win_length, hop_length)
spec_out = spec(torch.Tensor(a))
# obtain melscale The transformation matrix of
tmp_fb = F.create_fb_matrix(stft_len, 0, sample_rate//2, mel_len, sample_rate)
mel_out1 = torch.matmul(spec_out.transpose(0,1), tmp_fb).transpose(0,1)
print(" 3.2 torchaudio way split step to generate Mel spectrogram: ", mel_out1.shape)
# --------------part 4 librosa Mel spectrogram -----------------
from torchaudio import functional as F
n_fft = 20
win_length = 20
hop_length = 10
sample_rate = 16000
mel_len = 12
stft_len = n_fft//2+1
tmp_fb = F.create_fb_matrix(stft_len, 0, sample_rate//2, mel_len, sample_rate)
def Spectrogram(input, n_fft, win_length, hop_length):
stft_librosa = librosa.stft(y=input,
hop_length=hop_length,
n_fft=n_fft)
spec_librosa = pow(np.abs(stft_librosa),2)
return spec_librosa
spec_librosa2 = Spectrogram(a, n_fft, win_length, hop_length)
mel_out2 = np.transpose(np.matmul(np.transpose( spec_librosa2),tmp_fb.numpy()))
print("4. librosa way to generate Mel spectrogram: ", mel_out2.shape)
边栏推荐
猜你喜欢
System integration project management engineer certification high frequency examination site: prepare project scope management plan
分布式场景下,你知道有几种生成唯一ID的方式嘛?
如何利用AI技术优化独立站客服系统?听听专家怎么说!
医院在线问诊小程序源码 互联网医院源码 智慧医院源码
The company was jailed for nonstandard bug during the test ~ [cartoon version]
Infineon - GTM architecture -generic timer module
Talk about the SQL server version of DTM sub transaction barrier function
mysql for update 死锁问题排查
ForkJoinPool
MRO industrial products procurement management system: enable MRO enterprise procurement nodes to build a new digital procurement system
随机推荐
3.10 haas506 2.0开发教程-example-TFT
One script of unity actual combat realizes radar chart
EasyNVR平台设备通道均在线,操作出现“网络请求失败”是什么原因?
Countdowncatch and completabilefuture and cyclicbarrier
C# Winform程序界面优化实例
4个技巧告诉你,如何使用SMS促进业务销售?
Small program container technology to promote the operation efficiency of the park
《Go题库·15》go struct 能不能比较?
删除排序链表中的重复元素 II[链表节点统一操作--dummyHead]
Rust 文件系统处理之文件读写 - Rust 实践指南
Vulnerability recurrence ----- 38. Thinkphp5 5.0.23 Remote Code Execution Vulnerability
《被讨厌的勇气:“自我启发之父”阿德勒的哲学课》
MySQL cannot find mysql Temporary solution of sock file
uni-app进阶之自定义【day13】
Another CVPR 2022 paper was accused of plagiarism, and Ping An insurance researchers sued IBM Zurich team
100 examples of bug records of unity development (the first example) -- shader failure or bug after packaging
Openlayers roller shutter map
Deep learning compiler understanding
C# Winform程序界面优化实例
OneFlow源码解析:算子签名的自动推断