from scipy.io.wavfile import read
filepath = glob.glob('*.wav')
rates = []
datas = []
for fp in filepath:
    rate, data = read(fp)
    rates.append(rate)
    datas.append(data)

I get a list 'datas' which is :

[array([0, 0, 0, ..., 0, 0, 0], dtype=int16), array([0, 0, 0, ..., 0, 0, 1], dtype=int16), array([0, 0, 0, ..., 0, 0, 0], dtype=int16),..., array([0, 0, 0, ..., 0, 0, 0], dtype=int16)]

and I use

new_array = numpy.vstack([datas])

to get the new_array :

[[array([0, 0, 0, ..., 0, 0, 0], dtype=int16)
  array([0, 0, 0, ..., 0, 0, 1], dtype=int16)
  array([0, 0, 0, ..., 0, 0, 0], dtype=int16)
  ...
  array([0, 0, 0, ..., 0, 0, 0], dtype=int16)]]

But I really prefer one is :

(array([[ 0,  0,  0, ...,  0,  0,  0],
   [ 0,  0,  0, ...,  0,  0,  1],
   [ 0,  0,  0, ...,  0,  0,  0],
   ...,        
   [ 0,  0,  0, ...,  0,  0,  0]], dtype=int16)

Which function should I use?

Thanks.

share
    
I believe you don't need the extra [] in the vstack call. Just try: new_array = numpy.vstack(datas). – ely Oct 8 '13 at 17:45
    
I try to remove [], but I got a error message "ValueError: all the input array dimensions except for the concatenation axis must match exactly." – user2858910 Oct 8 '13 at 18:05
    
Can you print datas[0].shape? – ely Oct 8 '13 at 18:12
    
I print datas[0].shape , it's (1308662,) , and I print datas[1].shape , it's (1306358,). – user2858910 Oct 8 '13 at 18:26
    
Well, you won't be able to vertically stack the data if each potential row has a different length. So first you need to sort out why the data have different lengths if they are conceptually supposed to be rows from the same matrix. – ely Oct 8 '13 at 18:26
up vote 2 down vote accepted

The following works for me, so either the elements of datas are not flat arrays like your question suggests, the potential rows have different lengths (this turned out to be the reason, see comments), or perhaps you are using an older version that has a problem with a 1-dimensional object in vstack? (although I think that is unlikely)

In [14]: datas = [np.asarray([0, 0, 0, 0, 0, 0]), np.asarray([0, 0, 0, 0, 0, 1])]

In [15]: datas
Out[15]: [array([0, 0, 0, 0, 0, 0]), array([0, 0, 0, 0, 0, 1])]

In [16]: datas[0].shape
Out[16]: (6,)

In [17]: np.vstack(datas)
Out[17]:
array([[0, 0, 0, 0, 0, 0],
       [0, 0, 0, 0, 0, 1]])
share

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.