經典的深度學習網絡AlexNet使用數據擴充(Data Augmentation)的方式擴大數據集,取得較好的分類效果。在深度學習的圖像領域中,經過平移、 翻轉、加噪等方法進行數據擴充。可是,在音頻(Audio)領域中,如何進行數據擴充呢?python
音頻的數據擴充,主要有如下四種方式:git
音頻解析基於librosa音頻庫;矩陣操做基於scipy和numpy科學計算庫。github
如下是Python的實現方式:網絡
音頻剪裁dom
import librosa
from scipy.io import wavfile
y, sr = librosa.load("../data/love_illusion.mp3") # 讀取音頻
print y.shape, sr
wavfile.write("../data/love_illusion_20s.mp3", sr, y[20 * sr:40 * sr]) # 寫入音頻
複製代碼
音頻旋轉函數
import librosa
import numpy as np
from scipy.io import wavfile
y, sr = librosa.load("../data/raw/love_illusion_20s.mp3") # 讀取音頻
y = np.roll(y, sr*10)
print y.shape, sr
wavfile.write("../data/raw/xxx_roll.mp3", sr, y) # 寫入音頻
複製代碼
音頻調音,注:cv庫的resize函數含有插值功能。學習
import cv2
import librosa
from scipy.io import wavfile
y, sr = librosa.load("../data/raw/love_illusion_20s.mp3") # 讀取音頻
ly = len(y)
y_tune = cv2.resize(y, (1, int(len(y) * 1.2))).squeeze()
lc = len(y_tune) - ly
y_tune = y_tune[int(lc / 2):int(lc / 2) + ly]
print y.shape, sr
wavfile.write("../data/raw/xxx_tune.mp3", sr, y_tune) # 寫入音頻
複製代碼
音頻加噪,注:在添加隨機噪聲時,保留0值,不然刺耳難忍!大數據
import librosa
from scipy.io import wavfile
import numpy as np
y, sr = librosa.load("../data/raw/love_illusion_20s.mp3") # 讀取音頻
wn = np.random.randn(len(y))
y = np.where(y != 0.0, y + 0.02 * wn, 0.0) # 噪聲不要添加到0上!
print y.shape, sr
wavfile.write("../data/raw/love_illusion_20s_w.mp3", sr, y) # 寫入音頻
複製代碼
歡迎Follow個人GitHub:https://github.com/SpikeKingspa
By C. L. Wang @ 美圖雲事業部code
OK, that's all! Enjoy it!