我的桌面上存储了一个文件夹,其中包含 187 个 WAV 格式的音频文件。我想从音频文件中提取特征,我执行了以下代码来提取特征音频并将特征保存在 CSV 文件中,但是我获得了没有值的 excel 文件,它只包含每列的标题和 len 的输出(音频文件)是0..但它必须是187。我该如何解决这个问题???
from glob import glob
data_dir = './audio featur-extraction\audio-setA/'
audio_files = glob(data_dir + '*.wav')
len(audio_files)
from librosa import feature
import numpy as np
fn_list_i = [
feature.chroma_stft,
feature.spectral_centroid,
feature.spectral_bandwidth,
feature.spectral_rolloff,
]
fn_list_ii = [
feature.zero_crossing_rate
]
def get_feature_vector(y,sr):
feat_vect_i = [ np.mean(funct(y,sr)) for funct in fn_list_i]
feat_vect_ii = [ np.mean(funct(y)) for funct in fn_list_ii]
feature_vector = feat_vect_i + feat_vect_ii
return feature_vector
#build the matrix with normal audios featurized
audios_feat = []
for file in audio_files:
'''
y is the time series array of the audio file, a 1D np.ndarray
sr is the sampling rate, a number
'''
y,sr = librosa.load(file,sr=None)
feature_vector = get_feature_vector(y, sr)
audios_feat.append(feature_vector)
print('.', end= " ")
print(audios_feat)
#.........................
import csv
norm_output = 'normals_00.csv'
header =[
'chroma_stft',
'spectral_centroid',
'spectral_bandwidth',
'spectral_rolloff',
'zero_crossing_rate',
]
#WARNING : this overwrites the file each time. Be aware of this because feature extraction step takes time.
with open(norm_output,'+w') as f:
csv_writer = csv.writer(f, delimiter = ',')
csv_writer.writerow(header)
csv_writer.writerows(audios_feat)