Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit e48b493

Browse files
committed
Cleanup psd example.
- Show all figures at once. - Group axes setter calls to have them take less room relative to the psd calls, which are the object of the example. - Align the noverlap examples to make the parallelism between them clearer. - Don't introduce a second random state with a single use.
1 parent 165df1b commit e48b493

File tree

1 file changed

+22
-49
lines changed

1 file changed

+22
-49
lines changed
Lines changed: 22 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
"""
22
========
3-
Psd Demo
3+
PSD Demo
44
========
55
66
Plotting Power Spectral Density (PSD) in Matplotlib.
@@ -9,13 +9,12 @@
99
many useful libraries for computing a PSD. Below we demo a few examples
1010
of how this can be accomplished and visualized with Matplotlib.
1111
"""
12+
1213
import matplotlib.pyplot as plt
1314
import numpy as np
1415
import matplotlib.mlab as mlab
15-
import matplotlib.gridspec as gridspec
1616

17-
# Fixing random state for reproducibility
18-
np.random.seed(19680801)
17+
np.random.seed(19680801) # Fix random state for reproducibility.
1918

2019
dt = 0.01
2120
t = np.arange(0, 10, dt)
@@ -30,8 +29,6 @@
3029
ax0.plot(t, s)
3130
ax1.psd(s, 512, 1 / dt)
3231

33-
plt.show()
34-
3532
###############################################################################
3633
# Compare this with the equivalent Matlab code to accomplish the same thing::
3734
#
@@ -57,43 +54,35 @@
5754
y = 10. * np.sin(2 * np.pi * 4 * t) + 5. * np.sin(2 * np.pi * 4.25 * t)
5855
y = y + np.random.randn(*t.shape)
5956

60-
# Plot the raw time series
57+
# Plot the raw time series.
6158
fig = plt.figure(constrained_layout=True)
62-
gs = gridspec.GridSpec(2, 3, figure=fig)
59+
gs = fig.add_gridspec(2, 3)
6360
ax = fig.add_subplot(gs[0, :])
6461
ax.plot(t, y)
65-
ax.set_xlabel('time [s]')
66-
ax.set_ylabel('signal')
62+
ax.set(xlabel='time [s]', ylabel='signal')
6763

6864
# Plot the PSD with different amounts of zero padding. This uses the entire
69-
# time series at once
65+
# time series at once.
7066
ax2 = fig.add_subplot(gs[1, 0])
7167
ax2.psd(y, NFFT=len(t), pad_to=len(t), Fs=fs)
7268
ax2.psd(y, NFFT=len(t), pad_to=len(t) * 2, Fs=fs)
7369
ax2.psd(y, NFFT=len(t), pad_to=len(t) * 4, Fs=fs)
74-
ax2.set_title('zero padding')
70+
ax2.set(title='zero padding')
7571

