IndexError:使用多值命令行参数时列表索引之外的范围
我有两个代码:一个用于预处理音频文件,另一个用于生成MFCC。 MFCC代码具有3个多值命令行参数(帧长度,帧步长和FFT长度)。我将MFCC定义为:
def mfcc(audio,sample_rate,pre_emp):
for fl in args['frame_length']:
for fs in args['frame_step']:
for fft in args['fft_length']:
audio = np.pad(audio,(Paddinggg(fl,fs,sample_rate),0),mode='reflect')
audio = audio.astype('float32')
#Normalization
audio = tf.keras.utils.normalize(audio)
#Preemphasis
audio = Preemphasis(audio,pre_emp)
stfts = tf.signal.stft(audio,fl,fs,fft,window_fn=tf.signal.hann_window)
spectrograms = tf.abs(stfts)
num_spectrogram_bins = stfts.shape[-1]
lower_edge_hertz, upper_edge_hertz, num_mel_bins = 0.0, sample_rate/2.0, 32
linear_to_mel_weight_matrix = tf.signal.linear_to_mel_weight_matrix(num_mel_bins, num_spectrogram_bins, sample_rate, lower_edge_hertz,upper_edge_hertz)
mel_spectrograms = tf.tensordot(spectrograms, linear_to_mel_weight_matrix, 1)
mel_spectrograms.set_shape(spectrograms.shape[:-1].concatenate(linear_to_mel_weight_matrix.shape[-1:]))
# Compute a stabilized log to get log-magnitude mel-scale spectrograms.
log_mel_spectrograms = tf.math.log(mel_spectrograms + 1e-6)
return log_mel_spectrograms
用于预处理的代码使用上述函数,如下所示:
X = []
Y = []
preemphasis = 0.985
print("Feature Extraction Started")
for i,class_list in enumerate(data_list): #datalist = all files, class list = folder name in datalist, sample = path to the audio file in that particular class list
for j,samples in enumerate(class_list): #samples are of the form classes_name/audio file
if(samples.endswith('.wav')):
sample_rate,audio = wavfile.read(os.path.join(C["dire"],samples))
if(audio.size<sample_rate):
audio = np.pad(audio,(sample_rate-audio.size,0),mode="constant")
coeff = mfccwithpaddingandcmd.mfcc(audio,sample_rate,preemphasis) # 0.985 = preemphasis
X.append(coeff)
#print(X)
if(samples.split('/')[0] in classes):
Y.append(samples.split('/')[0])
elif(samples.split('/')[0]=='_background_noise_'):
Y.append('silence')
# #X= coefficient array and Y = name of the class
A = np.zeros((len(X),X[0].shape[0],X[0][0].shape[0]),dtype='object')
for i in range(0,len(X)):
A[i] = np.array(X[i]) #Converting list X into array A
# print(A.shape)
现在,当我尝试定义A时,我遇到了错误,
Traceback (most recent call last):
File "C:\Users\Aarti\.spyder-py3\preprocessingwithpaddingandcmd.py", line 151, in <module>
pp()
File "C:\Users\Aarti\.spyder-py3\preprocessingwithpaddingandcmd.py", line 104, in pp
A = np.zeros((len(X),X[0].shape[0],X[0][0].shape[0]),dtype='object')
IndexError: list index out of range
我怀疑这是因为多价值命令行参数。我正确吗?我如何解决这个问题,其中有多价命令行参数以及a的多个值?
I have two codes: one for preprocessing an audio file and another for generating the MFCC. The MFCC code has 3 multi-valued command line arguments (frame length, frame step and fft length). I have defined mfcc as:
def mfcc(audio,sample_rate,pre_emp):
for fl in args['frame_length']:
for fs in args['frame_step']:
for fft in args['fft_length']:
audio = np.pad(audio,(Paddinggg(fl,fs,sample_rate),0),mode='reflect')
audio = audio.astype('float32')
#Normalization
audio = tf.keras.utils.normalize(audio)
#Preemphasis
audio = Preemphasis(audio,pre_emp)
stfts = tf.signal.stft(audio,fl,fs,fft,window_fn=tf.signal.hann_window)
spectrograms = tf.abs(stfts)
num_spectrogram_bins = stfts.shape[-1]
lower_edge_hertz, upper_edge_hertz, num_mel_bins = 0.0, sample_rate/2.0, 32
linear_to_mel_weight_matrix = tf.signal.linear_to_mel_weight_matrix(num_mel_bins, num_spectrogram_bins, sample_rate, lower_edge_hertz,upper_edge_hertz)
mel_spectrograms = tf.tensordot(spectrograms, linear_to_mel_weight_matrix, 1)
mel_spectrograms.set_shape(spectrograms.shape[:-1].concatenate(linear_to_mel_weight_matrix.shape[-1:]))
# Compute a stabilized log to get log-magnitude mel-scale spectrograms.
log_mel_spectrograms = tf.math.log(mel_spectrograms + 1e-6)
return log_mel_spectrograms
The code for preprocessing uses the above function as shown below:
X = []
Y = []
preemphasis = 0.985
print("Feature Extraction Started")
for i,class_list in enumerate(data_list): #datalist = all files, class list = folder name in datalist, sample = path to the audio file in that particular class list
for j,samples in enumerate(class_list): #samples are of the form classes_name/audio file
if(samples.endswith('.wav')):
sample_rate,audio = wavfile.read(os.path.join(C["dire"],samples))
if(audio.size<sample_rate):
audio = np.pad(audio,(sample_rate-audio.size,0),mode="constant")
coeff = mfccwithpaddingandcmd.mfcc(audio,sample_rate,preemphasis) # 0.985 = preemphasis
X.append(coeff)
#print(X)
if(samples.split('/')[0] in classes):
Y.append(samples.split('/')[0])
elif(samples.split('/')[0]=='_background_noise_'):
Y.append('silence')
# #X= coefficient array and Y = name of the class
A = np.zeros((len(X),X[0].shape[0],X[0][0].shape[0]),dtype='object')
for i in range(0,len(X)):
A[i] = np.array(X[i]) #Converting list X into array A
# print(A.shape)
Now when I am trying to define A, I am getting the error
Traceback (most recent call last):
File "C:\Users\Aarti\.spyder-py3\preprocessingwithpaddingandcmd.py", line 151, in <module>
pp()
File "C:\Users\Aarti\.spyder-py3\preprocessingwithpaddingandcmd.py", line 104, in pp
A = np.zeros((len(X),X[0].shape[0],X[0][0].shape[0]),dtype='object')
IndexError: list index out of range
I have doubts that it is because of multi-valued command line arguments. Am I correct? How can I solve this problem where I have multi-valued command line arguments and so multiple values of A?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论