Python code error 2.7 -


i having trouble compiling program.

i new python.i using python 2.7.(anaconda)

here's code:

import numpy numpy import scipy scipy.io.wavfile import read scipy.io.wavfile import write def getwavdata(file):  return scipy.io.wavfile.read(file)[1] data=getwavdata('myvoice.wav') print data frameduration = 0.01  frequency          = 44100  numsamplesperframe = int(frequency * frameduration) framesize=int(frequency*frameduration) signalenergy=sum( [ abs(x)**2 x in data ] ) print 'signal energy',signalenergy threshold=signalenergy/3 print threshold  base=0 i=0   count=0  wordnumber=0  length=len(data)   print 'length of data is', length  word=[] while i<length:     #print 'entered while loop'         frame=data[base:framesize]     base=base+framesize     frameenergy=sum( [ abs(x)**2 x in frame ] )      if frameenergy < threshold:         count=count+1         word.append(frame)         word=numpy.array(word)     else :         count=0         word=[]      if count == 4:         print 'silence expected'         wordnumber=wordnumber+1         scipy.io.wavfile.write('word%d.wav' %wordnumber,44100,word)         count=0         word=[]       = i+1 

now problem when first ran program,it printed expected values.

it printed signalenergy=some value around 3000000 , threshold=some value around 1000000(1/3 of signalenergy) ,but error here:

scipy.io.wavfile.write('word%d.wav' %wordnumber,44100,word) 

error:list has no attribute array(something that.dont remember exact sentence)

i googled , found write() takes array 1 argument.

trying fix error has resulted in whole program not working.

now signalenergy , threshold,i getting array of 2 values,instead of single value.

the changes made:

while (base+framesize)<length:     #print 'entered while loop'     frame=data[base:base+framesize]     frameenergy=sum( [ abs(x)**2 x in frame ] )      print frameenergy     if frameenergy.all() < threshold.all():         count=count+1         word.append(frame)     else:         count=0         word=[]     if count == 4:         print 'word detected'         wordnumber=wordnumber+1         word = numpy.array(word)           scipy.io.wavfile.write('word'+str(wordnumber)+'.wav',44100,word)         count=0         word=[]     base = base + framesize  error:  [-29501  24682](**this giving signalenergy now**)   [-9834  8227]   length of data 122240 traceback (most recent call last):  file "c:\users\nancy semwal\documents\python scripts\program2.py",     line 34, in <module>    if frameenergy < threshold:   valueerror: truth value of array more 1 element ambiguous. use a.any() or a.all() 

i understand error,but unable why ran correctly first time?

what reason?what changes shall make?

it's hard read code , understand exact errors getting can see few obvious errors might move along:

  1. in first code, slice of list:
     frame=data[base:framesize]
    stop working once run through loop framesize time. since never increasing upper bound. slicing is:
     mylist[included_bottom_bound:excluded_top_bound]

    seem have fixed in second go-round though
     frame=data[base:base+framesize]
    although i'm not 100% sure intent is. looks looking through sound file low volume pieces. first time through walking 1 step ahead each time , looking @ next chunk of data, while 2nd time through @ chunk , move whole chunk sized step ahead. imagine sound file looked this:
     *--* 
    * (star) means high volume , - (dash) means low volume. imagine framesize 2. stepping 1 step @ time (as in attempt 1) you'd have 4 samples
    1. *-
    2. --
    3. -*
    4. *
    , you'd discover low volume section in (item #2 above).
    new way of doing things you'd end 2 samples
    1. *-
    2. -*
    , end missing low volume section you're hunting for.
  2. what's deal line:
     if frameenergy.all() < threshold.all():
    frameenergy sum it's going int or long or numeric @ least. can't all() on that. same deal threshold.

looking @ comments seems signalenergy not simple number? seems weird. maybe should put more print statements code , find out why.
downloaded sample wav file , ran code against , worked , printed bunch of wordx.wav files. didn't play mind code worked. fixed whitespace problems , such but.... here's final code:

<pre><code>import numpy numpy import scipy scipy.io.wavfile import read scipy.io.wavfile import write def getwavdata(file):     return scipy.io.wavfile.read(file)[1] data=getwavdata('e:\\music\\sounds\\carlin_letter.wav') print data frameduration = 0.01  frequency          = 44100  numsamplesperframe = int(frequency * frameduration) framesize=int(frequency*frameduration) signalenergy=sum( [ abs(x)**2 x in data ] ) print 'signal energy',signalenergy threshold=signalenergy/3 print threshold base=0 i=0 count=0 wordnumber=0 length=len(data) print 'length of data is', length word=[] while (base+framesize)&lt;length:     #print 'entered while loop'     frame=data[base:base+framesize]     frameenergy=sum( [ abs(x)**2 x in frame ] )      print frameenergy     if frameenergy &lt; threshold:         count=count+1         word.append(frame)     else:         count=0         word=[]     if count == 4:         print 'word detected'         wordnumber=wordnumber+1         word = numpy.array(word)           scipy.io.wavfile.write('e:\\music\\sounds\\word'+str(wordnumber)+'.wav',44100,word)         count=0         word=[]     base = base + framesize print "done"</code></pre> 

Comments

Popular posts from this blog

node.js - Using Node without global install -

How to access a php class file from PHPFox framework into javascript code written in simple HTML file? -

java - Null response to php query in android, even though php works properly -