librosa是一個很是強大的python語音信號處理的第三方庫,本文參考的是librosa的官方文檔,本文主要總結了一些重要,對我來講很是經常使用的功能。學會librosa後不再用用python去實現那些複雜的算法了,只須要一句語句就能輕鬆實現。html
先總結一下本文中經常使用的專業名詞:sr:採樣率、hop_length:幀移、overlapping:連續幀之間的重疊部分、n_fft:窗口大小、spectrum:頻譜、spectrogram:頻譜圖或叫作語譜圖、amplitude:振幅、mono:單聲道、stereo:立體聲python
librosa.load(path, sr=22050, mono=True, offset=0.0, duration=None)
讀取音頻文件。默認採樣率是22050,若是要保留音頻的原始採樣率,使用sr = None。git
參數:github
返回:算法
librosa.resample(y, orig_sr, target_sr, fix=True, scale=False)
從新採樣從orig_sr到target_sr的時間序列數組
參數:app
返回:函數
librosa.get_duration(y=None, sr=22050, S=None, n_fft=2048, hop_length=512, center=True, filename=None)
計算時間序列的的持續時間(以秒爲單位)spa
參數:.net
返回:
librosa.get_samplerate(path)
參數:
返回:音頻文件的採樣率
librosa.output.write_wav(path, y, sr, norm=False)
將時間序列輸出爲.wav文件
參數:
計算音頻時間序列的過零率。
librosa.feature.zero_crossing_rate(y, frame_length = 2048, hop_length = 512, center = True)
參數:
返回:
y, sr = librosa.load(librosa.util.example_audio_file()) print(librosa.feature.zero_crossing_rate(y)) # array([[ 0.134, 0.139, ..., 0.387, 0.322]])
librosa.display.waveplot(y, sr=22050, x_axis='time', offset=0.0, ax=None)
繪製波形的幅度包絡線
參數:
import librosa.display import matplotlib.pyplot as plt y, sr = librosa.load(librosa.util.example_audio_file(), duration=10) librosa.display.waveplot(y, sr=sr) plt.show()
librosa.stft(y, n_fft=2048, hop_length=None, win_length=None, window='hann', center=True, pad_mode='reflect')
短時傅立葉變換(STFT),返回一個複數矩陣使得D(f,t)
參數:
win_length=n_fft
。scipy.signal.hanning
返回:
librosa.istft(stft_matrix, hop_length=None, win_length=None, window='hann', center=True, length=None)
短時傅立葉逆變換(ISTFT),將複數值D(f,t)頻譜矩陣轉換爲時間序列y,窗函數、幀移等參數應與stft相同
參數:
scipy.signal.hanning
返回:
librosa.amplitude_to_db(S, ref=1.0)
將幅度頻譜轉換爲dB標度頻譜。也就是對S取對數。與這個函數相反的是librosa.db_to_amplitude(S)
參數:
返回:
librosa.core.power_to_db(S, ref=1.0)
將功率譜(幅度平方)轉換爲分貝(dB)單位,與這個函數相反的是librosa.db_to_power(S)
參數:
返回:
import librosa.display import numpy as np import matplotlib.pyplot as plt y, sr = librosa.load(librosa.util.example_audio_file()) S = np.abs(librosa.stft(y)) print(librosa.power_to_db(S ** 2)) # array([[-33.293, -27.32 , ..., -33.293, -33.293], # [-33.293, -25.723, ..., -33.293, -33.293], # ..., # [-33.293, -33.293, ..., -33.293, -33.293], # [-33.293, -33.293, ..., -33.293, -33.293]], dtype=float32) plt.figure() plt.subplot(2, 1, 1) librosa.display.specshow(S ** 2, sr=sr, y_axis='log') # 從波形獲取功率譜圖 plt.colorbar() plt.title('Power spectrogram') plt.subplot(2, 1, 2) # 相對於峯值功率計算dB, 那麼其餘的dB都是負的,注意看後邊cmp值 librosa.display.specshow(librosa.power_to_db(S ** 2, ref=np.max), sr=sr, y_axis='log', x_axis='time') plt.colorbar(format='%+2.0f dB') plt.title('Log-Power spectrogram') plt.set_cmap("autumn") plt.tight_layout() plt.show()
功率譜和dB功率譜
librosa.display.specshow(data, x_axis=None, y_axis=None, sr=22050, hop_length=512)
參數:
import librosa.display import numpy as np import matplotlib.pyplot as plt y, sr = librosa.load(librosa.util.example_audio_file()) plt.figure() D = librosa.amplitude_to_db(np.abs(librosa.stft(y)), ref=np.max) plt.subplot(2, 1, 1) librosa.display.specshow(D, y_axis='linear') plt.colorbar(format='%+2.0f dB') plt.title('線性頻率功率譜') plt.subplot(2, 1, 2) librosa.display.specshow(D, y_axis='log') plt.colorbar(format='%+2.0f dB') plt.title('對數頻率功率譜') plt.show()
librosa.filters.mel(sr, n_fft, n_mels=128, fmin=0.0, fmax=None, htk=False, norm=1)
建立一個濾波器組矩陣以將FFT合併成Mel頻率
參數:
返回:Mel變換矩陣
melfb = librosa.filters.mel(22050, 2048) # array([[ 0. , 0.016, ..., 0. , 0. ], # [ 0. , 0. , ..., 0. , 0. ], # ..., # [ 0. , 0. , ..., 0. , 0. ], # [ 0. , 0. , ..., 0. , 0. ]]) import matplotlib.pyplot as plt plt.figure() librosa.display.specshow(melfb, x_axis='linear') plt.ylabel('Mel filter') plt.title('Mel filter bank') plt.colorbar() plt.tight_layout() plt.show()
librosa.feature.melspectrogram(y=None, sr=22050, S=None, n_fft=2048, hop_length=512, win_length=None, window='hann', center=True, pad_mode='reflect', power=2.0)
計算Mel scaled 頻譜,
若是提供了頻譜圖輸入S,則經過mel_f.dot(S)將其直接映射到mel_f上。
若是提供了時間序列輸入y,sr,則首先計算其幅值頻譜S,而後經過mel_f.dot(S ** power)將其映射到mel scale上 。默認狀況下,power= 2在功率譜上運行。
參數:
win_length = n_fft
scipy.signal.get_window
scipy.signal.hanning
返回:Mel頻譜shape=(n_mels, t)
import librosa.display import numpy as np import matplotlib.pyplot as plt y, sr = librosa.load(librosa.util.example_audio_file()) # 方法一:使用時間序列求Mel頻譜 print(librosa.feature.melspectrogram(y=y, sr=sr)) # array([[ 2.891e-07, 2.548e-03, ..., 8.116e-09, 5.633e-09], # [ 1.986e-07, 1.162e-02, ..., 9.332e-08, 6.716e-09], # ..., # [ 3.668e-09, 2.029e-08, ..., 3.208e-09, 2.864e-09], # [ 2.561e-10, 2.096e-09, ..., 7.543e-10, 6.101e-10]]) # 方法二:使用stft頻譜求Mel頻譜 D = np.abs(librosa.stft(y)) ** 2 # stft頻譜 S = librosa.feature.melspectrogram(S=D) # 使用stft頻譜求Mel頻譜 plt.figure(figsize=(10, 4)) librosa.display.specshow(librosa.power_to_db(S, ref=np.max), y_axis='mel', fmax=8000, x_axis='time') plt.colorbar(format='%+2.0f dB') plt.title('Mel spectrogram') plt.tight_layout() plt.show()
Log-Mel Spectrogram特徵是目前在語音識別和環境聲音識別中很經常使用的一個特徵,因爲CNN在處理圖像上展示了強大的能力,使得音頻信號的頻譜圖特徵的使用越發普遍,甚至比MFCC使用的更多。在librosa中,Log-Mel Spectrogram特徵的提取只需幾行代碼:
import librosa y, sr = librosa.load('./train_nb.wav', sr=16000) # 提取 mel spectrogram feature melspec = librosa.feature.melspectrogram(y, sr, n_fft=1024, hop_length=512, n_mels=128) logmelspec = librosa.amplitude_to_db(melspec) # 轉換到對數刻度 print(logmelspec.shape) # (128, 65)
可見,Log-Mel Spectrogram特徵是二維數組的形式,128表示Mel頻率的維度(頻域),64爲時間幀長度(時域),因此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特徵只須要一個函數:
librosa.feature.mfcc(y=None, sr=22050, S=None, n_mfcc=20, dct_type=2, norm='ortho', **kwargs)
參數:
返回:
M: MFCC序列
import librosa y, sr = librosa.load('./train_nb.wav', sr=16000) # 提取 MFCC feature mfccs = librosa.feature.mfcc(y=y, sr=sr, n_mfcc=40) print(mfccs.shape) # (40, 65)