#!/usr/bin/env python # coding: utf-8 # In[ ]: import librosa import librosa.display import numpy as np import pandas as pd import scipy from scipy.stats import skew import matplotlib.pyplot as plt SAMPLE_RATE = 44100 def get_mfcc(name, path): data, _ = librosa.core.load(path + name, sr = SAMPLE_RATE) assert _ == SAMPLE_RATE try: ft1 = librosa.feature.mfcc(data, sr = SAMPLE_RATE, n_mfcc=30) ft2 = librosa.feature.zero_crossing_rate(data)[0] ft3 = librosa.feature.spectral_rolloff(data)[0] ft4 = librosa.feature.spectral_centroid(data)[0] ft5 = librosa.feature.spectral_contrast(data)[0] ft6 = librosa.feature.spectral_bandwidth(data)[0] ft1_trunc = np.hstack((np.mean(ft1, axis=1), np.std(ft1, axis=1), skew(ft1, axis = 1), np.max(ft1, axis = 1), np.median(ft1, axis = 1), np.min(ft1, axis = 1))) ft2_trunc = np.hstack((np.mean(ft2), np.std(ft2), skew(ft2), np.max(ft2), np.median(ft2), np.min(ft2))) ft3_trunc = np.hstack((np.mean(ft3), np.std(ft3), skew(ft3), np.max(ft3), np.median(ft3), np.min(ft3))) ft4_trunc = np.hstack((np.mean(ft4), np.std(ft4), skew(ft4), np.max(ft4), np.median(ft4), np.min(ft4))) ft5_trunc = np.hstack((np.mean(ft5), np.std(ft5), skew(ft5), np.max(ft5), np.median(ft5), np.min(ft5))) ft6_trunc = np.hstack((np.mean(ft6), np.std(ft6), skew(ft6), np.max(ft6), np.median(ft6), np.max(ft6))) return pd.Series(np.hstack((ft1_trunc, ft2_trunc, ft3_trunc, ft4_trunc, ft5_trunc, ft6_trunc))) except: print('bad file') return pd.Series([0]*210) def MFCC_spectrogram(y, sr): # 提取頻域音頻資料 MFCC spectrogram feature melspec = librosa.feature.melspectrogram(y, sr, n_fft=1024, hop_length=512, n_mels=128) # 轉成 log scale 已呈現頻譜資料 logmelspec = librosa.power_to_db(melspec) # 繪圖 plt.figure() librosa.display.specshow(logmelspec, sr=sr, x_axis='time', y_axis='mel') plt.title('Features of unknow frog by MFCC (Mel Frquency Cepstral Coefficients)') plt.show() def FeatureExtraction(fname): # 創建文件檔 test= pd.DataFrame({'fname':[fname]}) # 特徵提取 test_data = test['fname'].progress_apply(get_mfcc, path='') test_data = test_data.values return test_data