当前位置:网站首页>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)
边栏推荐
- Digital intelligent supplier management system solution for coal industry: data driven, supplier intelligent platform helps enterprises reduce costs and increase efficiency
- 漏洞复现----38、ThinkPHP5 5.0.23 远程代码执行漏洞
- Is it safe to open a mobile stock account? Is it reliable?
- 《Go题库·15》go struct 能不能比较?
- Oneortwo bugs in "software testing" are small things, but security vulnerabilities are big things. We must pay attention to them
- 使用excel快速生成sql语句
- The new Post-00 Software Test Engineer in 2022 is a champion
- 【TiDB】TiCDC canal_ Practical application of JSON
- EasyNVR平台设备通道均在线,操作出现“网络请求失败”是什么原因?
- VScode 状态条 StatusBar
猜你喜欢

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

Vulnerability recurrence ----37. Apache unomi Remote Code Execution Vulnerability (cve-2020-13942)

系统集成项目管理工程师认证高频考点:编制项目范围管理计划
![删除排序链表中的重复元素 II[链表节点统一操作--dummyHead]](/img/dd/7df8f11333125290b4b30183cfff64.png)
删除排序链表中的重复元素 II[链表节点统一操作--dummyHead]

System integration project management engineer certification high frequency examination site: prepare project scope management plan

php利用队列解决迷宫问题

Type ~ storage ~ variable in C #

教你30分钟快速搭建直播间

电子元器件行业在线采购系统精准匹配采购需求,撬动电子产业数字化发展

3.10 haas506 2.0开发教程-example-TFT
随机推荐
The easynvr platform equipment channels are all online. What is the reason for the "network request failure" in the operation?
Flink系列:checkpoint调优
【合集- 行业解决方案】如何搭建高性能的数据加速与数据编排平台
Sword finger offer 17 Print from 1 to maximum n digits
CODING 正式入驻腾讯会议应用市场!
Multipass中文文档-设置图形界面
Rhai - Rust 的嵌入式脚本引擎
电子元器件招标采购商城:优化传统采购业务,提速企业数字化升级
Small program container technology to promote the operation efficiency of the park
【TiDB】TiCDC canal_json的实际应用
System integration project management engineer certification high frequency examination site: prepare project scope management plan
Do you write API documents or code first?
Force deduction solution summary 1175- prime number arrangement
countdownlatch 和 completableFuture 和 CyclicBarrier
Rhai 脚本引擎的简单应用示例
At present, the big guys are joining the two streams of flinksql, cdcmysql and Kafka, and the results are put into MySQL or KA
How to do a good job in software system demand research? Seven weapons make it easy for you to do it
Vscode status bar statusbar
[cloud resident co creation] Huawei iconnect enables IOT terminals to connect at one touch
TiDB Dashboard里面可以写sql执行吗