def test_mtm_cross_spectrum(): """ Test the multi-taper cross-spectral estimation. Based on the example in doc/examples/multi_taper_coh.py """ NW = 4 K = 2 * NW - 1 N = 2 ** 10 n_reps = 10 n_freqs = N tapers, eigs = tsa.dpss_windows(N, NW, 2 * NW - 1) est_psd = [] for k in xrange(n_reps): data,nz,alpha = utils.ar_generator(N=N) fgrid, hz = tsa.freq_response(1.0, a=np.r_[1, -alpha], n_freqs=n_freqs) # 'one-sided', so multiply by 2: psd = 2 * (hz * hz.conj()).real tdata = tapers * data tspectra = np.fft.fft(tdata) L = N / 2 + 1 sides = 'onesided' w, _ = utils.adaptive_weights(tspectra, eigs, sides=sides) sxx = tsa.mtm_cross_spectrum(tspectra, tspectra, w, sides=sides) est_psd.append(sxx) fxx = np.mean(est_psd, 0) psd_ratio = np.mean(fxx / psd) # This is a rather lenient test, making sure that the average ratio is 1 to # within an order of magnitude. That is, that they are equal on average: npt.assert_array_almost_equal(psd_ratio, 1, decimal=1) # Test raising of error in case the inputs don't make sense: npt.assert_raises(ValueError, tsa.mtm_cross_spectrum, tspectra,np.r_[tspectra, tspectra], (w, w))
def test_mtm_cross_spectrum(): """ Test the multi-taper cross-spectral estimation. Based on the example in doc/examples/multi_taper_coh.py """ NW = 4 K = 2 * NW - 1 N = 2**10 n_reps = 10 n_freqs = N tapers, eigs = tsa.dpss_windows(N, NW, 2 * NW - 1) est_psd = [] for k in range(n_reps): data, nz, alpha = utils.ar_generator(N=N) fgrid, hz = tsa.freq_response(1.0, a=np.r_[1, -alpha], n_freqs=n_freqs) # 'one-sided', so multiply by 2: psd = 2 * (hz * hz.conj()).real tdata = tapers * data tspectra = fftpack.fft(tdata) L = N / 2 + 1 sides = 'onesided' w, _ = utils.adaptive_weights(tspectra, eigs, sides=sides) sxx = tsa.mtm_cross_spectrum(tspectra, tspectra, w, sides=sides) est_psd.append(sxx) fxx = np.mean(est_psd, 0) psd_ratio = np.mean(fxx / psd) # This is a rather lenient test, making sure that the average ratio is 1 to # within an order of magnitude. That is, that they are equal on average: npt.assert_array_almost_equal(psd_ratio, 1, decimal=1) # Test raising of error in case the inputs don't make sense: npt.assert_raises(ValueError, tsa.mtm_cross_spectrum, tspectra, np.r_[tspectra, tspectra], (w, w))
def test_multi_taper_psd_csd(): """ Test the multi taper psd and csd estimation functions. Based on the example in doc/examples/multi_taper_spectral_estimation.py """ N = 2**10 n_reps = 10 psd = [] est_psd = [] est_csd = [] for jk in [True, False]: for k in range(n_reps): for adaptive in [True, False]: ar_seq, nz, alpha = utils.ar_generator(N=N, drop_transients=10) ar_seq -= ar_seq.mean() fgrid, hz = tsa.freq_response(1.0, a=np.r_[1, -alpha], n_freqs=N) psd.append(2 * (hz * hz.conj()).real) f, psd_mt, nu = tsa.multi_taper_psd(ar_seq, adaptive=adaptive, jackknife=jk) est_psd.append(psd_mt) f, csd_mt = tsa.multi_taper_csd(np.vstack([ar_seq, ar_seq]), adaptive=adaptive) # Symmetrical in this case, so take one element out: est_csd.append(csd_mt[0][1]) fxx = np.mean(psd, axis=0) fxx_est1 = np.mean(est_psd, axis=0) fxx_est2 = np.mean(est_csd, axis=0) # Tests the psd: psd_ratio1 = np.mean(fxx_est1 / fxx) npt.assert_array_almost_equal(psd_ratio1, 1, decimal=-1) # Tests the csd: psd_ratio2 = np.mean(fxx_est2 / fxx) npt.assert_array_almost_equal(psd_ratio2, 1, decimal=-1)
def test_multi_taper_psd_csd(): """ Test the multi taper psd and csd estimation functions. Based on the example in doc/examples/multi_taper_spectral_estimation.py """ N = 2 ** 10 n_reps = 10 psd = [] est_psd = [] est_csd = [] for jk in [True, False]: for k in range(n_reps): for adaptive in [True, False]: ar_seq, nz, alpha = utils.ar_generator(N=N, drop_transients=10) ar_seq -= ar_seq.mean() fgrid, hz = tsa.freq_response(1.0, a=np.r_[1, -alpha], n_freqs=N) psd.append(2 * (hz * hz.conj()).real) f, psd_mt, nu = tsa.multi_taper_psd(ar_seq, adaptive=adaptive, jackknife=jk) est_psd.append(psd_mt) f, csd_mt = tsa.multi_taper_csd(np.vstack([ar_seq, ar_seq]), adaptive=adaptive) # Symmetrical in this case, so take one element out: est_csd.append(csd_mt[0][1]) fxx = np.mean(psd, axis=0) fxx_est1 = np.mean(est_psd, axis=0) fxx_est2 = np.mean(est_csd, axis=0) # Tests the psd: psd_ratio1 = np.mean(fxx_est1 / fxx) npt.assert_array_almost_equal(psd_ratio1, 1, decimal=-1) # Tests the csd: psd_ratio2 = np.mean(fxx_est2 / fxx) npt.assert_array_almost_equal(psd_ratio2, 1, decimal=-1)
Next, we generate a sequence with known spectral properties: """ N = 512 ar_seq, nz, alpha = utils.ar_generator(N=N, drop_transients=10) ar_seq -= ar_seq.mean() """ This is the true PSD for this sequence: """ fgrid, hz = tsa.freq_response(1.0, a=np.r_[1, -alpha], n_freqs=N) psd = (hz * hz.conj()).real """ This is a one-sided spectrum, so we double the power: """ psd *= 2 dB(psd, psd) """ We begin by using the naive periodogram function (:func:`tsa.periodogram` in
""" Next, we generate a sequence with known spectral properties: """ N = 512 ar_seq, nz, alpha = utils.ar_generator(N=N, drop_transients=10) ar_seq -= ar_seq.mean() """ This is the true PSD for this sequence: """ fgrid, hz = tsa.freq_response(1.0, a=np.r_[1, -alpha], n_freqs=N) psd = (hz * hz.conj()).real """ This is a one-sided spectrum, so we double the power: """ psd *= 2 dB(psd, psd) """ We begin by using the naive periodogram function (:func:`tsa.periodogram` in order to calculate the PSD and compare that to the true PSD calculated above.