如何修复“”数字浮点数64'对象不能解释为整数”

2024-06-28 10:52:19 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在研究抑郁症检测,当我试图运行这部分代码时,我收到了下面的错误消息。我使用的是python3.6

我想尽一切办法来修复这条线:

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

签署人:

^{pr2}$

或者

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

或者

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

另外,我已经将numpy版本从1.15.4改为1.11.0,但是我仍然有同样的问题。我不知道怎么解决这个问题。在

代码是:

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)

错误消息:

 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

Tags: importnumpyframesasnpzerosbewindow