title: python numpy error "TypeError: 'numpy.float64' object cannot be interpreted as an integer"
body: <p>I want to convert the .wav file into a Spectrogram.</p>
  
  <p>So I used this Python file.</p>
  
  <pre><code>import glob
  import numpy as np
  from matplotlib import pyplot as plt
  import scipy.io.wavfile as wav
  from numpy.lib import stride_tricks
  
  """ short time fourier transform of audio signal """
  def stft(sig, frameSize, overlapFac=0.5, window=np.hanning):
      win = window(frameSize)
      hopSize = int(frameSize - np.floor(overlapFac * frameSize))
  
      # zeros at beginning (thus center of 1st window should be for sample nr. 0)
      samples = np.append(np.zeros(np.floor(frameSize/2.0)), sig)
      # cols for windowing
      cols = np.ceil( (len(samples) - frameSize) / float(hopSize)) + 1
      # zeros at end (thus samples can be fully covered by frames)
      samples = np.append(samples, np.zeros(frameSize))
  
      frames = stride_tricks.as_strided(samples, shape=(cols, frameSize), strides=(samples.strides[0]*hopSize, samples.strides[0])).copy()
      frames *= win
  
      return np.fft.rfft(frames)    
  
  """ scale frequency axis logarithmically """    
  def logscale_spec(spec, sr=44100, factor=20.):
      timebins, freqbins = np.shape(spec)
  
      scale = np.linspace(0, 1, freqbins) ** factor
      scale *= (freqbins-1)/max(scale)
      scale = np.unique(np.round(scale))
  
      # create spectrogram with new freq bins
      newspec = np.complex128(np.zeros([timebins, len(scale)]))
      for i in range(0, len(scale)):
          if i == len(scale)-1:
              newspec[:,i] = np.sum(spec[:,scale[i]:], axis=1)
          else:        
              newspec[:,i] = np.sum(spec[:,scale[i]:scale[i+1]], axis=1)
  
      # list center freq of bins
      allfreqs = np.abs(np.fft.fftfreq(freqbins*2, 1./sr)[:freqbins+1])
      freqs = []
      for i in range(0, len(scale)):
          if i == len(scale)-1:
              freqs += [np.mean(allfreqs[scale[i]:])]
          else:
              freqs += [np.mean(allfreqs[scale[i]:scale[i+1]])]
  
      return newspec, freqs
  
  """ plot spectrogram"""
  def plotstft(audiopath, binsize=2**10, plotpath=None, colormap="jet"):
      samplerate, samples = wav.read(audiopath)
      s = stft(samples, binsize)
  
      sshow, freq = logscale_spec(s, factor=1.0, sr=samplerate)
      ims = 20.*np.log10(np.abs(sshow)/10e-6) # amplitude to decibel
  
      timebins, freqbins = np.shape(ims)
  
      plt.figure(figsize=(15, 7.5))
      plt.imshow(np.transpose(ims), origin="lower", aspect="auto", cmap=colormap, interpolation="none")
      plt.colorbar()
  
      plt.xlabel("time (s)")
      plt.ylabel("frequency (hz)")
      plt.xlim([0, timebins-1])
      plt.ylim([0, freqbins])
  
      xlocs = np.float32(np.linspace(0, timebins-1, 5))
      plt.xticks(xlocs, ["%.02f" % l for l in ((xlocs*len(samples)/timebins)+(0.5*binsize))/samplerate])
      ylocs = np.int16(np.round(np.linspace(0, freqbins-1, 10)))
      plt.yticks(ylocs, ["%.02f" % freq[i] for i in ylocs])
  
      if plotpath:
          plt.savefig(plotpath, bbox_inches="tight")
      else:
          plt.show()
  
      plt.clf()
  
  
  if __name__ == '__main__':
      path='../tf_files/data_audio/'
  
      folders=glob.glob(path+'*')
      for folder in folders:
          waves = glob.glob(folder+'/' + '*.wav')
          print (waves)
          if len(waves) == 0:
              continue
          for f in waves:
              #try:
              print ("Generating spectrograms..")
              plotstft(f)
              #except Exception as e:
                  #print ("Something went wrong while generating spectrogram:")
  </code></pre>
  
  <p>However, the result was different from what I expected.</p>
  
  <blockquote>
    <p>['../tf_files/data_audio/test_wav_files/22601-8-0-0_2(volume).wav',
    '../tf_files/data_audio/test_wav_files/22601-8-0-6_2(volume).wav',
    '../tf_files/data_audio/test_wav_files/518-4-0-0(volume).wav',
    '../tf_files/data_audio/test_wav_files/drill1.wav',
    '../tf_files/data_audio/test_wav_files/chunk0.wav',
    '../tf_files/data_audio/test_wav_files/siren2.wav',
    '../tf_files/data_audio/test_wav_files/bark2.wav',
    '../tf_files/data_audio/test_wav_files/bark3.wav',
    '../tf_files/data_audio/test_wav_files/14111-4-0-0_2(volume).wav',
    '../tf_files/data_audio/test_wav_files/drill2.wav',
    '../tf_files/data_audio/test_wav_files/22601-8-0-3_2(volume).wav',
    '../tf_files/data_audio/test_wav_files/siren1.wav',
    '../tf_files/data_audio/test_wav_files/siren3.wav',
    '../tf_files/data_audio/test_wav_files/518-4-0-3(volume).wav',
    '../tf_files/data_audio/test_wav_files/drill3.wav',
    '../tf_files/data_audio/test_wav_files/4910-3-0-0_2(volume).wav',
    '../tf_files/data_audio/test_wav_files/344-3-5-0(volume).wav',
    '../tf_files/data_audio/test_wav_files/bark1.wav',
    '../tf_files/data_audio/test_wav_files/344-3-1-0(volume).wav']</p>
    
    <p>Generating spectrograms..</p>
    
    <p>Traceback (most recent call last):</p>
    
    <p>File "z_make_spectrogram.py", line 95, in 
        plotstft(f)   File "z_make_spectrogram.py", line 54, in plotstft
        s = stft(samples, binsize)   File "z_make_spectrogram.py", line 13, in stft
        samples = np.append(np.zeros(np.floor(frameSize/2.0)), sig)</p>
    
    <p>TypeError: 'numpy.float64' object cannot be interpreted as an integer
    Error in sys.excepthook:</p>
    
    <p>Traceback (most recent call last):   File
    "/usr/lib/python3/dist-packages/apport_python_hook.py", line 63, in
    apport_excepthook
        from apport.fileutils import likely_packaged, get_recent_crashes   File "/usr/lib/python3/dist-packages/apport/<strong>init</strong>.py", line 5, in
    
        from apport.report import Report   File "/usr/lib/python3/dist-packages/apport/report.py", line 30, in
    
        import apport.fileutils   File "/usr/lib/python3/dist-packages/apport/fileutils.py", line 23, in
    
        from apport.packaging_impl import impl as packaging   File "/usr/lib/python3/dist-packages/apport/packaging_impl.py", line 23, in
    
        import apt   File "/usr/lib/python3/dist-packages/apt/<strong>init</strong>.py", line 23, in 
        import apt_pkg</p>
    
    <p>ModuleNotFoundError: No module named 'apt_pkg'</p>
    
    <p>Original exception was: Traceback (most recent call last):  </p>
    
    <p>File "z_make_spectrogram.py", line 95, in 
        plotstft(f)   File "z_make_spectrogram.py", line 54, in plotstft
        s = stft(samples, binsize)   File "z_make_spectrogram.py", line 13, in stft
        samples = np.append(np.zeros(np.floor(frameSize/2.0)), sig)</p>
    
    <p>TypeError: 'numpy.float64' object cannot be interpreted as an integer</p>
  </blockquote>
  
  <p>When the 13th line was fixed with this syntax, The same error occurred too.:</p>
  
  <pre><code>samples = np.append(np.zeros(np.floor(int(frameSize/2.0))), sig)
  </code></pre>
  
  <p>For reference, I am currently using the tensorflow 1.4.</p>
  
  <p>Therefore, I am not sure if it is OK to change the numpy version to 1.11.</p>
  
  <p>Is there a way to correct this error?</p>
  
  <p>.</p>
  
  <p>.</p>
  
  <ul>
  <li>edited</li>
  </ul>
  
  <p>I fixed 13th line.:</p>
  
  <pre><code>samples = np.append(np.zeros(frameSize//2), sig)
  </code></pre>
  
  <p>And, I got this <a href="https://i.stack.imgur.com/31Z42.png" rel="nofollow noreferrer">result</a>.</p>
  
  <p>The same error still occurs and I don't know why.</p>