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?
Jandjwhen it fails?print(n.shape, j, J)right beforeb = numpy.max( n[j:J] ).