0
import math
import pylab
from matplotlib.pylab import *
import numpy as np
import scipy.fftpack
from scipy.io.wavfile import read
w = read('c:/users/ggg.wav')
a=np.array(w[1])
l=len(a)
#from __future__ import division
#b=(l/w[0])
b=(float(l)/w[0])
c=b*1000
d=int(c/40)
print d
print l/d
e=l/d
for i in range(0,d):
    k[9]=np.array(a[(i*e)+1:((i+1)*e)])
print k

this is a python code to frame an audio signal. But when i executed this code,i got an error "ValueError: setting an array element with a sequence.". How can i avoid this error?

1
  • 2
    At which line is the error? Commented Sep 3, 2013 at 1:42

2 Answers 2

1

There is another problem with your code I can at least help you with:

You can't assign k[9] without k being undefined. E.g:

>>> k[9] = 'test'
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'k' is not defined

'k' needs to be defined as an array and needs to get the 'proper' index. You can't assign the index on it straight after. See the following examples:

>>> k[9]='test'
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IndexError: list assignment index out of range

and

>>> k = [None]*10
>>> k[9]='test'
>>> k
[None, None, None, None, None, None, None, None, None, 'test']
Sign up to request clarification or add additional context in comments.

3 Comments

actually sorry for the trouble. not k[9], i typed it mistakely. actually i mean k[i].
@santhu I think what he is indicating is you need to create k. You can do this by k = np.empty(d) before the for loop.
Thanks Ophion, I indeed meant that, but I've never worked with numpy so I had no clue it had to be done differently. :)
0

This works fine with a sample .wav

w = read('ggg.wav')
a = w[1]  # already an array
l=len(a)
b=(float(l)/w[0])
c=b*1000
d=int(c/40)
e=l/d
k = a[:e*d].reshape(d,e)
print k.shape
print k
print ''
k = [[] for i in range(d)]  # initialize python list
for i in range(0,d):
    k[i] = a[(i*e)+1:((i+1)*e)]
for i in k:
    print i
# or
k = np.zeros((d,e-1),dtype='int') # initialize np.array
for i in range(d):
    k[i,:] = a[(i*e)+1:((i+1)*e)]
print k

w[1] is already an np.array. I think what you want to break a into blocks e long. To do that, I truncated a and reshaped it, producing my k. Your indexing misses a[0], a[e], etc.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.