当前位置:网站首页>音频 librosa 库 与 torchaudio 库中 的 Mel- spectrogram 进行对比
音频 librosa 库 与 torchaudio 库中 的 Mel- spectrogram 进行对比
2022-06-30 17:32:00 【mingqian_chu】
四个部分介绍了代码生成
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)
# 使用 torchaudio 分解实现, 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))
# 得到melscale的转换矩阵
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)
边栏推荐
- 先写API文档还是先写代码?
- Vulnerability recurrence ----- 38. Thinkphp5 5.0.23 Remote Code Execution Vulnerability
- ForkJoinPool
- Talk about the SQL server version of DTM sub transaction barrier function
- Summary of methods for offline installation of chrome extensions in China
- LeetCode之合并二叉树
- MRO industrial products procurement management system: enable MRO enterprise procurement nodes to build a new digital procurement system
- C language structure
- 亲测flutter打包apk后大小,比较满意
- What if the apple watch fails to power on? Apple watch can not boot solution!
猜你喜欢

Apple Watch无法开机怎么办?苹果手表不能开机解决方法!

电子元器件招标采购商城:优化传统采购业务,提速企业数字化升级

Type ~ storage ~ variable in C #

LeetCode动态规划经典题(一)

Optimize with netcorebeauty Net core independent deployment directory structure

剑指 Offer 16. 数值的整数次方

If you want to learn software testing, you must see series, 2022 software testing engineer's career development

How to use AI technology to optimize the independent station customer service system? Listen to the experts!

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

CODING 正式入驻腾讯会议应用市场!
随机推荐
Tsinghua only ranks third? 2022 release of AI major ranking of Chinese Universities of soft science
NFT挖矿游GameFi链游系统开发搭建
Compilation problems and solutions of teamtalk winclient
MySQL advanced - index optimization (super detailed)
autocad中文语言锁定只读警报怎么解决?
Flink系列:checkpoint调优
[software testing] basic knowledge of software testing you need to know
OneFlow源码解析:算子签名的自动推断
In distributed scenarios, do you know how to generate unique IDs?
MySQL n'a pas pu trouver MySQL. Solution temporaire pour le fichier Sock
How to do a good job in software system demand research? Seven weapons make it easy for you to do it
Geoffrey Hinton: my 50 years of in-depth study and Research on mental skills
MySQL找不到mysql.sock文件的臨時解
The easynvr platform equipment channels are all online. What is the reason for the "network request failure" in the operation?
MySQL advanced - basic index and seven joins
Tensorflow2 ten must know for deep learning
【TiDB】TiCDC canal_json的实际应用
分布式场景下,你知道有几种生成唯一ID的方式嘛?
MySQL cannot find mysql Temporary solution of sock file
ForkJoinPool