| 
1 |  | -# This is a ported version of a MATLAB example from the signal processing  | 
2 |  | -# toolbox that showed some difference at one time between Matplotlib's and  | 
3 |  | -# MATLAB's scaling of the PSD.  This differs from psd_demo3.py in that  | 
4 |  | -# this uses a complex signal, so we can see that complex PSD's work properly  | 
 | 1 | +"""This is a ported version of a MATLAB example from the signal  | 
 | 2 | +processing toolbox that showed some difference at one time between  | 
 | 3 | +Matplotlib's and MATLAB's scaling of the PSD.  | 
 | 4 | +
  | 
 | 5 | +This differs from psd_demo3.py in that this uses a complex signal,  | 
 | 6 | +so we can see that complex PSD's work properly  | 
 | 7 | +
  | 
 | 8 | +"""  | 
 | 9 | + | 
5 | 10 | import numpy as np  | 
6 | 11 | import matplotlib.pyplot as plt  | 
7 | 12 | import matplotlib.mlab as mlab  | 
8 | 13 | 
 
  | 
 | 14 | +prng = np.random.RandomState(123456)  # to ensure reproducibility  | 
 | 15 | + | 
9 | 16 | fs = 1000  | 
10 | 17 | t = np.linspace(0, 0.3, 301)  | 
11 | 18 | A = np.array([2, 8]).reshape(-1, 1)  | 
12 | 19 | f = np.array([150, 140]).reshape(-1, 1)  | 
13 |  | -xn = (A * np.exp(2j * np.pi * f * t)).sum(axis=0) + 5 * np.random.randn(*t.shape)  | 
 | 20 | +xn = (A * np.exp(2j * np.pi * f * t)).sum(axis=0) + 5 * prng.randn(*t.shape)  | 
 | 21 | + | 
 | 22 | +fig, (ax0, ax1) = plt.subplots(ncols=2)  | 
14 | 23 | 
 
  | 
 | 24 | +fig.subplots_adjust(hspace=0.45, wspace=0.3)  | 
15 | 25 | yticks = np.arange(-50, 30, 10)  | 
16 |  | -xticks = np.arange(-500, 550, 100)  | 
17 |  | -plt.subplots_adjust(hspace=0.45, wspace=0.3)  | 
18 |  | -ax = plt.subplot(1, 2, 1)  | 
 | 26 | +yrange = (yticks[0], yticks[-1])  | 
 | 27 | +xticks = np.arange(-500, 550, 200)  | 
19 | 28 | 
 
  | 
20 |  | -plt.psd(xn, NFFT=301, Fs=fs, window=mlab.window_none, pad_to=1024,  | 
 | 29 | +ax0.psd(xn, NFFT=301, Fs=fs, window=mlab.window_none, pad_to=1024,  | 
21 | 30 |         scale_by_freq=True)  | 
22 |  | -plt.title('Periodogram')  | 
23 |  | -plt.yticks(yticks)  | 
24 |  | -plt.xticks(xticks)  | 
25 |  | -plt.grid(True)  | 
26 |  | -plt.xlim(-500, 500)  | 
27 |  | - | 
28 |  | -plt.subplot(1, 2, 2, sharex=ax, sharey=ax)  | 
29 |  | -plt.psd(xn, NFFT=150, Fs=fs, window=mlab.window_none, noverlap=75, pad_to=512,  | 
 | 31 | +ax0.set_title('Periodogram')  | 
 | 32 | +ax0.set_yticks(yticks)  | 
 | 33 | +ax0.set_xticks(xticks)  | 
 | 34 | +ax0.grid(True)  | 
 | 35 | +ax0.set_ylim(yrange)  | 
 | 36 | + | 
 | 37 | +ax1.psd(xn, NFFT=150, Fs=fs, window=mlab.window_none, pad_to=512, noverlap=75,  | 
30 | 38 |         scale_by_freq=True)  | 
31 |  | -plt.title('Welch')  | 
32 |  | -plt.xticks(xticks)  | 
33 |  | -plt.yticks(yticks)  | 
34 |  | -plt.ylabel('')  | 
35 |  | -plt.grid(True)  | 
36 |  | -plt.xlim(-500, 500)  | 
 | 39 | +ax1.set_title('Welch')  | 
 | 40 | +ax1.set_xticks(xticks)  | 
 | 41 | +ax1.set_yticks(yticks)  | 
 | 42 | +ax1.set_ylabel('')  # overwrite the y-label added by `psd`  | 
 | 43 | +ax1.grid(True)  | 
 | 44 | +ax1.set_ylim(yrange)  | 
37 | 45 | 
 
  | 
38 | 46 | plt.show()  | 
0 commit comments