我遇到此错误,有人可以帮助我吗?
TypeError:'numpy.float64'对象不能解释为整数。
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(int(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)
<ipython-input-113-e40a989a9c6b> in stft(sig, frameSize, overlapFac, window)
10 samples = np.append(samples, np.zeros(frameSize))
11
---> 12 frames = stride_tricks.as_strided(samples, shape=(cols, frameSize), strides=(samples.strides[0]*hopSize, samples.strides[0])).copy()
13 frames *= win
14
~\AppData\Roaming\Python\Python37\site-packages\numpy\lib\stride_tricks.py in as_strided(x, shape, strides, subok, writeable)
101 interface['strides'] = tuple(strides)
102
--> 103 array = np.asarray(DummyArray(interface, base=x))
104 # The route via `__interface__` does not preserve structured
105 # dtypes. Since dtype should remain unchanged, we set it explicitly.
~\AppData\Roaming\Python\Python37\site-packages\numpy\core\numeric.py in asarray(a, dtype, order)
536
537 """
--> 538 return array(a, dtype, copy=False, order=order)
539
540
TypeError: 'numpy.float64' object cannot be interpreted as an integer
我不知道问题出在哪里,我读过的是python版本的问题,但事实并非如此。我不知道该怎么解决!
答案 0 :(得分:1)
问题可能出在cols
变量上。 np.ceil
返回np.float64
;是的,它是一个整数值,但仍然是float dtype。重读np.ceil
文档。
In [77]: np.ceil(1.23)
Out[77]: 2.0
In [78]: type(_)
Out[78]: numpy.float64
In [79]: np.ones((2,_77))
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-79-af1036080a73> in <module>
----> 1 np.ones((2,_77))
/usr/local/lib/python3.6/dist-packages/numpy/core/numeric.py in ones(shape, dtype, order)
212
213 """
--> 214 a = empty(shape, dtype, order)
215 multiarray.copyto(a, 1, casting='unsafe')
216 return a
TypeError: 'numpy.float64' object cannot be interpreted as an integer
可以在math
包中找到替代方法:
In [81]: import math
In [82]: math.ceil
Out[82]: <function math.ceil>
In [83]: math.ceil(1.23)
Out[83]: 2
In [84]: np.ones((1,math.ceil(1.23)))
Out[84]: array([[1., 1.]])
cols = int(cols)
也应该起作用。
答案 1 :(得分:-1)
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(int(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=(int(cols), frameSize), strides=(samples.strides[0]*hopSize, samples.strides[0])).copy()
frames *= win
return np.fft.rfft(frames)
最后,我找到了解决方案。非常感谢!