santh
santh

Reputation: 23

Setting an array element with a sequence python

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?

Upvotes: 0

Views: 3373

Answers (2)

hpaulj
hpaulj

Reputation: 231395

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.

Upvotes: 0

Thijs de Z
Thijs de Z

Reputation: 375

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']

Upvotes: 1

Related Questions