赞
踩
读取音频
提取特征Log-Mel Spectrogram
MFCC
绘制波形图和梅尔频谱图
prerequisites
install
起始点检测 onset detection tutorial
Librosa是一个用于音频、音乐分析、处理的python工具包,一些常见的时频处理、特征提取、绘制声音图形等功能应有尽有,功能十分强大
pip install librosa
-专业名词:- sr:采样率、hop_length:帧移、overlapping:连续帧之间的重叠部分、n_fft:窗口大小、spectrum:频谱、spectrogram:频谱图或叫做语谱图、amplitude:振幅、mono:单声道、stereo:立体声
# 加载音频data,sample_rate = librosa.load('./test.wav')print('data:',data.shape,'sample_rate:',sample_rate)
# 特征提取:Log-Mel Spectrogram特征melspec = librosa.feature.melspectrogram(data, sample_rate, n_fft=1024, hop_length=512, n_mels=128)# n_fft指的是窗的大小,这里为1024;hop_length表示相邻窗之间的距离,这里为512,也就是相邻窗之间有50%的overlap;n_mels为mel bands的数量,这里设为128print('Mel频率的维度(频域):',melspec.shape[0],'Mel频率的时间帧长度(时域):',melspec.shape[1],'show data:',melspec)# 转化为对数: Log-Mel Spectrogram特征是音频信号的时频表示特征。logmelspec = librosa.power_to_db(melspec)print('Mel频率的维度(频域):',logmelspec.shape[0],'Mel频率的时间帧长度(时域):',logmelspec.shape[1],'show data:',logmelspec)
# MFCC特征是一种在自动语音识别和说话人识别中广泛使用的特征mfccs = librosa.feature.mfcc(y=data, sr=sample_rate, n_mfcc=40)print('mfccs',mfccs.shape,mfccs)
plt.figure()plt.subplot(2, 1, 1)# 绘制波形图librosa.display.waveplot(data,sample_rate)plt.title('beat waveform')plt.subplot(2, 1, 2)# 绘制频谱图librosa.display.specshow(logmelspec, sr=sample_rate, x_axis='time', y_axis='mel')plt.title('Mel spectrogram')plt.tight_layout() #保证图不重叠plt.show()
python=2.7+ 或python=3.5+
import numpy as npimport matplotlib.pyplot as pltimport madmomsignal,sample_rate = madmom.audio.signal.load_wave_file('data/sample.wav')print('sample',signal,type(signal),' sample rate',sample_rate)# 读取音频数字信号sig = madmom.audio.signal.Signal('data/sample.wav')print('sig',sig,' sample_rate',sig.sample_rate)# 将信号重采样framedsignal = madmom.audio.signal.FramedSignal(sig,frame_size=2048,hop_size=441)print('fs',framedsignal,framedsignal.frame_size,' [0]',framedsignal[0],' [10]',framedsignal[10] , ' fps',framedsignal.fps,' num_frames',framedsignal.num_frames,'hop_size',framedsignal.hop_size)fs = madmom.audio.signal.FramedSignal(sig, frame_size=2048, fps=200)print('fs',fs,fs.frame_size,' [0]',fs[0],' [10]',fs[10] , ' fps',fs.fps,' num_frames',fs.num_frames,'fs.hop_size',fs.hop_size)
# STFTstft = madmom.audio.stft.STFT(fs)print('stft',stft,type(stft),stft[0])# 频谱图spec = madmom.audio.spectrogram.Spectrogram(stft)plt.imshow(spec[:, :200].T, aspect='auto', origin='lower')plt.show()
from scipy.ndimage.filters import maximum_filterspec = madmom.audio.spectrogram.Spectrogram('data/sample.wav')print('spec',spec,type(spec))# calculate the differencediff = np.diff(spec, axis=0)# keep only the positive differencespos_diff = np.maximum(0, diff)# sum everything to get the spectral fluxsf = np.sum(pos_diff, axis=1)plt.figure()plt.imshow(spec[:, :200].T, origin='lower', aspect='auto')plt.show()plt.figure()plt.imshow(pos_diff[:, :200].T, origin='lower', aspect='auto')plt.show()plt.figure()plt.plot(sf)plt.show()
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。