IndexError:使用多值命令行参数时列表索引之外的范围

发布于 2025-01-29 10:27:31 字数 3092 浏览 4 评论 0原文

我有两个代码:一个用于预处理音频文件,另一个用于生成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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文