当前位置:网站首页>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)
边栏推荐
- Rust 书籍资料 - 芽之家书馆
- AI chief architect 10-aica-lanxiang, propeller frame design and core technology
- Summary of methods for offline installation of chrome extensions in China
- Leader: who can use redis expired monitoring to close orders and get out of here!
- 音频 librosa 库 与 torchaudio 库中 的 Mel- spectrogram 进行对比
- Four tips tell you how to use SMS to promote business sales?
- Flink系列:checkpoint调优
- How to solve the lock-in read-only alarm of AutoCAD Chinese language?
- 抖音最新Xbogus,signature生成js逆向分析
- 充值满赠,IM+RTC+X 全通信服务「回馈季」开启
猜你喜欢

Four tips tell you how to use SMS to promote business sales?

【TiDB】TiCDC canal_ Practical application of JSON

Type ~ storage ~ variable in C #

AI chief architect 10-aica-lanxiang, propeller frame design and core technology

The online procurement system of the electronic components industry accurately matches the procurement demand and leverages the digital development of the electronic industry

充值满赠,IM+RTC+X 全通信服务「回馈季」开启

又一篇CVPR 2022论文被指抄袭,平安保险研究者控诉IBM苏黎世团队

详解单例模式

Sword finger offer 16 Integer power of numeric value

挑选智能音箱时,首选“智能”还是“音质”?这篇文章给你答案
随机推荐
【TiDB】TiCDC canal_ Practical application of JSON
SaaS project management system solution for the financial service industry helps enterprises tap a broader growth service space
分布式事务
电子元器件招标采购商城:优化传统采购业务,提速企业数字化升级
Tensorflow2 深度学习十必知
Deep learning compiler understanding
又一篇CVPR 2022论文被指抄袭,平安保险研究者控诉IBM苏黎世团队
Leader: who can use redis expired monitoring to close orders and get out of here!
Adhering to the concept of 'home in China', 2022 BMW children's traffic safety training camp was launched
小程序容器技术,促进园区运营效率提升
Volcano engine was selected into the first "panorama of edge computing industry" in China
[Collection - industry solutions] how to build a high-performance data acceleration and data editing platform
Summary of methods for offline installation of chrome extensions in China
系统集成项目管理工程师认证高频考点:编制项目范围管理计划
漏洞复现----38、ThinkPHP5 5.0.23 远程代码执行漏洞
CODING 正式入驻腾讯会议应用市场!
Unity开发bug记录100例子(第1例)——打包后shader失效或者bug
Teach you to quickly set up a live studio in 30 minutes
[cloud resident co creation] Huawei iconnect enables IOT terminals to connect at one touch
100 examples of bug records of unity development (the first example) -- shader failure or bug after packaging