def test_dpss(self): """ Tests case for dpss. The resulting v tapers are tested. There is no test for theta or lambda. """ v = np.load datafile = os.path.join(os.path.dirname(__file__), 'data', 'dpss.npz') v = np.load(datafile)['v'] v2, lamb, theta = dpss(512, 2.5, 2) # No NaNs are supposed to be in the output. self.assertEqual(np.isnan(v2).any(), False) self.assertEqual(np.isnan(lamb).any(), False) self.assertEqual(np.isnan(theta).any(), False) # Taper 1, normalize for precision np.testing.assert_almost_equal(v2[:,0] / v[:,0], v[:,0] / v[:,0]) # Taper 2, normalize for precision np.testing.assert_almost_equal(v2[:,1] / v[:,1], v[:,1] / v[:,1]) # Do the same but with spline interpolation. v3, lamb2, thetha2 = dpss(512, 2.5, 2, nmax=400) # Test both tapers. They are not exactly equal therefore only two # digits are compared. np.testing.assert_almost_equal(v3 / v3, v2 / v3, 2)
def test_dpss(self): """ Tests case for dpss. The resulting v tapers are tested. There is no test for theta or lambda. """ v = np.load datafile = os.path.join(os.path.dirname(__file__), 'data', 'dpss.npz') v = np.load(datafile)['v'] v2, lamb, theta = dpss(512, 2.5, 2) # No NaNs are supposed to be in the output. self.assertEqual(np.isnan(v2).any(), False) self.assertEqual(np.isnan(lamb).any(), False) self.assertEqual(np.isnan(theta).any(), False) # Taper 1, normalize for precision np.testing.assert_almost_equal(v2[:, 0] / v[:, 0], v[:, 0] / v[:, 0]) # Taper 2, normalize for precision np.testing.assert_almost_equal(v2[:, 1] / v[:, 1], v[:, 1] / v[:, 1]) # Do the same but with spline interpolation. v3, lamb2, thetha2 = dpss(512, 2.5, 2, nmax=400) # Test both tapers. They are not exactly equal therefore only two # digits are compared. np.testing.assert_almost_equal(v3 / v3, v2 / v3, 2)
def multitaper_2D(EEG, npts=170, fw=3, number_of_tapers=5, fs=100, filename=None): tapers, _, _ = mtspec.dpss(npts=20, fw=3, number_of_tapers=5) tf = np.stack([ np.mean(np.power( np.abs([ signal.stft(EEG[j, :, i], fs=fs, window=tapers[:, t], nperseg=tapers.shape[0])[2] for t in range(tapers.shape[1]) ]), 2), axis=0) for i in range(EEG.shape[2]) ] for j in tqdm(range(EEG.shape[0]))) print("MultiTaper shape: ", tf.shape) if filename is None: np.save('tf_2D' + str(fs), tf) print("Saved as:", 'tf_2D' + str(fs)) else: np.save(filename, tf) print("Saved as:", filename)
import matplotlib as mpl mpl.rcParams['font.size'] = 9.0 import matplotlib.pyplot as plt from mtspec import dpss tapers, lamb, theta = dpss(512, 2.5, 10) ax = plt.figure().add_subplot(111) for i in xrange(10): ax.plot(tapers[:, i]) ax.set_xlim(0, len(tapers[:, 0]))
import matplotlib.pyplot as plt plt.style.use("ggplot") from mtspec import dpss tapers, _, _ = dpss(npts=512, fw=2.5, number_of_tapers=8) for i in range(8): plt.plot(tapers[:, i]) plt.xlim(0, len(tapers[:, 0])) plt.tight_layout() plt.show()
import seaborn import matplotlib.pyplot as plt from mtspec import dpss import os tapers, lamb, theta = dpss(512, 2.5, 10) output_folder = "_static" plt.figure(figsize=(8, 4)) for i in range(5): plt.plot(tapers[:, i], lw=10, alpha=0.4) plt.xlim(0, len(tapers[:, 0])) plt.ylim(-0.08, 0.08) plt.axis('off') plt.text(x=0, y=1, s="mtspec", transform=plt.gca().transAxes, va="top", fontsize=130, fontweight=1000, color="0.3", variant="small-caps") plt.subplots_adjust(left=0.02, right=0.98, bottom=0.02, top=0.98) plt.savefig(os.path.join(output_folder, "logo.png"), transparent=True) plt.savefig(os.path.join(output_folder, "logo.svg"), transparent=True) plt.savefig(os.path.join(output_folder, "logo.pdf"), transparent=True)