在计算数组每个值的绝对值时,我收到与 abs(): 'list' 的错误操作数类型相关的错误。源代码失败的部分是下一个:
x = amplitudex * sin((2 * pi * (frequency * 1) * t) + phase);
y = amplitudey * sin((2 * pi * (frequency * 2) * t) + phase);
z = amplitudez * sin((2 * pi * (frequency * 3) * t) + phase);
w= 0.55* (x + y + z);
....
n = len(w);
wf = [float(0)] * n;
for k in range(n): # For each output element
s = float(0);
for t in range(n): # For each input element
s += w[t] * cmath.exp(-2j * cmath.pi * t * k / n);
wf[k] = float(s);
sf = np.linspace(0.0, 1.0/(2.0*numCycles), numSamples/2);
#The calculation of absolute values causes error:
plot(sf, 2.0/numSamples * abs(wf[0:100]));
如何修复abs函数中的这个错误?我对这个错误感到困惑:(
Thanks
我看到你已经导入了 numpy 因为你正在使用np.linspace
在代码中。您可能会混淆 numpy 的 abs,它可以很高兴地在列表和数组上工作,而__builtin__.abs
,仅适用于标量。
改变这个:
abs(wf[0:100])
对此:
np.abs(wf[0:100])
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)