0

first post! I've looked through a lot of other posts on this problem but can't find anything that applies to my code.

I'm trying to read an audio file and then find the max and min values of the array of samples, x.
wavread() is a function defined in another module that I've imported.
It returns fs, x.
x is a one-dimensional array (x.shape = (150529,).)

def minMaxAudio(inputFile):
    (fs, x) = wavread(inputFile)
    max_val = numpy.amax(x)
    min_val = numpy.amin(x) 
    return (min_val, max_val)

when I type these lines individually into ipython, I get the result I want. but when I call this function from an imported .py file I get the error:

ValueError: The truth value of an array with more than one element is ambiguous.   
Use a.any() or a.all()

It highlights the last line (return statement) as the location of the error.

Every other post on this that I've looked at includes some sort of evaluation or comparison operator in the code. Mine doesn't have one... does it?!

Thanks!

Craicerjack
  • 6,203
  • 2
  • 31
  • 39
gwens
  • 1
  • 2
  • 2
    It's there a further stack trace? – Dave G Oct 18 '16 at 10:13
  • Agree with Dave. Looks like the error source is where you call the function. – Leonid Mednikov Oct 18 '16 at 10:18
  • I tried restarting ipython and now it works. I didn't know that just running 'import module' again wouldn't actually reload the file after making a change. You have to run reload(module). The confusing thing is that ipython was *printing out* the changed code when I called it... but not actually running that code, running an old version instead? Is this really correct behaviour? I'm on Ubuntu. – gwens Oct 18 '16 at 10:29
  • What do you mean "printing out the changed code?" Anyway, yes, importing a module again in the same session will not reload a file after making a change. That is normal – juanpa.arrivillaga Oct 18 '16 at 10:35
  • I mean that when the error was thrown, it printed out the lines of code as they are in my latest version, highlighting line 30 as the location of the error. There was originally an 'if' statement on that line and I suppose that is what was really throwing the error. But the way it displayed in the terminal was misleading. I realised the problem when I made another change to the file and line 30 became part of a comment block, but ipython was still showing an error on line 30. – gwens Oct 18 '16 at 10:40
  • @juanpa.arrivillaga I think this is a quirk of iPython. I use Enthought Canopy and it's the same - the module is not updated with the changed code but the stack trace will look into the new module and print the updated code surrounding the line that threw the error. – roganjosh Oct 18 '16 at 10:40
  • @roganjosh Aha. I see. – juanpa.arrivillaga Oct 18 '16 at 10:41
  • Possible duplicate of [ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()](http://stackoverflow.com/questions/10062954/valueerror-the-truth-value-of-an-array-with-more-than-one-element-is-ambiguous) – Anurag Sinha Oct 18 '16 at 11:59
  • Since reloading has solved the problem, I'd suggest deleting the question. Otherwise it might be closed as duplicate. – hpaulj Oct 18 '16 at 12:11

1 Answers1

0

Have you noticed that if your WAV file has more than one channel, say it is stereo, min_val and max_val will be arrays themselves?

Such a code would trigger the error you encounter:

min, max = minMaxAudio('acdc.wav')
# Assuming floats
if max > 1:
    print('saturation')

Whereas the following will work:

min, max = minMaxAudio('acdc.wav')
# Assuming floats
if np.any(max > 1):
    print('saturation')
Balzola
  • 456
  • 3
  • 11