76-
# Plot the PSD with different block sizes, Zero pad to the length of the
72+
# Plot the PSD with different block sizes, zero pad to the length of the
7773
# original data sequence.
7874
ax3 = fig.add_subplot(gs[1, 1], sharex=ax2, sharey=ax2)
7975
ax3.psd(y, NFFT=len(t), pad_to=len(t), Fs=fs)
8076
ax3.psd(y, NFFT=len(t) // 2, pad_to=len(t), Fs=fs)
8177
ax3.psd(y, NFFT=len(t) // 4, pad_to=len(t), Fs=fs)
82-
ax3.set_ylabel('')
83-
ax3.set_title('block size')
78+
ax3.set(ylabel='', title='block size')
8479

85-
# Plot the PSD with different amounts of overlap between blocks
80+
# Plot the PSD with different amounts of overlap between blocks.
8681
ax4 = fig.add_subplot(gs[1, 2], sharex=ax2, sharey=ax2)
87-
ax4.psd(y, NFFT=len(t) // 2, pad_to=len(t), noverlap=0, Fs=fs)
88-
ax4.psd(y, NFFT=len(t) // 2, pad_to=len(t),
89-
noverlap=int(0.05 * len(t) / 2.), Fs=fs)
90-
ax4.psd(y, NFFT=len(t) // 2, pad_to=len(t),
91-
noverlap=int(0.2 * len(t) / 2.), Fs=fs)
92-
ax4.set_ylabel('')
93-
ax4.set_title('overlap')
94-
95-
plt.show()
96-
82+
ax4.psd(y, NFFT=len(t) // 2, pad_to=len(t), Fs=fs, noverlap=0)
83+
ax4.psd(y, NFFT=len(t) // 2, pad_to=len(t), Fs=fs, noverlap=int(0.025*len(t)))
84+
ax4.psd(y, NFFT=len(t) // 2, pad_to=len(t), Fs=fs, noverlap=int(0.1*len(t)))
85+
ax4.set(ylabel='', title='overlap')
9786

9887
###############################################################################
9988
# This is a ported version of a MATLAB example from the signal
@@ -115,22 +104,14 @@
115104

116105
ax0.psd(xn, NFFT=301, Fs=fs, window=mlab.window_none, pad_to=1024,
117106
scale_by_freq=True)
118-
ax0.set_title('Periodogram')
119-
ax0.set_yticks(yticks)
120-
ax0.set_xticks(xticks)
107+
ax0.set(title='Periodogram', xticks=xticks, yticks=yticks, ylim=yrange)
121108
ax0.grid(True)
122-
ax0.set_ylim(yrange)
123109

124110
ax1.psd(xn, NFFT=150, Fs=fs, window=mlab.window_none, pad_to=512, noverlap=75,
125111
scale_by_freq=True)
126-
ax1.set_title('Welch')
127-
ax1.set_xticks(xticks)
128-
ax1.set_yticks(yticks)
129-
ax1.set_ylabel('') # overwrite the y-label added by `psd`
112+
ax1.set(title='Welch', xticks=xticks, yticks=yticks, ylim=yrange,
113+
ylabel='') # overwrite the y-label added by `psd`
130114
ax1.grid(True)
131-
ax1.set_ylim(yrange)
132-
133-
plt.show()
134115

135116
###############################################################################
136117
# This is a ported version of a MATLAB example from the signal
@@ -139,13 +120,11 @@
139120
#
140121
# It uses a complex signal so we can see that complex PSD's work properly.
141122

142-
prng = np.random.RandomState(19680801) # to ensure reproducibility
143-
144123
fs = 1000
145124
t = np.linspace(0, 0.3, 301)
146125
A = np.array([2, 8]).reshape(-1, 1)
147126
f = np.array([150, 140]).reshape(-1, 1)
148-
xn = (A * np.exp(2j * np.pi * f * t)).sum(axis=0) + 5 * prng.randn(*t.shape)
127+
xn = (A * np.exp(2j * np.pi * f * t)).sum(0) + 5 * np.random.randn(*t.shape)
149128

150129
fig, (ax0, ax1) = plt.subplots(ncols=2, constrained_layout=True)
151130

@@ -155,19 +134,13 @@
155134

156135
ax0.psd(xn, NFFT=301, Fs=fs, window=mlab.window_none, pad_to=1024,
157136
scale_by_freq=True)
158-
ax0.set_title('Periodogram')
159-
ax0.set_yticks(yticks)
160-
ax0.set_xticks(xticks)
137+
ax0.set(title='Periodogram', xticks=xticks, yticks=yticks, ylim=yrange)
161138
ax0.grid(True)
162-
ax0.set_ylim(yrange)
163139

164140
ax1.psd(xn, NFFT=150, Fs=fs, window=mlab.window_none, pad_to=512, noverlap=75,
165141
scale_by_freq=True)
166-
ax1.set_title('Welch')
167-
ax1.set_xticks(xticks)
168-
ax1.set_yticks(yticks)
169-
ax1.set_ylabel('') # overwrite the y-label added by `psd`
142+
ax1.set(title='Welch', xticks=xticks, yticks=yticks, ylim=yrange,
143+
ylabel='') # overwrite the y-label added by `psd`
170144
ax1.grid(True)
171-
ax1.set_ylim(yrange)
172145

173146
plt.show()

0 commit comments

Comments
 (0)