我正在生成大量的mel频谱图来训练用于音素检测的NN。
每个mel谱图(使用Python语言中的librosa.core.melspectrogram生成)表示为2D numpy数组,其中轴1(向量的长度)因谱图而异。它们的形状从(128,2)到(128,200)不等。
为了生成3D数组,所有频谱图都必须具有相同的形状,所以我猜我应该在小于200的向量的末尾添加零。然后,我可以将它们全部添加到Python列表中,对其调用np.array,然后就会生成一个3Dnumpy数组,对吧?
我自己也尝试过,但没有成功。感谢所有的帮助。
编辑:(代码已被请求,这基本上就是我想要做的)
spectrograms = []
for audio_array in all_audio_arrays:
audio_array, sr = librosa.core.load(audio_file, sr=sample_rate, mono=True)
melspectrogram = librosa.feature.melspectrogram(y=audio_array, sr=sample_rate, S=None, n_fft=window_size, hop_length=hop_length)
# melspectrogram is a 2D numpy array
# the shape could be between (128, 2) and (128, 200)
spectrograms.append(melspectrogram)
# I want this to be 3D
np.asarray(spectrograms)发布于 2018-04-18 03:06:28
我不能回答这是否是一种适合你的学习者填充零的方法。但是使用np.concatenate很容易做到这一点。
import numpy as np
a = np.ones((128,2))
b = np.ones((128,200))
padding = np.zeros((a.shape[0], b.shape[1] - a.shape[1])) #(128, 198)
a = np.concatenate((a, padding), axis=1)
print (a.shape)
>>> (128L, 200L)https://stackoverflow.com/questions/49885181
复制相似问题