我想对来自pyaudio的一些输入数据做一些分段立方Hermite插值:
#input comes as string and is converted to int16
numpyData = numpy.fromstring(inData, dtype=numpy.int16)
#x positions of my y data
x = numpy.arange(len(numpyData))
#as i always want to interpolate to 4096 these are my indices
interpolationIndices = numpy.linspace(0, len(numpyData), num=4096, endpoint=False)
#my interpolator and print for testing
f = interpolate.PchipInterpolator(x, numpyData)
print f(interpolationIndices[0])
错误日志现在说:
TypeError: array cannot be safely cast to required type
scipy.interpolate.PchipInterpolator的文档说它应该是一个实数值的数组但是因为我知道int16应该足够真实,我的x-pos数组也是单调的。
其他信息:
print numpyData
结果:
[3153 2362 5361 ..., 3206 -849 3241]
所以所有整数值都是预期的。
完整追踪:
Traceback (most recent call last):
File "/Users/myUser/Documents/workspace/myProject/AudioController.py", line 9, in callback
return self.file.getAudio(frame_count, scaleMethod()), pyaudio.paContinue
File "/Users/myUser/Documents/workspace/myProject/NewFileHandler.py", line 34, in getAudio
data = self.resample(data)
File "/Users/myUser/Documents/workspace/myProject/NewFileHandler.py", line 168, in resample
f = interpolate.PchipInterpolator(x, numpyData)
File "/Library/Python/2.7/site-packages/scipy/interpolate/polyint.py", line 932, in __init__
data[:,1] = PchipInterpolator._find_derivatives(xp, yp)
File "/Library/Python/2.7/site-packages/scipy/interpolate/polyint.py", line 982, in _find_derivatives
PchipInterpolator._edge_case(mk[0],dk[1], dk[0])
File "/Library/Python/2.7/site-packages/scipy/interpolate/polyint.py", line 947, in _edge_case
out[mask] = 1.0/(1.0/m0[mask]+1.0/d1[mask])
TypeError: array cannot be safely cast to required type
numpyData.dtype:
int16
numpy版本:
print numpy.__version__
1.6.2
scipy版本:
print scipy.__version__
0.13.2
答案 0 :(得分:3)
定义一个错误(见底部),并且可以在numpy 1.6.2中重现,并进行以下设置:
import scipy.interpolate
import numpy as np
y = np.array([0,1,2], dtype=np.int16)
x = np.arange(len(y))
然后引发异常:
>>> np.__version__
1.6.2
# code skipped
>>> scipy.interpolate.PchipInterpolator(x, y)
Traceback (most recent call last):
...
out[mask] = 1.0/(1.0/m0[mask]+1.0/d1[mask])
TypeError: array cannot be safely cast to required type
尽管numpy 1.8.0正常工作:
>>> np.__version__
1.8.0
# code skipped
>>> scipy.interpolate.PchipInterpolator(x, y)
<scipy.interpolate.polyint.PchipInterpolator object at 0x044371F0>
您的选择可能是升级numpy。
P.S。它是一个布尔索引错误,在跟踪器中找不到,以下是一个重现的简短示例:
out = np.array([0])
out[np.array([True])] = np.array([.5])