序言
Librosa是一个用于音频、音乐分析、处理的python工具包,一些常见的时频处理、特征提取、绘制声音图形等功能应有尽有,功能十分强大。本文主要介绍librosa的安装与使用方法。
Librosa官网提供了多种安装方法,详细如下:
最简单的方法就是进行pip安装,可以满足所有的依赖关系,命令如下:
pip install librosa
如果安装了Anaconda,可以通过conda命令安装:
conda install -c conda-forge librosa
直接使用源码安装,需要提前下载源码(https://github.com/librosa/librosa/releases/),通过下面命令安装:
tar xzf librosa-VERSION.tar.gz cd librosa-VERSION/ python setup.py install
这部分介绍了最常用的音频处理函数,包括音频读取函数load( ),重采样函数resample( ),短时傅里叶变换stft( ),幅度转换函数amplitude_to_db( )以及频率转换函数hz_to_mel( )等。这部分函数很多,详细可参考librosa官网 http://librosa.github.io/ librosa/core.html
本部分列举了一些常用的频谱特征的提取方法,包括常见的Mel Spectrogram、MFCC、CQT等。函数详细信息可参考http:// librosa.github.io/librosa/feature.html
包含了常用的频谱显示函数specshow( ), 波形显示函数waveplot( ),详细信息请参考http://librosa.github.io/librosa/display. html
-
>>>
import librosa
-
>>>
# Load a wav file
-
>>> y, sr = librosa.load(
'./beat.wav')
-
>>> y
-
array([
0.00000000e+00,
0.00000000e+00,
0.00000000e+00, ...,
-
8.12290182e-06,
1.34394732e-05,
0.00000000e+00], dtype=float32)
-
>>> sr
-
22050
Librosa默认的采样率是22050,如果需要读取原始采样率,需要设定参数sr=None:
-
>>>
import librosa
-
>>>
# Load a wav file
-
>>> y, sr = librosa.load(
'./beat.wav', sr=
None)
-
>>> sr
-
44100
可见,'beat.wav'的原始采样率为44100。如果需要重采样,只需要将采样率参数sr设定为你需要的值:
-
>>>
import librosa
-
>>>
# Load a wav file
-
>>> y, sr = librosa.load(
'./beat.wav', sr=
16000)
-
>>> sr
-
16000
Log-Mel Spectrogram特征是目前在语音识别和环境声音识别中很常用的一个特征,由于CNN在处理图像上展现了强大的能力,使得音频信号的频谱图特征的使用愈加广泛,甚至比MFCC使用的更多。在librosa中,Log-Mel Spectrogram特征的提取只需几行代码:
-
>>>
import librosa
-
>>>
# Load a wav file
-
>>> y, sr = librosa.load(
'./beat.wav', sr=
None)
-
>>>
# extract mel spectrogram feature
-
>>> melspec = librosa.feature.melspectrogram(y, sr, n_fft=
1024, hop_length=
512, n_mels=
128)
-
>>>
# convert to log scale
-
>>> logmelspec = librosa.power_to_db(melspec)
-
>>> logmelspec.shape
-
(
128,
194)
可见,Log-Mel Spectrogram特征是二维数组的形式,128表示Mel频率的维度(频域),194为时间帧长度(时域),所以Log-Mel Spectrogram特征是音频信号的时频表示特征。其中,n_fft指的是窗的大小,这里为1024;hop_length表示相邻窗之间的距离,这里为512,也就是相邻窗之间有50%的overlap;n_mels为mel bands的数量,这里设为128。
MFCC特征是一种在自动语音识别和说话人识别中广泛使用的特征。关于MFCC特征的详细信息,有兴趣的可以参考博客http:// blog.csdn.net/zzc15806/article/details/79246716。在librosa中,提取MFCC特征只需要一个函数:
-
>>>
import librosa
-
>>>
# Load a wav file
-
>>> y, sr = librosa.load(
'./beat.wav', sr=
None)
-
>>>
# extract mfcc feature
-
>>> mfccs = librosa.feature.mfcc(y=y, sr=sr, n_mfcc=
40)
-
>>> mfccs.shape
-
(
40,
194)
关于mfcc,这里就不在赘述。
Librosa还有很多其他音频特征的提取方法,比如CQT特征、chroma特征等,在第二部分“librosa常用功能”给了详细的介绍。
Librosa有显示声音波形函数waveplot( ):
-
>>>
import librosa
-
>>>
import librosa.display
-
>>>
# Load a wav file
-
>>> y, sr = librosa.load(
'./beat.wav', sr=
None)
-
>>>
# plot a wavform
-
>>> plt.figure()
-
>>> librosa.display.waveplot(y, sr)
-
>>> plt.title(
'Beat wavform')
-
>>> plt.show()
输出图形为:
Librosa有显示频谱图波形函数specshow( ):
-
>>>
import librosa
-
>>>
import librosa.display
-
>>>
# Load a wav file
-
>>> y, sr = librosa.load(
'./beat.wav', sr=
None)
-
>>>
# extract mel spectrogram feature
-
>>> melspec = librosa.feature.melspectrogram(y, sr, n_fft=
1024, hop_length=
512, n_mels=
128)
-
>>>
# convert to log scale
-
>>> logmelspec = librosa.power_to_db(melspec)
-
>>>
# plot mel spectrogram
-
>>> plt.figure()
-
>>> librosa.display.specshow(logmelspec, sr=sr, x_axis=
'time', y_axis=
'mel')
-
>>> plt.title(
'Beat wavform')
-
>>> plt.show()
输出结果为:
将声音波形和频谱图绘制在一张图表中:
-
>>>
import librosa
-
>>>
import librosa.display
-
>>>
# Load a wav file
-
>>> y, sr = librosa.load(
'./beat.wav', sr=
None)
-
>>>
# extract mel spectrogram feature
-
>>> melspec = librosa.feature.melspectrogram(y, sr, n_fft=
1024, hop_length=
512, n_mels=
128)
-
>>>
# convert to log scale
-
>>> logmelspec = librosa.power_to_db(melspec)
-
>>> plt.figure()
-
>>>
# plot a wavform
-
>>> plt.subplot(
2,
1,
1)
-
>>> librosa.display.waveplot(y, sr)
-
>>> plt.title(
'Beat wavform')
-
>>>
# plot mel spectrogram
-
>>> plt.subplot(
2,
1,
2)
-
>>> librosa.display.specshow(logmelspec, sr=sr, x_axis=
'time', y_axis=
'mel')
-
>>> plt.title(
'Mel spectrogram')
-
>>> plt.tight_layout()
#保证图不重叠
-
>>> plt.show()
输出结果为:
到这里,librosa的安装和简单使用就介绍完了。事实上,librosa远不止这些功能,关于librosa更多的使用方法还请大家参考librosa官网http://librosa.github.io/librosa/index.html
参考:http://librosa.github.io/librosa/index.html