.. _pylab_examples-psd_demo_complex:

pylab_examples example code: psd_demo_complex.py
================================================



.. plot:: /media/TOSHI/temp/numpy_scipy_matplotlib/matplotlib/matplotlib-1.1.1~rc2/doc/mpl_examples/pylab_examples/psd_demo_complex.py

::

    #This is a ported version of a MATLAB example from the signal processing
    #toolbox that showed some difference at one time between Matplotlib's and
    #MATLAB's scaling of the PSD.  This differs from psd_demo3.py in that
    #this uses a complex signal, so we can see that complex PSD's work properly
    import numpy as np
    import matplotlib.pyplot as plt
    import matplotlib.mlab as mlab
    
    fs = 1000
    t = np.linspace(0, 0.3, 301)
    A = np.array([2, 8]).reshape(-1, 1)
    f = np.array([150, 140]).reshape(-1, 1)
    xn = (A * np.exp(2j * np.pi * f * t)).sum(axis=0) + 5 * np.random.randn(*t.shape)
    
    yticks = np.arange(-50, 30, 10)
    xticks = np.arange(-500,550,100)
    plt.subplots_adjust(hspace=0.45, wspace=0.3)
    ax = plt.subplot(1, 2, 1)
    
    plt.psd(xn, NFFT=301, Fs=fs, window=mlab.window_none, pad_to=1024,
        scale_by_freq=True)
    plt.title('Periodogram')
    plt.yticks(yticks)
    plt.xticks(xticks)
    plt.grid(True)
    plt.xlim(-500, 500)
    
    plt.subplot(1, 2, 2, sharex=ax, sharey=ax)
    plt.psd(xn, NFFT=150, Fs=fs, window=mlab.window_none, noverlap=75, pad_to=512,
        scale_by_freq=True)
    plt.title('Welch')
    plt.xticks(xticks)
    plt.yticks(yticks)
    plt.ylabel('')
    plt.grid(True)
    plt.xlim(-500, 500)
    
    plt.show()
    

Keywords: python, matplotlib, pylab, example, codex (see :ref:`how-to-search-examples`)