当前位置:网站首页>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)
边栏推荐
- depends工具查看exe和dll依赖关系
- ForkJoinPool
- 使用excel快速生成sql语句
- iCloud照片无法上传或同步怎么办?
- 详解单例模式
- System integration project management engineer certification high frequency examination site: prepare project scope management plan
- Sign up for Huawei cloud proposition in the "Internet +" competition, and you can take many gifts!
- 火山引擎入选国内首个《边缘计算产业全景图》
- It's not easy to say I love you | use the minimum web API to upload files
- 《客从何处来》
猜你喜欢

Leader: who can use redis expired monitoring to close orders and get out of here!

Only black-and-white box test is required for test opening post? No, but also learn performance test

How to solve the lock-in read-only alarm of AutoCAD Chinese language?

Multipass Chinese document - setting graphical interface

Solution of enterprise supply chain system in medical industry: realize collaborative visualization of medical digital intelligent supply chain

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

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

煤炭行业数智化供应商管理系统解决方案:数据驱动,供应商智慧平台助力企业降本增效

Sword finger offer 16 Integer power of numeric value

Small program container technology to promote the operation efficiency of the park
随机推荐
Advanced embedded application of uni app [day14]
The company was jailed for nonstandard bug during the test ~ [cartoon version]
[零基础学IoT Pwn] 环境搭建
「杂谈」对数据分析未来的几点思考
详解单例模式
Research on the principle of Tencent persistence framework mmkv
Apple Watch无法开机怎么办?苹果手表不能开机解决方法!
深度学习编译器的理解
Helping the ultimate experience, best practice of volcano engine edge computing
One script of unity actual combat realizes radar chart
抖音最新Xbogus,signature生成js逆向分析
Only black-and-white box test is required for test opening post? No, but also learn performance test
教你30分钟快速搭建直播间
漏洞复现----35、uWSGI PHP 目录遍历漏洞 (CVE-2018-7490)
PC端微信多开
NFT挖矿游GameFi链游系统开发搭建
Dependencies tool to view exe and DLL dependencies
Hcip (Huawei Senior Network Security Engineer) (Experiment 8) (MPLS basic experiment)
How to use AI technology to optimize the independent station customer service system? Listen to the experts!
助力极致体验,火山引擎边缘计算最佳实践