当前位置:网站首页>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)
边栏推荐
- Classic problem of leetcode dynamic programming (I)
- Rust 书籍资料 - 芽之家书馆
- At present, the big guys are joining the two streams of flinksql, cdcmysql and Kafka, and the results are put into MySQL or KA
- Infineon - GTM architecture -generic timer module
- Tensorflow2 ten must know for deep learning
- Countdowncatch and completabilefuture and cyclicbarrier
- Geoffrey Hinton:我的五十年深度学习生涯与研究心法
- The online procurement system of the electronic components industry accurately matches the procurement demand and leverages the digital development of the electronic industry
- [Collection - industry solutions] how to build a high-performance data acceleration and data editing platform
- The easynvr platform equipment channels are all online. What is the reason for the "network request failure" in the operation?
猜你喜欢

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

CODING 正式入驻腾讯会议应用市场!

Teach you to quickly set up a live studio in 30 minutes

Another CVPR 2022 paper was accused of plagiarism, and Ping An insurance researchers sued IBM Zurich team

It's not easy to say I love you | use the minimum web API to upload files

Merged binary tree of leetcode

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

【TiDB】TiCDC canal_ Practical application of JSON

助力极致体验,火山引擎边缘计算最佳实践

Digital intelligent supplier management system solution for coal industry: data driven, supplier intelligent platform helps enterprises reduce costs and increase efficiency
随机推荐
Tensorflow2 深度学习十必知
Multipass中文文档-设置图形界面
NFT挖矿游GameFi链游系统开发搭建
抖音最新Xbogus,signature生成js逆向分析
How to solve the lock-in read-only alarm of AutoCAD Chinese language?
C# Winform程序界面优化实例
深度学习编译器的理解
Optimize with netcorebeauty Net core independent deployment directory structure
countdownlatch 和 completableFuture 和 CyclicBarrier
小程序容器技术,促进园区运营效率提升
《被讨厌的勇气:“自我启发之父”阿德勒的哲学课》
麻烦问下 Flink支持同步数据到 sqlserver么
使用excel快速生成sql语句
How to do a good job in software system demand research? Seven weapons make it easy for you to do it
详解单例模式
Summary of methods for offline installation of chrome extensions in China
Geoffrey Hinton:我的五十年深度学习生涯与研究心法
PyTorch学习(三)
One script of unity actual combat realizes radar chart
PC端微信多开