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

Skip to content

Commit f39cdb6

Browse files
committed
Merge pull request #1906 from toddrjen/test_spec
Spectral plot unit tests
2 parents 91b1d9f + a087288 commit f39cdb6

File tree

8 files changed

+403
-62
lines changed

8 files changed

+403
-62
lines changed

lib/matplotlib/tests/test_axes.py

Lines changed: 218 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1280,6 +1280,224 @@ def test_mixed_collection():
12801280
ax.set_ylim(0, 16)
12811281

12821282

1283+
@image_comparison(baseline_images=['specgram_freqs'], remove_text=True,
1284+
extensions=['png'])
1285+
def test_specgram_freqs():
1286+
n = 10000
1287+
Fs = 100.
1288+
1289+
fstims1 = [Fs/4, Fs/5, Fs/11]
1290+
fstims2 = [Fs/4.7, Fs/5.6, Fs/11.9]
1291+
1292+
NFFT = int(1000 * Fs / min(fstims1 + fstims2))
1293+
noverlap = int(NFFT / 2)
1294+
pad_to = int(2 ** np.ceil(np.log2(NFFT)))
1295+
1296+
x = np.arange(0, n, 1/Fs)
1297+
1298+
y1 = np.zeros(x.size)
1299+
y2 = np.zeros(x.size)
1300+
for fstim1, fstim2 in zip(fstims1, fstims2):
1301+
y1 += np.sin(fstim1 * x * np.pi * 2)
1302+
y2 += np.sin(fstim2 * x * np.pi * 2)
1303+
y = np.hstack([y1, y2])
1304+
1305+
fig = plt.figure()
1306+
ax1 = fig.add_subplot(3, 1, 1)
1307+
ax2 = fig.add_subplot(3, 1, 2)
1308+
ax3 = fig.add_subplot(3, 1, 3)
1309+
1310+
spec1 = ax1.specgram(y, NFFT=NFFT, Fs=Fs, noverlap=noverlap, pad_to=pad_to,
1311+
sides='default')
1312+
spec2 = ax2.specgram(y, NFFT=NFFT, Fs=Fs, noverlap=noverlap, pad_to=pad_to,
1313+
sides='onesided')
1314+
spec3 = ax3.specgram(y, NFFT=NFFT, Fs=Fs, noverlap=noverlap, pad_to=pad_to,
1315+
sides='twosided')
1316+
1317+
1318+
@image_comparison(baseline_images=['specgram_noise'], remove_text=True,
1319+
extensions=['png'])
1320+
def test_specgram_noise():
1321+
np.random.seed(0)
1322+
1323+
n = 10000
1324+
Fs = 100.
1325+
1326+
NFFT = int(1000 * Fs / 11)
1327+
noverlap = int(NFFT / 2)
1328+
pad_to = int(2 ** np.ceil(np.log2(NFFT)))
1329+
1330+
y1 = np.random.standard_normal(n)
1331+
y2 = np.random.rand(n)
1332+
y = np.hstack([y1, y2])
1333+
1334+
fig = plt.figure()
1335+
ax1 = fig.add_subplot(3, 1, 1)
1336+
ax2 = fig.add_subplot(3, 1, 2)
1337+
ax3 = fig.add_subplot(3, 1, 3)
1338+
1339+
spec1 = ax1.specgram(y, NFFT=NFFT, Fs=Fs, noverlap=noverlap, pad_to=pad_to,
1340+
sides='default')
1341+
spec2 = ax2.specgram(y, NFFT=NFFT, Fs=Fs, noverlap=noverlap, pad_to=pad_to,
1342+
sides='onesided')
1343+
spec3 = ax3.specgram(y, NFFT=NFFT, Fs=Fs, noverlap=noverlap, pad_to=pad_to,
1344+
sides='twosided')
1345+
1346+
1347+
@image_comparison(baseline_images=['psd_freqs'], remove_text=True,
1348+
extensions=['png'])
1349+
def test_psd_freqs():
1350+
n = 10000
1351+
Fs = 100.
1352+
1353+
fstims1 = [Fs/4, Fs/5, Fs/11]
1354+
fstims2 = [Fs/4.7, Fs/5.6, Fs/11.9]
1355+
1356+
NFFT = int(1000 * Fs / min(fstims1 + fstims2))
1357+
noverlap = int(NFFT / 2)
1358+
pad_to = int(2 ** np.ceil(np.log2(NFFT)))
1359+
1360+
x = np.arange(0, n, 1/Fs)
1361+
1362+
y1 = np.zeros(x.size)
1363+
y2 = np.zeros(x.size)
1364+
for fstim1, fstim2 in zip(fstims1, fstims2):
1365+
y1 += np.sin(fstim1 * x * np.pi * 2)
1366+
y2 += np.sin(fstim2 * x * np.pi * 2)
1367+
y = np.hstack([y1, y2])
1368+
1369+
fig = plt.figure()
1370+
ax1 = fig.add_subplot(3, 1, 1)
1371+
ax2 = fig.add_subplot(3, 1, 2)
1372+
ax3 = fig.add_subplot(3, 1, 3)
1373+
1374+
psd1 = ax1.psd(y, NFFT=NFFT, Fs=Fs, noverlap=noverlap, pad_to=pad_to,
1375+
sides='default')
1376+
psd2 = ax2.psd(y, NFFT=NFFT, Fs=Fs, noverlap=noverlap, pad_to=pad_to,
1377+
sides='onesided')
1378+
psd3 = ax3.psd(y, NFFT=NFFT, Fs=Fs, noverlap=noverlap, pad_to=pad_to,
1379+
sides='twosided')
1380+
1381+
ax1.set_xlabel('')
1382+
ax2.set_xlabel('')
1383+
ax3.set_xlabel('')
1384+
ax1.set_ylabel('')
1385+
ax2.set_ylabel('')
1386+
ax3.set_ylabel('')
1387+
1388+
1389+
@image_comparison(baseline_images=['psd_noise'], remove_text=True,
1390+
extensions=['png'])
1391+
def test_psd_noise():
1392+
np.random.seed(0)
1393+
1394+
n = 10000
1395+
Fs = 100.
1396+
1397+
NFFT = int(1000 * Fs / 11)
1398+
noverlap = int(NFFT / 2)
1399+
pad_to = int(2 ** np.ceil(np.log2(NFFT)))
1400+
1401+
y1 = np.random.standard_normal(n)
1402+
y2 = np.random.rand(n)
1403+
y = np.hstack([y1, y2])
1404+
1405+
fig = plt.figure()
1406+
ax1 = fig.add_subplot(3, 1, 1)
1407+
ax2 = fig.add_subplot(3, 1, 2)
1408+
ax3 = fig.add_subplot(3, 1, 3)
1409+
1410+
psd1 = ax1.psd(y, NFFT=NFFT, Fs=Fs, noverlap=noverlap, pad_to=pad_to,
1411+
sides='default')
1412+
psd2 = ax2.psd(y, NFFT=NFFT, Fs=Fs, noverlap=noverlap, pad_to=pad_to,
1413+
sides='onesided')
1414+
psd3 = ax3.psd(y, NFFT=NFFT, Fs=Fs, noverlap=noverlap, pad_to=pad_to,
1415+
sides='twosided')
1416+
1417+
ax1.set_xlabel('')
1418+
ax2.set_xlabel('')
1419+
ax3.set_xlabel('')
1420+
ax1.set_ylabel('')
1421+
ax2.set_ylabel('')
1422+
ax3.set_ylabel('')
1423+
1424+
1425+
@image_comparison(baseline_images=['csd_freqs'], remove_text=True,
1426+
extensions=['png'])
1427+
def test_csd_freqs():
1428+
n = 10000
1429+
Fs = 100.
1430+
1431+
fstims1 = [Fs/4, Fs/5, Fs/11]
1432+
fstims2 = [Fs/4.7, Fs/5.6, Fs/11.9]
1433+
1434+
NFFT = int(1000 * Fs / min(fstims1 + fstims2))
1435+
noverlap = int(NFFT / 2)
1436+
pad_to = int(2 ** np.ceil(np.log2(NFFT)))
1437+
1438+
x = np.arange(0, n, 1/Fs)
1439+
1440+
y1 = np.zeros(x.size)
1441+
y2 = np.zeros(x.size)
1442+
for fstim1, fstim2 in zip(fstims1, fstims2):
1443+
y1 += np.sin(fstim1 * x * np.pi * 2)
1444+
y2 += np.sin(fstim2 * x * np.pi * 2)
1445+
1446+
fig = plt.figure()
1447+
ax1 = fig.add_subplot(3, 1, 1)
1448+
ax2 = fig.add_subplot(3, 1, 2)
1449+
ax3 = fig.add_subplot(3, 1, 3)
1450+
1451+
csd1 = ax1.csd(y1, y2, NFFT=NFFT, Fs=Fs, noverlap=noverlap, pad_to=pad_to,
1452+
sides='default')
1453+
csd2 = ax2.csd(y1, y2, NFFT=NFFT, Fs=Fs, noverlap=noverlap, pad_to=pad_to,
1454+
sides='onesided')
1455+
csd3 = ax3.csd(y1, y2, NFFT=NFFT, Fs=Fs, noverlap=noverlap, pad_to=pad_to,
1456+
sides='twosided')
1457+
1458+
ax1.set_xlabel('')
1459+
ax2.set_xlabel('')
1460+
ax3.set_xlabel('')
1461+
ax1.set_ylabel('')
1462+
ax2.set_ylabel('')
1463+
ax3.set_ylabel('')
1464+
1465+
1466+
@image_comparison(baseline_images=['csd_noise'], remove_text=True,
1467+
extensions=['png'])
1468+
def test_csd_noise():
1469+
np.random.seed(0)
1470+
1471+
n = 10000
1472+
Fs = 100.
1473+
1474+
NFFT = int(1000 * Fs / 11)
1475+
noverlap = int(NFFT / 2)
1476+
pad_to = int(2 ** np.ceil(np.log2(NFFT)))
1477+
1478+
y1 = np.random.standard_normal(n)
1479+
y2 = np.random.rand(n)
1480+
1481+
fig = plt.figure()
1482+
ax1 = fig.add_subplot(3, 1, 1)
1483+
ax2 = fig.add_subplot(3, 1, 2)
1484+
ax3 = fig.add_subplot(3, 1, 3)
1485+
1486+
csd1 = ax1.csd(y1, y2, NFFT=NFFT, Fs=Fs, noverlap=noverlap, pad_to=pad_to,
1487+
sides='default')
1488+
csd2 = ax2.csd(y1, y2, NFFT=NFFT, Fs=Fs, noverlap=noverlap, pad_to=pad_to,
1489+
sides='onesided')
1490+
csd3 = ax3.csd(y1, y2, NFFT=NFFT, Fs=Fs, noverlap=noverlap, pad_to=pad_to,
1491+
sides='twosided')
1492+
1493+
ax1.set_xlabel('')
1494+
ax2.set_xlabel('')
1495+
ax3.set_xlabel('')
1496+
ax1.set_ylabel('')
1497+
ax2.set_ylabel('')
1498+
ax3.set_ylabel('')
1499+
1500+
12831501
if __name__ == '__main__':
12841502
import nose
12851503
nose.runmodule(argv=['-s', '--with-doctest'], exit=False)

0 commit comments

Comments
 (0)