0

我正在生成大量 mel 频谱图来训练 NN 进行音素检测。

每个 mel 频谱图(在 Python 中使用 librosa.core.melspectrogram 生成)都表示为一个 2D numpy 数组,其中轴 1(向量的长度)因频谱图而异。它们的形状从 (128, 2) 到 (128, 200) 不等。

为了生成 3D 数组,所有频谱图必须具有相同的形状,所以我猜我应该将零添加到小于 200 的向量的末端。然后我可以将它们全部添加到 Python 列表中,调用np.array 就可以生成一个 3D numpy 数组,对吧?

我自己尝试过,但没有成功。感谢所有帮助。

编辑:(已请求代码,这基本上是我想要做的)

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)
4

1 回答 1

0

我无法回答是否适合您的学习者用零填充。但是这样做很容易使用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)
于 2018-04-17T19:06:28.167 回答