Skip to content

Commit 382b319

Browse files
committed
Switch to OO code style & ensure y-range
1 parent 80a3f3e commit 382b319

File tree

1 file changed

+22
-17
lines changed

1 file changed

+22
-17
lines changed
Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
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.
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+
"""
46

57
import numpy as np
68
import matplotlib.pyplot as plt
@@ -12,25 +14,28 @@
1214
f = np.array([150, 140]).reshape(-1, 1)
1315
xn = (A * np.sin(2 * np.pi * f * t)).sum(axis=0) + 5 * np.random.randn(*t.shape)
1416

17+
fig, (ax0, ax1) = plt.subplots(ncols=2)
18+
19+
fig.subplots_adjust(hspace=0.45, wspace=0.3)
1520
yticks = np.arange(-50, 30, 10)
21+
yrange = (yticks[0], yticks[-1])
1622
xticks = np.arange(0, 550, 100)
17-
plt.subplots_adjust(hspace=0.45, wspace=0.3)
18-
plt.subplot(1, 2, 1)
1923

20-
plt.psd(xn, NFFT=301, Fs=fs, window=mlab.window_none, pad_to=1024,
24+
ax0.psd(xn, NFFT=301, Fs=fs, window=mlab.window_none, pad_to=1024,
2125
scale_by_freq=True)
22-
plt.title('Periodogram')
23-
plt.yticks(yticks)
24-
plt.xticks(xticks)
25-
plt.grid(True)
26+
ax0.set_title('Periodogram')
27+
ax0.set_yticks(yticks)
28+
ax0.set_xticks(xticks)
29+
ax0.grid(True)
30+
ax0.set_ylim(yrange)
2631

27-
plt.subplot(1, 2, 2)
28-
plt.psd(xn, NFFT=150, Fs=fs, window=mlab.window_none, noverlap=75, pad_to=512,
32+
ax1.psd(xn, NFFT=150, Fs=fs, window=mlab.window_none, pad_to=512, noverlap=75,
2933
scale_by_freq=True)
30-
plt.title('Welch')
31-
plt.xticks(xticks)
32-
plt.yticks(yticks)
33-
plt.ylabel('')
34-
plt.grid(True)
34+
ax1.set_title('Welch')
35+
ax1.set_xticks(xticks)
36+
ax1.set_yticks(yticks)
37+
ax1.set_ylabel('') # overwrite the y-label added by `psd`
38+
ax1.grid(True)
39+
ax1.set_ylim(yrange)
3540

3641
plt.show()

0 commit comments

Comments
 (0)