root
root

Reputation: 59

How to fix ''numpy.float64' object cannot be interpreted as an integer"

I am working on depression detection and when I tried to run this part of the code I got the error message below. I am using python 3.6

I have tried everything to fix this line :

samples = int (np.append(np.zeros(np.floor(frameSize/2.0)), sig))   

by:

samples = np.append(np.zeros(int (frameSize/2.0)), sig)

or

samples = np.append(np.zeros((frameSize//2), sig)

or

samples = np.append(np.zeros(np.floor((int(frameSize))/2.0)), sig)

Also I have changed the numpy version from 1.15.4 to 1.11.0 but I still have the same problem. I don't know how to fix this problem.

The code is :

import numpy as np
from numpy.lib import stride_tricks
import os
from PIL import Image
import scipy.io.wavfile as wav

def stft(sig, frameSize, overlapFac=0.5, window=np.hanning):
"""
Short-time Fourier transform of audio signal.
"""
   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)

ERROR message :

 File "E:/depression detection/features/spectrograms.py", line 21, in stft
 samples = int (np.append(np.zeros(np.floor(frameSize/2.0)), sig))

TypeError: 'numpy.float64' object cannot be interpreted as an integer

Upvotes: 1

Views: 9597

Answers (3)

Emmanuel Ameyaw
Emmanuel Ameyaw

Reputation: 101

Try these, they work for me,

replace frames and samples with these

frames = stride_tricks.as_strided((samples), shape=(int(cols), int(frameSize)), strides=((samples).strides[0]*int(hopSize)

samples = np.append(np.zeros(int(np.floor(frameSize/2.0))), sig)    

Upvotes: 0

Nikolay Frick
Nikolay Frick

Reputation: 2205

In my case I had to downgrade numpy version 1.18.0 to 1.17.4.

Upvotes: 5

Pravin RGMishra
Pravin RGMishra

Reputation: 119

because

    samples = np.append(samples, np.zeros(frameSize))

outputs a list..np.zeroes gives list

Upvotes: 0

Related Questions