返回

python-IndexError:使用多值命令行参数时列表索引超出范围

发布时间:2022-05-18 16:36:11 201
# node.js

我有两个代码:一个用于预处理音频文件,另一个用于生成 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 的多个值,我该如何解决这个问题?

特别声明:以上内容(图片及文字)均为互联网收集或者用户上传发布,本站仅提供信息存储服务!如有侵权或有涉及法律问题请联系我们。
举报
评论区(0)
按点赞数排序
用户头像