首页 > 解决方案 > 用于语音特征提取的 MGCA 技术显示此错误(IndexError: list index out of range)

问题描述

通过执行这个从 wav 文件中提取语音特征的程序,我在代码中遇到了问题,错误说 IndexError: list index out of range

文件“C:/Users/KALEEM/PycharmProjects/Speech_Processing/2-Speech_Signal_Processing_and_Classification-master/feature_extraction_techniques/mgca.py”,第 77 行,在 mel_Generalized() 文件“C:/Users/KALEEM/PycharmProjects/Speech_Processing/2-Speech_Signal_Processing_and_Classification- master/feature_extraction_techniques/mgca.py”,第 74 行,mel_Generalized mgca_feature_extraction(wav) 文件“C:/Users/KALEEM/PycharmProjects/Speech_Processing/2-Speech_Signal_Processing_and_Classification-master/feature_extraction_techniques/mgca.py”,第 66 行,mgca_feature_extraction writeFeatures (mgca_features,wav) 文件“C:/Users/KALEEM/PycharmProjects/Speech_Processing/2-Speech_Signal_Processing_and_Classification-master/feature_extraction_techniques/mgca.py”,第 46 行,在 writeFeatures wav = makeFormat(wav) 文件“C:/Users/KALEEM/PycharmProjects/Speech_Processing/2-Speech_Signal_Processing_and_Classification-master/feature_extraction_techniques/mgca.py”,第 53 行,在 makeFormat wav = wav.split('/')[ 1].split('-')[1] IndexError: 列表索引超出范围

进程以退出代码 1 结束

#!usr/bin/python
from pysptk import *
from scipy import hamming
import numpy.matlib
import scipy
import scipy.io.wavfile as wav
import numpy as np
import wave
from python_speech_features.sigproc import *
from math import *
from six.moves import input as raw_input


def readWavFile(wav):
        #given a path from the keyboard to read a .wav file
        #wav = raw_input('Give me the path of the .wav file you want to read: ')
        inputWav = 'C:/Users/KALEEM/PycharmProjects/Speech_Processing/2-Speech_Signal_Processing_and_Classification-master/feature_extraction_techniques'+wav
        return inputWav
#reading the .wav file (signal file) and extract the information we need
def initialize(inputWav):
        rate , signal  = wav.read(readWavFile(inputWav)) # returns a wave_read object , rate: sampling frequency
        sig = wave.open(readWavFile(inputWav))
        # signal is the numpy 2D array with the date of the .wav file
        # len(signal) number of samples
        sampwidth = sig.getsampwidth()
        print ('The sample rate of the audio is: ',rate)
        print ('Sampwidth: ',sampwidth)
        return signal ,  rate
#implementation of the low-pass filter
def lowPassFilter(signal, coeff=0.97):
        return np.append(signal[0], signal[1:] - coeff * signal[:-1]) #y[n] = x[n] - a*x[n-1] , a = 0.97 , a>0 for low-pass filters
def preEmphasis(wav):
        #taking the signal
        signal , rate = initialize(wav)
        #Pre-emphasis Stage
        preEmphasis = 0.97
        emphasizedSignal = lowPassFilter(signal)
        Time=np.linspace(0, len(signal)/rate, num=len(signal))
        EmphasizedTime=np.linspace(0, len(emphasizedSignal)/rate, num=len(emphasizedSignal))
        return emphasizedSignal, signal , rate

def writeFeatures(mgca_features,wav):
        #write in a txt file the output vectors of every sample
        f = open('mel_generalized_features.txt','a')#sample ID
        #f = open('mfcc_featuresLR.txt','a')#only to initiate the input for the ROC curve
        wav = makeFormat(wav)
        np.savetxt(f,mgca_features,newline=",")
        f.write(wav)
        f.write('\n')

def makeFormat(wav):
        #if i want to keep only the gender (male,female)
        wav = wav.split('/')[1].split('-')[1]
        #only to make the format for Logistic Regression
        if (wav=='Female'):
                wav='1'
        else:
                wav='0'
        return wav
def mgca_feature_extraction(wav):
        #I pre-emphasized the signal with a low pass filter
        emphasizedSignal,signal,rate = preEmphasis(wav)
        #and now I have the signal windowed
        emphasizedSignal*=np.hamming(len(emphasizedSignal))
        mgca_features = 'mgcep(emphasizedSignal,order=12)'
        writeFeatures(mgca_features,wav)
def mel_Generalized():
        folder = raw_input('Give the name of the folder that you want to read data: ')
        amount = raw_input('Give the number of samples in the specific folder: ')
        for x in range(1,int(amount)+1):
                wav = '/'+folder+'/'+str(x)+'.wav'
                print (wav)

        mgca_feature_extraction(wav)

#def main():
mel_Generalized()
#main()

标签: python-3.xnumpyspeech-recognitionfeature-extraction

解决方案


这个问题很可能是由于意外的输入,这对我们来说很难测试。

更具体地说,在下面的代码中:

def makeFormat(wav):
    #if i want to keep only the gender (male,female)
    wav = wav.split('/')[1].split('-')[1]
    #only to make the format for Logistic Regression
    if (wav=='Female'):
            wav='1'
    else:
            wav='0'
    return wav

我会假设这wav是一个类似str的对象(或者无论如何支持的东西.split())。拆分的结果通常是一个Iterable,例如一个list。如果这样Iterable有 0 或 1 个元素,尝试访问它的第二个元素(使用[1])会提高IndexError: list index out of range你得到的。在您的情况下,wav不包含足够/(至少 1 个)、足够-(也至少 1 个)或两者兼有。


推荐阅读