Python Numpy 警告:将复杂转换为真实丢弃虚部
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17958223/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Numpy warning:Casting Complex to real discards imaginary part
提问by marriam nayyer
i am trying a Matlab code in Python
我正在尝试用 Python 编写 Matlab 代码
my code gives a warning
我的代码发出警告
/usr/lib/python2.7/dist-packages/numpy/core/numeric.py:235: ComplexWarning: Casting complex values to real discards the imaginary part
/usr/lib/python2.7/dist-packages/numpy/core/numeric.py:235: ComplexWarning: Casting complex values to real 丢弃虚部
return array(a, dtype, copy=False, order=order)
返回数组(a,dtype,copy=False,order=order)
Python Code
Python代码
demod_1_a=mod_noisy*2*cos((2*pi*Fc*t)+phi)
N=10
Fc=40
Fs=1600
d=firwin(numtaps=N,cutoff=40,nyq=Fs/2)
print(len(d))
Hd=lfilter( d, 1.0, demod_1_a)
print(len(Hd))
y2=(convolve(Hd,raised))/Convfac
print(len(y2))
y2=y2[(sa/2)-1:-sa/2]
print(len(y2))
demod_3_a=y2[(sa/2)-1::sa]
print(len(demod_3_a))
demod_1_b=-1*mod_noisy*2*sin((2*pi*Fc*t)+phi)
Hd2=lfilter(d,1.0,demod_1_b)
y3=(convolve(Hd2,raised))/Convfac
y3=y3[(sa/2)-1:-sa/2]
demod_3_b=y3[(sa/2)-1::sa]
#########3333
#Demod
demod=demod_3_a+(1j)*demod_3_b
print((demod))
plot(demod,'wo')
show()
this code is giving me results but not desired results.i wanted to ask that how does this warning will effect my Code? and what is the solution to get rid out of this warning.Please help
这段代码给了我结果但不是想要的结果。我想问一下这个警告将如何影响我的代码?摆脱这个警告的解决方案是什么。请帮忙
回答by bob.sacamento
The warning is coming from the plot command -- I'm pretty sure. "plot" is meant to take a 1d, real array and put it on the screen. When it sees an array of complex numbers it does the best it can, i.e. discards the imaginary part and plot the real part.
警告来自 plot 命令——我很确定。“plot”是指将一个 1d 实数数组放在屏幕上。当它看到一个复数数组时,它会尽其所能,即丢弃虚部并绘制实部。
You might want to try something like
你可能想尝试类似的东西
plot(numpy.real(demod),'wo')
plot(numpy.imag(demod),'wo')
if you want to see both parts.
如果你想看到这两个部分。