Numpy warning:Casting Complex to real discards imaginary part

40,614

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.

You might want to try something like

plot(numpy.real(demod),'wo')
plot(numpy.imag(demod),'wo')

if you want to see both parts.

Share:
40,614
marriam nayyer
Author by

marriam nayyer

Updated on July 31, 2020

Comments

  • marriam nayyer
    marriam nayyer almost 4 years

    i am trying a Matlab code in Python

    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

    return array(a, dtype, copy=False, order=order)

    Python Code

    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