0

My code is:

import numpy
from scipy.io.wavfile import read

audio_file_location = 'file_location'

audio_file = read(audio_file_location)
n = numpy.array( audio_file[1],dtype=float )
size = n.size
w = 410
limit = 205
delta  = n.size/410
i = 0
j = 0
a = 1

while i < w:
    J = min(size, j+delta)
    if J > j:
        b = numpy.max( n[j:J] )
    else:
        break

    # Do stuff
    i = i+1
    j = j+delta
    if i >= limit:
        # just something to stop the script.
        # add a break point in the next line of code.
        # and then run the top part of the script two or 3 times.
        a = 1+a

When ran, it throws:

ValueError: zero-size array to reduction operation maximum which has no identity

Other NumPy arrays such as:

numpy.array(range(0, 176942))

and other audio files work ok. The error is thrown with several audio files, not just one.

What can the source of this error be?

I don't think the file is corrupted. But if it is,

What can I do to check/fix it?

Note:

The error will happen when i > limit for limit = 205, 2 or 3 iterations after this limit is reached. The values of j and J can vary depending on the length of the audio file. Examples of j and J values are: 197760, 198720; and 88786, 89217.

Full trace back:

File "<ipython-input-14-7d842d790860>", line 1, in <module>
runfile('.../Documents/wav_analizer/questoin_on_numpy.size.py', wdir='.../Documents/wav_analizer')

File ".../.local/lib/python2.7/site-packages/spyder/utils/site/sitecustomize.py", line 705, in runfile
execfile(filename, namespace)

File ".../.local/lib/python2.7/site-packages/spyder/utils/site/sitecustomize.py", line 94, in execfile
builtins.execfile(filename, *where)

File ".../Documents/wav_analizer/questoin_on_numpy.size.py", line 30, in <module>
b = numpy.max( n[j:J] )

File ".../.local/lib/python2.7/site-packages/numpy/core/fromnumeric.py", line 2320, in amax
out=out, **kwargs)

File ".../.local/lib/python2.7/site-packages/numpy/core/_methods.py", line 26, in _amax
return umr_maximum(a, axis, None, out, keepdims)

ValueError: zero-size array to reduction operation maximum which has no identity

Partial solution: ok, I partially solved the problem:

N = [[223,32],[34,653],[-123,54],[-34,23] ...[12,43],[,123,32]], 

so that explains the error. However, When I try to parse N, I get an index problem:

import os
import numpy
from scipy.io.wavfile import read

working_dir = os.path.dirname(os.path.realpath(__file__))
audio_file_location = os.path.join(working_dir, 'test_sounds/guitarup_full.wav')

audio_file = read(audio_file_location)

delta_t = audio_file[0] 
n = audio_file[1]     # this is a numpy array
print "Size",  n.size
print "Shape", n.shape

for i in range(0, n.size-1):
    a = n[i][0]
    b = n[i][1]

for i in range(0, n.shape[0]-1):
    a = n[i][0]
    b = n[i][1]

Output:

"Size" 176942
"Shape" (88471, 2)

/.local/lib/python2.7/site-packages/scipy/io/wavfile.py:273: WavFileWarning: Chunk (non-data) not understood, skipping it.
WavFileWarning)

Traceback (most recent call last):

File "<ipython-input-51-7d842d790860>", line 1, in <module>
runfile('/Documents/wav_analizer/questoin_on_numpy.size.py', wdir='/Documents/wav_analizer')

File "/.local/lib/python2.7/site-packages/spyder/utils/site/sitecustomize.py", line 705, in runfile
execfile(filename, namespace)

File "/.local/lib/python2.7/site-packages/spyder/utils/site/sitecustomize.py", line 94, in execfile
builtins.execfile(filename, *where)

File "/Documents/wav_analizer/questoin_on_numpy.size.py", line 26, in <module>
a = n[i][0]

IndexError: index 88471 is out of bounds for axis 0 with size 88471

Why the discrepancy between size and shape?

6
  • 2
    Please include the full traceback. Currently we don't know what line of code throws that error. Commented Jul 5, 2018 at 19:10
  • 1
    Is this Python 2 or 3? Commented Jul 5, 2018 at 19:11
  • 1
    Also, please give us enough information about the input to diagnose the problem. What's the size or shape of the array that fails? What's the value of J and j when it fails? Commented Jul 5, 2018 at 19:13
  • 2
    A print-debugging suggestion: add the line print(n.shape, j, J) right before b = numpy.max( n[j:J] ). Commented Jul 5, 2018 at 19:19
  • the error will happen when i > limit. The values of j and J can vary depending on the length of the audio file. examples of j and J values are: 197760, 198720 Commented Jul 5, 2018 at 19:56

2 Answers 2

1

I'll guess that this fails for sound files with only one channel, i.e. mono instead of stereo. In this case when you create the n array you take the first element of the channel instead of the fist channel as a whole.

You can try something like:

if audio_file.ndims > 1:
    audio_file = audio_file[1]
n = numpy.array(audio_file, dtype=float)

If the data is already one dimensional, use it directly. If the data has at least 2 channels, take the second one as you did in your code.

Sign up to request clarification or add additional context in comments.

4 Comments

but I already to: n = numpy.array( audio_file[1],dtype=float ),what is the difference?
I get: AttributeError: 'tuple' object has no attribute 'ndims'
The above is not a solution. See: docs.scipy.org/doc/scipy-0.19.0/reference/generated/… scipy.io.wavfile.read returns (int,numpy.array)
My bad, I thought the function returned only the data. I suppose that the origin of the error is still the number of channels, but my fix is wrong.
0

Solution:

I found a solution. The following script works:

import os
import scipy.io.wavfile
import numpy

audio_file_location = "your audio file location"

F = scipy.io.wavfile.read(audio_file_location)

delta_t = F[0]
N = F[1]

print "shape:", N.shape
print "dtype:", N.dtype
print "min, max:", N.min(), N.max()
print "first value:", N[0]
print ""

chennel_1 = numpy.zeros(N.size)
chennel_2 = numpy.zeros(N.size)

i = 0
for x in N:
    chennel_1[i] = x[0]
    chennel_2[i] = x[1]
    i = i+1

i = 0
while i < 100:
    print "ch_1", i, chennel_1[i]
    print "ch_2", i, chennel_2[i]
    i = i+1

Still don't understand the discrepancy in the shape vs size. If anyone can explain, that would be great.

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.