我正在使用从Github获得的以下代码。此代码提取了mfccs,色度,质谱图,tonnetz和光谱对比功能,以feat.np的形式输出。我想提取rmse,zerocross等其他功能,但是当我添加relevent代码时,在连接时会出错。
# coding= UTF-8
#
# Author: Fing
# Date : 2017-12-03
#
import glob
import os
import librosa
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.pyplot import specgram
import soundfile as sf
def extract_feature(file_name):
X, sample_rate = sf.read(file_name, dtype='float32')
if X.ndim > 1:
X = X[:,0]
X = X.T
# short term fourier transform
stft = np.abs(librosa.stft(X))
# mfcc
mfccs = np.mean(librosa.feature.mfcc(y=X, sr=sample_rate, n_mfcc=40).T,axis=0)
# chroma
chroma = np.mean(librosa.feature.chroma_stft(S=stft, sr=sample_rate).T,axis=0)
# melspectrogram
mel = np.mean(librosa.feature.melspectrogram(X, sr=sample_rate).T,axis=0)
# spectral contrast
contrast = np.mean(librosa.feature.spectral_contrast(S=stft, sr=sample_rate).T,axis=0)
tonnetz = np.mean(librosa.feature.tonnetz(y=librosa.effects.harmonic(X), sr=sample_rate).T,axis=0)
return mfccs,chroma,mel,contrast,tonnetz
def parse_audio_files(parent_dir,sub_dirs,file_ext='*.wav'):
features, labels = np.empty((0,193)), np.empty(0)
for label, sub_dir in enumerate(sub_dirs):
for fn in glob.glob(os.path.join(parent_dir, sub_dir, file_ext)):
try:
mfccs, chroma, mel, contrast,tonnetz = extract_feature(fn)
except Exception as e:
print("[Error] extract feature error. %s" % (e))
continue
ext_features = np.hstack([mfccs,chroma,mel,contrast,tonnetz])
print(ext_features)
print(features)
features = np.vstack([features,ext_features])
# labels = np.append(labels, fn.split('/')[1])
labels = np.append(labels, label)
print("extract %s features done" % (sub_dir))
return np.array(features), np.array(labels, dtype = np.int)
def one_hot_encode(labels):
n_labels = len(labels)
n_unique_labels = len(np.unique(labels))
one_hot_encode = np.zeros((n_labels,n_unique_labels))
one_hot_encode[np.arange(n_labels), labels] = 1
return one_hot_encode
# Get features and labels
r = os.listdir("data/")
r.sort()
features, labels = parse_audio_files('data', r)
np.save('feat.npy', features)
np.save('label.npy', labels)
`
此代码工作正常,但是当我想提取其他功能(如rmse,过零率等)时。当我添加
#rmse=np.mean(librosa.feature.rmse(y=X).T,axis=0)
我遇到以下错误
File "C:\Users\HP\Anaconda2\lib\site-packages\numpy\core\shape_base.py", line 237, in vstack
return _nx.concatenate([atleast_2d(_m) for _m in tup], 0)
ValueError: all the input array dimensions except for the concatenation axis must match exactly
我如何提取其他特征并同时进行连接。
答案 0 :(得分:0)
STFT和RMSE的形状不同于MFCC和其他功能。 STFT和MFCC是二维的,而其他则是一维的。