Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How should I put numpy in the list?

I’m a student and I’m going to divide the wave file into 5 seconds and preprocess it with MFCC, Python

This is the full np.array of the file. And np.array divided by 5 seconds.

print(audios[0].shape)
print(audios[0][0:int(sr*5)].shape

(9091892,)
(220500,)

So I wrote the following code to divide it into 5 seconds with numpy.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

audios_five = []
for i in range(len(audios)):#0~23
    print("audios"+str(i+1)+".wav")
    for j in range(len((audios[i])//int(sr*5))+1):
        audios_five[i].append((audios[i][int(sr*5)*j:int(sr*5)*(j+1)]).tolist())
        print("audios_five"+str(i+1)+"_"+str(j+1)+".wav")
        print(audios_five[i][j])

And that’s the result.

enter image description here

I ask for your help me. I have no idea what the problem is…

>Solution :

The problem

The problem is that the ‘list index is out of range’, meaning that i is not a valid index in audios_five.
This makes sense, given that audios_five is created as an empty list at the start of the code snippet. If you run the following (simplified) code, you get the same error:

test = []
test[0].append('something')

Yields

----> 1 test[0].append('something')
IndexError: list index out of range

The solution

For ease of notation, lets refer to (audios[i][int(sr*5)*j:int(sr*5)*(j+1)]).tolist() as x.

What is necessary depends on what you want to achieve.

Option 1: Create a nested list

I’m not much for dealing with audio in Python, but what your currently trying to do is append something to the first value in audios_five – i.e. create a nested list. If this is what you want, you first need an audios_five.append(#create some item) before you can append to this item with audio_five[0].append.

The result would be something like

audios_five = [[x1, x2, x3], [x4, x5, x6], ...]

Option 2: Create a flat list

However, if you want a flat list, so you just want to append x to the audios_five, then you would be looking at using audios_five.append(x).

The result would then be something like

audios_five = [x1, x2, x3, x4, ...]

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading