From 8dad86150221b51378896b934e45465a111a4485 Mon Sep 17 00:00:00 2001 From: Adrien F Vincent Date: Tue, 26 Jul 2016 23:50:40 +0200 Subject: [PATCH 1/3] Switch to OO code style & ensure y-range --- examples/pylab_examples/psd_demo_complex.py | 48 ++++++++++++--------- 1 file changed, 27 insertions(+), 21 deletions(-) diff --git a/examples/pylab_examples/psd_demo_complex.py b/examples/pylab_examples/psd_demo_complex.py index 7928cb364331..94bcd3c4caa3 100644 --- a/examples/pylab_examples/psd_demo_complex.py +++ b/examples/pylab_examples/psd_demo_complex.py @@ -1,7 +1,12 @@ -# 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 +"""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 @@ -12,27 +17,28 @@ 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) +fig, (ax0, ax1) = plt.subplots(ncols=2) + +fig.subplots_adjust(hspace=0.45, wspace=0.3) yticks = np.arange(-50, 30, 10) +yrange = (yticks[0], yticks[-1]) 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, +ax0.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, +ax0.set_title('Periodogram') +ax0.set_yticks(yticks) +ax0.set_xticks(xticks) +ax0.grid(True) +ax0.set_ylim(yrange) + +ax1.psd(xn, NFFT=150, Fs=fs, window=mlab.window_none, pad_to=512, noverlap=75, scale_by_freq=True) -plt.title('Welch') -plt.xticks(xticks) -plt.yticks(yticks) -plt.ylabel('') -plt.grid(True) -plt.xlim(-500, 500) +ax1.set_title('Welch') +ax1.set_xticks(xticks) +ax1.set_yticks(yticks) +ax1.set_ylabel('') # overwrite the y-label added by `psd` +ax1.grid(True) +ax1.set_ylim(yrange) plt.show() From 063bccf23b0d7fe058628f69ccefbdfbbc2c233d Mon Sep 17 00:00:00 2001 From: Adrien F Vincent Date: Tue, 26 Jul 2016 23:51:45 +0200 Subject: [PATCH 2/3] Reduce x-ticks amount (by 2) --- examples/pylab_examples/psd_demo_complex.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/pylab_examples/psd_demo_complex.py b/examples/pylab_examples/psd_demo_complex.py index 94bcd3c4caa3..9318f1d303c0 100644 --- a/examples/pylab_examples/psd_demo_complex.py +++ b/examples/pylab_examples/psd_demo_complex.py @@ -22,7 +22,7 @@ fig.subplots_adjust(hspace=0.45, wspace=0.3) yticks = np.arange(-50, 30, 10) yrange = (yticks[0], yticks[-1]) -xticks = np.arange(-500, 550, 100) +xticks = np.arange(-500, 550, 200) ax0.psd(xn, NFFT=301, Fs=fs, window=mlab.window_none, pad_to=1024, scale_by_freq=True) From f0d40808e9a010361f146d28d2829b7992d1bb60 Mon Sep 17 00:00:00 2001 From: Adrien F Vincent Date: Wed, 27 Jul 2016 00:42:09 +0200 Subject: [PATCH 3/3] Use a RandomState instance with a fixed seed --- examples/pylab_examples/psd_demo_complex.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/examples/pylab_examples/psd_demo_complex.py b/examples/pylab_examples/psd_demo_complex.py index 9318f1d303c0..334651837c38 100644 --- a/examples/pylab_examples/psd_demo_complex.py +++ b/examples/pylab_examples/psd_demo_complex.py @@ -11,11 +11,13 @@ import matplotlib.pyplot as plt import matplotlib.mlab as mlab +prng = np.random.RandomState(123456) # to ensure reproducibility + 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) +xn = (A * np.exp(2j * np.pi * f * t)).sum(axis=0) + 5 * prng.randn(*t.shape) fig, (ax0, ax1) = plt.subplots(ncols=2)