今天在跑语音频谱图的代码的时候出现了下面的错误:

Traceback (most recent call last):
File "/Users/admin/anaconda3/lib/python3.7/site-packages/numpy/core/function_base.py", line 117, in linspace
num = operator.index(num)
TypeError: 'float' object cannot be interpreted as an integer

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "spectrogram.py", line 14, in <module>
freqs = np.linspace(0, sampling_rate/2, fft_size/2+1)
File "<__array_function__ internals>", line 6, in linspace
File "/Users/admin/anaconda3/lib/python3.7/site-packages/numpy/core/function_base.py", line 121, in linspace
.format(type(num)))
TypeError: object of type <class 'float'> cannot be safely interpreted as an integer.

解决方法

freqs = np.linspace(0, sampling_rate/2, fft_size//2+1)

把第三个参数变成int类型就行了,我这里加了一个斜杠。

参考文献

【E-10】object of type <class ‘float’> cannot be safely interpreted as an integer.(numpy)